Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Szefler committed Apr 9, 2024
1 parent 34ce670 commit f473bd5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/robusta/core/sinks/sink_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def __init__(self, sink_params: SinkBaseParams, registry):
"grouping.notification_mode.summary.by"
)
key = keys[0]
if key not in ["labels", "attributes"]:
if key not in ["labels", "annotations"]:
raise ValueError(
f"Sink configuration: grouping.notification_mode.summary.by.{key} is invalid "
"(only labels/attributes allowed)"
"(only labels/annotations allowed)"
)
for label_or_attr_name in attr[key]:
self.finding_summary_header.append(f"{key[:-1]}:{label_or_attr_name}")
Expand Down
10 changes: 6 additions & 4 deletions src/robusta/core/sinks/slack/slack_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def handle_notification_grouping(self, finding: Finding, platform_enabled: bool)
return

if self.grouping_summary_mode:
# TODO summary_classification_header could be precomputed just as
# self.finding_summary_header is in SinkBase.__init__. This would
# simplify classify_finding quite a bit.
summary_classification, summary_classification_header = self.classify_finding(
finding_data, self.params.grouping.notification_mode.summary.by
)
Expand All @@ -72,7 +75,7 @@ def handle_notification_grouping(self, finding: Finding, platform_enabled: bool)
# Create the first Slack message
if self.grouping_summary_mode:
initial_counts = (1, 0) if status == FindingStatus.FIRING else (0, 1)
initial_counts_table = {group_by_classification: initial_counts}
initial_counts_table = {summary_classification: initial_counts}
self.finding_summary_counts[group_by_classification] = initial_counts_table
logging.info("Creating first Slack summarised thread")
slack_thread_ts = self.slack_sender.send_summary_message(
Expand Down Expand Up @@ -101,10 +104,9 @@ def classify_finding(self, finding_data: Dict, attributes: List) -> Tuple[Tuple[
logging.warning(f"Notification grouping: tried to group on non-existent attribute {attr}")
continue
values += (finding_data.get(attr),)
descriptions.append(f"{attr}={finding_data.get(attr)}")
descriptions.append(f"{'event_name' if attr=='identifier' else attr}={finding_data.get(attr)}")
elif isinstance(attr, dict):
if list(attr.keys()) not in [["labels"], ["attributes"]]:
logging.warning(f"Notification grouping: tried to group on non-existent attribute(s) {attr}")
# This is typically labels and annotations
top_level_attr_name = list(attr.keys())[0]
values += tuple(
finding_data.get(top_level_attr_name, {}).get(subitem_name)
Expand Down
19 changes: 14 additions & 5 deletions src/robusta/integrations/slack/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def send_summary_message(
self,
summary_classification_header: List[str],
finding_summary_header: List[str],
summary_table: Dict[tuple, Tuple[int, int]],
summary_table: Dict[Tuple[str], Tuple[int, int]],
sink_params: SlackSinkParams,
platform_enabled: bool,
msg_ts: str = None # message identifier (for updates)
Expand All @@ -400,14 +400,23 @@ def send_summary_message(
firing/resolved and a header describing the event group that this information concerns."""
logging.warning(f"XXX send_summary_table {summary_classification_header=} {finding_summary_header=} {summary_table=}")

blocks = [
self.__to_slack(MarkdownBlock("👀 Summary for: " + ", ".join(summary_classification_header)),
]
rows = []
for key, value in summary_table:
# key is a tuple of attribute names; value is a 2-tuple with the number of firing and resolved events
row = key + value
rows.append(row)

table_block = TableBlock(
headers=finding_summary_header + ["Firing", "Resolved"],
rows=rows
)
blocks = self.__to_slack(MarkdownBlock("👀 Summary for: " + ", ".join(summary_classification_header)))
blocks.extend(self.__to_slack(table_block))
try:
resp = self.slack_client.chat_postMessage(
# TODO: for the purpose of the summary, we pretend labels and annotations are empty. Is this okay?
channel=sink_params.get_slack_channel(self.cluster_name, {}, {}),
# text="A summary message",
text="Summary for: " + ", ".join(summary_classification_header),
blocks=blocks,
display_as_bot=True,
)
Expand Down

0 comments on commit f473bd5

Please sign in to comment.