Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat: alert template processor + integration in notifiers (#10750)

Abhishek Kumar Singh804ea2a7f896
diff --git a/cmd/community/Dockerfile b/cmd/community/Dockerfile
index 22433506f6..47dd12c0b4 100644
--- a/cmd/community/Dockerfile
+++ b/cmd/community/Dockerfile
@@ -11,7 +11,7 @@ RUN apk update && \
COPY ./target/${OS}-${TARGETARCH}/signoz-community /root/signoz
-COPY ./templates/email /root/templates
+COPY ./templates /root/templates
COPY frontend/build/ /etc/signoz/web/
RUN chmod 755 /root /root/signoz
diff --git a/cmd/community/Dockerfile.multi-arch b/cmd/community/Dockerfile.multi-arch
index 3a6c479a60..01b21fb8e2 100644
--- a/cmd/community/Dockerfile.multi-arch
+++ b/cmd/community/Dockerfile.multi-arch
@@ -12,7 +12,7 @@ RUN apk update && \
rm -rf /var/cache/apk/*
COPY ./target/${OS}-${ARCH}/signoz-community /root/signoz-community
-COPY ./templates/email /root/templates
+COPY ./templates /root/templates
COPY frontend/build/ /etc/signoz/web/
RUN chmod 755 /root /root/signoz-community
diff --git a/cmd/enterprise/Dockerfile b/cmd/enterprise/Dockerfile
index 798055afb1..3d095e4cea 100644
--- a/cmd/enterprise/Dockerfile
+++ b/cmd/enterprise/Dockerfile
@@ -11,7 +11,7 @@ RUN apk update && \
COPY ./target/${OS}-${TARGETARCH}/signoz /root/signoz
-COPY ./templates/email /root/templates
+COPY ./templates /root/templates
COPY frontend/build/ /etc/signoz/web/
RUN chmod 755 /root /root/signoz
diff --git a/cmd/enterprise/Dockerfile.integration b/cmd/enterprise/Dockerfile.integration
index 880b5d471b..3434b04da8 100644
--- a/cmd/enterprise/Dockerfile.integration
+++ b/cmd/enterprise/Dockerfile.integration
@@ -26,7 +26,7 @@ RUN go mod download
COPY ./cmd/ ./cmd/
COPY ./ee/ ./ee/
COPY ./pkg/ ./pkg/
-COPY ./templates/email /root/templates
+COPY ./templates /root/templates
COPY Makefile Makefile
RUN TARGET_DIR=/root ARCHS=${TARGETARCH} ZEUS_URL=${ZEUSURL} LICENSE_URL=${ZEUSURL}/api/v1 make go-build-enterprise-race
diff --git a/cmd/enterprise/Dockerfile.multi-arch b/cmd/enterprise/Dockerfile.multi-arch
index 776548fcb9..08513762a0 100644
--- a/cmd/enterprise/Dockerfile.multi-arch
+++ b/cmd/enterprise/Dockerfile.multi-arch
@@ -12,7 +12,7 @@ RUN apk update && \
rm -rf /var/cache/apk/*
COPY ./target/${OS}-${ARCH}/signoz /root/signoz
-COPY ./templates/email /root/templates
+COPY ./templates /root/templates
COPY frontend/build/ /etc/signoz/web/
RUN chmod 755 /root /root/signoz
diff --git a/cmd/enterprise/Dockerfile.with-web.integration b/cmd/enterprise/Dockerfile.with-web.integration
index 0a3df98502..f98554d169 100644
--- a/cmd/enterprise/Dockerfile.with-web.integration
+++ b/cmd/enterprise/Dockerfile.with-web.integration
@@ -35,7 +35,7 @@ RUN go mod download
COPY ./cmd/ ./cmd/
COPY ./ee/ ./ee/
COPY ./pkg/ ./pkg/
-COPY ./templates/email /root/templates
+COPY ./templates /root/templates
COPY Makefile Makefile
RUN TARGET_DIR=/root ARCHS=${TARGETARCH} ZEUS_URL=${ZEUSURL} LICENSE_URL=${ZEUSURL}/api/v1 make go-build-enterprise-race
diff --git a/conf/example.yaml b/conf/example.yaml
index 1ed81eb8be..3a242c2972 100644
--- a/conf/example.yaml
+++ b/conf/example.yaml
@@ -182,6 +182,11 @@ alertmanager:
poll_interval: 1m
# The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy). Used for generating relative and absolute links back to Alertmanager itself.
external_url: http://localhost:8080
+ # The list of globs from which SigNoz's alertmanager notification templates are loaded (e.g. the email.signoz.html layout).
+ # This mirrors the upstream alertmanager `templates` config option. The upstream default templates (default.tmpl, email.tmpl)
+ # are always loaded from the embedded alertmanager assets, so only SigNoz's own templates need to be listed here.
+ templates:
+ - /opt/signoz/conf/templates/alertmanager/*.gotmpl
# The global configuration for the alertmanager. All the exahustive fields can be found in the upstream: https://github.com/prometheus/alertmanager/blob/efa05feffd644ba4accb526e98a8c6545d26a783/config/config.go#L833
global:
# ResolveTimeout is the time after which an alert is declared resolved if it has not been updated.
diff --git a/pkg/alertmanager/alertmanagernotify/email/email.go b/pkg/alertmanager/alertmanagernotify/email/email.go
index e7dd9ad064..7b56d1923a 100644
--- a/pkg/alertmanager/alertmanagernotify/email/email.go
+++ b/pkg/alertmanager/alertmanagernotify/email/email.go
@@ -23,7 +23,13 @@ import (
"sync"
"time"
+ htmltemplate "html/template"
+
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/templating/markdownrenderer"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
+ "github.com/SigNoz/signoz/pkg/types/ruletypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/alertmanager/config"
@@ -34,20 +40,39 @@ import (
const (
Integration = "email"
+
+ // alertEmailLayoutTemplate is the name of the HTML layout template that
+ // wraps the rendered alert bodies. It is loaded into the notification
+ // template (n.tmpl) from the alertmanager templates config and lives at
+ // templates/alertmanager/email.gotmpl.
+ alertEmailLayoutTemplate = "email.signoz.html"
)
// Email implements a Notifier for email notifications.
type Email struct {
- conf *config.EmailConfig
- tmpl *template.Template
- logger *slog.Logger
- hostname string
+ conf *config.EmailConfig
+ tmpl *template.Template
+ logger *slog.Logger
+ hostname string
+ templater alertmanagertypes.Templater
+}
+
+// layoutData is the value passed to the email.signoz.html layout
+// template. It embeds NotificationTemplateData so templates can reference
+// `.Alert.Status`, `.Alert.TotalFiring`, `.Alert.TotalResolved`,
+// `.NotificationTemplateData.ExternalURL`, etc. alongside the rendered
+// Title and per-alert Bodies.
+type layoutData struct {
+ alertmanagertypes.NotificationTemplateData
+ Title string
+ Bodies []htmltemplate.HTML
}
var errNoAuthUsernameConfigured = errors.NewInternalf(errors.CodeInternal, "no auth username configured")
-// New returns a new Email notifier.
-func New(c *config.EmailConfig, t *template.Template, l *slog.Logger) *Email {
+// New returns a new Email notifier. When the email.signoz.html layout is
+// not defined in t, custom-body alerts fall back to plain <div>-wrapped HTML.
+func New(c *config.EmailConfig, t *template.Template, l *slog.Logger, templater alertmanagertypes.Templater) *Email {
if _, ok := c.Headers["Subject"]; !ok {
c.Headers["Subject"] = config.DefaultEmailSubject
}
@@ -63,7 +88,7 @@ func New(c *config.EmailConfig, t *template.Template, l *slog.Logger) *Email {
if err != nil {
h = "localhost.localdomain"
}
- return &Email{conf: c, tmpl: t, logger: l, hostname: h}
+ return &Email{conf: c, tmpl: t, logger: l, hostname: h, templater: templater}
}
// auth resolves a string of authentication mechanisms.
@@ -199,9 +224,9 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
if ok, mech := c.Extension("AUTH"); ok {
auth, err := n.auth(mech)
- if err != nil && err != errNoAuthUsernameConfigured {
+ if err != nil && !errors.Is(err, errNoAuthUsernameConfigured) {
return true, errors.WrapInternalf(err, errors.CodeInternal, "find auth mechanism")
- } else if err == errNoAuthUsernameConfigured {
+ } else if errors.Is(err, errNoAuthUsernameConfigured) {
n.logger.DebugContext(ctx, "no auth username configured. Attempting to send email without authenticating")
}
if auth != nil {
@@ -245,6 +270,16 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
}
}
+ // Prepare the content for the email. subject, when non-empty, overrides
+ // the configured Subject header for this notification only. We deliberately
+ // do not mutate n.conf.Headers here: the config map is shared across
+ // concurrent notifications to the same receiver.
+ subject, htmlBody, err := n.prepareContent(ctx, as)
+ if err != nil {
+ n.logger.ErrorContext(ctx, "failed to prepare notification content", errors.Attr(err))
+ return false, err
+ }
+
// Send the email headers and body.
message, err := c.Data()
if err != nil {
@@ -262,6 +297,10 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
buffer := &bytes.Buffer{}
for header, t := range n.conf.Headers {
+ if header == "Subject" {
+ fmt.Fprintf(buffer, "%s: %s\r\n", header, mime.QEncoding.Encode("utf-8", subject))
+ continue
+ }
value, err := n.tmpl.ExecuteTextString(t, data)
if err != nil {
return false, errors.WrapInternalf(err, errors.CodeInternal, "execute %q header template", header)
@@ -336,7 +375,7 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
}
}
- if len(n.conf.HTML) > 0 {
+ if htmlBody != "" {
// Html template
// Preferred alternative placed last per section 5.1.4 of RFC 2046
// https://www.ietf.org/rfc/rfc2046.txt
@@ -347,12 +386,8 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
if err != nil {
return false, errors.WrapInternalf(err, errors.CodeInternal, "create part for html template")
}
- body, err := n.tmpl.ExecuteHTMLString(n.conf.HTML, data)
- if err != nil {
- return false, errors.WrapInternalf(err, errors.CodeInternal, "execute html template")
- }
qw := quotedprintable.NewWriter(w)
- _, err = qw.Write([]byte(body))
+ _, err = qw.Write([]byte(htmlBody))
if err != nil {
return true, errors.WrapInternalf(err, errors.CodeInternal, "write HTML part")
}
@@ -381,6 +416,124 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
return false, nil
}
+// prepareContent returns a subject override (empty when the default config
+// Subject should be used) and the HTML body for the email. Callers must treat
+// the subject as local state and never write it back to n.conf.Headers.
+func (n *Email) prepareContent(ctx context.Context, alerts []*types.Alert) (string, string, error) {
+ customTitle, customBody := alertmanagertemplate.ExtractTemplatesFromAnnotations(alerts)
+ result, err := n.templater.Expand(ctx, alertmanagertypes.ExpandRequest{
+ TitleTemplate: customTitle,
+ BodyTemplate: customBody,
+ DefaultTitleTemplate: n.conf.Headers["Subject"],
+ DefaultBodyTemplate: n.conf.HTML,
+ }, alerts)
+ if err != nil {
+ return "", "", err
+ }
+
+ subject := result.Title
+
+ if !result.IsDefaultBody {
+ // Custom-body path: render each expanded markdown body to HTML, then
+ // wrap the whole thing in the email.signoz.html layout (or fall
+ // back to plain <div> wrapping when the layout template is not loaded).
+ for i, body := range result.Body {
+ if body == "" {
+ continue
+ }
+ rendered, err := markdownrenderer.RenderHTML(body)
+ if err != nil {
+ return "", "", err
+ }
+ result.Body[i] = rendered
+ }
+ appendRelatedLinkButtons(alerts, result.Body)
+ html, err := n.renderLayout(result)
+ if err != nil {
+ n.logger.WarnContext(ctx, "custom email template rendering failed, falling back to plain <div> wrap", errors.Attr(err))
+ return subject, wrapBodiesAsDivs(result.Body), nil
+ }
+ return subject, html, nil
+ }
+
+ return subject, result.Body[0], nil
+}
+
+// renderLayout wraps result in the email.signoz.html HTML layout loaded
+// into n.tmpl from the alertmanager templates config. Returns an error when the
+// layout template is not defined (e.g. in tests where no templates are loaded)
+// so prepareContent can fall back to plain <div> wrapping.
+func (n *Email) renderLayout(result *alertmanagertypes.ExpandResult) (string, error) {
+ bodies := make([]htmltemplate.HTML, 0, len(result.Body))
+ for _, b := range result.Body {
+ bodies = append(bodies, htmltemplate.HTML(b))
+ }
+ data := layoutData{Title: result.Title, Bodies: bodies}
+ if result.NotificationData != nil {
+ data.NotificationTemplateData = *result.NotificationData
+ }
+ html, err := n.tmpl.ExecuteHTMLString(`{{ template "`+alertEmailLayoutTemplate+`" . }}`, data)
+ if err != nil {
+ return "", errors.WrapInternalf(err, errors.CodeInternal, "failed to render email layout")
+ }
+ return html, nil
+}
+
+// appendRelatedLinkButtons appends "View Related Logs/Traces" buttons to each
+// per-alert body when the rule manager attached the corresponding annotation.
+// bodies is positionally aligned with alerts (see alertmanagertemplate.Prepare);
+// empty bodies are skipped so we never attach a button to an alert that produced
+// no visible content.
+func appendRelatedLinkButtons(alerts []*types.Alert, bodies []string) {
+ for i := range bodies {
+ if i >= len(alerts) || bodies[i] == "" {
+ continue
+ }
+ if link := alerts[i].Annotations[ruletypes.AnnotationRelatedLogs]; link != "" {
+ bodies[i] += htmlButton("View Related Logs", string(link))
+ }
+ if link := alerts[i].Annotations[ruletypes.AnnotationRelatedTraces]; link != "" {
+ bodies[i] += htmlButton("View Related Traces", string(link))
+ }
+ }
+}
+
+func wrapBodiesAsDivs(bodies []string) string {
+ var b strings.Builder
+ for _, part := range bodies {
+ if part == "" {
+ continue
+ }
+ b.WriteString("<div>")
+ b.WriteString(part)
+ b.WriteString("</div>")
+ }
+ return b.String()
+}
+
+func htmlButton(text, url string) string {
+ return fmt.Sprintf(`
+ <a href="%s" target="_blank" style="text-decoration: none;">
+<button style="
+ padding: 6px 16px;
+ /* Default System Font */
+ font-family: sans-serif;
+ font-size: 14px;
+ font-weight: 500;
+ line-height: 1.5;
+ /* Light Theme & Dynamic Background (Solid) */
+ color: #111827;
+ background-color: #f9fafb;
+ /* Static Outline */
+ border: 1px solid #d1d5db;
+ border-radius: 4px;
+ cursor: pointer;
+">
+ %s
+</button>
+</a>`, url, text)
+}
+
type loginAuth struct {
username, password string
}
diff --git a/pkg/alertmanager/alertmanagernotify/email/email_test.go b/pkg/alertmanager/alertmanagernotify/email/email_test.go
index 69d1f4943a..f7dd5c5ea9 100644
--- a/pkg/alertmanager/alertmanagernotify/email/email_test.go
+++ b/pkg/alertmanager/alertmanagernotify/email/email_test.go
@@ -8,6 +8,7 @@ import (
"context"
"fmt"
"io"
+ "log/slog"
"net"
"net/http"
"net/url"
@@ -17,7 +18,10 @@ import (
"testing"
"time"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
+ "github.com/SigNoz/signoz/pkg/types/ruletypes"
"github.com/emersion/go-smtp"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
@@ -42,6 +46,11 @@ const (
emailFrom = "[email protected]"
)
+// testTemplater returns a Templater bound to tmpl with a discard logger.
+func testTemplater(tmpl *template.Template) alertmanagertypes.Templater {
+ return alertmanagertemplate.New(tmpl, slog.New(slog.DiscardHandler))
+}
+
// email represents an email returned by the MailDev REST API.
// See https://github.com/djfarrelly/MailDev/blob/master/docs/rest.md.
type email struct {
@@ -162,7 +171,7 @@ func notifyEmailWithContext(ctx context.Context, t *testing.T, cfg *config.Email
return nil, false, err
}
- email := New(cfg, tmpl, promslog.NewNopLogger())
+ email := New(cfg, tmpl, promslog.NewNopLogger(), testTemplater(tmpl))
retry, err := email.Notify(ctx, firingAlert)
if err != nil {
@@ -706,7 +715,7 @@ func TestEmailRejected(t *testing.T) {
tmpl, firingAlert, err := prepare(cfg)
require.NoError(t, err)
- e := New(cfg, tmpl, promslog.NewNopLogger())
+ e := New(cfg, tmpl, promslog.NewNopLogger(), testTemplater(tmpl))
// Send the alert to mock SMTP server.
retry, err := e.Notify(context.Background(), firingAlert)
@@ -1030,6 +1039,135 @@ func TestEmailImplicitTLS(t *testing.T) {
}
}
+func TestPrepareContent(t *testing.T) {
+ t.Run("default title template; custom body template", func(t *testing.T) {
+ tmpl, err := template.FromGlobs([]string{})
+ require.NoError(t, err)
+ tmpl.ExternalURL, _ = url.Parse("http://am")
+
+ bodyTpl := "line $labels.instance"
+ a1 := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{
+ model.LabelName("instance"): model.LabelValue("one"),
+ },
+ Annotations: model.LabelSet{
+ model.LabelName(ruletypes.AnnotationBodyTemplate): model.LabelValue(bodyTpl),
+ },
+ },
+ }
+ a2 := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{
+ model.LabelName("instance"): model.LabelValue("two"),
+ },
+ Annotations: model.LabelSet{
+ model.LabelName(ruletypes.AnnotationBodyTemplate): model.LabelValue(bodyTpl),
+ },
+ },
+ }
+ alerts := []*types.Alert{a1, a2}
+
+ cfg := &config.EmailConfig{Headers: map[string]string{"Subject": "subj"}}
+ n := New(cfg, tmpl, promslog.NewNopLogger(), testTemplater(tmpl))
+
+ ctx := context.Background()
+ subject, htmlBody, err := n.prepareContent(ctx, alerts)
+ require.NoError(t, err)
+ require.Equal(t, "subj", subject)
+ require.Equal(t, "<div><p>line one</p>\n</div><div><p>line two</p>\n</div>", htmlBody)
+ })
+
+ t.Run("custom title template; default body HTML template", func(t *testing.T) {
+ tmpl, err := template.FromGlobs([]string{})
+ require.NoError(t, err)
+ tmpl.ExternalURL, _ = url.Parse("http://am")
+
+ firingAlert := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{},
+ Annotations: model.LabelSet{
+ model.LabelName(ruletypes.AnnotationTitleTemplate): model.LabelValue("fixed from $alert.status"),
+ },
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ }
+ alerts := []*types.Alert{firingAlert}
+ cfg := &config.EmailConfig{
+ Headers: map[string]string{},
+ HTML: "Status: {{ .Status }}",
+ }
+ n := New(cfg, tmpl, promslog.NewNopLogger(), testTemplater(tmpl))
+
+ ctx := context.Background()
+ subject, htmlBody, err := n.prepareContent(ctx, alerts)
+ require.NoError(t, err)
+ require.Equal(t, "Status: firing", htmlBody)
+ require.Equal(t, "fixed from firing", subject)
+ })
+
+ t.Run("default template without HTML", func(t *testing.T) {
+ cfg := &config.EmailConfig{Headers: map[string]string{"Subject": "the email subject"}}
+ tmpl, err := template.FromGlobs([]string{})
+ require.NoError(t, err)
+ tmpl.ExternalURL, _ = url.Parse("http://am")
+ n := New(cfg, tmpl, promslog.NewNopLogger(), testTemplater(tmpl))
+
+ firingAlert := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{},
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ }
+ alerts := []*types.Alert{firingAlert}
+ ctx := context.Background()
+ subject, htmlBody, err := n.prepareContent(ctx, alerts)
+ require.NoError(t, err)
+ require.Equal(t, "", htmlBody)
+ require.Equal(t, "the email subject", subject)
+ })
+
+ t.Run("custom title template; custom body template", func(t *testing.T) {
+ // Load the email.signoz.html layout into the notification template
+ // the same way the alertmanager server does via the templates config.
+ tmpl, err := template.FromGlobs([]string{"../../../../templates/alertmanager/*.gotmpl"})
+ require.NoError(t, err)
+ tmpl.ExternalURL, _ = url.Parse("http://am")
+
+ firingAlert := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{
+ model.LabelName("instance"): model.LabelValue("two"),
+ },
+ Annotations: model.LabelSet{
+ model.LabelName(ruletypes.AnnotationTitleTemplate): model.LabelValue("fixed from $alert.status"),
+ model.LabelName(ruletypes.AnnotationBodyTemplate): model.LabelValue("line $labels.instance"),
+ },
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ }
+ alerts := []*types.Alert{firingAlert}
+ cfg := &config.EmailConfig{
+ Headers: map[string]string{"Subject": "subject"},
+ HTML: "Well, what are you?",
+ }
+
+ n := New(cfg, tmpl, promslog.NewNopLogger(), testTemplater(tmpl))
+
+ ctx := context.Background()
+ subject, htmlBody, err := n.prepareContent(ctx, alerts)
+ require.NoError(t, err)
+ require.Contains(t, htmlBody, "<!DOCTYPE html>")
+ require.Contains(t, htmlBody, "<p>line two</p>")
+ require.NotContains(t, htmlBody, "Well, what are you?")
+ require.Equal(t, subject, "fixed from firing")
+ require.NotContains(t, subject, "subject")
+ })
+}
+
func ptrTo(b bool) *bool {
return &b
}
diff --git a/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2.go b/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2.go
index 912bf14792..79ca1aa465 100644
--- a/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2.go
+++ b/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2.go
@@ -15,7 +15,9 @@ import (
"slices"
"strings"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
@@ -44,6 +46,7 @@ type Notifier struct {
retrier *notify.Retrier
webhookURL *config.SecretURL
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
+ templater alertmanagertypes.Templater
}
// https://learn.microsoft.com/en-us/connectors/teams/?tabs=text1#adaptivecarditemschema
@@ -52,7 +55,7 @@ type Content struct {
Type string `json:"type"`
Version string `json:"version"`
Body []Body `json:"body"`
- Msteams Msteams `json:"msteams,omitempty"`
+ Msteams Msteams `json:"msteams,omitzero"`
Actions []Action `json:"actions"`
}
@@ -94,7 +97,7 @@ type teamsMessage struct {
}
// New returns a new notifier that uses the Microsoft Teams Power Platform connector.
-func New(c *config.MSTeamsV2Config, t *template.Template, titleLink string, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
+func New(c *config.MSTeamsV2Config, t *template.Template, titleLink string, l *slog.Logger, templater alertmanagertypes.Templater, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
client, err := notify.NewClientWithTracing(*c.HTTPConfig, Integration, httpOpts...)
if err != nil {
return nil, err
@@ -109,6 +112,7 @@ func New(c *config.MSTeamsV2Config, t *template.Template, titleLink string, l *s
retrier: &notify.Retrier{},
webhookURL: c.WebhookURL,
postJSONFunc: notify.PostJSON,
+ templater: templater,
}
return n, nil
@@ -128,25 +132,11 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}
- title := tmpl(n.conf.Title)
- if err != nil {
- return false, err
- }
-
titleLink := tmpl(n.titleLink)
if err != nil {
return false, err
}
- alerts := types.Alerts(as...)
- color := colorGrey
- switch alerts.Status() {
- case model.AlertFiring:
- color = colorRed
- case model.AlertResolved:
- color = colorGreen
- }
-
var url string
if n.conf.WebhookURL != nil {
url = n.conf.WebhookURL.String()
@@ -158,6 +148,12 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
url = strings.TrimSpace(string(content))
}
+ bodyBlocks, err := n.prepareContent(ctx, as)
+ if err != nil {
+ n.logger.ErrorContext(ctx, "failed to prepare notification content", errors.Attr(err))
+ return false, err
+ }
+
// A message as referenced in https://learn.microsoft.com/en-us/connectors/teams/?tabs=text1%2Cdotnet#request-body-schema
t := teamsMessage{
Type: "message",
@@ -169,17 +165,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
Schema: "http://adaptivecards.io/schemas/adaptive-card.json",
Type: "AdaptiveCard",
Version: "1.2",
- Body: []Body{
- {
- Type: "TextBlock",
- Text: title,
- Weight: "Bolder",
- Size: "Medium",
- Wrap: true,
- Style: "heading",
- Color: color,
- },
- },
+ Body: bodyBlocks,
Actions: []Action{
{
Type: "Action.OpenUrl",
@@ -195,20 +181,6 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
},
}
- // add labels and annotations to the body of all alerts
- for _, alert := range as {
- t.Attachments[0].Content.Body = append(t.Attachments[0].Content.Body, Body{
- Type: "TextBlock",
- Text: "Alerts",
- Weight: "Bolder",
- Size: "Medium",
- Wrap: true,
- Color: color,
- })
-
- t.Attachments[0].Content.Body = append(t.Attachments[0].Content.Body, n.createLabelsAndAnnotationsBody(alert)...)
- }
-
var payload bytes.Buffer
if err = json.NewEncoder(&payload).Encode(t); err != nil {
return false, err
@@ -228,6 +200,75 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return shouldRetry, err
}
+// prepareContent builds the Adaptive Card body blocks for the notification.
+// The first block is always the title; the remainder depends on whether the
+// alerts carried a custom body template.
+func (n *Notifier) prepareContent(ctx context.Context, alerts []*types.Alert) ([]Body, error) {
+ customTitle, customBody := alertmanagertemplate.ExtractTemplatesFromAnnotations(alerts)
+ result, err := n.templater.Expand(ctx, alertmanagertypes.ExpandRequest{
+ TitleTemplate: customTitle,
+ BodyTemplate: customBody,
+ DefaultTitleTemplate: n.conf.Title,
+ DefaultBodyTemplate: n.conf.Text,
+ }, alerts)
+ if err != nil {
+ return nil, err
+ }
+
+ color := colorGrey
+ switch types.Alerts(alerts...).Status() {
+ case model.AlertFiring:
+ color = colorRed
+ case model.AlertResolved:
+ color = colorGreen
+ }
+
+ blocks := []Body{{
+ Type: "TextBlock",
+ Text: result.Title,
+ Weight: "Bolder",
+ Size: "Medium",
+ Wrap: true,
+ Style: "heading",
+ Color: color,
+ }}
+
+ if result.IsDefaultBody {
+ for _, alert := range alerts {
+ blocks = append(blocks, Body{
+ Type: "TextBlock",
+ Text: "Alerts",
+ Weight: "Bolder",
+ Size: "Medium",
+ Wrap: true,
+ Color: color,
+ })
+ blocks = append(blocks, n.createLabelsAndAnnotationsBody(alert)...)
+ }
+ return blocks, nil
+ }
+
+ // Custom body path: result.Body is positionally aligned with alerts;
+ // entries for alerts whose template rendered empty are kept as "" so we
+ // can skip them here without shifting the per-alert color index.
+ for i, body := range result.Body {
+ if body == "" || i >= len(alerts) {
+ continue
+ }
+ perAlertColor := colorRed
+ if alerts[i].Resolved() {
+ perAlertColor = colorGreen
+ }
+ blocks = append(blocks, Body{
+ Type: "TextBlock",
+ Text: body,
+ Wrap: true,
+ Color: perAlertColor,
+ })
+ }
+ return blocks, nil
+}
+
func (*Notifier) createLabelsAndAnnotationsBody(alert *types.Alert) []Body {
bodies := []Body{}
bodies = append(bodies, Body{
@@ -258,7 +299,8 @@ func (*Notifier) createLabelsAndAnnotationsBody(alert *types.Alert) []Body {
annotationsFacts := []Fact{}
for k, v := range alert.Annotations {
- if slices.Contains([]string{"summary", "related_logs", "related_traces"}, string(k)) {
+ if slices.Contains([]string{"summary", "related_logs", "related_traces"}, string(k)) ||
+ alertmanagertypes.IsPrivateAnnotation(string(k)) {
continue
}
annotationsFacts = append(annotationsFacts, Fact{Title: string(k), Value: string(v)})
diff --git a/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2_test.go b/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2_test.go
index 7eb0cccc88..a29148a5d5 100644
--- a/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2_test.go
+++ b/pkg/alertmanager/alertmanagernotify/msteamsv2/msteamsv2_test.go
@@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"io"
+ "log/slog"
"net/http"
"net/http/httptest"
"net/url"
@@ -15,6 +16,9 @@ import (
"testing"
"time"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
+ "github.com/SigNoz/signoz/pkg/types/ruletypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
@@ -23,21 +27,28 @@ import (
test "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagernotify/alertmanagernotifytest"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
+ "github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
)
+func newTestTemplater(tmpl *template.Template) alertmanagertypes.Templater {
+ return alertmanagertemplate.New(tmpl, slog.New(slog.DiscardHandler))
+}
+
// This is a test URL that has been modified to not be valid.
var testWebhookURL, _ = url.Parse("https://example.westeurope.logic.azure.com:443/workflows/xxx/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=xxx")
func TestMSTeamsV2Retry(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.MSTeamsV2Config{
WebhookURL: &config.SecretURL{URL: testWebhookURL},
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
`{{ template "msteamsv2.default.titleLink" . }}`,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -64,14 +75,16 @@ func TestNotifier_Notify_WithReason(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.MSTeamsV2Config{
WebhookURL: &config.SecretURL{URL: testWebhookURL},
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
`{{ template "msteamsv2.default.titleLink" . }}`,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -153,7 +166,8 @@ func TestMSTeamsV2Templating(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
tc.cfg.WebhookURL = &config.SecretURL{URL: u}
tc.cfg.HTTPConfig = &commoncfg.HTTPClientConfig{}
- pd, err := New(tc.cfg, test.CreateTmpl(t), tc.titleLink, promslog.NewNopLogger())
+ tmpl := test.CreateTmpl(t)
+ pd, err := New(tc.cfg, tmpl, tc.titleLink, promslog.NewNopLogger(), newTestTemplater(tmpl))
require.NoError(t, err)
ctx := context.Background()
@@ -186,20 +200,124 @@ func TestMSTeamsV2RedactedURL(t *testing.T) {
defer fn()
secret := "secret"
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.MSTeamsV2Config{
WebhookURL: &config.SecretURL{URL: u},
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
`{{ template "msteamsv2.default.titleLink" . }}`,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
test.AssertNotifyLeaksNoSecret(ctx, t, notifier, secret)
}
+func TestPrepareContent(t *testing.T) {
+ t.Run("default template - firing alerts", func(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
+ notifier, err := New(
+ &config.MSTeamsV2Config{
+ WebhookURL: &config.SecretURL{URL: testWebhookURL},
+ HTTPConfig: &commoncfg.HTTPClientConfig{},
+ Title: "Alertname: {{ .CommonLabels.alertname }}",
+ },
+ tmpl,
+ `{{ template "msteamsv2.default.titleLink" . }}`,
+ promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
+ )
+ require.NoError(t, err)
+
+ ctx := context.Background()
+ ctx = notify.WithGroupKey(ctx, "1")
+
+ alerts := []*types.Alert{
+ {
+ Alert: model.Alert{
+ Labels: model.LabelSet{"alertname": "test"},
+ // Custom body template
+ Annotations: model.LabelSet{
+ ruletypes.AnnotationBodyTemplate: "Firing alert: $alertname",
+ },
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ },
+ }
+ blocks, err := notifier.prepareContent(ctx, alerts)
+ require.NoError(t, err)
+ require.NotEmpty(t, blocks)
+ // First block should be the title with color (firing = red)
+ require.Equal(t, "Bolder", blocks[0].Weight)
+ require.Equal(t, colorRed, blocks[0].Color)
+ // verify title text
+ require.Equal(t, "Alertname: test", blocks[0].Text)
+ // verify body text
+ require.Equal(t, "Firing alert: test", blocks[1].Text)
+ })
+
+ t.Run("custom template - per-alert color", func(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
+ notifier, err := New(
+ &config.MSTeamsV2Config{
+ WebhookURL: &config.SecretURL{URL: testWebhookURL},
+ HTTPConfig: &commoncfg.HTTPClientConfig{},
+ },
+ tmpl,
+ `{{ template "msteamsv2.default.titleLink" . }}`,
+ promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
+ )
+ require.NoError(t, err)
+
+ ctx := context.Background()
+ ctx = notify.WithGroupKey(ctx, "1")
+
+ alerts := []*types.Alert{
+ {
+ Alert: model.Alert{
+ Labels: model.LabelSet{"alertname": "test1"},
+ Annotations: model.LabelSet{
+ "summary": "test",
+ ruletypes.AnnotationTitleTemplate: "Custom Title",
+ ruletypes.AnnotationBodyTemplate: "custom body $alertname",
+ },
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ },
+ {
+ Alert: model.Alert{
+ Labels: model.LabelSet{"alertname": "test2"},
+ Annotations: model.LabelSet{
+ "summary": "test",
+ ruletypes.AnnotationTitleTemplate: "Custom Title",
+ ruletypes.AnnotationBodyTemplate: "custom body $alertname",
+ },
+ StartsAt: time.Now().Add(-time.Hour),
+ EndsAt: time.Now().Add(-time.Minute),
+ },
+ },
+ }
+ blocks, err := notifier.prepareContent(ctx, alerts)
+ require.NoError(t, err)
+ require.NotEmpty(t, blocks)
+ // total 3 blocks: title and 2 body blocks
+ require.True(t, len(blocks) == 3)
+ // First block: title color is overall color of the alerts
+ require.Equal(t, colorRed, blocks[0].Color)
+ // verify title text
+ require.Equal(t, "Custom Title", blocks[0].Text)
+ // Body blocks should have per-alert color
+ require.Equal(t, colorRed, blocks[1].Color) // firing
+ require.Equal(t, colorGreen, blocks[2].Color) // resolved
+ })
+}
+
func TestMSTeamsV2ReadingURLFromFile(t *testing.T) {
ctx, u, fn := test.GetContextWithCancelingURL()
defer fn()
@@ -209,14 +327,16 @@ func TestMSTeamsV2ReadingURLFromFile(t *testing.T) {
_, err = f.WriteString(u.String() + "\n")
require.NoError(t, err, "writing to temp file failed")
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.MSTeamsV2Config{
WebhookURLFile: f.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
`{{ template "msteamsv2.default.titleLink" . }}`,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
diff --git a/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie.go b/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie.go
index d30d0580e4..96bfc34675 100644
--- a/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie.go
+++ b/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie.go
@@ -15,7 +15,10 @@ import (
"os"
"strings"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/templating/markdownrenderer"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
@@ -34,25 +37,27 @@ const maxMessageLenRunes = 130
// Notifier implements a Notifier for OpsGenie notifications.
type Notifier struct {
- conf *config.OpsGenieConfig
- tmpl *template.Template
- logger *slog.Logger
- client *http.Client
- retrier *notify.Retrier
+ conf *config.OpsGenieConfig
+ tmpl *template.Template
+ logger *slog.Logger
+ client *http.Client
+ retrier *notify.Retrier
+ templater alertmanagertypes.Templater
}
// New returns a new OpsGenie notifier.
-func New(c *config.OpsGenieConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
+func New(c *config.OpsGenieConfig, t *template.Template, l *slog.Logger, templater alertmanagertypes.Templater, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
client, err := notify.NewClientWithTracing(*c.HTTPConfig, Integration, httpOpts...)
if err != nil {
return nil, err
}
return &Notifier{
- conf: c,
- tmpl: t,
- logger: l,
- client: client,
- retrier: &notify.Retrier{RetryCodes: []int{http.StatusTooManyRequests}},
+ conf: c,
+ tmpl: t,
+ logger: l,
+ client: client,
+ retrier: &notify.Retrier{RetryCodes: []int{http.StatusTooManyRequests}},
+ templater: templater,
}, nil
}
@@ -123,6 +128,55 @@ func safeSplit(s, sep string) []string {
return b
}
+// prepareContent expands alert templates and returns the OpsGenie-ready title
+// (truncated to the 130-rune limit) and HTML description. Custom bodies are
+// rendered to HTML and stitched together with <hr> dividers; default bodies
+// are joined with newlines (OpsGenie's legacy plain-text description).
+func (n *Notifier) prepareContent(ctx context.Context, alerts []*types.Alert) (string, string, error) {
+ customTitle, customBody := alertmanagertemplate.ExtractTemplatesFromAnnotations(alerts)
+ result, err := n.templater.Expand(ctx, alertmanagertypes.ExpandRequest{
+ TitleTemplate: customTitle,
+ BodyTemplate: customBody,
+ DefaultTitleTemplate: n.conf.Message,
+ DefaultBodyTemplate: n.conf.Description,
+ }, alerts)
+ if err != nil {
+ return "", "", err
+ }
+
+ var description string
+ if result.IsDefaultBody {
+ description = strings.Join(result.Body, "\n")
+ } else {
+ var b strings.Builder
+ first := true
+ for _, part := range result.Body {
+ if part == "" {
+ continue
+ }
+ rendered, renderErr := markdownrenderer.RenderHTML(part)
+ if renderErr != nil {
+ return "", "", renderErr
+ }
+ if !first {
+ b.WriteString("<hr>")
+ }
+ b.WriteString("<div>")
+ b.WriteString(rendered)
+ b.WriteString("</div>")
+ first = false
+ }
+ description = b.String()
+ }
+
+ title, truncated := notify.TruncateInRunes(result.Title, maxMessageLenRunes)
+ if truncated {
+ n.logger.WarnContext(ctx, "Truncated message", slog.Int("max_runes", maxMessageLenRunes))
+ }
+
+ return title, description, nil
+}
+
// Create requests for a list of alerts.
func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*http.Request, bool, error) {
key, err := notify.ExtractGroupKey(ctx)
@@ -168,9 +222,10 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
}
requests = append(requests, req.WithContext(ctx))
default:
- message, truncated := notify.TruncateInRunes(tmpl(n.conf.Message), maxMessageLenRunes)
- if truncated {
- logger.WarnContext(ctx, "Truncated message", slog.Any("alert", key), slog.Int("max_runes", maxMessageLenRunes))
+ message, description, err := n.prepareContent(ctx, as)
+ if err != nil {
+ n.logger.ErrorContext(ctx, "failed to prepare notification content", errors.Attr(err))
+ return nil, false, err
}
createEndpointURL := n.conf.APIURL.Copy()
@@ -209,7 +264,7 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
msg := &opsGenieCreateMessage{
Alias: alias,
Message: message,
- Description: tmpl(n.conf.Description),
+ Description: description,
Details: details,
Source: tmpl(n.conf.Source),
Responders: responders,
diff --git a/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie_test.go b/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie_test.go
index a367dec09e..887ec266cb 100644
--- a/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie_test.go
+++ b/pkg/alertmanager/alertmanagernotify/opsgenie/opsgenie_test.go
@@ -8,12 +8,16 @@ import (
"context"
"fmt"
"io"
+ "log/slog"
"net/http"
"net/url"
"os"
"testing"
"time"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
+ "github.com/SigNoz/signoz/pkg/types/ruletypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
@@ -22,16 +26,23 @@ import (
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/notify/test"
+ "github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
)
+func newTestTemplater(tmpl *template.Template) alertmanagertypes.Templater {
+ return alertmanagertemplate.New(tmpl, slog.New(slog.DiscardHandler))
+}
+
func TestOpsGenieRetry(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.OpsGenieConfig{
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -47,14 +58,16 @@ func TestOpsGenieRedactedURL(t *testing.T) {
defer fn()
key := "key"
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.OpsGenieConfig{
APIURL: &config.URL{URL: u},
APIKey: config.Secret(key),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -72,14 +85,16 @@ func TestGettingOpsGegineApikeyFromFile(t *testing.T) {
_, err = f.WriteString(key)
require.NoError(t, err, "writing to temp file failed")
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.OpsGenieConfig{
APIURL: &config.URL{URL: u},
APIKeyFile: f.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -202,7 +217,7 @@ func TestOpsGenie(t *testing.T) {
},
} {
t.Run(tc.title, func(t *testing.T) {
- notifier, err := New(tc.cfg, tmpl, logger)
+ notifier, err := New(tc.cfg, tmpl, logger, newTestTemplater(tmpl))
require.NoError(t, err)
ctx := context.Background()
@@ -278,7 +293,7 @@ func TestOpsGenieWithUpdate(t *testing.T) {
APIURL: &config.URL{URL: u},
HTTPConfig: &commoncfg.HTTPClientConfig{},
}
- notifierWithUpdate, err := New(&opsGenieConfigWithUpdate, tmpl, promslog.NewNopLogger())
+ notifierWithUpdate, err := New(&opsGenieConfigWithUpdate, tmpl, promslog.NewNopLogger(), newTestTemplater(tmpl))
alert := &types.Alert{
Alert: model.Alert{
StartsAt: time.Now(),
@@ -321,7 +336,7 @@ func TestOpsGenieApiKeyFile(t *testing.T) {
APIURL: &config.URL{URL: u},
HTTPConfig: &commoncfg.HTTPClientConfig{},
}
- notifierWithUpdate, err := New(&opsGenieConfigWithUpdate, tmpl, promslog.NewNopLogger())
+ notifierWithUpdate, err := New(&opsGenieConfigWithUpdate, tmpl, promslog.NewNopLogger(), newTestTemplater(tmpl))
require.NoError(t, err)
requests, _, err := notifierWithUpdate.createRequests(ctx)
@@ -329,6 +344,99 @@ func TestOpsGenieApiKeyFile(t *testing.T) {
require.Equal(t, "GenieKey my_secret_api_key", requests[0].Header.Get("Authorization"))
}
+func TestPrepareContent(t *testing.T) {
+ t.Run("default template", func(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
+ logger := promslog.NewNopLogger()
+
+ notifier := &Notifier{
+ conf: &config.OpsGenieConfig{
+ Message: `{{ .CommonLabels.Message }}`,
+ Description: `{{ .CommonLabels.Description }}`,
+ },
+ tmpl: tmpl,
+ logger: logger,
+ templater: newTestTemplater(tmpl),
+ }
+
+ ctx := context.Background()
+ ctx = notify.WithGroupKey(ctx, "1")
+
+ alert := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{
+ "Message": "Firing alert: test",
+ "Description": "Check runbook for more details",
+ },
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ }
+
+ alerts := []*types.Alert{alert}
+
+ title, desc, prepErr := notifier.prepareContent(ctx, alerts)
+ require.NoError(t, prepErr)
+ require.Equal(t, "Firing alert: test", title)
+ require.Equal(t, "Check runbook for more details", desc)
+ })
+
+ t.Run("custom template", func(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
+ logger := promslog.NewNopLogger()
+
+ notifier := &Notifier{
+ conf: &config.OpsGenieConfig{
+ Message: `{{ .CommonLabels.Message }}`,
+ Description: `{{ .CommonLabels.Description }}`,
+ },
+ tmpl: tmpl,
+ logger: logger,
+ templater: newTestTemplater(tmpl),
+ }
+
+ ctx := context.Background()
+ ctx = notify.WithGroupKey(ctx, "1")
+
+ alert1 := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{
+ "service": "payment",
+ "namespace": "potter-the-harry",
+ },
+ Annotations: model.LabelSet{
+ ruletypes.AnnotationTitleTemplate: "High request throughput for $service",
+ ruletypes.AnnotationBodyTemplate: "Alert firing in NS: $labels.namespace",
+ },
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ }
+ alert2 := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{
+ "service": "payment",
+ "namespace": "smart-the-rat",
+ },
+ Annotations: model.LabelSet{
+ ruletypes.AnnotationTitleTemplate: "High request throughput for $service",
+ ruletypes.AnnotationBodyTemplate: "Alert firing in NS: $labels.namespace",
+ },
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ }
+
+ alerts := []*types.Alert{alert1, alert2}
+
+ title, desc, err := notifier.prepareContent(ctx, alerts)
+ require.NoError(t, err)
+ require.Equal(t, "High request throughput for payment", title)
+ // Each alert body wrapped in <div>, separated by <hr>
+ require.Equal(t, "<div><p>Alert firing in NS: potter-the-harry</p>\n</div><hr><div><p>Alert firing in NS: smart-the-rat</p>\n</div>", desc)
+ })
+}
+
func readBody(t *testing.T, r *http.Request) string {
t.Helper()
body, err := io.ReadAll(r.Body)
diff --git a/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty.go b/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty.go
index b72813138b..0214af4a1f 100644
--- a/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty.go
+++ b/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty.go
@@ -15,7 +15,9 @@ import (
"os"
"strings"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
"github.com/alecthomas/units"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
@@ -40,21 +42,22 @@ const (
// Notifier implements a Notifier for PagerDuty notifications.
type Notifier struct {
- conf *config.PagerdutyConfig
- tmpl *template.Template
- logger *slog.Logger
- apiV1 string // for tests.
- client *http.Client
- retrier *notify.Retrier
+ conf *config.PagerdutyConfig
+ tmpl *template.Template
+ logger *slog.Logger
+ apiV1 string // for tests.
+ client *http.Client
+ retrier *notify.Retrier
+ templater alertmanagertypes.Templater
}
// New returns a new PagerDuty notifier.
-func New(c *config.PagerdutyConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
+func New(c *config.PagerdutyConfig, t *template.Template, l *slog.Logger, templater alertmanagertypes.Templater, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
client, err := notify.NewClientWithTracing(*c.HTTPConfig, Integration, httpOpts...)
if err != nil {
return nil, err
}
- n := &Notifier{conf: c, tmpl: t, logger: l, client: client}
+ n := &Notifier{conf: c, tmpl: t, logger: l, client: client, templater: templater}
if c.ServiceKey != "" || c.ServiceKeyFile != "" {
n.apiV1 = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
// Retrying can solve the issue on 403 (rate limiting) and 5xx response codes.
@@ -143,11 +146,12 @@ func (n *Notifier) notifyV1(
key notify.Key,
data *template.Data,
details map[string]any,
+ title string,
) (bool, error) {
var tmplErr error
tmpl := notify.TmplText(n.tmpl, data, &tmplErr)
- description, truncated := notify.TruncateInRunes(tmpl(n.conf.Description), maxV1DescriptionLenRunes)
+ description, truncated := notify.TruncateInRunes(title, maxV1DescriptionLenRunes)
if truncated {
n.logger.WarnContext(ctx, "Truncated description", slog.Any("key", key), slog.Int("max_runes", maxV1DescriptionLenRunes))
}
@@ -203,6 +207,7 @@ func (n *Notifier) notifyV2(
key notify.Key,
data *template.Data,
details map[string]any,
+ title string,
) (bool, error) {
var tmplErr error
tmpl := notify.TmplText(n.tmpl, data, &tmplErr)
@@ -211,7 +216,7 @@ func (n *Notifier) notifyV2(
n.conf.Severity = "error"
}
- summary, truncated := notify.TruncateInRunes(tmpl(n.conf.Description), maxV2SummaryLenRunes)
+ summary, truncated := notify.TruncateInRunes(title, maxV2SummaryLenRunes)
if truncated {
n.logger.WarnContext(ctx, "Truncated summary", slog.Any("key", key), slog.Int("max_runes", maxV2SummaryLenRunes))
}
@@ -294,6 +299,22 @@ func (n *Notifier) notifyV2(
return retry, err
}
+// prepareTitle expands the notification title. PagerDuty has no body surface
+// we care about — the description/summary field is what users see as the
+// incident headline, so we feed the configured Description as the default
+// title template and ignore any custom body_template entirely.
+func (n *Notifier) prepareTitle(ctx context.Context, alerts []*types.Alert) (string, error) {
+ customTitle, _ := alertmanagertemplate.ExtractTemplatesFromAnnotations(alerts)
+ result, err := n.templater.Expand(ctx, alertmanagertypes.ExpandRequest{
+ TitleTemplate: customTitle,
+ DefaultTitleTemplate: n.conf.Description,
+ }, alerts)
+ if err != nil {
+ return "", err
+ }
+ return result.Title, nil
+}
+
// Notify implements the Notifier interface.
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
key, err := notify.ExtractGroupKey(ctx)
@@ -302,6 +323,12 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}
logger := n.logger.With(slog.Any("group_key", key))
+ title, err := n.prepareTitle(ctx, as)
+ if err != nil {
+ n.logger.ErrorContext(ctx, "failed to prepare notification content", errors.Attr(err))
+ return false, err
+ }
+
var (
alerts = types.Alerts(as...)
data = notify.GetTemplateData(ctx, n.tmpl, as, logger)
@@ -329,7 +356,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if n.apiV1 != "" {
nf = n.notifyV1
}
- retry, err := nf(ctx, eventType, key, data, details)
+ retry, err := nf(ctx, eventType, key, data, details, title)
if err != nil {
if ctx.Err() != nil {
err = errors.WrapInternalf(err, errors.CodeInternal, "failed to notify PagerDuty: %v", context.Cause(ctx))
diff --git a/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty_test.go b/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty_test.go
index 34994cd3b0..4cea24e116 100644
--- a/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty_test.go
+++ b/pkg/alertmanager/alertmanagernotify/pagerduty/pagerduty_test.go
@@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"io"
+ "log/slog"
"net/http"
"net/http/httptest"
"net/url"
@@ -17,7 +18,10 @@ import (
"testing"
"time"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
+ "github.com/SigNoz/signoz/pkg/types/ruletypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
@@ -30,14 +34,20 @@ import (
"github.com/prometheus/alertmanager/types"
)
+func newTestTemplater(tmpl *template.Template) alertmanagertypes.Templater {
+ return alertmanagertemplate.New(tmpl, slog.New(slog.DiscardHandler))
+}
+
func TestPagerDutyRetryV1(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.PagerdutyConfig{
ServiceKey: config.Secret("01234567890123456789012345678901"),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -49,13 +59,15 @@ func TestPagerDutyRetryV1(t *testing.T) {
}
func TestPagerDutyRetryV2(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.PagerdutyConfig{
RoutingKey: config.Secret("01234567890123456789012345678901"),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -71,13 +83,15 @@ func TestPagerDutyRedactedURLV1(t *testing.T) {
defer fn()
key := "01234567890123456789012345678901"
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.PagerdutyConfig{
ServiceKey: config.Secret(key),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
notifier.apiV1 = u.String()
@@ -90,14 +104,16 @@ func TestPagerDutyRedactedURLV2(t *testing.T) {
defer fn()
key := "01234567890123456789012345678901"
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.PagerdutyConfig{
URL: &config.URL{URL: u},
RoutingKey: config.Secret(key),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -114,13 +130,15 @@ func TestPagerDutyV1ServiceKeyFromFile(t *testing.T) {
ctx, u, fn := test.GetContextWithCancelingURL()
defer fn()
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.PagerdutyConfig{
ServiceKeyFile: f.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
notifier.apiV1 = u.String()
@@ -138,14 +156,16 @@ func TestPagerDutyV2RoutingKeyFromFile(t *testing.T) {
ctx, u, fn := test.GetContextWithCancelingURL()
defer fn()
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.PagerdutyConfig{
URL: &config.URL{URL: u},
RoutingKeyFile: f.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -302,7 +322,8 @@ func TestPagerDutyTemplating(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
tc.cfg.URL = &config.URL{URL: u}
tc.cfg.HTTPConfig = &commoncfg.HTTPClientConfig{}
- pd, err := New(tc.cfg, test.CreateTmpl(t), promslog.NewNopLogger())
+ tmpl := test.CreateTmpl(t)
+ pd, err := New(tc.cfg, tmpl, promslog.NewNopLogger(), newTestTemplater(tmpl))
require.NoError(t, err)
if pd.apiV1 != "" {
pd.apiV1 = u.String()
@@ -392,13 +413,15 @@ func TestEventSizeEnforcement(t *testing.T) {
Details: bigDetailsV1,
}
+ tmpl := test.CreateTmpl(t)
notifierV1, err := New(
&config.PagerdutyConfig{
ServiceKey: config.Secret("01234567890123456789012345678901"),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -420,8 +443,9 @@ func TestEventSizeEnforcement(t *testing.T) {
RoutingKey: config.Secret("01234567890123456789012345678901"),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -536,7 +560,8 @@ func TestPagerDutyEmptySrcHref(t *testing.T) {
Links: links,
}
- pagerDuty, err := New(&pagerDutyConfig, test.CreateTmpl(t), promslog.NewNopLogger())
+ pdTmpl := test.CreateTmpl(t)
+ pagerDuty, err := New(&pagerDutyConfig, pdTmpl, promslog.NewNopLogger(), newTestTemplater(pdTmpl))
require.NoError(t, err)
ctx := context.Background()
@@ -603,7 +628,8 @@ func TestPagerDutyTimeout(t *testing.T) {
Timeout: tt.timeout,
}
- pd, err := New(&cfg, test.CreateTmpl(t), promslog.NewNopLogger())
+ tmpl := test.CreateTmpl(t)
+ pd, err := New(&cfg, tmpl, promslog.NewNopLogger(), newTestTemplater(tmpl))
require.NoError(t, err)
ctx := context.Background()
@@ -881,3 +907,79 @@ func TestRenderDetails(t *testing.T) {
})
}
}
+
+func TestPrepareContent(t *testing.T) {
+ prepareContext := func() context.Context {
+ ctx := context.Background()
+ ctx = notify.WithGroupKey(ctx, "1")
+ ctx = notify.WithReceiverName(ctx, "test-receiver")
+ ctx = notify.WithGroupLabels(ctx, model.LabelSet{"alertname": "HighCPU for Payment service"})
+ return ctx
+ }
+ t.Run("default template uses go text template config for title", func(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
+ notifier, err := New(
+ &config.PagerdutyConfig{
+ RoutingKey: config.Secret("01234567890123456789012345678901"),
+ HTTPConfig: &commoncfg.HTTPClientConfig{},
+ Description: `{{ .CommonLabels.alertname }} ({{ .Status | toUpper }})`,
+ },
+ tmpl,
+ promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
+ )
+ require.NoError(t, err)
+
+ ctx := prepareContext()
+
+ alerts := []*types.Alert{
+ {
+ Alert: model.Alert{
+ Labels: model.LabelSet{"alertname": "HighCPU for Payment service"},
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ },
+ },
+ }
+
+ title, err := notifier.prepareTitle(ctx, alerts)
+ require.NoError(t, err)
+ require.Equal(t, "HighCPU for Payment service (FIRING)", title)
+ })
+
+ t.Run("custom template uses $variable annotation for title", func(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
+ notifier, err := New(
+ &config.PagerdutyConfig{
+ RoutingKey: config.Secret("01234567890123456789012345678901"),
+ HTTPConfig: &commoncfg.HTTPClientConfig{},
+ },
+ tmpl,
+ promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
+ )
+ require.NoError(t, err)
+
+ ctx := prepareContext()
+
+ alerts := []*types.Alert{
+ {
+ Alert: model.Alert{
+ Labels: model.LabelSet{
+ "alertname": "HighCPU",
+ "service": "api-server",
+ },
+ Annotations: model.LabelSet{
+ ruletypes.AnnotationTitleTemplate: "$rule.name on $service is in $alert.status state",
+ },
+ StartsAt: time.Now().Add(-time.Hour),
+ EndsAt: time.Now(),
+ },
+ },
+ }
+
+ title, err := notifier.prepareTitle(ctx, alerts)
+ require.NoError(t, err)
+ require.Equal(t, "HighCPU on api-server is in resolved state", title)
+ })
+}
diff --git a/pkg/alertmanager/alertmanagernotify/receiver.go b/pkg/alertmanager/alertmanagernotify/receiver.go
index 59c4ee1b4b..6f04cf8b84 100644
--- a/pkg/alertmanager/alertmanagernotify/receiver.go
+++ b/pkg/alertmanager/alertmanagernotify/receiver.go
@@ -26,7 +26,7 @@ var customNotifierIntegrations = []string{
msteamsv2.Integration,
}
-func NewReceiverIntegrations(nc alertmanagertypes.Receiver, tmpl *template.Template, logger *slog.Logger) ([]notify.Integration, error) {
+func NewReceiverIntegrations(nc alertmanagertypes.Receiver, tmpl *template.Template, logger *slog.Logger, templater alertmanagertypes.Templater) ([]notify.Integration, error) {
upstreamIntegrations, err := receiver.BuildReceiverIntegrations(nc, tmpl, logger)
if err != nil {
return nil, err
@@ -53,23 +53,25 @@ func NewReceiverIntegrations(nc alertmanagertypes.Receiver, tmpl *template.Templ
}
for i, c := range nc.WebhookConfigs {
- add(webhook.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return webhook.New(c, tmpl, l) })
+ add(webhook.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return webhook.New(c, tmpl, l, templater) })
}
for i, c := range nc.EmailConfigs {
- add(email.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return email.New(c, tmpl, l), nil })
+ add(email.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) {
+ return email.New(c, tmpl, l, templater), nil
+ })
}
for i, c := range nc.PagerdutyConfigs {
- add(pagerduty.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return pagerduty.New(c, tmpl, l) })
+ add(pagerduty.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return pagerduty.New(c, tmpl, l, templater) })
}
for i, c := range nc.OpsGenieConfigs {
- add(opsgenie.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return opsgenie.New(c, tmpl, l) })
+ add(opsgenie.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return opsgenie.New(c, tmpl, l, templater) })
}
for i, c := range nc.SlackConfigs {
- add(slack.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return slack.New(c, tmpl, l) })
+ add(slack.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) { return slack.New(c, tmpl, l, templater) })
}
for i, c := range nc.MSTeamsV2Configs {
add(msteamsv2.Integration, i, c, func(l *slog.Logger) (notify.Notifier, error) {
- return msteamsv2.New(c, tmpl, `{{ template "msteamsv2.default.titleLink" . }}`, l)
+ return msteamsv2.New(c, tmpl, `{{ template "msteamsv2.default.titleLink" . }}`, l, templater)
})
}
diff --git a/pkg/alertmanager/alertmanagernotify/slack/slack.go b/pkg/alertmanager/alertmanagernotify/slack/slack.go
index 6123a151f6..d63e4caaee 100644
--- a/pkg/alertmanager/alertmanagernotify/slack/slack.go
+++ b/pkg/alertmanager/alertmanagernotify/slack/slack.go
@@ -14,7 +14,11 @@ import (
"os"
"strings"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/templating/markdownrenderer"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
+ "github.com/SigNoz/signoz/pkg/types/ruletypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/alertmanager/config"
@@ -25,6 +29,8 @@ import (
const (
Integration = "slack"
+ colorRed = "#FF0000"
+ colorGreen = "#00FF00"
)
// https://api.slack.com/reference/messaging/attachments#legacy_fields - 1024, no units given, assuming runes or characters.
@@ -32,17 +38,18 @@ const maxTitleLenRunes = 1024
// Notifier implements a Notifier for Slack notifications.
type Notifier struct {
- conf *config.SlackConfig
- tmpl *template.Template
- logger *slog.Logger
- client *http.Client
- retrier *notify.Retrier
+ conf *config.SlackConfig
+ tmpl *template.Template
+ logger *slog.Logger
+ client *http.Client
+ retrier *notify.Retrier
+ templater alertmanagertypes.Templater
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
}
// New returns a new Slack notification handler.
-func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
+func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, templater alertmanagertypes.Templater, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
client, err := notify.NewClientWithTracing(*c.HTTPConfig, Integration, httpOpts...)
if err != nil {
return nil, err
@@ -54,6 +61,7 @@ func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, httpOpts .
logger: l,
client: client,
retrier: &notify.Retrier{},
+ templater: templater,
postJSONFunc: notify.PostJSON,
}, nil
}
@@ -81,9 +89,10 @@ type attachment struct {
Actions []config.SlackAction `json:"actions,omitempty"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
- Footer string `json:"footer"`
+ Footer string `json:"footer,omitempty"`
Color string `json:"color,omitempty"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
+ Blocks []any `json:"blocks,omitempty"`
}
// Notify implements the Notifier interface.
@@ -100,79 +109,15 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
data = notify.GetTemplateData(ctx, n.tmpl, as, logger)
tmplText = notify.TmplText(n.tmpl, data, &err)
)
- var markdownIn []string
- if len(n.conf.MrkdwnIn) == 0 {
- markdownIn = []string{"fallback", "pretext", "text"}
- } else {
- markdownIn = n.conf.MrkdwnIn
+ attachments, err := n.prepareContent(ctx, as, tmplText)
+ if err != nil {
+ n.logger.ErrorContext(ctx, "failed to prepare notification content", errors.Attr(err))
+ return false, err
}
- title, truncated := notify.TruncateInRunes(tmplText(n.conf.Title), maxTitleLenRunes)
- if truncated {
- logger.WarnContext(ctx, "Truncated title", slog.Int("max_runes", maxTitleLenRunes))
- }
- att := &attachment{
- Title: title,
- TitleLink: tmplText(n.conf.TitleLink),
- Pretext: tmplText(n.conf.Pretext),
- Text: tmplText(n.conf.Text),
- Fallback: tmplText(n.conf.Fallback),
- CallbackID: tmplText(n.conf.CallbackID),
- ImageURL: tmplText(n.conf.ImageURL),
- ThumbURL: tmplText(n.conf.ThumbURL),
- Footer: tmplText(n.conf.Footer),
- Color: tmplText(n.conf.Color),
- MrkdwnIn: markdownIn,
- }
-
- numFields := len(n.conf.Fields)
- if numFields > 0 {
- fields := make([]config.SlackField, numFields)
- for index, field := range n.conf.Fields {
- // Check if short was defined for the field otherwise fallback to the global setting
- var short bool
- if field.Short != nil {
- short = *field.Short
- } else {
- short = n.conf.ShortFields
- }
-
- // Rebuild the field by executing any templates and setting the new value for short
- fields[index] = config.SlackField{
- Title: tmplText(field.Title),
- Value: tmplText(field.Value),
- Short: &short,
- }
- }
- att.Fields = fields
- }
-
- numActions := len(n.conf.Actions)
- if numActions > 0 {
- actions := make([]config.SlackAction, numActions)
- for index, action := range n.conf.Actions {
- slackAction := config.SlackAction{
- Type: tmplText(action.Type),
- Text: tmplText(action.Text),
- URL: tmplText(action.URL),
- Style: tmplText(action.Style),
- Name: tmplText(action.Name),
- Value: tmplText(action.Value),
- }
-
- if action.ConfirmField != nil {
- slackAction.ConfirmField = &config.SlackConfirmationField{
- Title: tmplText(action.ConfirmField.Title),
- Text: tmplText(action.ConfirmField.Text),
- OkText: tmplText(action.ConfirmField.OkText),
- DismissText: tmplText(action.ConfirmField.DismissText),
- }
- }
-
- actions[index] = slackAction
- }
- att.Actions = actions
+ if len(attachments) > 0 {
+ n.addFieldsAndActions(&attachments[0], tmplText)
}
req := &request{
@@ -182,7 +127,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
IconURL: tmplText(n.conf.IconURL),
LinkNames: n.conf.LinkNames,
Text: tmplText(n.conf.MessageText),
- Attachments: []attachment{*att},
+ Attachments: attachments,
}
if err != nil {
return false, err
@@ -238,6 +183,150 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return retry, nil
}
+// prepareContent expands alert templates and returns the Slack attachment(s)
+// ready to send. When alerts carry a custom body template, one title-only
+// attachment plus one body attachment per alert is returned so that each alert
+// can get its own firing/resolved color and per-alert action buttons.
+func (n *Notifier) prepareContent(ctx context.Context, alerts []*types.Alert, tmplText func(string) string) ([]attachment, error) {
+ customTitle, customBody := alertmanagertemplate.ExtractTemplatesFromAnnotations(alerts)
+ result, err := n.templater.Expand(ctx, alertmanagertypes.ExpandRequest{
+ TitleTemplate: customTitle,
+ BodyTemplate: customBody,
+ DefaultTitleTemplate: n.conf.Title,
+ DefaultBodyTemplate: n.conf.Text,
+ }, alerts)
+ if err != nil {
+ return nil, err
+ }
+
+ title, truncated := notify.TruncateInRunes(result.Title, maxTitleLenRunes)
+ if truncated {
+ n.logger.WarnContext(ctx, "Truncated title", slog.Int("max_runes", maxTitleLenRunes))
+ }
+
+ if result.IsDefaultBody {
+ var markdownIn []string
+ if len(n.conf.MrkdwnIn) == 0 {
+ markdownIn = []string{"fallback", "pretext", "text"}
+ } else {
+ markdownIn = n.conf.MrkdwnIn
+ }
+ return []attachment{
+ {
+ Title: title,
+ TitleLink: tmplText(n.conf.TitleLink),
+ Pretext: tmplText(n.conf.Pretext),
+ Text: result.Body[0],
+ Fallback: tmplText(n.conf.Fallback),
+ CallbackID: tmplText(n.conf.CallbackID),
+ ImageURL: tmplText(n.conf.ImageURL),
+ ThumbURL: tmplText(n.conf.ThumbURL),
+ Footer: tmplText(n.conf.Footer),
+ Color: tmplText(n.conf.Color),
+ MrkdwnIn: markdownIn,
+ },
+ }, nil
+ }
+
+ // Custom template path: one title attachment + one attachment per
+ // non-empty alert body. result.Body is positionally aligned with alerts,
+ // so we index alerts[i] directly and skip empty entries.
+ attachments := make([]attachment, 0, 1+len(result.Body))
+ attachments = append(attachments, attachment{
+ Title: title,
+ TitleLink: tmplText(n.conf.TitleLink),
+ })
+
+ for i, body := range result.Body {
+ if body == "" || i >= len(alerts) {
+ continue
+ }
+
+ // Custom bodies are authored in markdown; render each non-empty body to
+ // Slack's mrkdwn flavour. Default bodies skip this because the Text
+ // template is already channel-ready.
+ rendered, renderErr := markdownrenderer.RenderSlackMrkdwn(body)
+ if renderErr != nil {
+ return nil, renderErr
+ }
+
+ color := colorRed
+ if alerts[i].Resolved() {
+ color = colorGreen
+ }
+ attachments = append(attachments, attachment{
+ Text: rendered,
+ Color: color,
+ MrkdwnIn: []string{"text"},
+ Actions: buildRelatedLinkActions(alerts[i]),
+ })
+ }
+
+ return attachments, nil
+}
+
+// buildRelatedLinkActions returns the "View Related Logs/Traces" action
+// buttons for an alert, or nil when no related-link annotations are present.
+func buildRelatedLinkActions(alert *types.Alert) []config.SlackAction {
+ var actions []config.SlackAction
+ if link := alert.Annotations[ruletypes.AnnotationRelatedLogs]; link != "" {
+ actions = append(actions, config.SlackAction{Type: "button", Text: "View Related Logs", URL: string(link)})
+ }
+ if link := alert.Annotations[ruletypes.AnnotationRelatedTraces]; link != "" {
+ actions = append(actions, config.SlackAction{Type: "button", Text: "View Related Traces", URL: string(link)})
+ }
+ return actions
+}
+
+// addFieldsAndActions populates fields and actions on the attachment from the Slack config.
+func (n *Notifier) addFieldsAndActions(att *attachment, tmplText func(string) string) {
+ numFields := len(n.conf.Fields)
+ if numFields > 0 {
+ fields := make([]config.SlackField, numFields)
+ for index, field := range n.conf.Fields {
+ var short bool
+ if field.Short != nil {
+ short = *field.Short
+ } else {
+ short = n.conf.ShortFields
+ }
+ fields[index] = config.SlackField{
+ Title: tmplText(field.Title),
+ Value: tmplText(field.Value),
+ Short: &short,
+ }
+ }
+ att.Fields = fields
+ }
+
+ numActions := len(n.conf.Actions)
+ if numActions > 0 {
+ actions := make([]config.SlackAction, numActions)
+ for index, action := range n.conf.Actions {
+ slackAction := config.SlackAction{
+ Type: tmplText(action.Type),
+ Text: tmplText(action.Text),
+ URL: tmplText(action.URL),
+ Style: tmplText(action.Style),
+ Name: tmplText(action.Name),
+ Value: tmplText(action.Value),
+ }
+
+ if action.ConfirmField != nil {
+ slackAction.ConfirmField = &config.SlackConfirmationField{
+ Title: tmplText(action.ConfirmField.Title),
+ Text: tmplText(action.ConfirmField.Text),
+ OkText: tmplText(action.ConfirmField.OkText),
+ DismissText: tmplText(action.ConfirmField.DismissText),
+ }
+ }
+
+ actions[index] = slackAction
+ }
+ att.Actions = actions
+ }
+}
+
// checkResponseError parses out the error message from Slack API response.
func checkResponseError(resp *http.Response) (bool, error) {
body, err := io.ReadAll(resp.Body)
diff --git a/pkg/alertmanager/alertmanagernotify/slack/slack_test.go b/pkg/alertmanager/alertmanagernotify/slack/slack_test.go
index 120266cc71..b684634a5a 100644
--- a/pkg/alertmanager/alertmanagernotify/slack/slack_test.go
+++ b/pkg/alertmanager/alertmanagernotify/slack/slack_test.go
@@ -17,6 +17,9 @@ import (
"testing"
"time"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
+ "github.com/SigNoz/signoz/pkg/types/ruletypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
@@ -29,13 +32,19 @@ import (
"github.com/prometheus/alertmanager/types"
)
+func newTestTemplater(tmpl *template.Template) alertmanagertypes.Templater {
+ return alertmanagertemplate.New(tmpl, slog.New(slog.DiscardHandler))
+}
+
func TestSlackRetry(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.SlackConfig{
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -49,13 +58,15 @@ func TestSlackRedactedURL(t *testing.T) {
ctx, u, fn := test.GetContextWithCancelingURL()
defer fn()
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.SlackConfig{
APIURL: &config.SecretURL{URL: u},
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -71,13 +82,15 @@ func TestGettingSlackURLFromFile(t *testing.T) {
_, err = f.WriteString(u.String())
require.NoError(t, err, "writing to temp file failed")
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.SlackConfig{
APIURLFile: f.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -93,13 +106,15 @@ func TestTrimmingSlackURLFromFile(t *testing.T) {
_, err = f.WriteString(u.String() + "\n\n")
require.NoError(t, err, "writing to temp file failed")
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.SlackConfig{
APIURLFile: f.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -184,6 +199,7 @@ func TestNotifier_Notify_WithReason(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
apiurl, _ := url.Parse("https://slack.com/post.Message")
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.SlackConfig{
NotifierConfig: config.NotifierConfig{},
@@ -191,8 +207,9 @@ func TestNotifier_Notify_WithReason(t *testing.T) {
APIURL: &config.SecretURL{URL: apiurl},
Channel: "channelname",
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
@@ -242,6 +259,7 @@ func TestSlackTimeout(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
u, _ := url.Parse("https://slack.com/post.Message")
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.SlackConfig{
NotifierConfig: config.NotifierConfig{},
@@ -250,8 +268,9 @@ func TestSlackTimeout(t *testing.T) {
Channel: "channelname",
Timeout: tt.timeout,
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ newTestTemplater(tmpl),
)
require.NoError(t, err)
notifier.postJSONFunc = func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error) {
@@ -282,6 +301,225 @@ func TestSlackTimeout(t *testing.T) {
}
}
+// setupTestContext creates a context with group key, receiver name, and group labels
+// required by the notification processor.
+func setupTestContext() context.Context {
+ ctx := context.Background()
+ ctx = notify.WithGroupKey(ctx, "test-group")
+ ctx = notify.WithReceiverName(ctx, "slack")
+ ctx = notify.WithGroupLabels(ctx, model.LabelSet{
+ "alertname": "TestAlert",
+ "severity": "critical",
+ })
+ return ctx
+}
+
+func TestPrepareContent(t *testing.T) {
+ t.Run("default template uses go text template config for title and body", func(t *testing.T) {
+ // When alerts have no custom annotation templates (title_template / body_template),
+ tmpl := test.CreateTmpl(t)
+ templater := newTestTemplater(tmpl)
+ notifier := &Notifier{
+ conf: &config.SlackConfig{
+ Title: `{{ .CommonLabels.alertname }} ({{ .Status | toUpper }})`,
+ Text: `{{ range .Alerts }}Alert: {{ .Labels.alertname }} - severity {{ .Labels.severity }}{{ end }}`,
+ Color: `{{ if eq .Status "firing" }}danger{{ else }}good{{ end }}`,
+ TitleLink: "https://alertmanager.signoz.com",
+ },
+ tmpl: tmpl,
+ logger: slog.New(slog.DiscardHandler),
+ templater: templater,
+ }
+
+ ctx := setupTestContext()
+ alerts := []*types.Alert{
+ {Alert: model.Alert{
+ Labels: model.LabelSet{ruletypes.LabelAlertName: "HighCPU", ruletypes.LabelSeverityName: "critical"},
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ }},
+ }
+
+ // Build tmplText the same way Notify does
+ var err error
+ data := notify.GetTemplateData(ctx, tmpl, alerts, slog.New(slog.DiscardHandler))
+ tmplText := notify.TmplText(tmpl, data, &err)
+
+ atts, attErr := notifier.prepareContent(ctx, alerts, tmplText)
+ require.NoError(t, attErr)
+ require.NoError(t, err)
+ require.Len(t, atts, 1)
+
+ require.Equal(t, "HighCPU (FIRING)", atts[0].Title)
+ require.Equal(t, "Alert: HighCPU - severity critical", atts[0].Text)
+ // Color is templated — firing alert should be "danger"
+ require.Equal(t, "danger", atts[0].Color)
+ // No BlockKit blocks for default template
+ require.Nil(t, atts[0].Blocks)
+ // Default markdownIn when config has none
+ require.Equal(t, []string{"fallback", "pretext", "text"}, atts[0].MrkdwnIn)
+ })
+
+ t.Run("custom template produces 1+N attachments with per-alert color", func(t *testing.T) {
+ // When alerts carry custom $variable annotation templates (title_template / body_template)
+ tmpl := test.CreateTmpl(t)
+ templater := newTestTemplater(tmpl)
+ notifier := &Notifier{
+ conf: &config.SlackConfig{
+ Title: "default title fallback",
+ Text: "default text fallback",
+ TitleLink: "https://alertmanager.signoz.com",
+ },
+ tmpl: tmpl,
+ logger: slog.New(slog.DiscardHandler),
+ templater: templater,
+ }
+ tmplText := func(s string) string { return s }
+
+ bodyTemplate := `## $rule.name
+
+**Service:** *$labels.service*
+**Instance:** *$labels.instance*
+**Region:** *$labels.region*
+**Method:** *$labels.http_method*
+
+---
+
+| Metric | Value |
+|--------|-------|
+| **Current** | *$value* |
+| **Threshold** | *$threshold.value* |
+
+**Status:** $alert.status | **Severity:** $labels.severity`
+ titleTemplate := "[$alert.status] $rule.name — $labels.service"
+
+ ctx := setupTestContext()
+ firingAlert := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{ruletypes.LabelAlertName: "HighCPU", ruletypes.LabelSeverityName: "critical", "service": "api-server", "instance": "i-0abc123", "region": "us-east-1", "http_method": "GET"},
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ Annotations: model.LabelSet{
+ ruletypes.AnnotationTitleTemplate: model.LabelValue(titleTemplate),
+ ruletypes.AnnotationBodyTemplate: model.LabelValue(bodyTemplate),
+ "value": "100",
+ "threshold.value": "200",
+ },
+ },
+ }
+ resolvedAlert := &types.Alert{
+ Alert: model.Alert{
+ Labels: model.LabelSet{ruletypes.LabelAlertName: "HighCPU", ruletypes.LabelSeverityName: "critical", "service": "api-server", "instance": "i-0abc123", "region": "us-east-1", "http_method": "GET"},
+ StartsAt: time.Now().Add(-2 * time.Hour),
+ EndsAt: time.Now().Add(-time.Hour),
+ Annotations: model.LabelSet{
+ ruletypes.AnnotationTitleTemplate: model.LabelValue(titleTemplate),
+ ruletypes.AnnotationBodyTemplate: model.LabelValue(bodyTemplate),
+ "value": "50",
+ "threshold.value": "200",
+ },
+ },
+ }
+
+ atts, err := notifier.prepareContent(ctx, []*types.Alert{firingAlert, resolvedAlert}, tmplText)
+ require.NoError(t, err)
+
+ // 1 title attachment + 2 body attachments (one per alert)
+ require.Len(t, atts, 3)
+
+ // First attachment: title-only, no color, no blocks
+ require.Equal(t, "[firing] HighCPU — api-server", atts[0].Title)
+ require.Empty(t, atts[0].Color)
+ require.Nil(t, atts[0].Blocks)
+ require.Equal(t, "https://alertmanager.signoz.com", atts[0].TitleLink)
+
+ expectedFiringBody := "*HighCPU*\n\n" +
+ "*Service:* _api-server_\n*Instance:* _i-0abc123_\n*Region:* _us-east-1_\n*Method:* _GET_\n\n" +
+ "---\n\n" +
+ "```\nMetric | Value\n----------|------\nCurrent | 100 \nThreshold | 200 \n```\n\n" +
+ "*Status:* firing | *Severity:* critical\n\n"
+
+ expectedResolvedBody := "*HighCPU*\n\n" +
+ "*Service:* _api-server_\n*Instance:* _i-0abc123_\n*Region:* _us-east-1_\n*Method:* _GET_\n\n" +
+ "---\n\n" +
+ "```\nMetric | Value\n----------|------\nCurrent | 50 \nThreshold | 200 \n```\n\n" +
+ "*Status:* resolved | *Severity:* critical\n\n"
+
+ // Second attachment: firing alert body rendered as slack mrkdwn text, red color
+ require.Nil(t, atts[1].Blocks)
+ require.Equal(t, "#FF0000", atts[1].Color)
+ require.Equal(t, []string{"text"}, atts[1].MrkdwnIn)
+ require.Equal(t, expectedFiringBody, atts[1].Text)
+
+ // Third attachment: resolved alert body rendered as slack mrkdwn text, green color
+ require.Nil(t, atts[2].Blocks)
+ require.Equal(t, "#00FF00", atts[2].Color)
+ require.Equal(t, []string{"text"}, atts[2].MrkdwnIn)
+ require.Equal(t, expectedResolvedBody, atts[2].Text)
+ })
+
+ t.Run("default template with fields and actions", func(t *testing.T) {
+ // Verifies that addFieldsAndActions (called from Notify after prepareContent)
+ // correctly populates fields and actions on the attachment from config.
+ tmpl := test.CreateTmpl(t)
+ templater := newTestTemplater(tmpl)
+ short := true
+ notifier := &Notifier{
+ conf: &config.SlackConfig{
+ Title: `{{ .CommonLabels.alertname }}`,
+ Text: "alert text",
+ Color: "warning",
+ Fields: []*config.SlackField{
+ {Title: "Severity", Value: "critical", Short: &short},
+ {Title: "Service", Value: "api-server", Short: &short},
+ },
+ Actions: []*config.SlackAction{
+ {Type: "button", Text: "View Alert", URL: "https://alertmanager.signoz.com"},
+ },
+ TitleLink: "https://alertmanager.signoz.com",
+ },
+ tmpl: tmpl,
+ logger: slog.New(slog.DiscardHandler),
+ templater: templater,
+ }
+ tmplText := func(s string) string { return s }
+
+ ctx := setupTestContext()
+ alerts := []*types.Alert{
+ {Alert: model.Alert{
+ Labels: model.LabelSet{ruletypes.LabelAlertName: "TestAlert"},
+ StartsAt: time.Now(),
+ EndsAt: time.Now().Add(time.Hour),
+ }},
+ }
+ atts, err := notifier.prepareContent(ctx, alerts, tmplText)
+ require.NoError(t, err)
+ require.Len(t, atts, 1)
+
+ // prepareContent does not populate fields/actions — that's done by
+ // addFieldsAndActions which is called from Notify.
+ require.Nil(t, atts[0].Fields)
+ require.Nil(t, atts[0].Actions)
+
+ // Simulate what Notify does after prepareContent
+ notifier.addFieldsAndActions(&atts[0], tmplText)
+
+ // Verify fields
+ require.Len(t, atts[0].Fields, 2)
+ require.Equal(t, "Severity", atts[0].Fields[0].Title)
+ require.Equal(t, "critical", atts[0].Fields[0].Value)
+ require.True(t, *atts[0].Fields[0].Short)
+ require.Equal(t, "Service", atts[0].Fields[1].Title)
+ require.Equal(t, "api-server", atts[0].Fields[1].Value)
+
+ // Verify actions
+ require.Len(t, atts[0].Actions, 1)
+ require.Equal(t, "button", atts[0].Actions[0].Type)
+ require.Equal(t, "View Alert", atts[0].Actions[0].Text)
+ require.Equal(t, "https://alertmanager.signoz.com", atts[0].Actions[0].URL)
+ })
+}
+
func TestSlackMessageField(t *testing.T) {
// 1. Setup a fake Slack server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -329,7 +567,7 @@ func TestSlackMessageField(t *testing.T) {
tmpl.ExternalURL = u
logger := slog.New(slog.DiscardHandler)
- notifier, err := New(conf, tmpl, logger)
+ notifier, err := New(conf, tmpl, logger, newTestTemplater(tmpl))
if err != nil {
t.Fatal(err)
}
diff --git a/pkg/alertmanager/alertmanagernotify/webhook/webhook.go b/pkg/alertmanager/alertmanagernotify/webhook/webhook.go
index 8536a0b100..72df4fca88 100644
--- a/pkg/alertmanager/alertmanagernotify/webhook/webhook.go
+++ b/pkg/alertmanager/alertmanagernotify/webhook/webhook.go
@@ -14,6 +14,7 @@ import (
"strings"
"github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/alertmanager/config"
@@ -28,15 +29,16 @@ const (
// Notifier implements a Notifier for generic webhooks.
type Notifier struct {
- conf *config.WebhookConfig
- tmpl *template.Template
- logger *slog.Logger
- client *http.Client
- retrier *notify.Retrier
+ conf *config.WebhookConfig
+ tmpl *template.Template
+ logger *slog.Logger
+ client *http.Client
+ retrier *notify.Retrier
+ templater alertmanagertypes.Templater
}
// New returns a new Webhook.
-func New(conf *config.WebhookConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
+func New(conf *config.WebhookConfig, t *template.Template, l *slog.Logger, templater alertmanagertypes.Templater, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
client, err := notify.NewClientWithTracing(*conf.HTTPConfig, Integration, httpOpts...)
if err != nil {
return nil, err
@@ -48,7 +50,8 @@ func New(conf *config.WebhookConfig, t *template.Template, l *slog.Logger, httpO
client: client,
// Webhooks are assumed to respond with 2xx response codes on a successful
// request and 5xx response codes are assumed to be recoverable.
- retrier: &notify.Retrier{},
+ retrier: &notify.Retrier{},
+ templater: templater,
}, nil
}
diff --git a/pkg/alertmanager/alertmanagernotify/webhook/webhook_test.go b/pkg/alertmanager/alertmanagernotify/webhook/webhook_test.go
index 0ebde1093c..0f92a5cd73 100644
--- a/pkg/alertmanager/alertmanagernotify/webhook/webhook_test.go
+++ b/pkg/alertmanager/alertmanagernotify/webhook/webhook_test.go
@@ -9,6 +9,7 @@ import (
"context"
"fmt"
"io"
+ "log/slog"
"net/http"
"net/http/httptest"
"os"
@@ -20,6 +21,7 @@ import (
"github.com/prometheus/common/promslog"
"github.com/stretchr/testify/require"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/notify/test"
@@ -27,13 +29,15 @@ import (
)
func TestWebhookRetry(t *testing.T) {
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.WebhookConfig{
URL: config.SecretTemplateURL("http://example.com"),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ alertmanagertemplate.New(tmpl, slog.Default()),
)
if err != nil {
require.NoError(t, err)
@@ -96,13 +100,16 @@ func TestWebhookRedactedURL(t *testing.T) {
defer fn()
secret := "secret"
+ tmpl := test.CreateTmpl(t)
+ templater := alertmanagertemplate.New(tmpl, slog.Default())
notifier, err := New(
&config.WebhookConfig{
URL: config.SecretTemplateURL(u.String()),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ templater,
)
require.NoError(t, err)
@@ -118,13 +125,15 @@ func TestWebhookReadingURLFromFile(t *testing.T) {
_, err = f.WriteString(u.String() + "\n")
require.NoError(t, err, "writing to temp file failed")
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.WebhookConfig{
URLFile: f.Name(),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ alertmanagertemplate.New(tmpl, slog.Default()),
)
require.NoError(t, err)
@@ -178,13 +187,15 @@ func TestWebhookURLTemplating(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
calledURL = "" // Reset for each test
+ tmpl := test.CreateTmpl(t)
notifier, err := New(
&config.WebhookConfig{
URL: config.SecretTemplateURL(tc.url),
HTTPConfig: &commoncfg.HTTPClientConfig{},
},
- test.CreateTmpl(t),
+ tmpl,
promslog.NewNopLogger(),
+ alertmanagertemplate.New(tmpl, slog.Default()),
)
require.NoError(t, err)
diff --git a/pkg/alertmanager/alertmanagerserver/config.go b/pkg/alertmanager/alertmanagerserver/config.go
index 47549473e2..f970a93d5c 100644
--- a/pkg/alertmanager/alertmanagerserver/config.go
+++ b/pkg/alertmanager/alertmanagerserver/config.go
@@ -28,6 +28,13 @@ type Config struct {
// Configuration for the notification log.
NFLog NFLogConfig `mapstructure:"nflog"`
+
+ // Templates is the list of globs from which SigNoz's alertmanager notification
+ // templates are loaded (e.g. the email.signoz.html layout). This mirrors the
+ // upstream alertmanager `templates` config option (https://github.com/prometheus/alertmanager/blob/3b06b97af4d146e141af92885a185891eb79a5b0/config/config.go#L412).
+ // The upstream default templates (default.tmpl, email.tmpl) are always loaded
+ // from the embedded alertmanager assets, so only SigNoz's own templates are listed here.
+ Templates []string `mapstructure:"templates"`
}
type AlertsConfig struct {
@@ -100,5 +107,6 @@ func NewConfig() Config {
MaintenanceInterval: 15 * time.Minute,
Retention: 120 * time.Hour,
},
+ Templates: []string{"/root/templates/alertmanager/*.gotmpl"},
}
}
diff --git a/pkg/alertmanager/alertmanagerserver/server.go b/pkg/alertmanager/alertmanagerserver/server.go
index 5efd7ccc90..da12b4c2c5 100644
--- a/pkg/alertmanager/alertmanagerserver/server.go
+++ b/pkg/alertmanager/alertmanagerserver/server.go
@@ -10,6 +10,7 @@ import (
"github.com/prometheus/alertmanager/types"
"golang.org/x/sync/errgroup"
+ "github.com/SigNoz/signoz/pkg/errors"
"github.com/prometheus/alertmanager/dispatch"
"github.com/prometheus/alertmanager/featurecontrol"
"github.com/prometheus/alertmanager/inhibit"
@@ -23,8 +24,8 @@ import (
"github.com/prometheus/common/model"
"github.com/SigNoz/signoz/pkg/alertmanager/alertmanagernotify"
+ "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/alertmanager/nfmanager"
- "github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
)
@@ -65,6 +66,7 @@ type Server struct {
muter *MaintenanceMuter
marker *types.MemMarker
tmpl *template.Template
+ templater alertmanagertypes.Templater
wg sync.WaitGroup
stopc chan struct{}
notificationManager nfmanager.NotificationManager
@@ -242,13 +244,21 @@ func (server *Server) SetConfig(ctx context.Context, alertmanagerConfig *alertma
config := alertmanagerConfig.AlertmanagerConfig()
var err error
- server.tmpl, err = alertmanagertypes.FromGlobs(config.Templates)
+ // Load SigNoz's alertmanager notification templates from the configured
+ // globs. The upstream default templates (default.tmpl, email.tmpl) are
+ // always loaded from the embedded alertmanager assets inside FromGlobs, so
+ // only SigNoz's own templates (e.g. the email.signoz.html layout) are listed
+ // here. The upstream config.Templates field is not used: SigNoz never
+ // populates it (there is no per-org template configuration).
+ server.tmpl, err = alertmanagertypes.FromGlobs(server.srvConfig.Templates)
if err != nil {
return err
}
server.tmpl.ExternalURL = server.srvConfig.ExternalURL
+ server.templater = alertmanagertemplate.New(server.tmpl, server.logger)
+
// Build the routing tree and record which receivers are used.
routes := dispatch.NewRoute(config.Route, nil)
activeReceivers := make(map[string]struct{})
@@ -265,7 +275,7 @@ func (server *Server) SetConfig(ctx context.Context, alertmanagerConfig *alertma
server.logger.InfoContext(ctx, "skipping creation of receiver not referenced by any route", slog.String("receiver", rcv.Name))
continue
}
- integrations, err := alertmanagernotify.NewReceiverIntegrations(rcv, server.tmpl, server.logger)
+ integrations, err := alertmanagernotify.NewReceiverIntegrations(rcv, server.tmpl, server.logger, server.templater)
if err != nil {
return err
}
@@ -342,7 +352,7 @@ func (server *Server) SetConfig(ctx context.Context, alertmanagerConfig *alertma
func (server *Server) TestReceiver(ctx context.Context, receiver alertmanagertypes.Receiver) error {
testAlert := alertmanagertypes.NewTestAlert(receiver, time.Now(), time.Now())
- return alertmanagertypes.TestReceiver(ctx, receiver, alertmanagernotify.NewReceiverIntegrations, server.alertmanagerConfig, server.tmpl, server.logger, testAlert.Labels, testAlert)
+ return alertmanagertypes.TestReceiver(ctx, receiver, alertmanagernotify.NewReceiverIntegrations, server.alertmanagerConfig, server.tmpl, server.logger, server.templater, testAlert.Labels, testAlert)
}
func (server *Server) TestAlert(ctx context.Context, receiversMap map[*alertmanagertypes.PostableAlert][]string, config *alertmanagertypes.NotificationConfig) error {
@@ -425,6 +435,7 @@ func (server *Server) TestAlert(ctx context.Context, receiversMap map[*alertmana
server.alertmanagerConfig,
server.tmpl,
server.logger,
+ server.templater,
group.groupLabels,
group.alerts...,
)
diff --git a/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate.go b/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate.go
index 83fbf0ed6e..b302add4f4 100644
--- a/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate.go
+++ b/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate.go
@@ -15,13 +15,6 @@ import (
"github.com/prometheus/common/model"
)
-// Templater expands user-authored title and body templates against a group
-// of alerts and returns channel-ready strings along with the aggregate data
-// a caller might reuse (e.g. to render an email layout around the body).
-type Templater interface {
- Expand(ctx context.Context, req alertmanagertypes.ExpandRequest, alerts []*types.Alert) (*alertmanagertypes.ExpandResult, error)
-}
-
type templater struct {
tmpl *template.Template
logger *slog.Logger
@@ -29,7 +22,7 @@ type templater struct {
// New returns a Templater bound to the given Prometheus alertmanager
// template and logger.
-func New(tmpl *template.Template, logger *slog.Logger) Templater {
+func New(tmpl *template.Template, logger *slog.Logger) alertmanagertypes.Templater {
return &templater{tmpl: tmpl, logger: logger}
}
@@ -137,6 +130,9 @@ func (at *templater) expandTitle(
}
// expandBody expands the body template for each individual alert. Returns nil if the template is empty.
+// Non-nil results are positionally aligned with ntd.Alerts: sb[i] corresponds to alerts[i], and
+// entries for alerts whose template expands to empty are kept as "" so callers can index per-alert
+// metadata (related links, firing/resolved color) by the same index.
func (at *templater) expandBody(
bodyTemplate string,
ntd *alertmanagertypes.NotificationTemplateData,
@@ -144,7 +140,7 @@ func (at *templater) expandBody(
if bodyTemplate == "" {
return nil, nil, nil
}
- var sb []string
+ sb := make([]string, len(ntd.Alerts))
missingVars := make(map[string]bool)
for i := range ntd.Alerts {
processRes, err := preProcessTemplateAndData(bodyTemplate, &ntd.Alerts[i])
@@ -155,13 +151,10 @@ func (at *templater) expandBody(
if err != nil {
return nil, nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "failed to execute custom body template: %s", err.Error())
}
- // add unknown variables and templated text to the result
for k := range processRes.UnknownVars {
missingVars[k] = true
}
- if strings.TrimSpace(part) != "" {
- sb = append(sb, strings.TrimSpace(part))
- }
+ sb[i] = strings.TrimSpace(part)
}
return sb, missingVars, nil
}
@@ -189,17 +182,20 @@ func (at *templater) buildNotificationTemplateData(
externalURL = at.tmpl.ExternalURL.String()
}
- commonAnnotations := extractCommonKV(alerts, func(a *types.Alert) model.LabelSet { return a.Annotations })
+ // Raw (including private `_*`) kv first so buildRuleInfo can read the
+ // private rule annotations. The filtered copies are what ends up
+ // on the template-visible surfaces.
+ rawCommonAnnotations := extractCommonKV(alerts, func(a *types.Alert) model.LabelSet { return a.Annotations })
commonLabels := extractCommonKV(alerts, func(a *types.Alert) model.LabelSet { return a.Labels })
// aggregate labels and annotations from all alerts
labels := aggregateKV(alerts, func(a *types.Alert) model.LabelSet { return a.Labels })
annotations := aggregateKV(alerts, func(a *types.Alert) model.LabelSet { return a.Annotations })
- // Strip private annotations from surfaces visible to templates or
- // notifications; the structured fields on AlertInfo/RuleInfo already hold
- // anything a template needs from them.
- commonAnnotations = alertmanagertypes.FilterPublicAnnotations(commonAnnotations)
+ // Strip private annotations from template-visible surfaces; the structured
+ // fields on AlertInfo/RuleInfo already hold anything a template needs from
+ // them.
+ commonAnnotations := alertmanagertypes.FilterPublicAnnotations(rawCommonAnnotations)
annotations = alertmanagertypes.FilterPublicAnnotations(annotations)
// build the alert data slice
@@ -233,7 +229,7 @@ func (at *templater) buildNotificationTemplateData(
TotalFiring: firing,
TotalResolved: resolved,
},
- Rule: buildRuleInfo(commonLabels, commonAnnotations),
+ Rule: buildRuleInfo(commonLabels, rawCommonAnnotations),
GroupLabels: gl,
CommonLabels: commonLabels,
CommonAnnotations: commonAnnotations,
diff --git a/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate_test.go b/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate_test.go
index 043f579318..94054dac46 100644
--- a/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate_test.go
+++ b/pkg/alertmanager/alertmanagertemplate/alertmanagertemplate_test.go
@@ -19,7 +19,7 @@ import (
// testSetup returns an AlertTemplater and a context pre-populated with group key,
// receiver name, and group labels for use in tests.
-func testSetup(t *testing.T) (Templater, context.Context) {
+func testSetup(t *testing.T) (alertmanagertypes.Templater, context.Context) {
t.Helper()
tmpl := test.CreateTmpl(t)
ctx := context.Background()
diff --git a/pkg/emailing/config.go b/pkg/emailing/config.go
index 8407525361..853d0520d1 100644
--- a/pkg/emailing/config.go
+++ b/pkg/emailing/config.go
@@ -65,7 +65,7 @@ func newConfig() factory.Config {
return &Config{
Enabled: false,
Templates: Templates{
- Directory: "/root/templates",
+ Directory: "/root/templates/email",
Format: Format{
Header: Header{
Enabled: false,
diff --git a/pkg/query-service/rules/threshold_rule.go b/pkg/query-service/rules/threshold_rule.go
index 2ef2d6183d..1b7d8be37e 100644
--- a/pkg/query-service/rules/threshold_rule.go
+++ b/pkg/query-service/rules/threshold_rule.go
@@ -352,13 +352,13 @@ func (r *ThresholdRule) Eval(ctx context.Context, ts time.Time) (int, error) {
link := r.prepareLinksToTraces(ctx, ts, smpl.Metric)
if link != "" && r.hostFromSource() != "" {
r.logger.InfoContext(ctx, "adding traces link to annotations", slog.String("annotation.link", fmt.Sprintf("%s/traces-explorer?%s", r.hostFromSource(), link)))
- annotations = append(annotations, ruletypes.Label{Name: "related_traces", Value: fmt.Sprintf("%s/traces-explorer?%s", r.hostFromSource(), link)})
+ annotations = append(annotations, ruletypes.Label{Name: ruletypes.AnnotationRelatedTraces, Value: fmt.Sprintf("%s/traces-explorer?%s", r.hostFromSource(), link)})
}
case ruletypes.AlertTypeLogs:
link := r.prepareLinksToLogs(ctx, ts, smpl.Metric)
if link != "" && r.hostFromSource() != "" {
r.logger.InfoContext(ctx, "adding logs link to annotations", slog.String("annotation.link", fmt.Sprintf("%s/logs/logs-explorer?%s", r.hostFromSource(), link)))
- annotations = append(annotations, ruletypes.Label{Name: "related_logs", Value: fmt.Sprintf("%s/logs/logs-explorer?%s", r.hostFromSource(), link)})
+ annotations = append(annotations, ruletypes.Label{Name: ruletypes.AnnotationRelatedLogs, Value: fmt.Sprintf("%s/logs/logs-explorer?%s", r.hostFromSource(), link)})
}
}
diff --git a/pkg/query-service/rules/threshold_rule_test.go b/pkg/query-service/rules/threshold_rule_test.go
index 50b8cf7cf1..00ed1d4769 100644
--- a/pkg/query-service/rules/threshold_rule_test.go
+++ b/pkg/query-service/rules/threshold_rule_test.go
@@ -869,7 +869,7 @@ func TestThresholdRuleTracesLink(t *testing.T) {
assert.Equal(t, c.expectAlerts, alertsFound, "case %d", idx)
for _, item := range rule.Active {
for name, value := range item.Annotations.Map() {
- if name == "related_traces" {
+ if name == ruletypes.AnnotationRelatedTraces {
assert.NotEmpty(t, value, "case %d", idx)
assert.Contains(t, value, "GET")
}
@@ -986,7 +986,7 @@ func TestThresholdRuleLogsLink(t *testing.T) {
assert.Equal(t, c.expectAlerts, alertsFound, "case %d", idx)
for _, item := range rule.Active {
for name, value := range item.Annotations.Map() {
- if name == "related_logs" {
+ if name == ruletypes.AnnotationRelatedLogs {
assert.NotEmpty(t, value, "case %d", idx)
assert.Contains(t, value, "testcontainer")
}
diff --git a/pkg/types/alertmanagertypes/notification.go b/pkg/types/alertmanagertypes/notification.go
new file mode 100644
index 0000000000..5a7c9788e2
--- /dev/null
+++ b/pkg/types/alertmanagertypes/notification.go
@@ -0,0 +1,20 @@
+package alertmanagertypes
+
+import (
+ "context"
+ "log/slog"
+
+ "github.com/prometheus/alertmanager/notify"
+ "github.com/prometheus/alertmanager/template"
+ "github.com/prometheus/alertmanager/types"
+)
+
+// Templater expands user-authored title and body templates against a group
+// of alerts. Implemented by pkg/alertmanager/alertmanagertemplate.
+type Templater interface {
+ Expand(ctx context.Context, req ExpandRequest, alerts []*types.Alert) (*ExpandResult, error)
+}
+
+// ReceiverIntegrationsFunc constructs the notify.Integration list for a
+// configured receiver.
+type ReceiverIntegrationsFunc = func(nc Receiver, tmpl *template.Template, logger *slog.Logger, templater Templater) ([]notify.Integration, error)
diff --git a/pkg/types/alertmanagertypes/receiver.go b/pkg/types/alertmanagertypes/receiver.go
index 7058f26475..43456b02b2 100644
--- a/pkg/types/alertmanagertypes/receiver.go
+++ b/pkg/types/alertmanagertypes/receiver.go
@@ -19,8 +19,7 @@ import (
type (
// Receiver is the type for the receiver configuration.
- Receiver = config.Receiver
- ReceiverIntegrationsFunc = func(nc Receiver, tmpl *template.Template, logger *slog.Logger) ([]notify.Integration, error)
+ Receiver = config.Receiver
)
// Creates a new receiver from a string. The input is initialized with the default values from the upstream alertmanager.
@@ -51,7 +50,7 @@ func NewReceiver(input string) (Receiver, error) {
return receiverWithDefaults, nil
}
-func TestReceiver(ctx context.Context, receiver Receiver, receiverIntegrationsFunc ReceiverIntegrationsFunc, config *Config, tmpl *template.Template, logger *slog.Logger, lSet model.LabelSet, alert ...*Alert) error {
+func TestReceiver(ctx context.Context, receiver Receiver, receiverIntegrationsFunc ReceiverIntegrationsFunc, config *Config, tmpl *template.Template, logger *slog.Logger, templater Templater, lSet model.LabelSet, alert ...*Alert) error {
ctx = notify.WithGroupKey(ctx, fmt.Sprintf("%s-%s-%d", receiver.Name, lSet.Fingerprint(), time.Now().Unix()))
ctx = notify.WithGroupLabels(ctx, lSet)
ctx = notify.WithReceiverName(ctx, receiver.Name)
@@ -73,7 +72,7 @@ func TestReceiver(ctx context.Context, receiver Receiver, receiverIntegrationsFu
return err
}
- integrations, err := receiverIntegrationsFunc(receiver, tmpl, logger)
+ integrations, err := receiverIntegrationsFunc(receiver, tmpl, logger, templater)
if err != nil {
return err
}
diff --git a/pkg/types/ruletypes/compare.go b/pkg/types/ruletypes/compare.go
index 187cf8e0e4..37e711f1a2 100644
--- a/pkg/types/ruletypes/compare.go
+++ b/pkg/types/ruletypes/compare.go
@@ -77,6 +77,28 @@ func (c CompareOperator) Normalize() CompareOperator {
}
}
+// Literal returns the canonical literal (string) form of the operator.
+func (c CompareOperator) Literal() string {
+ switch c.Normalize() {
+ case ValueIsAbove:
+ return ValueIsAboveLiteral.StringValue()
+ case ValueIsBelow:
+ return ValueIsBelowLiteral.StringValue()
+ case ValueIsEq:
+ return ValueIsEqLiteral.StringValue()
+ case ValueIsNotEq:
+ return ValueIsNotEqLiteral.StringValue()
+ case ValueAboveOrEq:
+ return ValueAboveOrEqLiteral.StringValue()
+ case ValueBelowOrEq:
+ return ValueBelowOrEqLiteral.StringValue()
+ case ValueOutsideBounds:
+ return ValueOutsideBoundsLiteral.StringValue()
+ default:
+ return c.StringValue()
+ }
+}
+
func (c CompareOperator) Validate() error {
switch c {
case ValueIsAbove,
diff --git a/pkg/types/ruletypes/match.go b/pkg/types/ruletypes/match.go
index 76f15aefbc..81ca342dd0 100644
--- a/pkg/types/ruletypes/match.go
+++ b/pkg/types/ruletypes/match.go
@@ -56,6 +56,24 @@ func (m MatchType) Normalize() MatchType {
}
}
+// Literal returns the canonical literal (string) form of the match type.
+func (m MatchType) Literal() string {
+ switch m.Normalize() {
+ case AtleastOnce:
+ return AtleastOnceLiteral.StringValue()
+ case AllTheTimes:
+ return AllTheTimesLiteral.StringValue()
+ case OnAverage:
+ return OnAverageLiteral.StringValue()
+ case InTotal:
+ return InTotalLiteral.StringValue()
+ case Last:
+ return LastLiteral.StringValue()
+ default:
+ return m.StringValue()
+ }
+}
+
func (m MatchType) Validate() error {
switch m {
case
diff --git a/pkg/types/ruletypes/result_types.go b/pkg/types/ruletypes/result_types.go
index 19bd13dda7..2c3eb04f6e 100644
--- a/pkg/types/ruletypes/result_types.go
+++ b/pkg/types/ruletypes/result_types.go
@@ -24,6 +24,10 @@ type Sample struct {
RecoveryTarget *float64
TargetUnit string
+
+ // CompareOperator and MatchType carry the threshold evaluation context
+ CompareOperator CompareOperator
+ MatchType MatchType
}
func (s Sample) String() string {
diff --git a/pkg/types/ruletypes/threshold.go b/pkg/types/ruletypes/threshold.go
index 71db7d5a66..a8e5f3a11c 100644
--- a/pkg/types/ruletypes/threshold.go
+++ b/pkg/types/ruletypes/threshold.go
@@ -188,6 +188,8 @@ func (r BasicRuleThresholds) Eval(s *qbtypes.TimeSeries, unit string, evalData E
smpl.RecoveryTarget = threshold.RecoveryTarget
}
smpl.TargetUnit = threshold.TargetUnit
+ smpl.CompareOperator = threshold.CompareOperator
+ smpl.MatchType = threshold.MatchType
resultVector = append(resultVector, smpl)
continue
} else if evalData.SendUnmatched {
@@ -197,10 +199,12 @@ func (r BasicRuleThresholds) Eval(s *qbtypes.TimeSeries, unit string, evalData E
}
// prepare the sample with the first point of the series
smpl := Sample{
- Point: Point{T: series.Values[0].Timestamp, V: series.Values[0].Value},
- Metric: PrepareSampleLabelsForRule(series.Labels, threshold.Name),
- Target: *threshold.TargetValue,
- TargetUnit: threshold.TargetUnit,
+ Point: Point{T: series.Values[0].Timestamp, V: series.Values[0].Value},
+ Metric: PrepareSampleLabelsForRule(series.Labels, threshold.Name),
+ Target: *threshold.TargetValue,
+ TargetUnit: threshold.TargetUnit,
+ CompareOperator: threshold.CompareOperator,
+ MatchType: threshold.MatchType,
}
if threshold.RecoveryTarget != nil {
smpl.RecoveryTarget = threshold.RecoveryTarget
@@ -222,6 +226,8 @@ func (r BasicRuleThresholds) Eval(s *qbtypes.TimeSeries, unit string, evalData E
smpl.Target = *threshold.TargetValue
smpl.RecoveryTarget = threshold.RecoveryTarget
smpl.TargetUnit = threshold.TargetUnit
+ smpl.CompareOperator = threshold.CompareOperator
+ smpl.MatchType = threshold.MatchType
// IsRecovering to notify that metrics is in recovery stage
smpl.IsRecovering = true
resultVector = append(resultVector, smpl)
diff --git a/templates/alertmanager/email.gotmpl b/templates/alertmanager/email.gotmpl
new file mode 100644
index 0000000000..f2af4ebf27
--- /dev/null
+++ b/templates/alertmanager/email.gotmpl
@@ -0,0 +1,122 @@
+{{ define "email.signoz.html" }}
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <title>{{.Title}}</title>
+ <style>
+ code {
+ background: #f0f0f0;
+ padding: 2px 6px;
+ border-radius: 3px;
+ font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
+ font-size: 13px;
+ }
+ pre {
+ background: #f0f0f0;
+ padding: 12px 16px;
+ border-radius: 6px;
+ font-size: 13px;
+ overflow-x: auto;
+ white-space: pre;
+ }
+ pre code {
+ background: none;
+ padding: 0;
+ border-radius: 0;
+ font-size: inherit;
+ }
+ table:not([role="presentation"]) {
+ width: 100%;
+ border-collapse: collapse;
+ font-size: 14px;
+ }
+ table:not([role="presentation"]) th {
+ font-weight: 600;
+ text-align: left;
+ padding: 8px 12px;
+ border-bottom: 2px solid #d0d0d0;
+ }
+ table:not([role="presentation"]) td {
+ padding: 8px 12px;
+ border-bottom: 1px solid #e8e8e8;
+ }
+ table:not([role="presentation"]) tr:last-child td {
+ border-bottom: none;
+ }
+ </style>
+</head>
+
+<body style="margin:0;padding:0;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif;line-height:1.6;color:#333;background:#fff">
+ <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0" style="background:#fff">
+ <tr>
+ <td align="center" style="padding:0">
+ <table role="presentation" width="600" cellspacing="0" cellpadding="0" border="0" style="max-width:600px;width:100%;border:1px solid #e2e2e2;border-radius:12px;overflow:hidden">
+
+ <tr>
+ <td align="center" style="padding:20px 20px 12px">
+ <h2 style="margin:0 0 8px;font-size:20px;color:#333">{{.Title}}</h2>
+ <p style="margin:0;font-size:14px;color:#666">
+ Status: <strong>{{.Alert.Status}}</strong>
+ {{if .Alert.TotalFiring}} | Firing: <strong style="color:#e53e3e">{{.Alert.TotalFiring}}</strong>{{end}}
+ {{if .Alert.TotalResolved}} | Resolved: <strong style="color:#38a169">{{.Alert.TotalResolved}}</strong>{{end}}
+ </p>
+ </td>
+ </tr>
+
+ <tr>
+ <td style="padding:0 20px">
+ <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0">
+ <tr><td style="border-top:1px solid #e2e2e2;font-size:0;line-height:0" height="1">&nbsp;</td></tr>
+ </table>
+ </td>
+ </tr>
+
+ {{range .Bodies}}
+ <tr>
+ <td style="padding:8px 20px">
+ <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0">
+ <tr>
+ <td style="padding:16px;background:#fafafa;border:1px solid #e8e8e8;border-radius:6px">
+ {{.}}
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ {{end}}
+
+ {{if .NotificationTemplateData.ExternalURL}}
+ <tr>
+ <td style="padding:16px 20px">
+ <table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0">
+ <tr>
+ <td align="center">
+ <a href="{{.NotificationTemplateData.ExternalURL}}" target="_blank" style="display:inline-block;padding:12px 32px;font-size:14px;font-weight:600;color:#fff;background:#4E74F8;text-decoration:none;border-radius:4px">
+ View in SigNoz
+ </a>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ {{end}}
+
+ <tr>
+ <td align="center" style="padding:8px 16px 16px">
+ <p style="margin:0;font-size:12px;color:#999;line-height:1.5">
+ Sent by SigNoz AlertManager
+ </p>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+</body>
+
+</html>
+{{ end }}
\ No newline at end of file