Skip to content

Commit

Permalink
Username & subteam name representation changes to make them compatibl…
Browse files Browse the repository at this point in the history
…e with k8s labels (#1286)

* username & subteam name representation changes to make them compatible with k8s labels

* update MentionParams docs

* add ref to mention_enricher in docs
  • Loading branch information
Robert Szefler authored Feb 14, 2024
1 parent 73961fd commit 713898c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ We can automate this with Robusta:
1. See :ref:`on_pod_oom_killed<on_pod_oom_killed>`
2. See :ref:`pod_graph_enricher<pod_graph_enricher>`
3. See :ref:`pod_oom_killer_enricher<pod_oom_killer_enricher>`
4. See :ref:`mention_enricher<mention_enricher>`

Here is the result in Slack:

Expand Down
23 changes: 19 additions & 4 deletions playbooks/robusta_playbooks/alerts_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import defaultdict
from datetime import datetime
from string import Template
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Iterable

import requests
from hikaru.model.rel_1_26 import Node
Expand Down Expand Up @@ -445,11 +445,11 @@ def foreign_logs_enricher(event: ExecutionBaseEvent, params: ForeignLogParams):

class MentionParams(ActionParams):
"""
:var static_mentions: List of Slack user ids/groups ids to be mentioned
:var mentions_label: A alert label, or Kubernetes resource label, in which the value contains a comma separated ids to mention
:var static_mentions: List of Slack user ids/subteam ids to be mentioned
:var mentions_label: An alert label, or Kubernetes resource label, in which the value contains a dot separated ids to mention
:var message_template: Optional. Custom mention message. Default: `"Hey: $mentions"`
:example static_mentions: ["<@U44V9P1JJ1Z>", "<!subteam^S22H3Q3Q111>"]
:example static_mentions: ["U44V9P1JJ1Z", "S22H3Q3Q111"]
"""

static_mentions: Optional[List[str]]
Expand Down Expand Up @@ -500,5 +500,20 @@ def mention_enricher(event: KubernetesResourceEvent, params: MentionParams):
if params.static_mentions:
mentions = mentions.union(params.static_mentions)

mentions = mention_to_slack_format(mentions)

message = params.message_template.replace("$mentions", " ".join(mentions))
event.add_enrichment([MarkdownBlock(message)])


def mention_to_slack_format(mentions: Iterable[str]) -> List[str]:
result = []
for mentions_spec in mentions:
for mention in mentions_spec.split('.'):
if mention.startswith("U"):
result.append(f"<@{mention}>")
elif mention.startswith("S"):
result.append(f"<!subteam^{mention}>")
else:
raise ValueError(f"unknown mention format: {mention}")
return result

0 comments on commit 713898c

Please sign in to comment.