Skip to content

Commit

Permalink
pr changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-Robusta committed Jan 8, 2025
1 parent bfeeb99 commit 942c279
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 67 deletions.
5 changes: 5 additions & 0 deletions docs/playbook-reference/actions/miscellaneous.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ ArgoCD

.. robusta-action:: playbooks.robusta_playbooks.argo_cd.argo_app_sync

Slack-OpsGenie sync
^^^^^^^^^^^^^^

.. robusta-action:: playbooks.robusta_playbooks.sink_enrichments.opsgenie_slack_enricher

Kubernetes Optimization
-----------------------

Expand Down
91 changes: 42 additions & 49 deletions playbooks/robusta_playbooks/sink_enrichments.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,52 @@ def ack_opsgenie_alert_from_slack(event: ExecutionBaseEvent, params: OpsGenieAck
"""
Sends an ack to opsgenie alert
"""
def ack_opsgenie_alert() -> None:
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}"
)

if not params.slack_message:
logging.warning("No action Slack found, unable to update slack message.")
return

# slack action block
actions = params.slack_message.get("actions", [])
if not actions:
logging.warning("No actions found in the Slack message.")
return

block_id = actions[0].get("block_id")
if not block_id:
logging.warning("Block ID is missing in the first action of the Slack message.")
return

event.emit_event(
"replace_callback_with_string",
slack_message=params.slack_message,
block_id=block_id,
message_string=f"✅ *OpsGenie Ack by @{params.slack_username}*"
)

ack_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}"
)

if not params.slack_message:
logging.warning("No action Slack found, unable to update slack message.")
return

# slack action block
actions = params.slack_message.get("actions", [])
if not actions:
logging.warning("No actions found in the Slack message.")
return

block_id = actions[0].get("block_id")
if not block_id:
logging.warning("Block ID is missing in the first action of the Slack message.")
return

event.emit_event(
"replace_callback_with_string",
slack_message=params.slack_message,
block_id=block_id,
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 ack_slack_opsgenie_enricher(alert: PrometheusKubernetesAlert):
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.
Add a button to the alert - clicking it will ask chat gpt to help find a solution.
"""
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_enrichment(
[
CallbackBlock(
Expand All @@ -88,12 +96,6 @@ def ack_slack_opsgenie_enricher(alert: PrometheusKubernetesAlert):
)


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


def normalize_url_base(url_base: str) -> str:
"""
Expand All @@ -104,13 +106,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('/')


@action
def opsgenie_link_enricher(alert: PrometheusKubernetesAlert, params: OpsGenieLinkParams):
"""
Adds a link to finding of for the opsgenie alert.
"""
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))
return url_base.rstrip('/')
4 changes: 2 additions & 2 deletions src/robusta/core/reporting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from abc import ABC, abstractmethod
from datetime import datetime
from enum import Enum
from strenum import StrEnum
from typing import Any, Dict, List, Optional, Union
from urllib.parse import urlencode

from pydantic.main import BaseModel
from strenum import StrEnum

from robusta.core.discovery.top_service_resolver import TopServiceResolver
from robusta.core.model.env_vars import ROBUSTA_UI_DOMAIN
Expand Down Expand Up @@ -94,7 +94,7 @@ def to_emoji(self) -> str:
class LinkType(StrEnum):
VIDEO = "video"
PROMETHEUS_GENERATOR_URL = "prometheus_generator_url"
OPSGENIE = "opsgenie"
OPSGENIE_LIST_ALERT_BY_ALIAS = "opsgenie_list_alert_by_alias"


class Link(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions src/robusta/core/sinks/opsgenie/opsgenie_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def handle_event(self, event_name: str, **kwargs):
if event_name == "opsgenie_ack":
self.__ack_alert(**kwargs)
else:
logging.warning("OpsGenieSink subscriber called with unknown event")
logging.warning(f"OpsGenieSink subscriber called with unknown event {event_name}")

def __close_alert(self, finding: Finding):
body = opsgenie_sdk.CloseAlertPayload(
Expand All @@ -63,7 +63,7 @@ def __ack_alert(self, fingerprint: str, user: str, note: str):
body = opsgenie_sdk.AcknowledgeAlertPayload(
user=user,
note=note,
source="Robusta OpsGenie Ack",
source="Robusta",
)
try:
self.alert_api.acknowledge_alert(
Expand Down
18 changes: 5 additions & 13 deletions src/robusta/core/sinks/slack/slack_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,23 @@ def __replace_callback_with_string(self, slack_message, block_id, message_string
raise ValueError("Missing required fields: channel_id, message_ts, or blocks.")

# Update the specific block
updated_blocks = []
block_found = False

for block in blocks:
for i, block in enumerate(blocks):
if block.get("block_id") == block_id:
updated_blocks.append({
blocks[i] = {
"type": "section",
"block_id": block_id,
"text": {
"type": "mrkdwn",
"text": message_string
}
})
block_found = True
else:
updated_blocks.append(block)

if not block_found:
raise ValueError(f"No block found with block_id: {block_id}")
}
break

# Call the shorter update function
return self.slack_sender.update_slack_message(
channel=channel_id,
ts=message_ts,
blocks=updated_blocks,
blocks=blocks,
text=message_string
)

Expand Down
1 change: 0 additions & 1 deletion src/robusta/integrations/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class SlackUserID(BaseModel):
class SlackActionsMessage(BaseModel):
actions: List[SlackActionRequest]
user: Optional[SlackUserID]
message: Optional[Dict[str, Any]]


class ActionRequestReceiver:
Expand Down

0 comments on commit 942c279

Please sign in to comment.