Skip to content

Commit

Permalink
added documentation and comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-Robusta committed Jan 9, 2025
1 parent 84bc668 commit 41ab137
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
24 changes: 24 additions & 0 deletions docs/configuration/sinks/Opsgenie.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,27 @@ Save the file and run
.. image:: /images/deployment-babysitter-opsgenie.png
:width: 1000
:align: center


Action to connect Slack to OpsGenie
------------------------------------------------

The `opsgenie_slack_enricher` action enriches Slack alerts with OpsGenie integration. It performs the following:

- Adds a button in Slack to acknowledge the OpsGenie alert directly.
- Includes a link in Slack messages that redirects to the alert in OpsGenie for easy access.

To use this action, ensure it is included in your playbook configuration.

**Example Configuration:**

.. code-block:: yaml
customPlaybooks:
- actions:
- opsgenie_slack_enricher:
url_base: team-name.app.eu.opsgenie.com
triggers:
- on_prometheus_alert: {}
With this integration, teams can efficiently manage OpsGenie alerts directly from Slack.
26 changes: 17 additions & 9 deletions playbooks/robusta_playbooks/sink_enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SlackCallbackParams(ActionParams):
:var slack_username: The username that clicked the slack callback. - Auto-populated by slack
:var slack_message: The message from the slack callback. - Auto-populated by slack
"""

slack_username: Optional[str]
slack_message: Optional[Any]

Expand All @@ -26,19 +27,20 @@ class OpsGenieAckParams(SlackCallbackParams):
"""
:var alertmanager_url: Alternative Alert Manager url to send requests.
"""

alert_fingerprint: str


@action
def ack_opsgenie_alert_from_slack(event: ExecutionBaseEvent, params: OpsGenieAckParams):
"""
Sends an ack to opsgenie alert
Sends an ack to opsgenie alert
"""
event.emit_event(
"opsgenie_ack",
fingerprint=params.alert_fingerprint,
user=params.slack_username,
note=f"This alert was ack-ed from a Robusta Slack message by {params.slack_username}"
note=f"This alert was ack-ed from a Robusta Slack message by {params.slack_username}",
)

if not params.slack_message:
Expand All @@ -60,31 +62,38 @@ def ack_opsgenie_alert_from_slack(event: ExecutionBaseEvent, params: OpsGenieAck
"replace_callback_with_string",
slack_message=params.slack_message,
block_id=block_id,
message_string=f"✅ *OpsGenie Ack by @{params.slack_username}*"
message_string=f"✅ *OpsGenie Ack by @{params.slack_username}*",
)


class OpsGenieLinkParams(ActionParams):
"""
:var url_base: The base url for your opsgenie account for example: "robusta-test-url.app.eu.opsgenie.com"
"""

url_base: str


@action
def opsgenie_slack_enricher(alert: PrometheusKubernetesAlert, params: OpsGenieLinkParams):
"""
Add a button to the alert - clicking it will ask chat gpt to help find a solution.
Adds a button in slack to ack an opsGenie alert
Adds a Link to slack to the alert in opsgenie
"""
normalized_url_base = normalize_url_base(params.url_base)
alert.add_link(Link(url=f"https://{normalized_url_base}/alert/list?query=alias:{alert.alert.fingerprint}",
name="OpsGenie Alert", type=LinkType.OPSGENIE_LIST_ALERT_BY_ALIAS))
alert.add_link(
Link(
url=f"https://{normalized_url_base}/alert/list?query=alias:{alert.alert.fingerprint}",
name="OpsGenie Alert",
type=LinkType.OPSGENIE_LIST_ALERT_BY_ALIAS,
)
)

alert.add_enrichment(
[
CallbackBlock(
{
f'Ack Opsgenie Alert': CallbackChoice(
f"Ack Opsgenie Alert": CallbackChoice(
action=ack_opsgenie_alert_from_slack,
action_params=OpsGenieAckParams(
alert_fingerprint=alert.alert.fingerprint,
Expand All @@ -96,7 +105,6 @@ def opsgenie_slack_enricher(alert: PrometheusKubernetesAlert, params: OpsGenieLi
)



def normalize_url_base(url_base: str) -> str:
"""
Normalize the url_base to remove 'https://' or 'http://' and any trailing slashes.
Expand All @@ -106,4 +114,4 @@ def normalize_url_base(url_base: str) -> str:
url_base = parsed_url.netloc if parsed_url.netloc else parsed_url.path

# Remove trailing slash if present
return url_base.rstrip('/')
return url_base.rstrip("/")

0 comments on commit 41ab137

Please sign in to comment.