Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
115 changes: 115 additions & 0 deletions docs/configuration/sinks/slack.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,118 @@ 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": "section",
"text": {
"type": "mrkdwn",
"text": "{{ status_emoji }} *[{{ status_text }}] {{ title }}*{% if mention %} {{ mention }}{% endif %}"
}
}

{
"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 %}
]
}

{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{% if labels %}*Labels:*\\n\\n{% for key, value in labels.items() %}• *{{ key }}*: {{ value }}\\n\\n{% endfor %}{% else %}*Labels:* _None_{% 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 |
+-----------------------------+-------------------------------------------------------------+
| ``description`` | The alert description |
+-----------------------------+-------------------------------------------------------------+
| ``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 |
+-----------------------------+-------------------------------------------------------------+
| ``labels`` | Kubernetes labels on the subject resource (dict) |
+-----------------------------+-------------------------------------------------------------+
| ``annotations`` | Kubernetes annotations on the subject resource (dict) |
+-----------------------------+-------------------------------------------------------------+
| ``fingerprint`` | The unique identifier for the alert |
+-----------------------------+-------------------------------------------------------------+
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
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
5 changes: 4 additions & 1 deletion src/robusta/core/sinks/slack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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

# to prevent circular imports in SlackSender, SlackSinkParams and SlackSinkPreviewParams
__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)

34 changes: 34 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,34 @@
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 typing import Optional, Dict
from pydantic import validator


class SlackSinkPreviewParams(SlackSinkParams):
#TODO: improve the SlackSinkPreviewParams so the slack_custom_templates can be defined once globally and
# only a template name needs to be passed to each channel in the config
slack_custom_templates: Optional[Dict[str, str]] = None # Template name -> custom template content
hide_buttons: bool = False
hide_enrichments: bool = False


@validator('slack_custom_templates')
def check_one_item(cls, v):
if v is not None and len(v) != 1:
raise ValueError("slack_custom_templates must contain exactly one key-value pair")
return v

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

return next(iter(self.slack_custom_templates.values()))


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
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 %}
]
}
95 changes: 95 additions & 0 deletions src/robusta/core/sinks/slack/templates/template_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import json
import logging
import os
from typing import Any, Dict, List

from jinja2 import Environment, FileSystemLoader, Template, select_autoescape

# Get the directory where our templates are stored
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))


class SlackTemplateLoader:
"""
Loads and renders Jinja2 templates for Slack messages.
"""

def __init__(self):
"""Initialize the template environment."""
self.env = Environment(
loader=FileSystemLoader(TEMPLATE_DIR),
autoescape=select_autoescape(["html", "xml"]),
trim_blocks=True,
lstrip_blocks=True,
)
# Cache for templates
self._templates: Dict[str, Template] = {}

def get_template(self, template_name: str) -> Template:
"""
Get a template by name, loading from file if not already cached.

Args:
template_name: The name of the template file (e.g., "header.j2")

Returns:
A Jinja2 Template object
"""
if template_name not in self._templates:
try:
self._templates[template_name] = self.env.get_template(template_name)
except Exception as e:
logging.error(f"Error loading template {template_name}: {e}")
# Return a simple default template as fallback
return Template("Template loading error")

return self._templates[template_name]

def render_to_blocks(self, template: Template, context: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Render a Jinja2 Template object using the provided context and parse the result as JSON to get Slack blocks.
Args:
template: A Jinja2 Template object
context: Dictionary of variables to pass to the template
Returns:
List of Slack block objects (dictionaries)
"""
try:
rendered = template.render(**context)
blocks = []
for block_str in rendered.strip().split("\n\n"):
if not block_str.strip():
continue
try:
# Try to parse as JSON, but if it fails, log and skip
block = json.loads(block_str)
blocks.append(block)
except json.JSONDecodeError as e:
logging.exception(f"Error parsing JSON from template output: {e}")
logging.warning(f"Problematic JSON (repr): {repr(block_str)}")
continue # Skip this block and continue
except Exception as e:
logging.error(f"Unexpected error parsing block: {e}")
logging.warning(f"Problematic JSON (repr): {repr(block_str)}")
continue
return blocks
except Exception as e:
logging.error(f"Error rendering template: {e}")
return []

def render_custom_template_to_blocks(self, custom_template: str, context: Dict[str, Any]) -> List[Dict[str, Any]]:
try:
template = Template(custom_template)
return self.render_to_blocks(template, context)
except Exception as e:
logging.error(f"Error rendering custom template: {e}")
return self.render_default_template_to_blocks(context)
Comment thread
Avi-Robusta marked this conversation as resolved.

def render_default_template_to_blocks(self, context: Dict[str, Any]) -> List[Dict[str, Any]]:
DEFAULT_TEMPLATE_NAME="header.j2"
template = self.get_template(DEFAULT_TEMPLATE_NAME)
return self.render_to_blocks(template, context)


# Singleton instance
template_loader = SlackTemplateLoader()
Loading
Loading