Skip to content

Commit 7277a57

Browse files
adamantalarikalon1
andauthored
feat(buttons): add button enricher (#1943)
* feat(buttons): add static and templated button enricher * fix quotation marks * unify button enricher, fine-tune missing templates, skip if url is empty --------- Co-authored-by: arik <alon.arik@gmail.com>
1 parent 8013f54 commit 7277a57

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

docs/playbook-reference/actions/event-enrichment.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ These actions enrich Prometheus alerts and only support the :ref:`on_prometheus_
122122

123123
.. robusta-action:: playbooks.robusta_playbooks.alerts_integration.template_enricher
124124

125+
.. robusta-action:: playbooks.robusta_playbooks.alerts_integration.button_enricher
126+
125127
.. robusta-action:: playbooks.robusta_playbooks.alerts_integration.stack_overflow_enricher
126128

127129
.. robusta-action:: playbooks.robusta_playbooks.alerts_integration.default_enricher

docs/playbook-reference/prometheus-examples/link-alert-enrichment.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Implementation
1010

1111
In this example, we add links to the alert ``KubeContainerCPURequestAlert`` that we created in a :ref:`previous tutorial <Create Custom Prometheus Alerts>`.
1212

13-
Add the following YAML to the ``customPlaybooks`` Helm value and :ref:`update Robusta <Simple Upgrade>`.
13+
Below there are three alternatives ways to enrich the alert with links. Apply the YAML to the ``customPlaybooks`` Helm value and :ref:`update Robusta <Simple Upgrade>`.
1414

1515
.. code-block:: yaml
1616
@@ -29,6 +29,10 @@ Add the following YAML to the ``customPlaybooks`` Helm value and :ref:`update Ro
2929
:scroll: Playbook <https://playbook-url/|Handling High Resource Utilization>
3030
:github: Adjust CPU requests <https://github.com/YourRepository/|in the `Prod-sre` repository>
3131
:notion: Internal Docs on <https://notion.com/path-to-docs/|Customizing CPU requests>
32+
- button_enricher:
33+
button_text: "Playbook for ${team}"
34+
button_url: "https://playbook-url/team/${team}"
35+
3236
3337
.. code-annotations::
3438
1. We're using custom emojis here that correspond to GitHub and Notion logos. Before you configure this, follow `this guide <https://slack.com/intl/en-gb/help/articles/206870177-Add-customised-emoji-and-aliases-to-your-workspace>`_ to add emojis to your workspace.

playbooks/robusta_playbooks/alerts_integration.py

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
from robusta.core.reporting.blocks import TableBlockFormat
5151
from robusta.utils.parsing import format_event_templated_string
5252

53-
5453
class SeverityParams(ActionParams):
5554
"""
5655
:var severity: severity level that should be silenced.
@@ -379,6 +378,20 @@ class TemplateParams(ActionParams):
379378
template: str = ""
380379

381380

381+
def _get_labels(event: KubernetesResourceEvent, default_value: str = "<missing>") -> Dict[str, Any]:
382+
labels: Dict[str, Any] = defaultdict(lambda: default_value)
383+
if isinstance(event, PrometheusKubernetesAlert):
384+
labels.update(event.alert.labels)
385+
labels.update(event.alert.annotations)
386+
labels.update(vars(event.get_alert_subject()))
387+
labels["kind"] = labels["subject_type"].value
388+
elif isinstance(event, KubernetesResourceEvent):
389+
labels.update(vars(event.get_subject()))
390+
labels["kind"] = labels["subject_type"].value
391+
392+
return labels
393+
394+
382395
@action
383396
def template_enricher(event: KubernetesResourceEvent, params: TemplateParams):
384397
"""
@@ -395,22 +408,45 @@ def template_enricher(event: KubernetesResourceEvent, params: TemplateParams):
395408
The template can include all markdown directives supported by Slack.
396409
Note that Slack markdown links use a different format than GitHub.
397410
"""
398-
labels: Dict[str, Any] = defaultdict(lambda: "<missing>")
399-
if isinstance(event, PrometheusKubernetesAlert):
400-
labels.update(event.alert.labels)
401-
labels.update(event.alert.annotations)
402-
labels.update(vars(event.get_alert_subject()))
403-
labels["kind"] = labels["subject_type"].value
404-
elif isinstance(event, KubernetesResourceEvent):
405-
labels.update(vars(event.get_subject()))
406-
labels["kind"] = labels["subject_type"].value
407-
411+
labels = _get_labels(event)
408412
template = Template(params.template)
409413
event.add_enrichment(
410414
[MarkdownBlock(template.safe_substitute(labels))],
411415
)
412416

413417

418+
class TemplatedButtonParams(ActionParams):
419+
"""
420+
:var button_text: The templated text of the button
421+
:var button_url: The templated URL of the button
422+
"""
423+
button_text: str
424+
button_url: str
425+
426+
427+
@action
428+
def button_enricher(event: KubernetesResourceEvent, params: TemplatedButtonParams):
429+
"""
430+
Create a button with a templated text and URL.
431+
You can inject the k8s subject info and additionally on Prometheus alerts, any of the alert's Prometheus labels.
432+
433+
Common variables to use are ${name}, ${kind}, ${namespace}, and ${node}
434+
435+
A variable like ${foo} will be replaced by the value of info/label foo.
436+
If it isn't present, then the text will be skipped.
437+
438+
If the button_url is empty, then the button will not be added.
439+
440+
Check example for adding a template link.
441+
442+
"""
443+
labels = _get_labels(event, default_value="")
444+
button_text = Template(params.button_text).safe_substitute(labels)
445+
button_url = Template(params.button_url).safe_substitute(labels)
446+
if button_url.strip() != "":
447+
event.add_link(Link(url=button_url, name=button_text))
448+
449+
414450
class SearchTermParams(ActionParams):
415451
"""
416452
:var search_term: StackOverflow search term

0 commit comments

Comments
 (0)