From 5e9f5e408ce557f70673bfee4d04e0e432d92221 Mon Sep 17 00:00:00 2001 From: Garel Leo Date: Mon, 21 Oct 2024 11:57:11 +0200 Subject: [PATCH] Add opsgenie sink extra details from alert labels (#1587) --- .../core/sinks/opsgenie/opsgenie_sink.py | 29 ++++++++++++++----- .../sinks/opsgenie/opsgenie_sink_params.py | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/robusta/core/sinks/opsgenie/opsgenie_sink.py b/src/robusta/core/sinks/opsgenie/opsgenie_sink.py index 4284ad224..e1abe551d 100644 --- a/src/robusta/core/sinks/opsgenie/opsgenie_sink.py +++ b/src/robusta/core/sinks/opsgenie/opsgenie_sink.py @@ -32,6 +32,9 @@ def __init__(self, sink_config: OpsGenieSinkConfigWrapper, registry): if sink_config.opsgenie_sink.host is not None: self.conf.host = sink_config.opsgenie_sink.host + if sink_config.opsgenie_sink.extra_details_labels is not None: + self.conf.extra_details_labels = sink_config.opsgenie_sink.extra_details_labels + self.api_client = opsgenie_sdk.api_client.ApiClient(configuration=self.conf) self.alert_api = opsgenie_sdk.AlertApi(api_client=self.api_client) @@ -52,6 +55,7 @@ def __close_alert(self, finding: Finding): def __open_alert(self, finding: Finding, platform_enabled: bool): description = self.__to_description(finding, platform_enabled) + details = self.__to_details(finding) self.tags.insert(0, self.cluster_name) body = opsgenie_sdk.CreateAlertPayload( source="Robusta", @@ -59,13 +63,7 @@ def __open_alert(self, finding: Finding, platform_enabled: bool): description=description, alias=finding.fingerprint, responders=[{"name": team, "type": "team"} for team in self.teams], - details={ - "Resource": finding.subject.name, - "Cluster": self.cluster_name, - "Namespace": finding.subject.namespace, - "Node": finding.subject.node, - "Source": str(finding.source.name), - }, + details=details, tags=self.tags, entity=finding.service_key, priority=PRIORITY_MAP.get(finding.severity, "P3"), @@ -96,6 +94,23 @@ def __to_description(self, finding: Finding, platform_enabled: bool) -> str: return f"{description}{self.__enrichments_as_text(finding.enrichments)}" + def __to_details(self, finding: Finding) -> dict: + details = { + "Resource": finding.subject.name, + "Cluster": self.cluster_name, + "Namespace": finding.subject.namespace, + "Node": finding.subject.node, + "Source": str(finding.source.name), + } + lower_details_key = [k.lower() for k in details.keys()] + # If there are extra details labels in the config extra_details_labels, + # add them without altering the already existing details. + if self.conf.extra_details_labels: + for key, value in finding.subject.labels: + if key in self.conf.extra_details_labels and not key in lower_details_key: + details[key] = value + return details + @classmethod def __enrichments_as_text(cls, enrichments: List[Enrichment]) -> str: transformer = Transformer() diff --git a/src/robusta/core/sinks/opsgenie/opsgenie_sink_params.py b/src/robusta/core/sinks/opsgenie/opsgenie_sink_params.py index 9a69ddc72..b4bc9eea2 100644 --- a/src/robusta/core/sinks/opsgenie/opsgenie_sink_params.py +++ b/src/robusta/core/sinks/opsgenie/opsgenie_sink_params.py @@ -9,6 +9,7 @@ class OpsGenieSinkParams(SinkBaseParams): teams: List[str] = [] tags: List[str] = [] host: Optional[str] = None # NOTE: If None, the default value will be used from opsgenie_sdk + extra_details_labels: Optional[bool] = None @classmethod def _get_sink_type(cls):