Skip to content

Commit 3dd7a92

Browse files
committed
pre-testing
1 parent 5ffa178 commit 3dd7a92

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

docs/configuration/sinks/Opsgenie.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ We can add Prometheus alert labels into Opsgenie alert extra details by setting
1414
Configuring the OpsGenie sink
1515
------------------------------------------------
1616

17-
.. admonition:: Add this to your generated_values.yaml
17+
The OpsGenie sink supports static and dynamic team routing, optional fallback teams, tagging, and enrichment with Kubernetes labels.
18+
19+
.. admonition:: Add this to your `generated_values.yaml`
1820

1921
.. code-block:: yaml
2022
@@ -23,13 +25,22 @@ Configuring the OpsGenie sink
2325
name: ops_genie_sink
2426
api_key: OpsGenie integration API key # configured from OpsGenie team integration
2527
teams:
26-
- "noc"
27-
- "sre"
28+
- "noc" # Static team
29+
- "$labels.team" # Dynamic team routing based on alert label
30+
default_team: "oncall" # Optional fallback team for Dynamic team routing
2831
tags:
2932
- "prod a"
3033
extra_details_labels: false # optional, default is false
3134
32-
Save the file and run
35+
In this example:
36+
37+
- Alerts will be routed to the **"noc"** team by default.
38+
- If the alert includes a **"team" label**, it will also be routed to the value of that label.
39+
- If the **"team" label is missing**, the alert will be routed to the **"oncall"** team as a fallback.
40+
- The tag **"prod a"** will be included with every alert.
41+
- Kubernetes labels **will not be added** to alert details, as `extra_details_labels` is set to `false`.
42+
43+
Save the file and apply the configuration:
3344

3445
.. code-block:: bash
3546
:name: cb-add-opsgenie-sink

src/robusta/core/sinks/opsgenie/opsgenie_sink.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import opsgenie_sdk
55

66
from robusta.core.reporting.base import Enrichment, Finding, FindingSeverity
7+
from robusta.core.sinks.common.channel_transformer import ChannelTransformer
78
from robusta.core.sinks.opsgenie.opsgenie_sink_params import OpsGenieSinkConfigWrapper
89
from robusta.core.sinks.sink_base import SinkBase
910
from robusta.core.sinks.transformer import Transformer
@@ -22,9 +23,18 @@ def __init__(self, sink_config: OpsGenieSinkConfigWrapper, registry):
2223

2324
self.api_key = sink_config.opsgenie_sink.api_key
2425
self.teams = sink_config.opsgenie_sink.teams
26+
self.default_team = sink_config.opsgenie_sink.default_team
2527
self.tags = sink_config.opsgenie_sink.tags
2628
self.extra_details_labels = sink_config.opsgenie_sink.extra_details_labels
2729

30+
# Check for dangerous configuration
31+
has_templates = any("$" in team for team in self.teams)
32+
if has_templates and not self.default_team:
33+
logging.warning(
34+
"OpsGenie sink is configured with templated team names but no default_team specified. "
35+
"Alerts may fail to route if the required label or annotation is missing."
36+
)
37+
2838
opsgenie_sdk.configuration.Configuration.set_default(None)
2939
self.conf = opsgenie_sdk.configuration.Configuration()
3040
self.conf.api_key["Authorization"] = self.api_key
@@ -73,17 +83,48 @@ def __ack_alert(self, fingerprint: str, user: str, note: str):
7383
except opsgenie_sdk.ApiException as err:
7484
logging.error(f"Error acking opsGenie alert {fingerprint} {err}", exc_info=True)
7585

86+
def __get_teams(self, finding: Finding) -> List[str]:
87+
teams = []
88+
for team_template in self.teams:
89+
try:
90+
if "$" in team_template:
91+
# Only process templates that contain variables
92+
team = ChannelTransformer.template(
93+
team_template,
94+
self.default_team, # Use default_team as fallback
95+
self.cluster_name,
96+
finding.subject.labels,
97+
finding.subject.annotations,
98+
)
99+
if team: # Only add non-null teams
100+
teams.append(team)
101+
else:
102+
# Use static team name directly
103+
teams.append(team_template)
104+
except Exception as e:
105+
logging.warning(
106+
f"Failed to process team template {team_template} for alert subject {finding.service_key}: {e}"
107+
)
108+
continue
109+
return teams
110+
76111
def __open_alert(self, finding: Finding, platform_enabled: bool):
77112
description = self.__to_description(finding, platform_enabled)
78113
details = self.__to_details(finding)
79114
tags = self.tags.copy()
80115
tags.insert(0, self.cluster_name)
116+
117+
# Get teams based on templates
118+
teams = self.__get_teams(finding)
119+
if not teams and self.teams: # dynamic routing failed and no default team configured
120+
logging.warning(f"No valid teams resolved for finding {finding.title}. Alert may not be routed properly.")
121+
81122
body = opsgenie_sdk.CreateAlertPayload(
82123
source="Robusta",
83124
message=finding.title,
84125
description=description,
85126
alias=finding.fingerprint,
86-
responders=[{"name": team, "type": "team"} for team in self.teams],
127+
responders=[{"name": team, "type": "team"} for team in teams],
87128
details=details,
88129
tags=tags,
89130
entity=finding.service_key,

src/robusta/core/sinks/opsgenie/opsgenie_sink_params.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class OpsGenieSinkParams(SinkBaseParams):
88
api_key: str
99
teams: List[str] = []
10+
default_team: Optional[str] = None
1011
tags: List[str] = []
1112
host: Optional[str] = None # NOTE: If None, the default value will be used from opsgenie_sdk
1213
extra_details_labels: Optional[bool] = False

0 commit comments

Comments
 (0)