Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ce49b90
WIP
aantn Mar 24, 2025
96a69e2
Update sender.py
aantn Mar 27, 2025
0f3710f
Update sender.py
aantn Mar 27, 2025
cdec3f7
Update sender.py
aantn Mar 27, 2025
7ff23d6
Update sender.py
aantn Mar 27, 2025
26ed319
Update sender.py
aantn Mar 27, 2025
ef3214d
Update sender.py
aantn Mar 27, 2025
d79a00d
Update sender.py
aantn Mar 27, 2025
64bafc5
Update sender.py
aantn Mar 27, 2025
787846a
incorporate feedback from igor
aantn Mar 27, 2025
c693938
Merge branch 'master' into claude-slack
aantn Apr 21, 2025
d72e001
add templating
aantn Apr 21, 2025
c4866c3
remove unused code
aantn Apr 21, 2025
092f0c8
add logs
aantn Apr 21, 2025
8c1a9a8
simplify code - always use jinja
aantn Apr 21, 2025
9b15ca1
add legacy option
aantn Apr 21, 2025
6bafca4
Merge branch 'master' into claude-slack
Avi-Robusta May 14, 2025
9281648
bugfix sending to slack
Avi-Robusta May 14, 2025
92c1b76
fixing template
Avi-Robusta May 14, 2025
4e6b055
refactoring code
Avi-Robusta May 14, 2025
abe1c74
saving progress
Avi-Robusta May 15, 2025
cca0d57
added tests
Avi-Robusta May 15, 2025
ea81979
refactoring changes
Avi-Robusta May 15, 2025
5df775d
misc changes
Avi-Robusta May 15, 2025
ed0178c
changess
Avi-Robusta May 15, 2025
567372c
working version
Avi-Robusta May 15, 2025
317fbf4
refactor send finding to slack
Avi-Robusta May 15, 2025
bb5d1c2
added space
Avi-Robusta May 15, 2025
181adc1
removing unneeded code atm
Avi-Robusta May 15, 2025
d4a85a1
refactor
Avi-Robusta May 15, 2025
edd0cbd
fixing
Avi-Robusta May 15, 2025
4650e18
docs update
Avi-Robusta May 15, 2025
ba20164
updated tests
Avi-Robusta May 19, 2025
c468f5a
bugfixes and header support
Avi-Robusta May 21, 2025
fa8a68e
fixed mentions
Avi-Robusta May 21, 2025
fcad848
added channel override test
Avi-Robusta May 21, 2025
b1dda3b
refactoring, removing useless changes
Avi-Robusta May 21, 2025
a73bf41
Merge branch 'master' into claude-slack
Avi-Robusta May 21, 2025
3c1f709
refactoring sender
Avi-Robusta May 21, 2025
57f094c
remove unneeded code
Avi-Robusta May 21, 2025
7b9daf2
added additional features
Avi-Robusta May 25, 2025
6546ba5
changed manual tests
Avi-Robusta May 25, 2025
640ae37
add aggregation key
Avi-Robusta May 25, 2025
1a7ee23
pr changes
Avi-Robusta May 26, 2025
e5094c4
added fingerprint to template
Avi-Robusta May 26, 2025
24d32c7
Merge branch 'master' into claude-slack
arikalon1 May 26, 2025
052d6f4
Merge branch 'master' into claude-slack
Avi-Robusta Jun 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions docs/configuration/sinks/slack.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,174 @@ your own. This is not recommended for most companies due to the added complexity

When using a custom Slack app, callback buttons are not supported due to complexities in how Slack handles incoming
messages. :ref:`Contact us if you need assistance. <Getting Support>`
Message Templating
-------------------------------------------------------------------

Slack messages can be customized using Jinja2 templates. Robusta includes default templates that match the standard format, but you can override them for custom formatting.

To use custom templates, add them to the ``custom_templates`` parameter:

.. code-block:: yaml

sinksConfig:
- slack_sink:
name: main_slack_sink
slack_channel: "#alerts"
api_key: xoxb-112...
custom_templates:
header.j2: |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{{ status_emoji }} *CUSTOM ALERT: {{ title }}*"
}
}

{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": ":bell: {{ alert_type }} on cluster {{ cluster_name }}"
},
{
"type": "mrkdwn",
"text": "{{ severity_emoji }} {{ severity }}"
}
]
}

Templates use Slack's Block Kit format and must generate valid JSON. Each template block is separated by double newlines (``\n\n``).

Available template variables:

+-----------------------------+-------------------------------------------------------------+
| Variable | Description |
+=============================+=============================================================+
| ``title`` | The alert title |
+-----------------------------+-------------------------------------------------------------+
| ``status_text`` | "Firing" or "Resolved" |
+-----------------------------+-------------------------------------------------------------+
| ``status_emoji`` | "⚠️" (for firing) or "✅" (for resolved) |
+-----------------------------+-------------------------------------------------------------+
| ``severity`` | Alert severity (e.g., "Warning", "Critical") |
+-----------------------------+-------------------------------------------------------------+
| ``severity_emoji`` | Emoji for the severity level |
+-----------------------------+-------------------------------------------------------------+
| ``alert_type`` | "Alert", "K8s Event", or "Notification" |
+-----------------------------+-------------------------------------------------------------+
| ``cluster_name`` | The name of the cluster |
+-----------------------------+-------------------------------------------------------------+
| ``platform_enabled`` | Boolean indicating if Robusta platform is enabled |
+-----------------------------+-------------------------------------------------------------+
| ``include_investigate_link``| Boolean for including investigate link |
+-----------------------------+-------------------------------------------------------------+
| ``investigate_uri`` | URI for investigation |
+-----------------------------+-------------------------------------------------------------+
| ``resource_text`` | Resource identifier (e.g., "Pod/namespace/name") |
+-----------------------------+-------------------------------------------------------------+
| ``resource_emoji`` | Emoji for the resource type |
+-----------------------------+-------------------------------------------------------------+
| ``finding`` | The complete finding object as JSON |
+-----------------------------+-------------------------------------------------------------+

Currently available templates:

* ``header.j2`` - The header section of alert notificationsTemplate Styles and Customization
-------------------------------------------------------------------

Slack messages in Robusta can be customized using different template styles and Jinja2 templates.

Template Styles
~~~~~~~~~~~~~~

Robusta supports two built-in template styles:

1. **default** - Modern JIRA-style formatting (default)
2. **legacy** - Classic formatting matching Robusta's original style

To select a template style:

.. code-block:: yaml

sinksConfig:
- slack_sink:
name: main_slack_sink
slack_channel: "#alerts"
api_key: xoxb-112...
template_style: "legacy" # Use "default" or "legacy"

Custom Templates
~~~~~~~~~~~~~~~

For complete control over message formatting, you can provide custom Jinja2 templates:

.. code-block:: yaml

sinksConfig:
- slack_sink:
name: main_slack_sink
slack_channel: "#alerts"
api_key: xoxb-112...
custom_templates:
header.j2: |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{{ status_emoji }} *CUSTOM ALERT: {{ title }}*"
}
}

{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": ":bell: {{ alert_type }} on cluster {{ cluster_name }}"
},
{
"type": "mrkdwn",
"text": "{{ severity_emoji }} {{ severity }}"
}
]
}

Templates use Slack's Block Kit format and must generate valid JSON. Each template block is separated by double newlines (``\n\n``).

Available template variables:

+-----------------------------+-------------------------------------------------------------+
| Variable | Description |
+=============================+=============================================================+
| ``title`` | The alert title |
+-----------------------------+-------------------------------------------------------------+
| ``status_text`` | "Firing" or "Resolved" |
+-----------------------------+-------------------------------------------------------------+
| ``status_emoji`` | "⚠️" (for firing) or "✅" (for resolved) |
+-----------------------------+-------------------------------------------------------------+
| ``severity`` | Alert severity (e.g., "Warning", "Critical") |
+-----------------------------+-------------------------------------------------------------+
| ``severity_emoji`` | Emoji for the severity level |
+-----------------------------+-------------------------------------------------------------+
| ``alert_type`` | "Alert", "K8s Event", or "Notification" |
+-----------------------------+-------------------------------------------------------------+
| ``cluster_name`` | The name of the cluster |
+-----------------------------+-------------------------------------------------------------+
| ``platform_enabled`` | Boolean indicating if Robusta platform is enabled |
+-----------------------------+-------------------------------------------------------------+
| ``include_investigate_link``| Boolean for including investigate link |
+-----------------------------+-------------------------------------------------------------+
| ``investigate_uri`` | URI for investigation |
+-----------------------------+-------------------------------------------------------------+
| ``resource_text`` | Resource identifier (e.g., "Pod/namespace/name") |
+-----------------------------+-------------------------------------------------------------+
| ``resource_emoji`` | Emoji for the resource type |
+-----------------------------+-------------------------------------------------------------+
| ``finding`` | The complete finding object with all alert data |
+-----------------------------+-------------------------------------------------------------+

Currently available templates:

* ``header.j2`` - The header section of alert notifications
36 changes: 36 additions & 0 deletions src/robusta/core/reporting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ def __init__(

def __str__(self):
return f"annotations: {self.annotations} Enrichment: {self.blocks} "

def to_dict(self):
return {
"blocks": [block.dict() for block in self.blocks],
"annotations": self.annotations,
"enrichment_type": self.enrichment_type,
"title": self.title,
}


class FilterableScopeMatcher(BaseScopeMatcher):
Expand Down Expand Up @@ -404,3 +412,31 @@ def __calculate_fingerprint(subject: FindingSubject, source: FindingSource, aggr
# if not, generate with logic similar to alertmanager
s = f"{subject.subject_type},{subject.name},{subject.namespace},{subject.node},{source.value}{aggregation_key}"
return hashlib.sha256(s.encode()).hexdigest()

def to_json(self):
return {
"title": self.title,
"aggregation_key": self.aggregation_key,
"severity": self.severity.name,
"source": self.source.name,
"description": self.description,
"subject": {
"name": self.subject.name,
"subject_type": self.subject.subject_type.value,
"namespace": self.subject.namespace,
"node": self.subject.node,
"container": self.subject.container,
"labels": self.subject.labels,
"annotations": self.subject.annotations,
},
"finding_type": self.finding_type.name,
"failure": self.failure,
"creation_date": self.creation_date,
"fingerprint": self.fingerprint,
"starts_at": self.starts_at,
"ends_at": self.ends_at,
"add_silence_url": self.add_silence_url,
"silence_labels": self.silence_labels,
"enrichments": [enrichment.to_dict() for enrichment in self.enrichments],
"links": [link.dict() for link in self.links],
}
2 changes: 2 additions & 0 deletions src/robusta/core/sinks/sink_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from robusta.core.sinks.sink_base import SinkBase
from robusta.core.sinks.sink_config import SinkConfigBase
from robusta.core.sinks.slack import SlackSink, SlackSinkConfigWrapper
from robusta.core.sinks.slack.preview import SlackSinkPreview, SlackSinkPreviewConfigWrapper
from robusta.core.sinks.telegram import TelegramSink, TelegramSinkConfigWrapper
from robusta.core.sinks.victorops import VictoropsConfigWrapper, VictoropsSink
from robusta.core.sinks.webex import WebexSink, WebexSinkConfigWrapper
Expand All @@ -35,6 +36,7 @@
class SinkFactory:
__sink_config_mapping: Dict[Type[SinkConfigBase], Type[SinkBase]] = {
SlackSinkConfigWrapper: SlackSink,
SlackSinkPreviewConfigWrapper: SlackSinkPreview,
RocketchatSinkConfigWrapper: RocketchatSink,
RobustaSinkConfigWrapper: RobustaSink,
MsTeamsSinkConfigWrapper: MsTeamsSink,
Expand Down
2 changes: 2 additions & 0 deletions src/robusta/core/sinks/slack/preview/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from robusta.core.sinks.slack.preview.slack_sink_preview import SlackSinkPreview
from robusta.core.sinks.slack.preview.slack_sink_params_preview import SlackSinkPreviewConfigWrapper, SlackSinkPreviewParams
14 changes: 14 additions & 0 deletions src/robusta/core/sinks/slack/preview/slack_sink_preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from robusta.core.sinks.slack.preview.slack_sink_preview_params import SlackSinkPreviewConfigWrapper, SlackSinkPreviewParams
from robusta.core.sinks.slack.slack_sink import SlackSink
from robusta.integrations import slack as slack_module


class SlackSinkPreview(SlackSink):
params: SlackSinkPreviewParams

def __init__(self, sink_config: SlackSinkPreviewConfigWrapper, registry):
self.slack_sender = slack_module.SlackSender(
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel, is_preview=True
)
super().__init__(sink_config.slack_sink, registry, self.slack_sender)
Comment thread
Avi-Robusta marked this conversation as resolved.
Outdated

43 changes: 43 additions & 0 deletions src/robusta/core/sinks/slack/preview/slack_sink_preview_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from robusta.core.sinks.sink_base_params import SinkBaseParams
from robusta.core.sinks.sink_config import SinkConfigBase
from robusta.core.sinks.slack import SlackSinkParams
from enum import Enum
from typing import Optional, Dict


class SlackTemplateStyle(str, Enum):
DEFAULT = "default"
LEGACY = "legacy"


class SlackSinkPreviewParams(SlackSinkParams):
template_style: SlackTemplateStyle = SlackTemplateStyle.DEFAULT # Use "legacy" for old-style formatting
slack_custom_templates: Optional[Dict[str, str]] = None # Template name -> custom template content
template_name: Optional[str] = None

Comment thread
Avi-Robusta marked this conversation as resolved.
def get_effective_template_name(self) -> str:
"""
Returns the template name to use for this sink. If template_name is set, use it.
Otherwise, use 'legacy.j2' if template_style is legacy, else 'header.j2'.
"""
if self.template_name:
return self.template_name
if self.template_style == SlackTemplateStyle.LEGACY:
return "legacy.j2"
return "header.j2"

def get_custom_template(self) -> Optional[str]:
"""
Returns the custom template string for the effective template name, if it exists.
"""
template_name = self.get_effective_template_name()
if self.slack_custom_templates and template_name in self.slack_custom_templates:
return self.slack_custom_templates[template_name]
return None


class SlackSinkPreviewConfigWrapper(SinkConfigBase):
slack_sink: SlackSinkPreviewParams

def get_params(self) -> SinkBaseParams:
return self.slack_sink
11 changes: 7 additions & 4 deletions src/robusta/core/sinks/slack/slack_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
class SlackSink(SinkBase):
params: SlackSinkParams

def __init__(self, sink_config: SlackSinkConfigWrapper, registry):
def __init__(self, sink_config: SlackSinkConfigWrapper, registry, slack_sender=None):
super().__init__(sink_config.slack_sink, registry)
self.slack_channel = sink_config.slack_sink.slack_channel
self.api_key = sink_config.slack_sink.api_key
self.slack_sender = slack_module.SlackSender(
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel
)
if slack_sender:
self.slack_sender = slack_sender
else:
self.slack_sender = slack_module.SlackSender(
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel
)
self.registry.subscribe("replace_callback_with_string", self)

def handle_event(self, event_name: str, **kwargs):
Expand Down
35 changes: 32 additions & 3 deletions src/robusta/core/sinks/slack/slack_sink_params.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
from robusta.core.sinks.sink_base_params import SinkBaseParams
from robusta.core.sinks.sink_config import SinkConfigBase
from robusta.core.sinks.common import ChannelTransformer

from typing import Optional
from enum import Enum
from typing import Optional, Dict, Literal
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
from pydantic import validator


class SlackTemplateStyle(str, Enum):
DEFAULT = "default"
LEGACY = "legacy"


class SlackSinkParams(SinkBaseParams):
slack_channel: str
api_key: str
channel_override: Optional[str] = None
max_log_file_limit_kb: int = 1000
investigate_link: bool = True

template_style: SlackTemplateStyle = SlackTemplateStyle.DEFAULT # Use "legacy" for old-style formatting
slack_custom_templates: Optional[Dict[str, str]] = None # Template name -> custom template content
template_name: Optional[str] = None

def get_effective_template_name(self) -> str:
"""
Returns the template name to use for this sink. If template_name is set, use it.
Otherwise, use 'legacy.j2' if template_style is legacy, else 'header.j2'.
"""
if self.template_name:
return self.template_name
if self.template_style == SlackTemplateStyle.LEGACY:
return "legacy.j2"
return "header.j2"

def get_custom_template(self) -> Optional[str]:
"""
Returns the custom template string for the effective template name, if it exists.
"""
template_name = self.get_effective_template_name()
if self.slack_custom_templates and template_name in self.slack_custom_templates:
return self.slack_custom_templates[template_name]
return None

@classmethod
def _supports_grouping(cls):
return True
Expand All @@ -30,4 +59,4 @@ class SlackSinkConfigWrapper(SinkConfigBase):
slack_sink: SlackSinkParams

def get_params(self) -> SinkBaseParams:
return self.slack_sink
return self.slack_sink
Loading
Loading