Skip to content

Commit

Permalink
Add opsgenie sink extra details from alert labels
Browse files Browse the repository at this point in the history
  • Loading branch information
sk4mi committed Oct 10, 2024
1 parent 001028c commit 2fc8f99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/robusta/core/sinks/opsgenie/opsgenie_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -52,20 +55,15 @@ 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, platform_enabled)
self.tags.insert(0, self.cluster_name)
body = opsgenie_sdk.CreateAlertPayload(
source="Robusta",
message=finding.title,
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"),
Expand Down Expand Up @@ -96,6 +94,21 @@ 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, platform_enabled: bool) -> dict:
details = {
"Resource": finding.subject.name,
"Cluster": self.cluster_name,
"Namespace": finding.subject.namespace,
"Node": finding.subject.node,
"Source": str(finding.source.name),
}
# Add extra details labels from config extra_details_labels if it exists
if self.conf.extra_details_labels:
for key, value in finding.subject.labels:
if key in self.conf.extra_details_labels:
details[key] = value
return details

@classmethod
def __enrichments_as_text(cls, enrichments: List[Enrichment]) -> str:
transformer = Transformer()
Expand Down
1 change: 1 addition & 0 deletions src/robusta/core/sinks/opsgenie/opsgenie_sink_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2fc8f99

Please sign in to comment.