-
Notifications
You must be signed in to change notification settings - Fork 288
feat(buttons): add button enricher #1943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughAdds a helper Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Prom as PrometheusAlert
participant Enricher as EnricherDispatcher
participant Labels as _get_labels
participant Template as template_enricher
participant Button as button_enricher
participant Alert as EnrichedAlert
Prom->>Enricher: incoming alert event
Enricher->>Labels: extract labels/subject
Labels-->>Enricher: labels dict
Enricher->>Template: apply templates (uses labels)
Template-->>Enricher: adds templated links
Enricher->>Button: apply button_enricher (templates text/url using labels)
Button-->>Enricher: adds button link
Enricher->>Alert: attach links/buttons and emit enriched alert
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (3)
playbooks/robusta_playbooks/alerts_integration.py (2)
441-444: Consider validating the templated URL.After template substitution, the resulting URL could be invalid (e.g., if a label contains invalid characters or is missing, resulting in
"<missing>"). The existingis_valid_urlhelper (line 223) could validate the final URL before creating the link, similar to howdefault_enrichervalidates the generator URL (line 243).Apply this diff to add URL validation:
labels = _get_labels(event) button_text = Template(params.button_text).safe_substitute(labels) button_url = Template(params.button_url).safe_substitute(labels) +if not is_valid_url(button_url): + logging.warning(f"Skipping templated button with invalid URL: {button_url}") + return event.add_link(Link(url=button_url, name=button_text))
452-453: Consider validating static URLs for consistency.While static URLs come from configuration and are less likely to be invalid, adding validation using the existing
is_valid_urlhelper would provide consistent error handling and catch configuration mistakes early.Apply this diff to add validation:
+if not is_valid_url(params.button_url): + logging.warning(f"Skipping static button with invalid URL: {params.button_url}") + return event.add_link(Link(url=params.button_url, name=params.button_text))docs/playbook-reference/prometheus-examples/link-alert-enrichment.rst (1)
32-34: Consider documenting behavior for missing labels.The templated button example uses
${team}, but doesn't explain what happens if theteamlabel doesn't exist on the alert. Users might benefit from knowing that missing labels will display as"<missing>"in the button text or URL (as documented in the action's docstring).Consider adding a brief note after line 37:
.. note:: For templated buttons, if a referenced label doesn't exist (e.g., ``${team}``), it will be replaced with the text ``<missing>`` in the final button.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
docs/playbook-reference/actions/event-enrichment.rst(1 hunks)docs/playbook-reference/prometheus-examples/link-alert-enrichment.rst(2 hunks)playbooks/robusta_playbooks/alerts_integration.py(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
playbooks/robusta_playbooks/alerts_integration.py (5)
src/robusta/integrations/kubernetes/autogenerated/events.py (15)
KubernetesResourceEvent(126-238)get_subject(215-223)get_subject(394-402)get_subject(416-424)get_subject(451-459)get_subject(473-481)get_subject(508-516)get_subject(530-538)get_subject(565-573)get_subject(587-595)get_subject(622-630)get_subject(644-652)get_subject(679-687)get_subject(701-709)get_subject(736-744)src/robusta/integrations/prometheus/models.py (3)
PrometheusKubernetesAlert(73-249)get_alert_subject(148-203)get_subject(226-227)src/robusta/core/model/events.py (4)
get_subject(170-171)add_enrichment(116-127)add_link(100-103)ExecutionBaseEvent(49-175)src/robusta/core/reporting/base.py (3)
add_enrichment(343-360)add_link(362-365)Link(99-112)src/robusta/core/reporting/blocks.py (1)
MarkdownBlock(42-61)
🪛 Ruff (0.14.1)
playbooks/robusta_playbooks/alerts_integration.py
431-431: Docstring contains ambiguous ’ (RIGHT SINGLE QUOTATION MARK). Did you mean ``` (GRAVE ACCENT)?
(RUF002)
436-436: Docstring contains ambiguous ’ (RIGHT SINGLE QUOTATION MARK). Did you mean ``` (GRAVE ACCENT)?
(RUF002)
🔇 Additional comments (4)
playbooks/robusta_playbooks/alerts_integration.py (3)
381-392: LGTM! Good refactoring.The helper function extracts common label-gathering logic, reducing code duplication between
template_enricherand the new button enrichers. The implementation correctly handles both event types and provides sensible defaults for missing values.
411-411: LGTM! Clean refactoring.The change correctly delegates to the new
_get_labelshelper, maintaining the same behavior while improving code reuse.
456-461: LGTM! Appropriate use of broader event type.The static enricher correctly accepts
ExecutionBaseEvent(a broader base type thanKubernetesResourceEventused by the templated version), which makes sense since no label extraction is needed. This allows the static button to work with more event types.docs/playbook-reference/prometheus-examples/link-alert-enrichment.rst (1)
32-37: LGTM! Clear examples for the new button enrichers.The examples effectively demonstrate both enricher types and their configuration. The templated example shows variable substitution with
${team}, and the static example shows a straightforward URL button.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
playbooks/robusta_playbooks/alerts_integration.py (3)
381-392: Consider explicit handling for unexpected event types.The function will silently return a dict containing only
"<missing>"default values if the event is neitherPrometheusKubernetesAlertnorKubernetesResourceEvent. While the type hint suggests onlyKubernetesResourceEventshould be passed, adding an explicit check or log warning could prevent silent failures if the function is called with an unexpected event type in the future.Consider adding a log warning for the else case:
def _get_labels(event: KubernetesResourceEvent) -> Dict[str, Any]: labels: Dict[str, Any] = defaultdict(lambda: "<missing>") if isinstance(event, PrometheusKubernetesAlert): labels.update(event.alert.labels) labels.update(event.alert.annotations) labels.update(vars(event.get_alert_subject())) labels["kind"] = labels["subject_type"].value elif isinstance(event, KubernetesResourceEvent): labels.update(vars(event.get_subject())) labels["kind"] = labels["subject_type"].value + else: + logging.warning(f"_get_labels called with unexpected event type: {type(event)}") return labels
427-444: Validate templated URL before adding the link.After template substitution, the
button_urlmight contain"<missing>"placeholders or result in an invalid URL. The existingis_valid_url()helper (lines 223-232) is already used bydefault_enricherto validate URLs before adding links. Consider applying the same validation here to prevent invalid links from being added.Apply this diff to add URL validation:
@action def templated_button_enricher(event: KubernetesResourceEvent, params: TemplatedButtonParams): """ Create a button with a templated text and URL. You can inject the k8s subject info and additionally on Prometheus alerts, any of the alert's Prometheus labels. Common variables to use are ${name}, ${kind}, ${namespace}, and ${node} A variable like ${foo} will be replaced by the value of info/label foo. If it isn't present then the text "<missing>" will be used instead. Check example for adding a template link. """ labels = _get_labels(event) button_text = Template(params.button_text).safe_substitute(labels) button_url = Template(params.button_url).safe_substitute(labels) + if not is_valid_url(button_url): + logging.warning(f"Skipping invalid templated button URL: {button_url}") + return event.add_link(Link(url=button_url, name=button_text))
456-461: Validate static URL before adding the link.The
button_urlparameter should be validated to ensure it's a valid URL before adding the link. Invalid URLs could cause issues in message rendering or break Slack/other sink integrations. Consider using the existingis_valid_url()helper (lines 223-232) for consistency withdefault_enricher.Apply this diff to add URL validation:
@action def static_button_enricher(event: ExecutionBaseEvent, params: StaticButtonParams): """ Create a button with a static text and URL. """ + if not is_valid_url(params.button_url): + logging.warning(f"Skipping invalid button URL: {params.button_url}") + return event.add_link(Link(url=params.button_url, name=params.button_text))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
playbooks/robusta_playbooks/alerts_integration.py(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
playbooks/robusta_playbooks/alerts_integration.py (3)
src/robusta/integrations/kubernetes/autogenerated/events.py (15)
KubernetesResourceEvent(126-238)get_subject(215-223)get_subject(394-402)get_subject(416-424)get_subject(451-459)get_subject(473-481)get_subject(508-516)get_subject(530-538)get_subject(565-573)get_subject(587-595)get_subject(622-630)get_subject(644-652)get_subject(679-687)get_subject(701-709)get_subject(736-744)src/robusta/integrations/prometheus/models.py (2)
get_alert_subject(148-203)get_subject(226-227)src/robusta/core/model/events.py (4)
get_subject(170-171)add_enrichment(116-127)add_link(100-103)ExecutionBaseEvent(49-175)
🔇 Additional comments (1)
playbooks/robusta_playbooks/alerts_integration.py (1)
411-411: Good refactor to eliminate code duplication.Extracting label gathering logic into
_get_labelsimproves maintainability and eliminates duplication betweentemplate_enricherand the newtemplated_button_enricher.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (1)
playbooks/robusta_playbooks/alerts_integration.py (1)
430-436: Docstring quotation mark issue (already flagged).This is the same quotation mark issue that was already noted in the past review comments. The docstring uses RIGHT SINGLE QUOTATION MARK (') instead of GRAVE ACCENT (`) for inline code references (lines 433, 435).
🧹 Nitpick comments (1)
playbooks/robusta_playbooks/alerts_integration.py (1)
443-447: Consider validating button_text to avoid empty button labels.If all variables in
button_textare missing, the templated result will be an empty string (sincedefault_value=""). Adding a button with no label could be confusing for users. Consider checking thatbutton_textis non-empty before adding the link.For example:
labels = _get_labels(event, default_value="") button_text = Template(params.button_text).safe_substitute(labels) button_url = Template(params.button_url).safe_substitute(labels) - if button_url.strip() != "": + if button_text.strip() and button_url.strip(): event.add_link(Link(url=button_url, name=button_text))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
docs/playbook-reference/actions/event-enrichment.rst(1 hunks)docs/playbook-reference/prometheus-examples/link-alert-enrichment.rst(2 hunks)playbooks/robusta_playbooks/alerts_integration.py(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- docs/playbook-reference/prometheus-examples/link-alert-enrichment.rst
- docs/playbook-reference/actions/event-enrichment.rst
🧰 Additional context used
🧬 Code graph analysis (1)
playbooks/robusta_playbooks/alerts_integration.py (3)
src/robusta/integrations/kubernetes/autogenerated/events.py (15)
KubernetesResourceEvent(126-238)get_subject(215-223)get_subject(394-402)get_subject(416-424)get_subject(451-459)get_subject(473-481)get_subject(508-516)get_subject(530-538)get_subject(565-573)get_subject(587-595)get_subject(622-630)get_subject(644-652)get_subject(679-687)get_subject(701-709)get_subject(736-744)src/robusta/integrations/prometheus/models.py (2)
get_alert_subject(148-203)get_subject(226-227)src/robusta/core/reporting/base.py (1)
Link(99-112)
🔇 Additional comments (3)
playbooks/robusta_playbooks/alerts_integration.py (3)
381-392: LGTM! Good refactoring to centralize label extraction.The
_get_labelshelper consolidates label gathering logic that was previously inline. It properly handles bothPrometheusKubernetesAlertandKubernetesResourceEventtypes, and the defaultdict pattern provides clean fallback behavior for missing keys.
411-411: LGTM! Clean refactoring.Delegating to
_get_labelssimplifies the function while preserving the original behavior.
418-424: LGTM! Clear parameter definition.The parameter class is straightforward and follows the established pattern in the codebase.
|
this enricher has been implemented as an external action in https://github.com/zocks-communications/robusta-button-enricher until this PR is merged to the core actions |
arikalon1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice work!
Thanks for the contribution @adamantal
Description
Adding support for attaching buttons to the action. It supports templating, with the same set of substitutions as the
template_enricherwhich was recommended as an alternative.Also updated the docs with the custom button example.
Possibly fixing #735 and #814
Testing
tested on top of 0.29.0, worked properly for me 👌
(Note: I successfully rickrolled the team during the testing)
Concrete example: