Skip to content
Merged
Show file tree
Hide file tree
Changes from 36 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
95 changes: 95 additions & 0 deletions docs/configuration/sinks/slack.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,98 @@ 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 change your `slack_sink` to `slack_sink_preview`, and add your templates to the ``slack_custom_templates`` parameter:

.. code-block:: yaml

sinksConfig:
- slack_sink_preview:
api_key: xoxb-198...
name: preview_slack_sink
slack_channel: demo-slack-preview
slack_custom_templates:
custom_template.j2: |-
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Custom Alert Format:\n {{ status_emoji }} [{{ status_text }}] {{ title }}",
"emoji": true
}
}

{
"type": "divider"
}

{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Type:* {{ alert_type }}"
},
{
"type": "mrkdwn",
"text": "*Severity:* {{ severity_emoji }} {{ severity }}"
},
{
"type": "mrkdwn",
"text": "*Cluster:* {{ cluster_name }}"
}
{% if resource_text %}
,
{
"type": "mrkdwn",
"text": "*Resource:*\n{{ resource_text }}"
}
{% endif %}
]
}


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 |
+-----------------------------+-------------------------------------------------------------+
| ``investigate_uri`` | URI for investigation |
+-----------------------------+-------------------------------------------------------------+
| ``resource_text`` | Resource identifier (e.g., "Pod/namespace/name") |
+-----------------------------+-------------------------------------------------------------+
| ``subject_kind`` | The Kubernetes resource kind (e.g., "Pod", "Deployment") |
+-----------------------------+-------------------------------------------------------------+
| ``subject_namespace`` | The Kubernetes namespace |
+-----------------------------+-------------------------------------------------------------+
| ``subject_name`` | The name of the Kubernetes resource |
+-----------------------------+-------------------------------------------------------------+
| ``resource_emoji`` | Emoji for the resource type |
+-----------------------------+-------------------------------------------------------------+
| ``mention`` | Any @mentions extracted from the title |
+-----------------------------+-------------------------------------------------------------+
| ``finding`` | The complete finding object with all alert data |
+-----------------------------+-------------------------------------------------------------+

2 changes: 2 additions & 0 deletions src/robusta/core/model/runner_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from robusta.core.sinks.rocketchat.rocketchat_sink_params import RocketchatSinkConfigWrapper
from robusta.core.sinks.servicenow.servicenow_sink_params import ServiceNowSinkConfigWrapper
from robusta.core.sinks.slack.slack_sink_params import SlackSinkConfigWrapper
from robusta.core.sinks.slack.preview.slack_sink_preview_params import SlackSinkPreviewConfigWrapper
from robusta.core.sinks.mail.mail_sink_params import MailSinkConfigWrapper
from robusta.core.sinks.telegram.telegram_sink_params import TelegramSinkConfigWrapper
from robusta.core.sinks.victorops.victorops_sink_params import VictoropsConfigWrapper
Expand Down Expand Up @@ -55,6 +56,7 @@ class RunnerConfig(BaseModel):
Union[
RobustaSinkConfigWrapper,
SlackSinkConfigWrapper,
SlackSinkPreviewConfigWrapper,
DataDogSinkConfigWrapper,
KafkaSinkConfigWrapper,
MsTeamsSinkConfigWrapper,
Expand Down
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],
}
3 changes: 3 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,8 @@
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.slack_sink_preview_params import SlackSinkPreviewConfigWrapper
from robusta.core.sinks.slack.preview.slack_sink_preview import SlackSinkPreview
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 +37,7 @@
class SinkFactory:
__sink_config_mapping: Dict[Type[SinkConfigBase], Type[SinkBase]] = {
SlackSinkConfigWrapper: SlackSink,
SlackSinkPreviewConfigWrapper: SlackSinkPreview,
RocketchatSinkConfigWrapper: RocketchatSink,
RobustaSinkConfigWrapper: RobustaSink,
MsTeamsSinkConfigWrapper: MsTeamsSink,
Expand Down
4 changes: 3 additions & 1 deletion src/robusta/core/sinks/slack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from robusta.core.sinks.slack.slack_sink_params import SlackSinkParams, SlackSinkConfigWrapper
from robusta.core.sinks.slack.slack_sink import SlackSink
from robusta.core.sinks.slack.slack_sink_params import SlackSinkConfigWrapper, SlackSinkParams

__all__ = ["SlackSink", "SlackSinkParams", "SlackSinkConfigWrapper"]
10 changes: 10 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,10 @@
from robusta.core.sinks.slack.preview.slack_sink_preview_params import SlackSinkPreviewConfigWrapper, SlackSinkPreviewParams
from robusta.core.sinks.slack.slack_sink import SlackSink


class SlackSinkPreview(SlackSink):
params: SlackSinkPreviewParams

def __init__(self, sink_config: SlackSinkPreviewConfigWrapper, registry):
super().__init__(sink_config, registry, is_preview=True)

42 changes: 42 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,42 @@
from robusta.core.sinks.sink_base_params import SinkBaseParams
from robusta.core.sinks.sink_config import SinkConfigBase
from robusta.core.sinks.slack.slack_sink_params import SlackSinkParams
from enum import Enum
from typing import Optional, Dict, Any


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


class SlackSinkPreviewParams(SlackSinkParams):
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.slack_custom_templates and len(self.slack_custom_templates) == 1:
return next(iter(self.slack_custom_templates))
return "header.j2"
Comment thread
Avi-Robusta marked this conversation as resolved.
Outdated

def get_custom_template(self) -> Optional[str]:
"""Get the custom template for the current template style"""
if not self.slack_custom_templates:
return None

template_name = self.get_effective_template_name()
if template_name not in self.slack_custom_templates:
return None

return self.slack_custom_templates[template_name]


class SlackSinkPreviewConfigWrapper(SinkConfigBase):
slack_sink_preview: SlackSinkPreviewParams

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

def __init__(self, sink_config: SlackSinkConfigWrapper, registry):
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
def __init__(self, sink_config: SlackSinkConfigWrapper, registry, is_preview=False):
slack_sink_params = sink_config.get_params()
super().__init__(slack_sink_params, registry)
self.slack_channel = slack_sink_params.slack_channel
self.api_key = slack_sink_params.api_key
self.slack_sender = slack_module.SlackSender(
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel, is_preview
)
self.registry.subscribe("replace_callback_with_string", self)

Expand Down
1 change: 0 additions & 1 deletion src/robusta/core/sinks/slack/slack_sink_params.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 pydantic import validator

Expand Down
73 changes: 73 additions & 0 deletions src/robusta/core/sinks/slack/templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Slack Message Templates

This directory contains the Jinja2 templates used to render Slack messages.

## How Templates Work

Slack messages are rendered using [Jinja2](https://jinja.palletsprojects.com/) templates. Each template produces one or more Slack Block Kit blocks in JSON format.

Templates are separated by double newlines (`\n\n`) to indicate separate blocks. Each block must be valid JSON that conforms to the [Slack Block Kit](https://api.slack.com/block-kit) format.

## Available Templates

- `header.j2`: The header section of alert notifications, including title and metadata

## Customizing Templates

Users can customize templates in the Robusta configuration by providing their own template content:

```yaml
sinks:
slack:
slack_sink:
name: slack
slack_channel: "#alerts"
api_key: "${SLACK_TOKEN}"
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 }}"
}
]
}
```

## Template Variables

Each template has access to different variables. Here are the variables available in the `header.j2` template:

| 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 indicating if investigate link should be included |
| `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 |

## Fallback Behavior

If Jinja2 is not available, a built-in fallback implementation will be used that produces the same output as the default template.
1 change: 1 addition & 0 deletions src/robusta/core/sinks/slack/templates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Slack message templates package
36 changes: 36 additions & 0 deletions src/robusta/core/sinks/slack/templates/header.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{# Default template for Slack message headers #}
{# This creates a JIRA-style header with title and context blocks #}

{# First create the title block with status #}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{{ status_emoji }} *[{{ status_text }}] {% if platform_enabled and include_investigate_link %}<{{ investigate_uri }}|{{ title }}>{% else %}{{ title }}{% endif %}*{% if mention %} {{ mention }}{% endif %}"
}
}

{# Then create the context block with metadata #}
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": ":bell: Type: {{ alert_type }}"
},
{
"type": "mrkdwn",
"text": "{{ severity_emoji }} Severity: {{ severity }}"
},
{
"type": "mrkdwn",
"text": ":globe_with_meridians: Cluster: {{ cluster_name }}"
}
{% if resource_text %}
,{
"type": "mrkdwn",
"text": "{{ resource_emoji }} Resource: {{ resource_text }}"
}
{% endif %}
]
}
Loading