Compare commits

..

4 Commits

Author SHA1 Message Date
Naman Verma
24e2c703d7 test: remove v2 rules list api test 2026-11-16 16:44:16 +05:30
Naman Verma
e20c197566 Merge branch 'main' into nv/rules-list-severity 2026-11-16 16:09:44 +05:30
Naman Verma
0215d7b50c fix: regen frontend spec and fix type issues 2026-11-16 16:09:10 +05:30
Naman Verma
60fb8397d0 feat: return threshold names as severity if label is absent 2026-11-16 15:46:54 +05:30
43 changed files with 442 additions and 1109 deletions

View File

@@ -11861,6 +11861,13 @@ components:
allOf:
- $ref: '#/components/schemas/RuletypesRuleType'
description: 'The evaluation strategy: threshold_rule, promql_rule or anomaly_rule.'
severities:
description: 'The severities the rule''s alerts carry: the severity label
when set, else each distinct threshold name in lowercase, ordered critical,
error, warning, info, then other names. Read-only.'
items:
type: string
type: array
state:
allOf:
- $ref: '#/components/schemas/RuletypesAlertState'
@@ -11877,6 +11884,7 @@ components:
- alert
- alertType
- ruleType
- severities
type: object
RuletypesListableRuleViews:
properties:

View File

@@ -52,7 +52,7 @@ func (f *formatter) JSONIsArray(column, path string) []byte {
func (f *formatter) JSONArrayElements(column, path, alias string) ([]byte, []byte) {
var sql []byte
sql = append(sql, "jsonb_array_elements("...)
sql = f.bunf.AppendIdent(sql, column)
sql = append(sql, f.TextToJsonColumn(column)...)
sql = append(sql, f.convertJSONPathToPostgresWithMode(path, false)...)
sql = append(sql, ") AS "...)
sql = f.bunf.AppendIdent(sql, alias)

View File

@@ -213,28 +213,28 @@ func TestJSONArrayElements(t *testing.T) {
column: "data",
path: "$",
alias: "elem",
expected: `jsonb_array_elements("data") AS "elem"`,
expected: `jsonb_array_elements("data"::jsonb) AS "elem"`,
},
{
name: "root path empty",
column: "data",
path: "",
alias: "elem",
expected: `jsonb_array_elements("data") AS "elem"`,
expected: `jsonb_array_elements("data"::jsonb) AS "elem"`,
},
{
name: "nested path",
column: "metadata",
path: "$.items",
alias: "item",
expected: `jsonb_array_elements("metadata"->'items') AS "item"`,
expected: `jsonb_array_elements("metadata"::jsonb->'items') AS "item"`,
},
{
name: "deeply nested path",
column: "json_col",
path: "$.user.tags",
alias: "tag",
expected: `jsonb_array_elements("json_col"->'user'->'tags') AS "tag"`,
expected: `jsonb_array_elements("json_col"::jsonb->'user'->'tags') AS "tag"`,
},
}

View File

@@ -13013,6 +13013,11 @@ export interface RuletypesListableRuleDTO {
* @description The evaluation strategy: threshold_rule, promql_rule or anomaly_rule.
*/
ruleType: RuletypesRuleTypeDTO;
/**
* @type array
* @description The severities the rule's alerts carry: the severity label when set, else each distinct threshold name in lowercase, ordered critical, error, warning, info, then other names. Read-only.
*/
severities: string[];
/**
* @description The current evaluation state: inactive, pending, recovering, firing, nodata or disabled.
*/

View File

@@ -20,7 +20,8 @@ export const SlackInitialConfig: Partial<AlertmanagertypesChannelSlackConfigDTO>
*Summary:* {{ .Annotations.summary }}
*Description:* {{ .Annotations.description }}
{{ template "signoz.relatedLinks.slack" . }}
*RelatedLogs:* {{ if gt (len .Annotations.related_logs) 0 -}} View in <{{ .Annotations.related_logs }}|logs explorer> {{- end}}
*RelatedTraces:* {{ if gt (len .Annotations.related_traces) 0 -}} View in <{{ .Annotations.related_traces }}|traces explorer> {{- end}}
*Details:*
{{ range .Labels.SortedPairs }} • *{{ .Name }}:* {{ .Value }}
@@ -140,7 +141,11 @@ export const JsmOpsInitialConfig: Partial<AlertmanagertypesChannelJSMOpsConfigDT
{{ end }}{{ if .GeneratorURL }}[View in SigNoz]({{ .GeneratorURL }})
{{ end }}{{ template "signoz.relatedLinks.markdown" . }}{{ end }}`,
{{ end }}{{ if .Annotations.related_logs }}[View related logs]({{ .Annotations.related_logs }})
{{ end }}{{ if .Annotations.related_traces }}[View related traces]({{ .Annotations.related_traces }})
{{ end }}{{ end }}`,
priority:
'{{ if eq (index .Alerts 0).Labels.severity "critical" }}P1{{ else if eq (index .Alerts 0).Labels.severity "warning" }}P2{{ else if eq (index .Alerts 0).Labels.severity "info" }}P3{{ else }}P4{{ end }}',
tags: 'signoz-alert',
@@ -163,7 +168,11 @@ export const IncidentIOInitialConfig: Partial<AlertmanagertypesChannelIncidentIO
{{ end }}{{ if .GeneratorURL }}[View in SigNoz]({{ .GeneratorURL }})
{{ end }}{{ template "signoz.relatedLinks.markdown" . }}{{ end }}`,
{{ end }}{{ if .Annotations.related_logs }}[View related logs]({{ .Annotations.related_logs }})
{{ end }}{{ if .Annotations.related_traces }}[View related traces]({{ .Annotations.related_traces }})
{{ end }}{{ end }}`,
};
export const EmailInitialConfig: Partial<AlertmanagertypesChannelEmailConfigDTO> =

View File

@@ -131,7 +131,11 @@ export const listRulesV3Response = (
return {
status: 'success',
data: {
rules: matching.slice(offset, offset + limit),
// every mock rule carries a severity label, so the threshold-name fallback is not mirrored
rules: matching.slice(offset, offset + limit).map((rule) => ({
...rule,
severities: rule.labels?.severity ? [rule.labels.severity] : [],
})),
total: matching.length,
labels: uniqueLabelPairs(rules),
reservedKeywords: RULES_RESERVED_KEYWORDS,

View File

@@ -13,7 +13,7 @@ export const editAlertChannelInitialValue = {
export const slackTitleDefaultValue = `[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }} {{- if gt (len .CommonLabels) (len .GroupLabels) -}} {{" "}}( {{- with .CommonLabels.Remove .GroupLabels.Names }} {{- range $index, $label := .SortedPairs -}} {{ if $index }}, {{ end }} {{- $label.Name }}="{{ $label.Value -}}" {{- end }} {{- end -}} ) {{- end }}`;
export const slackDescriptionDefaultValue = `{{ range .Alerts -}} *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }} *Summary:* {{ .Annotations.summary }} *Description:* {{ .Annotations.description }} {{ template "signoz.relatedLinks.slack" . }} *Details:* {{ range .Labels.SortedPairs }} • *{{ .Name }}:* {{ .Value }} {{ end }} {{ end }}`;
export const slackDescriptionDefaultValue = `{{ range .Alerts -}} *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }} *Summary:* {{ .Annotations.summary }} *Description:* {{ .Annotations.description }} *RelatedLogs:* {{ if gt (len .Annotations.related_logs) 0 -}} View in <{{ .Annotations.related_logs }}|logs explorer> {{- end}} *RelatedTraces:* {{ if gt (len .Annotations.related_traces) 0 -}} View in <{{ .Annotations.related_traces }}|traces explorer> {{- end}} *Details:* {{ range .Labels.SortedPairs }} • *{{ .Name }}:* {{ .Value }} {{ end }} {{ end }}`;
export const googleChatTitleDefaultValue = `[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }}`;

View File

@@ -43,7 +43,11 @@ export const alertRulesPage = (
return {
status: 'success',
data: {
rules: rules.slice(offset, offset + limit),
// every mock rule carries a severity label, so the threshold-name fallback is not mirrored
rules: rules.slice(offset, offset + limit).map((rule) => ({
...rule,
severities: rule.labels?.severity ? [rule.labels.severity] : [],
})),
total: rules.length,
labels: [...labels.values()],
reservedKeywords: [...RESERVED_RULE_KEYWORDS],

View File

@@ -29,6 +29,7 @@ import (
"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"
@@ -488,8 +489,11 @@ func appendRelatedLinkButtons(alerts []*types.Alert, bodies []string) {
if i >= len(alerts) || bodies[i] == "" {
continue
}
for _, link := range alertmanagertypes.BuildRelatedLinks(alerts[i].Annotations) {
bodies[i] += htmlButton(link.Label, link.URL)
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))
}
}
}

View File

@@ -1159,58 +1159,3 @@ func TestPrepareContent(t *testing.T) {
func ptrTo(b bool) *bool {
return &b
}
func TestAppendRelatedLinkButtons(t *testing.T) {
testCases := []struct {
name string
annotations model.LabelSet
body string
expectedOrder []string
}{
{
name: "BothLinks_LogsBeforeTraces",
annotations: model.LabelSet{"related_traces": "https://signoz.example/traces?a=1", "related_logs": "https://signoz.example/logs?a=1"},
body: "<p>body</p>",
expectedOrder: []string{"https://signoz.example/logs?a=1", "View Related Logs", "https://signoz.example/traces?a=1", "View Related Traces"},
},
{
name: "TracesOnly_SingleButton",
annotations: model.LabelSet{"related_traces": "https://signoz.example/traces?b=2"},
body: "<p>body</p>",
expectedOrder: []string{"https://signoz.example/traces?b=2", "View Related Traces"},
},
{
name: "NoLinks_BodyUnchanged",
annotations: model.LabelSet{"summary": "s"},
body: "<p>body</p>",
expectedOrder: nil,
},
{
name: "EmptyBody_NoButtons",
annotations: model.LabelSet{"related_logs": "https://signoz.example/logs?c=3"},
body: "",
expectedOrder: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
alerts := []*types.Alert{{Alert: model.Alert{Annotations: testCase.annotations}}}
bodies := []string{testCase.body}
appendRelatedLinkButtons(alerts, bodies)
if testCase.expectedOrder == nil {
assert.Equal(t, testCase.body, bodies[0])
return
}
assert.True(t, strings.HasPrefix(bodies[0], testCase.body))
last := -1
for _, needle := range testCase.expectedOrder {
idx := strings.Index(bodies[0], needle)
assert.Greater(t, idx, last, needle)
last = idx
}
})
}
}

View File

@@ -243,9 +243,13 @@ func statusLine(alerts []*types.Alert) string {
// the annotations the ruler attaches to each alert. Empty links are skipped.
func relatedButtons(alert *types.Alert) []button {
var buttons []button
for _, link := range alertmanagertypes.BuildRelatedLinks(alert.Annotations) {
buttons = append(buttons, button{Text: link.Label, OnClick: onClick{OpenLink: openLink{URL: link.URL}}})
add := func(text, u string) {
if u != "" {
buttons = append(buttons, button{Text: text, OnClick: onClick{OpenLink: openLink{URL: u}}})
}
}
add("View Related Logs", string(alert.Annotations[ruletypes.AnnotationRelatedLogs]))
add("View Related Traces", string(alert.Annotations[ruletypes.AnnotationRelatedTraces]))
return buttons
}

View File

@@ -334,8 +334,8 @@ func TestGoogleChatLinkButtons(t *testing.T) {
ruletypes.LabelRuleSource: "https://signoz.example/alerts/1",
},
annotations: model.LabelSet{
alertmanagertypes.AnnotationRelatedLogs: "https://signoz.example/logs",
alertmanagertypes.AnnotationRelatedTraces: "https://signoz.example/traces",
ruletypes.AnnotationRelatedLogs: "https://signoz.example/logs",
ruletypes.AnnotationRelatedTraces: "https://signoz.example/traces",
},
wantButtons: map[string]string{
"Open in SigNoz": "https://signoz.example/alerts/1",
@@ -416,8 +416,8 @@ func TestGoogleChatMultiAlertSections(t *testing.T) {
ruletypes.LabelRuleSource: "https://signoz.example/alerts/1",
},
Annotations: model.LabelSet{
ruletypes.AnnotationBodyTemplate: "an alert fired",
alertmanagertypes.AnnotationRelatedLogs: model.LabelValue(logs),
ruletypes.AnnotationBodyTemplate: "an alert fired",
ruletypes.AnnotationRelatedLogs: model.LabelValue(logs),
},
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Minute),
@@ -464,7 +464,7 @@ func TestGoogleChatDefaultBodyGrouped(t *testing.T) {
ruletypes.LabelRuleSource: "https://signoz.example/alerts/1",
},
Annotations: model.LabelSet{
alertmanagertypes.AnnotationRelatedLogs: model.LabelValue("https://signoz.example/logs?pod=" + pod),
ruletypes.AnnotationRelatedLogs: model.LabelValue("https://signoz.example/logs?pod=" + pod),
},
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Minute),

View File

@@ -11,10 +11,10 @@ import (
"testing"
"time"
test "github.com/SigNoz/signoz/pkg/alertmanager/alertmanagernotify/alertmanagernotifytest"
"github.com/SigNoz/signoz/pkg/alertmanager/alertmanagertemplate"
"github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/notify/test"
"github.com/prometheus/alertmanager/types"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
@@ -111,7 +111,7 @@ func TestNotifyFiringEvent(t *testing.T) {
assert.Contains(t, ev.Description, "**Alert:** HighCPU (critical)")
assert.Contains(t, ev.Description, "**Summary:** cpu high")
assert.Contains(t, ev.Description, "[View in SigNoz](https://signoz.example/alerts/edit?ruleId=1)")
assert.Contains(t, ev.Description, "[View Related Logs](https://signoz.example/logs?q=1)")
assert.Contains(t, ev.Description, "[View related logs](https://signoz.example/logs?q=1)")
assert.Equal(t, map[string]string{
"alertname": "HighCPU",
"severity": "critical",

View File

@@ -193,9 +193,8 @@ func wikiDeepLinks(alerts []*types.Alert) string {
}
}
add("Open in SigNoz", string(a.Labels[ruletypes.LabelRuleSource]))
for _, link := range alertmanagertypes.BuildRelatedLinks(a.Annotations) {
add(link.Label, link.URL)
}
add("View Related Logs", string(a.Annotations[ruletypes.AnnotationRelatedLogs]))
add("View Related Traces", string(a.Annotations[ruletypes.AnnotationRelatedTraces]))
return strings.Join(parts, " · ")
}
@@ -311,9 +310,8 @@ func deepLinks(alerts []*types.Alert) map[string]any {
})
}
add("Open in SigNoz", string(a.Labels[ruletypes.LabelRuleSource]))
for _, link := range alertmanagertypes.BuildRelatedLinks(a.Annotations) {
add(link.Label, link.URL)
}
add("View Related Logs", string(a.Annotations[ruletypes.AnnotationRelatedLogs]))
add("View Related Traces", string(a.Annotations[ruletypes.AnnotationRelatedTraces]))
if len(parts) == 0 {
return nil
}

View File

@@ -320,7 +320,7 @@ func TestNotifyRichDescriptionPanelAndLinks(t *testing.T) {
m := newMockJira(t)
a := alert(true)
a.Labels[ruletypes.LabelRuleSource] = model.LabelValue("https://app.signoz.io/alerts?ruleId=1")
a.Annotations[alertmanagertypes.AnnotationRelatedLogs] = model.LabelValue("https://app.signoz.io/logs")
a.Annotations[ruletypes.AnnotationRelatedLogs] = model.LabelValue("https://app.signoz.io/logs")
_, err := newNotifier(t, m).Notify(ctx(), a)
require.NoError(t, err)
@@ -460,7 +460,7 @@ func TestNotifySelfHosted(t *testing.T) {
linkedAlert := func(firing bool) *types.Alert {
a := alert(firing)
a.Labels[ruletypes.LabelRuleSource] = "https://app.signoz.io/alerts?ruleId=1"
a.Annotations[alertmanagertypes.AnnotationRelatedLogs] = "https://app.signoz.io/logs"
a.Annotations[ruletypes.AnnotationRelatedLogs] = "https://app.signoz.io/logs"
return a
}
testCases := []struct {
@@ -615,52 +615,3 @@ func TestResolveCloudID(t *testing.T) {
})
}
}
func TestDeepLinks(t *testing.T) {
testCases := []struct {
name string
labels model.LabelSet
annotations model.LabelSet
expectedTexts []string
}{
{
name: "RuleSourceAndBothLinks_DotSeparatedInOrder",
labels: model.LabelSet{"ruleSource": "https://signoz.example/alerts?ruleId=1"},
annotations: model.LabelSet{"related_traces": "https://signoz.example/traces?a=1", "related_logs": "https://signoz.example/logs?a=1"},
expectedTexts: []string{"Open in SigNoz", " · ", "View Related Logs", " · ", "View Related Traces"},
},
{
name: "TracesOnly_NoRuleSource",
labels: model.LabelSet{},
annotations: model.LabelSet{"related_traces": "https://signoz.example/traces?b=2"},
expectedTexts: []string{"View Related Traces"},
},
{
name: "NoLinks_Nil",
labels: model.LabelSet{},
annotations: model.LabelSet{"summary": "s"},
expectedTexts: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
alerts := []*types.Alert{{Alert: model.Alert{Labels: testCase.labels, Annotations: testCase.annotations}}}
paragraph := deepLinks(alerts)
if testCase.expectedTexts == nil {
assert.Nil(t, paragraph)
return
}
require.NotNil(t, paragraph)
content, ok := paragraph["content"].([]any)
require.True(t, ok)
texts := make([]string, 0, len(content))
for _, part := range content {
texts = append(texts, part.(map[string]any)["text"].(string))
}
assert.Equal(t, testCase.expectedTexts, texts)
})
}
}

View File

@@ -18,6 +18,7 @@ import (
"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"
@@ -268,8 +269,11 @@ func (n *Notifier) prepareContent(ctx context.Context, alerts []*types.Alert, tm
// buttons for an alert, or nil when no related-link annotations are present.
func buildRelatedLinkActions(alert *types.Alert) []config.SlackAction {
var actions []config.SlackAction
for _, link := range alertmanagertypes.BuildRelatedLinks(alert.Annotations) {
actions = append(actions, config.SlackAction{Type: "button", Text: link.Label, URL: link.URL})
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
}

View File

@@ -571,37 +571,3 @@ func TestSlackMessageField(t *testing.T) {
_, err = notifier.Notify(ctx)
require.NoError(t, err, "Notify failed")
}
func TestBuildRelatedLinkActions(t *testing.T) {
testCases := []struct {
name string
annotations model.LabelSet
expected []config.SlackAction
}{
{
name: "BothLinks_LogsBeforeTraces",
annotations: model.LabelSet{"related_traces": "https://signoz.example/traces?a=1", "related_logs": "https://signoz.example/logs?a=1"},
expected: []config.SlackAction{
{Type: "button", Text: "View Related Logs", URL: "https://signoz.example/logs?a=1"},
{Type: "button", Text: "View Related Traces", URL: "https://signoz.example/traces?a=1"},
},
},
{
name: "LogsOnly_SingleAction",
annotations: model.LabelSet{"related_logs": "https://signoz.example/logs?b=2"},
expected: []config.SlackAction{{Type: "button", Text: "View Related Logs", URL: "https://signoz.example/logs?b=2"}},
},
{
name: "NoLinks_Nil",
annotations: model.LabelSet{"summary": "s"},
expected: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
alert := &types.Alert{Alert: model.Alert{Annotations: testCase.annotations}}
assert.Equal(t, testCase.expected, buildRelatedLinkActions(alert))
})
}
}

View File

@@ -267,8 +267,8 @@ func buildAlertData(a *types.Alert, receiver string) alertmanagertypes.AlertData
IsRecovering: labels[ruletypes.LabelIsRecovering] == "true",
},
Rule: buildRuleInfo(labels, annotations),
Log: alertmanagertypes.LinkInfo{URL: annotations[alertmanagertypes.AnnotationRelatedLogs]},
Trace: alertmanagertypes.LinkInfo{URL: annotations[alertmanagertypes.AnnotationRelatedTraces]},
Log: alertmanagertypes.LinkInfo{URL: annotations[ruletypes.AnnotationRelatedLogs]},
Trace: alertmanagertypes.LinkInfo{URL: annotations[ruletypes.AnnotationRelatedTraces]},
Labels: labels,
// Strip private annotations once the structured fields above have
// been populated from the raw map.

View File

@@ -190,58 +190,6 @@ Description: Request rate exceeded 10k/s`},
wantBody: []string{"auth-service (firing)", "payment-service (resolved)"},
wantIsDefaultBody: false,
},
{
// Rule templates hand the block the preprocessed per-alert map, so the
// related links must resolve from its lowercase annotations.
name: "related links block: rule body renders markdown links from preprocessed data",
alerts: []*types.Alert{
createAlert(
map[string]string{ruletypes.LabelAlertName: "HighErrorRate"},
map[string]string{"related_logs": "https://signoz.example/logs?q=1", "related_traces": "https://signoz.example/traces?q=1"},
true,
),
},
input: alertmanagertypes.ExpandRequest{
TitleTemplate: "$rule.name",
BodyTemplate: `$rule.name fired.{{ "\n\n" }}{{ template "signoz.relatedLinks.markdown" . }}`,
},
wantTitle: "HighErrorRate",
wantBody: []string{"HighErrorRate fired.\n\n[View Related Logs](https://signoz.example/logs?q=1)\n\n[View Related Traces](https://signoz.example/traces?q=1)"},
wantIsDefaultBody: false,
},
{
name: "related links block: rule body per alert, slack format, links absent on one alert",
alerts: []*types.Alert{
createAlert(map[string]string{ruletypes.LabelAlertName: "PodCrashLoop", "pod": "api-1"}, map[string]string{"related_logs": "https://signoz.example/logs?pod=api-1"}, true),
createAlert(map[string]string{ruletypes.LabelAlertName: "PodCrashLoop", "pod": "api-2"}, nil, true),
},
input: alertmanagertypes.ExpandRequest{
TitleTemplate: "$rule.name",
BodyTemplate: `$labels.pod: {{ template "signoz.relatedLinks.slack" . }}`,
},
wantTitle: "PodCrashLoop",
wantBody: []string{"api-1: <https://signoz.example/logs?pod=api-1|View Related Logs>", "api-2:"},
wantIsDefaultBody: false,
},
{
// Channel default templates range over upstream template.Alert values,
// the other shape the block has to accept.
name: "related links block: channel default body renders from template.Alert",
alerts: []*types.Alert{
createAlert(
map[string]string{ruletypes.LabelAlertName: "HighLatency"},
map[string]string{"related_traces": "https://signoz.example/traces?q=2"},
true,
),
},
input: alertmanagertypes.ExpandRequest{
DefaultTitleTemplate: "{{ .CommonLabels.alertname }}",
DefaultBodyTemplate: `{{ range .Alerts }}{{ template "signoz.relatedLinks.slack" . }}{{ end }}`,
},
wantTitle: "HighLatency",
wantBody: []string{"<https://signoz.example/traces?q=2|View Related Traces>"},
wantIsDefaultBody: true,
},
{
// $environment is not a known AlertData or NotificationTemplateData field,
// so it lands in MissingVars and renders as "<no value>" in the output.

View File

@@ -5,6 +5,8 @@ import (
"slices"
"strings"
"github.com/huandu/go-sqlbuilder"
grammar "github.com/SigNoz/signoz/pkg/parser/filterquery/grammar"
"github.com/SigNoz/signoz/pkg/parser/filterquery/sqlcompiler"
qbtypesv5 "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
@@ -12,13 +14,15 @@ import (
)
const (
ruleDataColumn = "rule.data"
ruleLabelsField = "labels"
nameJSONPath = "$.alert"
descriptionPath = "$.description"
labelsJSONPath = "$.labels"
alertTypePath = "$.alertType"
ruleTypePath = "$.ruleType"
ruleDataColumn = "rule.data"
ruleLabelsField = "labels"
nameJSONPath = "$.alert"
descriptionPath = "$.description"
labelsJSONPath = "$.labels"
alertTypePath = "$.alertType"
ruleTypePath = "$.ruleType"
thresholdSpecPath = "$.condition.thresholds.spec"
thresholdNamePath = "$.name"
)
// ruleFieldResolver maps rule list DSL keys; a non-reserved key is a case-sensitive label lookup.
@@ -53,7 +57,7 @@ func (r ruleFieldResolver) ResolveComparison(v *sqlcompiler.Visitor, rawKey stri
}
_, reservedAllowed := allowedOperations[operation]
// reserved severity is itself the severity-label lookup; an identical spelling would duplicate the predicate
// reserved severity already reads the severity label; an identical spelling would duplicate that predicate
if ruletypes.DSLKey(key) == ruletypes.DSLKeySeverity && rawKey == string(ruletypes.DSLKeySeverity) {
labelAllowed = false
}
@@ -86,8 +90,7 @@ func (r ruleFieldResolver) resolveReservedKey(v *sqlcompiler.Visitor, ctx *gramm
columnExpression := string(v.Formatter.JSONExtractString(ruleDataColumn, nameJSONPath))
return v.BuildStringOperation(v.Sb, ctx, operation, columnExpression, string(key))
case ruletypes.DSLKeySeverity:
// severity is an alias for labels.severity, sharing its missing-label semantics.
return r.labelComparison(v, ctx, operation, "severity")
return r.severityComparison(v, ctx, operation)
case ruletypes.DSLKeyCreatedBy:
return v.BuildStringOperation(v.Sb, ctx, operation, "rule.created_by", string(key))
case ruletypes.DSLKeyUpdatedBy:
@@ -105,6 +108,33 @@ func (r ruleFieldResolver) resolveReservedKey(v *sqlcompiler.Visitor, ctx *gramm
return ""
}
// severityComparison mirrors ruletypes' storedRuleData.extractSeverities. A positive term holds when any
// threshold name matches and a negative term when none does, so the EXISTS takes the positive operator.
func (ruleFieldResolver) severityComparison(v *sqlcompiler.Visitor, ctx *grammar.ComparisonContext, operation qbtypesv5.FilterOperator) string {
key := string(ruletypes.DSLKeySeverity)
labelExpression := string(v.Formatter.JSONExtractMapValue(ruleDataColumn, ruleLabelsField, ruletypes.LabelSeverityName))
labelPredicate := v.BuildStringOperation(v.Sb, ctx, operation, labelExpression, key)
if labelPredicate == "" {
return ""
}
positiveOperation := operation
if operation.IsNegativeOperator() {
positiveOperation = operation.Inverse()
}
thresholdsFrom, threshold := v.Formatter.JSONArrayElements(ruleDataColumn, thresholdSpecPath, "threshold")
nameExpression := string(v.Formatter.LowerExpression(string(v.Formatter.JSONExtractString(string(threshold), thresholdNamePath))))
subqueryBuilder := sqlbuilder.NewSelectBuilder()
subqueryBuilder.Select("1").From(string(thresholdsFrom)).Where(v.BuildStringOperation(subqueryBuilder, ctx, positiveOperation, nameExpression, key))
thresholdsPredicate := v.Sb.Exists(subqueryBuilder)
if operation.IsNegativeOperator() {
thresholdsPredicate = v.Sb.NotExists(subqueryBuilder)
}
return fmt.Sprintf("CASE WHEN %s IS NOT NULL THEN %s ELSE %s END", labelExpression, labelPredicate, thresholdsPredicate)
}
// A missing label evaluates as the empty string for every value operator; EXISTS/NOT EXISTS test the raw extraction.
func (ruleFieldResolver) labelComparison(v *sqlcompiler.Visitor, ctx *grammar.ComparisonContext, operation qbtypesv5.FilterOperator, labelKey string) string {
columnExpression := string(v.Formatter.JSONExtractMapValue(ruleDataColumn, ruleLabelsField, labelKey))

View File

@@ -122,33 +122,55 @@ func TestCompileName(t *testing.T) {
func TestCompileSeverityAndLabels(t *testing.T) {
runCompileCases(t, []compileCase{
{
subtestName: "SeverityEquals_TargetsLabelsMap",
subtestName: "SeverityEquals_LabelElseThresholdNames",
dslQueryToCompile: "severity = 'critical'",
expectedSQL: `COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') = ?`,
expectedArgs: []any{"critical"},
expectedSQL: `CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') = ? ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) = ?) END`,
expectedArgs: []any{"critical", "critical"},
},
{
subtestName: "SeverityNotEquals_MissingLabelAsEmptyString",
subtestName: "SeverityNotEquals_NoThresholdNameMatches",
dslQueryToCompile: "severity != 'critical'",
expectedSQL: `COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') <> ?`,
expectedArgs: []any{"critical"},
expectedSQL: `CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') <> ? ` +
`ELSE NOT EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) = ?) END`,
expectedArgs: []any{"critical", "critical"},
},
{
subtestName: "SeverityNotEqualsEmpty_ExcludesRulesWithoutSeverity",
dslQueryToCompile: "severity != ''",
expectedSQL: `COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') <> ?`,
expectedArgs: []any{""},
subtestName: "SeverityIn_AnyThresholdNameMatches",
dslQueryToCompile: "severity IN ['critical', 'error']",
expectedSQL: `CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') IN (?, ?) ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) IN (?, ?)) END`,
expectedArgs: []any{"critical", "error", "critical", "error"},
},
{
subtestName: "SeverityExists_ThroughAlias",
dslQueryToCompile: "severity EXISTS",
subtestName: "SeverityNotIn_NoThresholdNameMatches",
dslQueryToCompile: "severity NOT IN ['critical', 'error']",
expectedSQL: `CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') NOT IN (?, ?) ` +
`ELSE NOT EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) IN (?, ?)) END`,
expectedArgs: []any{"critical", "error", "critical", "error"},
},
{
subtestName: "SeverityContains_EscapesWildcardsInEveryBranch",
dslQueryToCompile: "severity CONTAINS 'p_'",
expectedSQL: `CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') LIKE ? ESCAPE '\' ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) LIKE ? ESCAPE '\') END`,
expectedArgs: []any{`%p\_%`, `%p\_%`},
},
{
subtestName: "SeverityExists_Rejected",
dslQueryToCompile: "severity EXISTS",
expectedErrShouldContain: `operator EXISTS is not allowed for key "severity"`,
},
{
subtestName: "SeverityNotExists_Rejected",
dslQueryToCompile: "severity NOT EXISTS",
expectedErrShouldContain: `operator NOT EXISTS is not allowed for key "severity"`,
},
{
subtestName: "LabelsSeverityExists_LabelOnly",
dslQueryToCompile: "labels.severity EXISTS",
expectedSQL: `json_extract("rule"."data", '$.labels."severity"') IS NOT NULL`,
},
{
subtestName: "SeverityNotExists_ThroughAlias",
dslQueryToCompile: "severity NOT EXISTS",
expectedSQL: `json_extract("rule"."data", '$.labels."severity"') IS NULL`,
},
{
subtestName: "LabelEquals",
dslQueryToCompile: "labels.team = 'infra'",
@@ -287,8 +309,9 @@ func TestCompileComposition(t *testing.T) {
subtestName: "OrOfNameAndSeverity",
dslQueryToCompile: "name CONTAINS 'pay' OR severity = 'critical'",
expectedSQL: `((json_extract("rule"."data", '$.alert') LIKE ? ESCAPE '\' OR COALESCE(json_extract("rule"."data", '$.labels."name"'), '') LIKE ? ESCAPE '\') ` +
`OR COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') = ?)`,
expectedArgs: []any{"%pay%", "%pay%", "critical"},
`OR CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') = ? ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) = ?) END)`,
expectedArgs: []any{"%pay%", "%pay%", "critical", "critical"},
},
})
}
@@ -301,9 +324,10 @@ func TestCompileComplexExamples(t *testing.T) {
`AND severity IN ['critical', 'error'] AND created_by != 'ops@signoz.io'`,
expectedSQL: `((json_extract("rule"."data", '$.alert') LIKE ? ESCAPE '\' OR COALESCE(json_extract("rule"."data", '$.labels."name"'), '') LIKE ? ESCAPE '\') ` +
`AND COALESCE(json_extract("rule"."data", '$.labels."team"'), '') = ? ` +
`AND COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') IN (?, ?) ` +
`AND CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') IN (?, ?) ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) IN (?, ?)) END ` +
`AND (rule.created_by <> ? AND COALESCE(json_extract("rule"."data", '$.labels."created_by"'), '') <> ?))`,
expectedArgs: []any{"%latency%", "%latency%", "payments", "critical", "error", "ops@signoz.io", "ops@signoz.io"},
expectedArgs: []any{"%latency%", "%latency%", "payments", "critical", "error", "critical", "error", "ops@signoz.io", "ops@signoz.io"},
},
{
subtestName: "NestedOrAnd_WithParens",
@@ -311,9 +335,10 @@ func TestCompileComplexExamples(t *testing.T) {
`AND (severity = 'critical' OR labels.team EXISTS)`,
expectedSQL: `((COALESCE(json_extract("rule"."data", '$.labels."env"'), '') IN (?, ?) ` +
`OR (json_extract("rule"."data", '$.alert') LIKE ? ESCAPE '\' OR COALESCE(json_extract("rule"."data", '$.labels."name"'), '') LIKE ? ESCAPE '\')) ` +
`AND (COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') = ? ` +
`AND (CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') = ? ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) = ?) END ` +
`OR json_extract("rule"."data", '$.labels."team"') IS NOT NULL))`,
expectedArgs: []any{"prod", "staging", "%prod%", "%prod%", "critical"},
expectedArgs: []any{"prod", "staging", "%prod%", "%prod%", "critical", "critical"},
},
{
subtestName: "NotOverGroup_AndedWithEnum",
@@ -376,17 +401,19 @@ func TestCompileReservedLabelCollisions(t *testing.T) {
expectedArgs: []any{"x", "x"},
},
{
subtestName: "SeverityExactSpelling_SinglePredicate",
subtestName: "SeverityExactSpelling_NoLabelPredicateAdded",
dslQueryToCompile: "severity = 'critical'",
expectedSQL: `COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') = ?`,
expectedArgs: []any{"critical"},
expectedSQL: `CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') = ? ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) = ?) END`,
expectedArgs: []any{"critical", "critical"},
},
{
subtestName: "SeverityDifferentCase_MatchesBothLabelSpellings",
subtestName: "SeverityDifferentCase_MatchesReservedOrExactCaseLabel",
dslQueryToCompile: "Severity = 'critical'",
expectedSQL: `(COALESCE(json_extract("rule"."data", '$.labels."severity"'), '') = ? ` +
expectedSQL: `(CASE WHEN json_extract("rule"."data", '$.labels."severity"') IS NOT NULL THEN json_extract("rule"."data", '$.labels."severity"') = ? ` +
`ELSE EXISTS (SELECT 1 FROM json_each("rule"."data", '$.condition.thresholds.spec') AS "threshold" WHERE lower(json_extract("threshold"."value", '$.name')) = ?) END ` +
`OR COALESCE(json_extract("rule"."data", '$.labels."Severity"'), '') = ?)`,
expectedArgs: []any{"critical", "critical"},
expectedArgs: []any{"critical", "critical", "critical"},
},
{
subtestName: "RangeOperator_ReservedOnly",

View File

@@ -12,7 +12,6 @@ import (
"github.com/SigNoz/signoz/pkg/contextlinks"
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/querier"
"github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
"github.com/SigNoz/signoz/pkg/types/ctxtypes"
"github.com/SigNoz/signoz/pkg/types/instrumentationtypes"
"github.com/SigNoz/signoz/pkg/types/rulestatehistorytypes"
@@ -305,14 +304,14 @@ func (r *ThresholdRule) Eval(ctx context.Context, ts time.Time) (int, error) {
}
link := r.ExternalURL(explorerPath, params)
r.logger.InfoContext(ctx, "adding traces link to annotations", slog.String("annotation.link", link))
annotations = append(annotations, ruletypes.Label{Name: alertmanagertypes.AnnotationRelatedTraces, Value: link})
annotations = append(annotations, ruletypes.Label{Name: ruletypes.AnnotationRelatedTraces, Value: link})
}
case ruletypes.AlertTypeLogs:
params := r.prepareParamsForLogs(ctx, ts, smpl.Metric)
if len(params) > 0 {
link := r.ExternalURL("logs/logs-explorer", params)
r.logger.InfoContext(ctx, "adding logs link to annotations", slog.String("annotation.link", link))
annotations = append(annotations, ruletypes.Label{Name: alertmanagertypes.AnnotationRelatedLogs, Value: link})
annotations = append(annotations, ruletypes.Label{Name: ruletypes.AnnotationRelatedLogs, Value: link})
}
}

View File

@@ -17,7 +17,6 @@ import (
"github.com/SigNoz/signoz/pkg/instrumentation/instrumentationtest"
"github.com/SigNoz/signoz/pkg/telemetrystore"
"github.com/SigNoz/signoz/pkg/telemetrystore/telemetrystoretest"
"github.com/SigNoz/signoz/pkg/types/alertmanagertypes"
"github.com/SigNoz/signoz/pkg/types/metrictypes"
qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
"github.com/SigNoz/signoz/pkg/types/ruletypes"
@@ -891,7 +890,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 == alertmanagertypes.AnnotationRelatedTraces {
if name == ruletypes.AnnotationRelatedTraces {
assert.NotEmpty(t, value, "case %d", idx)
assert.Contains(t, value, "GET")
}
@@ -987,7 +986,7 @@ func TestThresholdRuleAITracesLink(t *testing.T) {
assert.Equal(t, c.expectAlerts, alertsFound, "case %d", idx)
for _, item := range rule.Active {
link := item.Annotations.Map()[alertmanagertypes.AnnotationRelatedTraces]
link := item.Annotations.Map()[ruletypes.AnnotationRelatedTraces]
assert.True(t, strings.HasPrefix(link, "http://localhost:8080/ai-observability/explorer?"), "case %d: %s", idx, link)
assert.Contains(t, link, "builder_ai_query", "case %d", idx)
assert.Contains(t, link, "llm-gateway", "case %d", idx)
@@ -1099,7 +1098,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 == alertmanagertypes.AnnotationRelatedLogs {
if name == ruletypes.AnnotationRelatedLogs {
assert.NotEmpty(t, value, "case %d", idx)
assert.Contains(t, value, "testcontainer")
}

View File

@@ -275,7 +275,6 @@ func NewSQLMigrationProviderFactories(
sqlmigration.NewMigrateRulesV1ToV2Alpha1Factory(sqlstore),
sqlmigration.NewAddLLMPricingRuleTuplesFactory(sqlstore),
sqlmigration.NewRemoveUnsupportedQuickFiltersFactory(),
sqlmigration.NewMigrateRelatedLinksTemplateFactory(),
)
}

View File

@@ -1,535 +0,0 @@
package sqlmigration
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"log/slog"
"regexp"
"slices"
"strings"
"text/template/parse"
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/factory"
"github.com/uptrace/bun"
"github.com/uptrace/bun/migrate"
)
const (
relatedLinksSlackHeader = "*RelatedLogs:*"
)
var (
relatedLinksActionPattern = regexp.MustCompile(`(?s)\{\{-?(.*?)-?\}\}`)
relatedLogsReferencePattern = regexp.MustCompile(`\.Annotations\.related_logs(?:[^A-Za-z0-9_]|$)`)
relatedTracesReferencePattern = regexp.MustCompile(`\.Annotations\.related_traces(?:[^A-Za-z0-9_]|$)`)
relatedLinksSlackKind = relatedLinksChannelKind{
configsKey: "slack_configs",
field: "text",
replacement: `{{ template "signoz.relatedLinks.slack" . }}`,
defaults: []string{
"*RelatedLogs:* {{ .Annotations.related_logs }}\n *RelatedTraces:* {{ .Annotations.related_traces }}",
"*RelatedLogs:* {{ if gt (len .Annotations.related_logs) 0 -}} View in <{{ .Annotations.related_logs }}|logs explorer> {{- end}}\n *RelatedTraces:* {{ if gt (len .Annotations.related_traces) 0 -}} View in <{{ .Annotations.related_traces }}|traces explorer> {{- end}}",
},
}
relatedLinksMSTeamsKind = relatedLinksChannelKind{
configsKey: "msteamsv2_configs",
field: "text",
replacement: relatedLinksSlackKind.replacement,
defaults: relatedLinksSlackKind.defaults,
}
relatedLinksIncidentIOKind = relatedLinksChannelKind{
configsKey: "incidentio_configs",
field: "description",
replacement: `{{ template "signoz.relatedLinks.markdown" . }}`,
defaults: []string{
"{{ if .Annotations.related_logs }}[View related logs]({{ .Annotations.related_logs }})\n\n{{ end }}{{ if .Annotations.related_traces }}[View related traces]({{ .Annotations.related_traces }})\n\n{{ end }}",
},
}
relatedLinksJSMOpsKind = relatedLinksChannelKind{
configsKey: "jsmops_configs",
field: "description",
replacement: relatedLinksIncidentIOKind.replacement,
defaults: relatedLinksIncidentIOKind.defaults,
}
relatedLinksTelegramKind = relatedLinksChannelKind{
configsKey: "telegram_configs",
field: "message",
replacement: `{{ template "signoz.relatedLinks.html" . }}`,
defaults: []string{
"{{ if .Annotations.related_logs }}<a href=\"{{ .Annotations.related_logs | html }}\">View related logs</a>\n{{ end }}{{ if .Annotations.related_traces }}<a href=\"{{ .Annotations.related_traces | html }}\">View related traces</a>\n{{ end }}",
},
}
relatedLinksKindsByChannelType = map[string]relatedLinksChannelKind{
"slack": relatedLinksSlackKind,
"msteams": relatedLinksMSTeamsKind,
"incidentio": relatedLinksIncidentIOKind,
"jsmops": relatedLinksJSMOpsKind,
"telegram": relatedLinksTelegramKind,
}
errRelatedLinksCustomised = errors.New(errors.TypeInvalidInput, errors.CodeInvalidInput, "related links section differs from the shipped defaults")
errRelatedLinksUnrecognised = errors.New(errors.TypeInvalidInput, errors.CodeInvalidInput, "related links section not in the expected shape")
)
type migrateRelatedLinksTemplate struct {
logger *slog.Logger
}
func NewMigrateRelatedLinksTemplateFactory() factory.ProviderFactory[SQLMigration, Config] {
return factory.NewProviderFactory(
factory.MustNewName("migrate_related_links_template"),
func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) {
return &migrateRelatedLinksTemplate{logger: ps.Logger}, nil
},
)
}
func (migration *migrateRelatedLinksTemplate) Register(migrations *migrate.Migrations) error {
return migrations.Register(migration.Up, migration.Down)
}
func (migration *migrateRelatedLinksTemplate) Up(ctx context.Context, db *bun.DB) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() {
_ = tx.Rollback()
}()
if err := migration.migrateConfigs(ctx, tx); err != nil {
return err
}
if err := migration.migrateChannels(ctx, tx); err != nil {
return err
}
return tx.Commit()
}
func (migration *migrateRelatedLinksTemplate) Down(context.Context, *bun.DB) error {
return nil
}
func (migration *migrateRelatedLinksTemplate) migrateConfigs(ctx context.Context, tx bun.Tx) error {
rows := make([]*relatedLinksConfigRow, 0)
if err := tx.NewSelect().Model(&rows).Scan(ctx); err != nil {
return err
}
for _, row := range rows {
cfg := make(map[string]json.RawMessage)
if err := json.Unmarshal([]byte(row.Config), &cfg); err != nil {
migration.logger.WarnContext(ctx, "skipping alertmanager config with unreadable config", slog.String("config_id", row.ID), errors.Attr(err))
continue
}
receiversRaw, ok := cfg["receivers"]
if !ok || string(receiversRaw) == "null" {
continue
}
receivers := make([]map[string]json.RawMessage, 0)
if err := json.Unmarshal(receiversRaw, &receivers); err != nil {
migration.logger.WarnContext(ctx, "skipping alertmanager config with unreadable receivers", slog.String("config_id", row.ID), errors.Attr(err))
continue
}
changed := false
for _, receiver := range receivers {
var receiverName string
_ = json.Unmarshal(receiver["name"], &receiverName)
for _, kind := range []relatedLinksChannelKind{relatedLinksSlackKind, relatedLinksMSTeamsKind, relatedLinksIncidentIOKind, relatedLinksJSMOpsKind, relatedLinksTelegramKind} {
rewritten, err := rewriteRelatedLinksInReceiver(receiver, kind)
if err != nil {
migration.logger.WarnContext(ctx, "leaving receiver related links template as is", slog.String("config_id", row.ID), slog.String("receiver", receiverName), slog.String("configs_key", kind.configsKey), errors.Attr(err))
}
changed = changed || rewritten
}
}
if !changed {
continue
}
newReceivers, err := json.Marshal(receivers)
if err != nil {
return err
}
cfg["receivers"] = newReceivers
newConfig, err := json.Marshal(cfg)
if err != nil {
return err
}
if _, err := tx.NewUpdate().
Model((*relatedLinksConfigRow)(nil)).
Set("config = ?", string(newConfig)).
Set("hash = ?", fmt.Sprintf("%x", md5.Sum(newConfig))).
Where("id = ?", row.ID).
Exec(ctx); err != nil {
return err
}
}
return nil
}
func (migration *migrateRelatedLinksTemplate) migrateChannels(ctx context.Context, tx bun.Tx) error {
channelTypes := make([]string, 0, len(relatedLinksKindsByChannelType))
for channelType := range relatedLinksKindsByChannelType {
channelTypes = append(channelTypes, channelType)
}
rows := make([]*relatedLinksChannelRow, 0)
if err := tx.NewSelect().Model(&rows).Where("type IN (?)", bun.In(channelTypes)).Scan(ctx); err != nil {
return err
}
for _, row := range rows {
kind := relatedLinksKindsByChannelType[row.Type]
query := tx.NewUpdate().Model((*relatedLinksChannelRow)(nil)).Where("id = ?", row.ID)
changed := false
receiver := make(map[string]json.RawMessage)
if err := json.Unmarshal([]byte(row.Data), &receiver); err != nil {
migration.logger.WarnContext(ctx, "skipping notification channel with unreadable data", slog.String("org_id", row.OrgID), slog.String("channel_id", row.ID), errors.Attr(err))
continue
}
dataChanged, err := rewriteRelatedLinksInReceiver(receiver, kind)
if err != nil {
migration.logger.WarnContext(ctx, "leaving notification channel data template as is", slog.String("org_id", row.OrgID), slog.String("channel_id", row.ID), errors.Attr(err))
}
if dataChanged {
newData, err := json.Marshal(receiver)
if err != nil {
return err
}
query = query.Set("data = ?", string(newData))
changed = true
}
if row.Spec != "" {
spec := make(map[string]json.RawMessage)
if err := json.Unmarshal([]byte(row.Spec), &spec); err != nil {
migration.logger.WarnContext(ctx, "skipping notification channel spec with unreadable json", slog.String("org_id", row.OrgID), slog.String("channel_id", row.ID), errors.Attr(err))
} else {
specChanged, err := rewriteRelatedLinksInField(spec, kind)
if err != nil {
migration.logger.WarnContext(ctx, "leaving notification channel spec template as is", slog.String("org_id", row.OrgID), slog.String("channel_id", row.ID), errors.Attr(err))
}
if specChanged {
newSpec, err := json.Marshal(spec)
if err != nil {
return err
}
query = query.Set("spec = ?", string(newSpec))
changed = true
}
}
}
if !changed {
continue
}
if _, err := query.Exec(ctx); err != nil {
return err
}
}
return nil
}
type relatedLinksChannelKind struct {
configsKey string
field string
replacement string
defaults []string
}
type relatedLinksConfigRow struct {
bun.BaseModel `bun:"table:alertmanager_config"`
ID string `bun:"id"`
Config string `bun:"config"`
}
type relatedLinksChannelRow struct {
bun.BaseModel `bun:"table:notification_channel"`
ID string `bun:"id"`
OrgID string `bun:"org_id"`
Type string `bun:"type"`
Data string `bun:"data"`
Spec string `bun:"spec,nullzero"`
}
// relatedLinksChunk is one text run or one {{...}} action of a template, with
// its byte span in the source and the action's trimmed inner text.
type relatedLinksChunk struct {
start int
end int
action bool
inner string
}
func rewriteRelatedLinksInReceiver(receiver map[string]json.RawMessage, kind relatedLinksChannelKind) (bool, error) {
configsRaw, ok := receiver[kind.configsKey]
if !ok || string(configsRaw) == "null" {
return false, nil
}
configs := make([]map[string]json.RawMessage, 0)
if err := json.Unmarshal(configsRaw, &configs); err != nil {
return false, err
}
changed := false
var firstErr error
for _, config := range configs {
fieldChanged, err := rewriteRelatedLinksInField(config, kind)
if err != nil && firstErr == nil {
firstErr = err
}
changed = changed || fieldChanged
}
if !changed {
return false, firstErr
}
newConfigs, err := json.Marshal(configs)
if err != nil {
return false, err
}
receiver[kind.configsKey] = newConfigs
return true, firstErr
}
func rewriteRelatedLinksInField(object map[string]json.RawMessage, kind relatedLinksChannelKind) (bool, error) {
fieldRaw, ok := object[kind.field]
if !ok || string(fieldRaw) == "null" {
return false, nil
}
var tmpl string
if err := json.Unmarshal(fieldRaw, &tmpl); err != nil {
return false, err
}
rewritten, changed, err := rewriteRelatedLinks(tmpl, kind)
if err != nil || !changed {
return false, err
}
newField, err := json.Marshal(rewritten)
if err != nil {
return false, err
}
object[kind.field] = newField
return true, nil
}
// rewriteRelatedLinks swaps every related links section of tmpl for the kind's
// block call. It returns the input unchanged when tmpl never references the
// related links annotations, and an error when it does but a section was
// customised, is not in the expected shape or does not parse.
func rewriteRelatedLinks(tmpl string, kind relatedLinksChannelKind) (string, bool, error) {
shipped := make([]string, 0, len(kind.defaults))
for _, snippet := range kind.defaults {
normalized, err := normalizeRelatedLinksTemplate(snippet)
if err != nil {
return tmpl, false, err
}
shipped = append(shipped, normalized)
}
rewritten := tmpl
changed := false
for {
chunks := lexRelatedLinksChunks(rewritten)
start, end, found := findRelatedLinksSection(rewritten, chunks)
if !found {
if !changed && !strings.Contains(rewritten, kind.replacement) && (indexRelatedLinksAction(chunks, 0, relatedLogsReferencePattern) >= 0 || indexRelatedLinksAction(chunks, 0, relatedTracesReferencePattern) >= 0) {
return tmpl, false, errRelatedLinksUnrecognised
}
break
}
section, err := normalizeRelatedLinksTemplate(rewritten[start:end])
if err != nil {
return tmpl, false, err
}
if !slices.Contains(shipped, section) {
return tmpl, false, errRelatedLinksCustomised
}
rewritten = rewritten[:start] + kind.replacement + rewritten[end:]
changed = true
}
if !changed {
return tmpl, false, nil
}
if _, err := normalizeRelatedLinksTemplate(rewritten); err != nil {
return tmpl, false, err
}
return rewritten, true, nil
}
func lexRelatedLinksChunks(tmpl string) []relatedLinksChunk {
var chunks []relatedLinksChunk
pos := 0
for _, match := range relatedLinksActionPattern.FindAllStringSubmatchIndex(tmpl, -1) {
if match[0] > pos {
chunks = append(chunks, relatedLinksChunk{start: pos, end: match[0]})
}
chunks = append(chunks, relatedLinksChunk{start: match[0], end: match[1], action: true, inner: strings.TrimSpace(tmpl[match[2]:match[3]])})
pos = match[1]
}
if pos < len(tmpl) {
chunks = append(chunks, relatedLinksChunk{start: pos, end: len(tmpl)})
}
return chunks
}
// findRelatedLinksSection spans from the first action naming related_logs (or
// the Slack header text before it) to the end of the first action naming
// related_traces that follows.
func findRelatedLinksSection(tmpl string, chunks []relatedLinksChunk) (int, int, bool) {
logsIdx := indexRelatedLinksAction(chunks, 0, relatedLogsReferencePattern)
if logsIdx < 0 {
return 0, 0, false
}
logsEnd := indexRelatedLinksBlockEnd(chunks, logsIdx)
if logsEnd < 0 {
return 0, 0, false
}
tracesIdx := indexRelatedLinksAction(chunks, logsEnd+1, relatedTracesReferencePattern)
if tracesIdx < 0 {
return 0, 0, false
}
tracesEnd := indexRelatedLinksBlockEnd(chunks, tracesIdx)
if tracesEnd < 0 {
return 0, 0, false
}
start := chunks[logsIdx].start
if logsIdx > 0 && !chunks[logsIdx-1].action {
previous := tmpl[chunks[logsIdx-1].start:chunks[logsIdx-1].end]
if header := strings.LastIndex(previous, relatedLinksSlackHeader); header >= 0 {
start = chunks[logsIdx-1].start + header
}
}
return start, chunks[tracesEnd].end, true
}
// indexRelatedLinksAction skips comments so a mention inside {{/* */}} never
// anchors a section.
func indexRelatedLinksAction(chunks []relatedLinksChunk, from int, reference *regexp.Regexp) int {
for i := from; i < len(chunks); i++ {
if chunks[i].action && !strings.HasPrefix(chunks[i].inner, "/*") && reference.MatchString(chunks[i].inner) {
return i
}
}
return -1
}
// indexRelatedLinksBlockEnd returns the chunk closing the block opened at idx,
// or idx itself for a bare action.
func indexRelatedLinksBlockEnd(chunks []relatedLinksChunk, idx int) int {
if !opensRelatedLinksBlock(chunks[idx].inner) {
return idx
}
depth := 0
for i := idx; i < len(chunks); i++ {
if !chunks[i].action {
continue
}
if opensRelatedLinksBlock(chunks[i].inner) {
depth++
continue
}
if chunks[i].inner == "end" {
depth--
if depth == 0 {
return i
}
}
}
return -1
}
func opensRelatedLinksBlock(inner string) bool {
for _, keyword := range []string{"if ", "range ", "with ", "define ", "block "} {
if strings.HasPrefix(inner, keyword) {
return true
}
}
return false
}
// normalizeRelatedLinksTemplate re-emits a parsed template with action spacing
// canonicalised and text runs collapsed to single spaces, so two snippets that
// differ only in whitespace compare equal.
func normalizeRelatedLinksTemplate(tmpl string) (string, error) {
tree := parse.New("related_links")
tree.Mode = parse.SkipFuncCheck
parsed, err := tree.Parse(tmpl, "{{", "}}", map[string]*parse.Tree{})
if err != nil {
return "", err
}
var builder strings.Builder
writeNormalizedRelatedLinksNode(&builder, parsed.Root)
return builder.String(), nil
}
func writeNormalizedRelatedLinksNode(builder *strings.Builder, node parse.Node) {
switch node := node.(type) {
case *parse.ListNode:
for _, child := range node.Nodes {
writeNormalizedRelatedLinksNode(builder, child)
}
case *parse.TextNode:
builder.WriteString(strings.Join(strings.Fields(string(node.Text)), " "))
builder.WriteString("|")
case *parse.IfNode:
builder.WriteString("{{if ")
builder.WriteString(node.Pipe.String())
builder.WriteString("}}")
writeNormalizedRelatedLinksNode(builder, node.List)
if node.ElseList != nil {
builder.WriteString("{{else}}")
writeNormalizedRelatedLinksNode(builder, node.ElseList)
}
builder.WriteString("{{end}}")
default:
builder.WriteString(node.String())
}
}

View File

@@ -89,7 +89,11 @@ const (
{{ end }}{{ if .GeneratorURL }}[View in SigNoz]({{ .GeneratorURL }})
{{ end }}{{ template "signoz.relatedLinks.markdown" . }}{{ end }}`
{{ end }}{{ if .Annotations.related_logs }}[View related logs]({{ .Annotations.related_logs }})
{{ end }}{{ if .Annotations.related_traces }}[View related traces]({{ .Annotations.related_traces }})
{{ end }}{{ end }}`
)
// IncidentIOReceiverConfig is the SigNoz incident.io receiver, backed by an

View File

@@ -93,7 +93,11 @@ const (
{{ end }}{{ if .GeneratorURL }}[View in SigNoz]({{ .GeneratorURL }})
{{ end }}{{ template "signoz.relatedLinks.markdown" . }}{{ end }}`
{{ end }}{{ if .Annotations.related_logs }}[View related logs]({{ .Annotations.related_logs }})
{{ end }}{{ if .Annotations.related_traces }}[View related traces]({{ .Annotations.related_traces }})
{{ end }}{{ end }}`
)
// JSMOpsReceiverConfig is the SigNoz Jira Service Management Ops receiver. It is

View File

@@ -22,7 +22,9 @@ const (
{{ if .Annotations.summary }}<b>Summary:</b> {{ .Annotations.summary | html }}
{{ end }}{{ if .Annotations.description }}<b>Description:</b> {{ .Annotations.description | html }}
{{ end }}{{ if .GeneratorURL }}<a href="{{ .GeneratorURL | html }}">View in SigNoz</a>
{{ end }}{{ template "signoz.relatedLinks.html" . }}
{{ end }}{{ if .Annotations.related_logs }}<a href="{{ .Annotations.related_logs | html }}">View related logs</a>
{{ end }}{{ if .Annotations.related_traces }}<a href="{{ .Annotations.related_traces | html }}">View related traces</a>
{{ end }}
{{ end }}`
)

View File

@@ -100,9 +100,9 @@ func TestDefaultTelegramTemplatesEscapeAlertContent(t *testing.T) {
assert.NotContains(t, rendered, "[FIRING:1]", "the title is sent separately")
assert.Contains(t, rendered, "<b>Summary:</b> latency &gt; 2s")
assert.Contains(t, rendered, `<a href="http://localhost:8080/alerts?a=1&amp;b=2">View in SigNoz</a>`)
assert.Contains(t, rendered, `<a href="http://localhost:8080/logs?a=1&amp;b=2">View Related Logs</a>`)
assert.Contains(t, rendered, `<a href="http://localhost:8080/logs?a=1&amp;b=2">View related logs</a>`)
assert.NotContains(t, rendered, "&amp;lt;", "double-escaped")
assert.NotContains(t, rendered, "View Related Traces")
assert.NotContains(t, rendered, "View related traces")
})
}
}

View File

@@ -1,51 +0,0 @@
package alertmanagertypes
import (
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/common/model"
)
const (
AnnotationRelatedLogs = "related_logs"
AnnotationRelatedTraces = "related_traces"
)
const (
relatedLogsLabel = "View Related Logs"
relatedTracesLabel = "View Related Traces"
)
type RelatedLink struct {
Label string
URL string
}
func BuildRelatedLinks(annotations model.LabelSet) []RelatedLink {
return newRelatedLinks(string(annotations[AnnotationRelatedLogs]), string(annotations[AnnotationRelatedTraces]))
}
// resolveRelatedLinksForTemplate backs the relatedLinks template func: channel templates pass template.Alert, rule templates pass the preprocessed map.
func resolveRelatedLinksForTemplate(alert any) []RelatedLink {
switch a := alert.(type) {
case template.Alert:
return newRelatedLinks(a.Annotations[AnnotationRelatedLogs], a.Annotations[AnnotationRelatedTraces])
case map[string]any:
annotations, _ := a["annotations"].(template.KV)
return newRelatedLinks(annotations[AnnotationRelatedLogs], annotations[AnnotationRelatedTraces])
}
return nil
}
func newRelatedLinks(logsURL, tracesURL string) []RelatedLink {
var links []RelatedLink
if logsURL != "" {
links = append(links, RelatedLink{Label: relatedLogsLabel, URL: logsURL})
}
if tracesURL != "" {
links = append(links, RelatedLink{Label: relatedTracesLabel, URL: tracesURL})
}
return links
}

View File

@@ -1,100 +0,0 @@
package alertmanagertypes
import (
"testing"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
)
func TestBuildRelatedLinks(t *testing.T) {
testCases := []struct {
name string
annotations model.LabelSet
expected []RelatedLink
}{
{
name: "Both_LogsFirst",
annotations: model.LabelSet{"related_traces": "https://signoz.io/traces-explorer?a=1", "related_logs": "https://signoz.io/logs/logs-explorer?a=1"},
expected: []RelatedLink{
{Label: "View Related Logs", URL: "https://signoz.io/logs/logs-explorer?a=1"},
{Label: "View Related Traces", URL: "https://signoz.io/traces-explorer?a=1"},
},
},
{
name: "LogsOnly",
annotations: model.LabelSet{"related_logs": "https://signoz.io/logs/logs-explorer?b=2", "summary": "s"},
expected: []RelatedLink{{Label: "View Related Logs", URL: "https://signoz.io/logs/logs-explorer?b=2"}},
},
{
name: "TracesOnly",
annotations: model.LabelSet{"related_traces": "https://signoz.io/traces-explorer?c=3"},
expected: []RelatedLink{{Label: "View Related Traces", URL: "https://signoz.io/traces-explorer?c=3"}},
},
{
name: "EmptyValues_NoLinks",
annotations: model.LabelSet{"related_logs": "", "related_traces": ""},
expected: nil,
},
{
name: "Nil_NoLinks",
annotations: nil,
expected: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.expected, BuildRelatedLinks(testCase.annotations))
})
}
}
func TestResolveRelatedLinksForTemplate(t *testing.T) {
testCases := []struct {
name string
alert any
expected []RelatedLink
}{
{
name: "ChannelTemplate_Both_LogsFirst",
alert: template.Alert{Annotations: template.KV{"related_traces": "https://signoz.io/traces-explorer?d=4", "related_logs": "https://signoz.io/logs/logs-explorer?d=4"}},
expected: []RelatedLink{
{Label: "View Related Logs", URL: "https://signoz.io/logs/logs-explorer?d=4"},
{Label: "View Related Traces", URL: "https://signoz.io/traces-explorer?d=4"},
},
},
{
name: "ChannelTemplate_NoAnnotations_NoLinks",
alert: template.Alert{Labels: template.KV{"alertname": "a"}},
expected: nil,
},
{
name: "RuleTemplate_LogsOnly",
alert: map[string]any{"annotations": template.KV{"related_logs": "https://signoz.io/logs/logs-explorer?f=6"}, "labels": template.KV{"alertname": "a"}},
expected: []RelatedLink{{Label: "View Related Logs", URL: "https://signoz.io/logs/logs-explorer?f=6"}},
},
{
name: "RuleTemplate_NoAnnotations_NoLinks",
alert: map[string]any{"labels": template.KV{"alertname": "a"}},
expected: nil,
},
{
name: "Nil_NoLinks",
alert: nil,
expected: nil,
},
{
name: "UnsupportedShape_NoLinks",
alert: template.KV{"related_logs": "https://signoz.io/logs/logs-explorer?k=11"},
expected: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.expected, resolveRelatedLinksForTemplate(testCase.alert))
})
}
}

View File

@@ -19,7 +19,6 @@ func AdditionalFuncMap() tmpltext.FuncMap {
"urlescape": func(value string) tmplhtml.HTML {
return tmplhtml.HTML(url.QueryEscape(value))
},
"relatedLinks": resolveRelatedLinksForTemplate,
}
}
@@ -46,12 +45,6 @@ func FromGlobs(paths []string) (*alertmanagertemplate.Template, error) {
{{ define "__ruleIdPath" }}{{- $isTestAlert := "" -}}{{- range .CommonLabels.SortedPairs -}}{{- if eq .Name "testalert" -}}{{- if eq .Value "true" -}}{{- $isTestAlert = "true" -}}{{- end -}}{{- end -}}{{- end -}}{{- range .CommonLabels.SortedPairs -}}{{- if eq .Name "ruleId" -}}{{- if ne .Value "" -}}/edit?ruleId={{ .Value | urlescape }}{{- if $isTestAlert -}}&isTestAlert=true{{- end -}}{{- end -}}{{- end -}}{{- end -}}{{- end }}
{{ define "__alertmanagerURL" }}{{ .ExternalURL }}/alerts{{ template "__ruleIdPath" . }}{{ end }}
{{ define "msteamsv2.default.titleLink" }}{{ template "__alertmanagerURL" . }}{{ end }}
{{ define "signoz.relatedLinks.slack" }}{{ range $i, $link := relatedLinks . }}{{ if $i }} · {{ end }}<{{ $link.URL }}|{{ $link.Label }}>{{ end }}{{ end }}
{{ define "signoz.relatedLinks.markdown" }}{{ range relatedLinks . }}[{{ .Label }}]({{ .URL }})
{{ end }}{{ end }}
{{ define "signoz.relatedLinks.html" }}{{ range relatedLinks . }}<a href="{{ .URL | html }}">{{ .Label | html }}</a>
{{ end }}{{ end }}
`))); err != nil {
return nil, errors.WrapInternalf(err, errors.CodeInternal, "error parsing alertmanager templates")
}

View File

@@ -201,116 +201,3 @@ func TestFromGlobs(t *testing.T) {
})
}
}
func TestFromGlobsRelatedLinks(t *testing.T) {
tmpl, err := FromGlobs([]string{})
require.NoError(t, err)
tmpl.ExternalURL = &url.URL{Scheme: "http", Host: "localhost:8080"}
testCases := []struct {
name string
template string
annotations model.LabelSet
expected string
}{
{
name: "Slack_Both_DotSeparated",
template: `{{ template "signoz.relatedLinks.slack" . }}`,
annotations: model.LabelSet{"related_logs": "https://signoz.io/logs/logs-explorer?a=1", "related_traces": "https://signoz.io/traces-explorer?a=1"},
expected: "<https://signoz.io/logs/logs-explorer?a=1|View Related Logs> · <https://signoz.io/traces-explorer?a=1|View Related Traces>",
},
{
name: "Slack_LogsOnly_NoSeparator",
template: `{{ template "signoz.relatedLinks.slack" . }}`,
annotations: model.LabelSet{"related_logs": "https://signoz.io/logs/logs-explorer?b=2"},
expected: "<https://signoz.io/logs/logs-explorer?b=2|View Related Logs>",
},
{
name: "Slack_NoLinks_Empty",
template: `{{ template "signoz.relatedLinks.slack" . }}`,
annotations: model.LabelSet{"summary": "s"},
expected: "",
},
{
name: "Markdown_Both_BlankLineAfterEach",
template: `{{ template "signoz.relatedLinks.markdown" . }}`,
annotations: model.LabelSet{"related_logs": "https://signoz.io/logs/logs-explorer?c=3", "related_traces": "https://signoz.io/traces-explorer?c=3"},
expected: "[View Related Logs](https://signoz.io/logs/logs-explorer?c=3)\n\n[View Related Traces](https://signoz.io/traces-explorer?c=3)\n\n",
},
{
name: "Markdown_TracesOnly",
template: `{{ template "signoz.relatedLinks.markdown" . }}`,
annotations: model.LabelSet{"related_traces": "https://signoz.io/traces-explorer?d=4"},
expected: "[View Related Traces](https://signoz.io/traces-explorer?d=4)\n\n",
},
{
name: "Markdown_NoLinks_Empty",
template: `{{ template "signoz.relatedLinks.markdown" . }}`,
annotations: model.LabelSet{},
expected: "",
},
{
name: "Function_DirectRange",
template: `{{ range relatedLinks . }}{{ .Label }}={{ .URL }};{{ end }}`,
annotations: model.LabelSet{"related_logs": "https://signoz.io/logs/logs-explorer?e=5", "related_traces": "https://signoz.io/traces-explorer?e=5"},
expected: "View Related Logs=https://signoz.io/logs/logs-explorer?e=5;View Related Traces=https://signoz.io/traces-explorer?e=5;",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
alert := &types.Alert{Alert: model.Alert{Labels: model.LabelSet{"alertname": "a"}, Annotations: testCase.annotations}, UpdatedAt: time.Now()}
data := tmpl.Data("__receiver", model.LabelSet{}, alert)
got, err := tmpl.ExecuteTextString(`{{ range .Alerts }}`+testCase.template+`{{ end }}`, data)
require.NoError(t, err)
assert.Equal(t, testCase.expected, got)
})
}
}
func TestFromGlobsRelatedLinksHTML(t *testing.T) {
tmpl, err := FromGlobs([]string{})
require.NoError(t, err)
tmpl.ExternalURL = &url.URL{Scheme: "http", Host: "localhost:8080"}
testCases := []struct {
name string
annotations model.LabelSet
expected string
}{
{
name: "Both_EscapedHref_LineEach",
annotations: model.LabelSet{"related_logs": "http://localhost:8080/logs?a=1&b=2", "related_traces": "http://localhost:8080/traces?a=1&b=2"},
expected: "<a href=\"http://localhost:8080/logs?a=1&amp;b=2\">View Related Logs</a>\n<a href=\"http://localhost:8080/traces?a=1&amp;b=2\">View Related Traces</a>\n",
},
{
name: "LogsOnly",
annotations: model.LabelSet{"related_logs": "http://localhost:8080/logs?c=3"},
expected: "<a href=\"http://localhost:8080/logs?c=3\">View Related Logs</a>\n",
},
{
name: "NoLinks_Empty",
annotations: model.LabelSet{"summary": "s"},
expected: "",
},
}
renderers := map[string]func(string, any) (string, error){
"Text": tmpl.ExecuteTextString,
"HTML": tmpl.ExecuteHTMLString,
}
for _, testCase := range testCases {
for engine, render := range renderers {
t.Run(testCase.name+"_"+engine, func(t *testing.T) {
alert := &types.Alert{Alert: model.Alert{Labels: model.LabelSet{"alertname": "a"}, Annotations: testCase.annotations}, UpdatedAt: time.Now()}
data := tmpl.Data("__receiver", model.LabelSet{}, alert)
got, err := render(`{{ range .Alerts }}{{ template "signoz.relatedLinks.html" . }}{{ end }}`, data)
require.NoError(t, err)
assert.Equal(t, testCase.expected, got)
})
}
}
}

View File

@@ -32,6 +32,8 @@ const (
const (
AnnotationTitleTemplate = "_title_template"
AnnotationBodyTemplate = "_body_template"
AnnotationRelatedLogs = "related_logs"
AnnotationRelatedTraces = "related_traces"
AnnotationValue = "value"
AnnotationThresholdValue = "threshold.value"
AnnotationCompareOp = "compare_op"

View File

@@ -45,8 +45,8 @@ func ReservedFilterKeys() []DSLKey {
// ReservedOps lists the operators each reserved DSL key accepts; `labels.<key>` terms use LabelsKeyOps.
var ReservedOps = map[DSLKey]map[qbtypesv5.FilterOperator]struct{}{
DSLKeyName: stringSearchOps(),
// severity aliases labels.severity, so it takes the labels operator set.
DSLKeySeverity: LabelsKeyOps,
// every rule has severities (see storedRuleData.extractSeverities), so EXISTS and NOT EXISTS do not apply.
DSLKeySeverity: stringSearchOps(),
DSLKeyCreatedBy: stringSearchOps(),
DSLKeyUpdatedBy: stringSearchOps(),
DSLKeyCreatedAt: numericRangeOps(),

View File

@@ -21,6 +21,7 @@ type ListableRule struct {
RuleType RuleType `json:"ruleType" required:"true" description:"The evaluation strategy: threshold_rule, promql_rule or anomaly_rule."`
Disabled bool `json:"disabled" description:"Whether the rule is paused."`
Labels map[string]string `json:"labels,omitempty" description:"The labels attached to alerts the rule fires."`
Severities []string `json:"severities" required:"true" nullable:"false" description:"The severities the rule's alerts carry: the severity label when set, else each distinct threshold name in lowercase, ordered critical, error, warning, info, then other names. Read-only."`
types.TimeAuditable
types.UserAuditable
}
@@ -37,9 +38,32 @@ type storedRuleData struct {
CompositeQuery struct {
QueryType QueryType `json:"queryType"`
} `json:"compositeQuery"`
Thresholds *RuleThresholdData `json:"thresholds"`
} `json:"condition"`
}
func (data storedRuleData) extractSeverities() []string {
if severity, ok := data.Labels[LabelSeverityName]; ok {
return []string{severity}
}
severities := make([]string, 0)
if data.Condition.Thresholds != nil {
if basic, ok := data.Condition.Thresholds.Spec.(BasicRuleThresholds); ok {
for _, threshold := range basic {
severity := strings.ToLower(threshold.Name)
if !slices.Contains(severities, severity) {
severities = append(severities, severity)
}
}
}
}
slices.SortFunc(severities, func(a, b string) int {
return cmp.Or(cmp.Compare(severityDisplayRank[b], severityDisplayRank[a]), strings.Compare(a, b))
})
return severities
}
// ToListableRule leaves State zero; the caller overlays evaluation state.
func (rule *StorableRule) ToListableRule() (*ListableRule, error) {
data := storedRuleData{}
@@ -64,6 +88,7 @@ func (rule *StorableRule) ToListableRule() (*ListableRule, error) {
RuleType: data.RuleType,
Disabled: data.Disabled,
Labels: data.Labels,
Severities: data.extractSeverities(),
TimeAuditable: rule.TimeAuditable,
UserAuditable: rule.UserAuditable,
}, nil
@@ -156,8 +181,14 @@ func compareListableRules(a, b *ListableRule, sortBy ListSort) int {
case ListSortState:
return cmp.Compare(a.State.DisplayRank(), b.State.DisplayRank())
case ListSortSeverity:
severityA := a.Labels["severity"]
severityB := b.Labels["severity"]
// a rule sorts by its highest severity, the first one listed
severityA, severityB := "", ""
if len(a.Severities) > 0 {
severityA = a.Severities[0]
}
if len(b.Severities) > 0 {
severityB = b.Severities[0]
}
rankA := severityDisplayRank[strings.ToLower(severityA)]
rankB := severityDisplayRank[strings.ToLower(severityB)]
if rankA != rankB {

View File

@@ -19,7 +19,7 @@ func listableRule(name string, state AlertState, severity string, updatedAt time
},
}
if severity != "" {
rule.Labels = map[string]string{"severity": severity}
rule.Severities = []string{severity}
}
return rule
}
@@ -53,6 +53,7 @@ func TestToListableRule(t *testing.T) {
assert.Equal(t, RuleTypeThreshold, listable.RuleType)
assert.True(t, listable.Disabled)
assert.Equal(t, map[string]string{"severity": "critical"}, listable.Labels)
assert.Equal(t, []string{"critical"}, listable.Severities)
assert.Equal(t, created, listable.CreatedAt)
assert.Equal(t, "creator@signoz.io", listable.CreatedBy)
assert.Equal(t, updated, listable.UpdatedAt)

View File

@@ -101,7 +101,7 @@ slack_default_config = {
{
"api_url": "services/TEAM_ID/BOT_ID/TOKEN_ID", # base_url configured on runtime
"title": '[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }}\n {{- if gt (len .CommonLabels) (len .GroupLabels) -}}\n {{" "}}(\n {{- with .CommonLabels.Remove .GroupLabels.Names }}\n {{- range $index, $label := .SortedPairs -}}\n {{ if $index }}, {{ end }}\n {{- $label.Name }}="{{ $label.Value -}}"\n {{- end }}\n {{- end -}}\n )\n {{- end }}',
"text": '{{ range .Alerts -}}\r\n *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}\r\n\r\n *Summary:* {{ .Annotations.summary }}\r\n *Description:* {{ .Annotations.description }}\r\n {{ template "signoz.relatedLinks.slack" . }}\r\n\r\n *Details:*\r\n {{ range .Labels.SortedPairs -}}\r\n {{- if ne .Name "ruleId" -}}\r\n \u2022 *{{ .Name }}:* {{ .Value }}\r\n {{ end -}}\r\n {{ end -}}\r\n{{ end }}',
"text": '{{ range .Alerts -}}\r\n *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}\r\n\r\n *Summary:* {{ .Annotations.summary }}\r\n *Description:* {{ .Annotations.description }}\r\n *RelatedLogs:* {{ if gt (len .Annotations.related_logs) 0 -}} View in <{{ .Annotations.related_logs }}|logs explorer> {{- end}}\r\n *RelatedTraces:* {{ if gt (len .Annotations.related_traces) 0 -}} View in <{{ .Annotations.related_traces }}|traces explorer> {{- end}}\r\n\r\n *Details:*\r\n {{ range .Labels.SortedPairs -}}\r\n {{- if ne .Name "ruleId" -}}\r\n \u2022 *{{ .Name }}:* {{ .Value }}\r\n {{ end -}}\r\n {{ end -}}\r\n{{ end }}',
}
],
}
@@ -111,7 +111,7 @@ msteams_default_config = {
{
"webhook_url": "msteams/webhook_url", # base_url configured on runtime
"title": '[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }}\n {{- if gt (len .CommonLabels) (len .GroupLabels) -}}\n {{" "}}(\n {{- with .CommonLabels.Remove .GroupLabels.Names }}\n {{- range $index, $label := .SortedPairs -}}\n {{ if $index }}, {{ end }}\n {{- $label.Name }}="{{ $label.Value -}}"\n {{- end }}\n {{- end -}}\n )\n {{- end }}',
"text": '{{ range .Alerts -}}\r\n *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}\r\n\r\n *Summary:* {{ .Annotations.summary }}\r\n *Description:* {{ .Annotations.description }}\r\n {{ template "signoz.relatedLinks.slack" . }}\r\n\r\n *Details:*\r\n {{ range .Labels.SortedPairs -}}\r\n {{- if ne .Name "ruleId" -}}\r\n \u2022 *{{ .Name }}:* {{ .Value }}\r\n {{ end -}}\r\n {{ end -}}\r\n{{ end }}',
"text": '{{ range .Alerts -}}\r\n *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}\r\n\r\n *Summary:* {{ .Annotations.summary }}\r\n *Description:* {{ .Annotations.description }}\r\n *RelatedLogs:* {{ if gt (len .Annotations.related_logs) 0 -}} View in <{{ .Annotations.related_logs }}|logs explorer> {{- end}}\r\n *RelatedTraces:* {{ if gt (len .Annotations.related_traces) 0 -}} View in <{{ .Annotations.related_traces }}|traces explorer> {{- end}}\r\n\r\n *Details:*\r\n {{ range .Labels.SortedPairs -}}\r\n {{- if ne .Name "ruleId" -}}\r\n \u2022 *{{ .Name }}:* {{ .Value }}\r\n {{ end -}}\r\n {{ end -}}\r\n{{ end }}',
}
],
}
@@ -161,7 +161,7 @@ email_default_config = {
"email_configs": [
{
"to": "test@example.com",
"html": '<html><body>{{ range .Alerts -}}\r\n *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}\r\n\r\n *Summary:* {{ .Annotations.summary }}\r\n *Description:* {{ .Annotations.description }}\r\n {{ template "signoz.relatedLinks.slack" . }}\r\n\r\n *Details:*\r\n {{ range .Labels.SortedPairs -}}\r\n {{- if ne .Name "ruleId" -}}\r\n \u2022 *{{ .Name }}:* {{ .Value }}\r\n {{ end -}}\r\n {{ end -}}\r\n{{ end }}</body></html>',
"html": '<html><body>{{ range .Alerts -}}\r\n *Alert:* {{ .Labels.alertname }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}\r\n\r\n *Summary:* {{ .Annotations.summary }}\r\n *Description:* {{ .Annotations.description }}\r\n *RelatedLogs:* {{ if gt (len .Annotations.related_logs) 0 -}} View in <{{ .Annotations.related_logs }}|logs explorer> {{- end}}\r\n *RelatedTraces:* {{ if gt (len .Annotations.related_traces) 0 -}} View in <{{ .Annotations.related_traces }}|traces explorer> {{- end}}\r\n\r\n *Details:*\r\n {{ range .Labels.SortedPairs -}}\r\n {{- if ne .Name "ruleId" -}}\r\n \u2022 *{{ .Name }}:* {{ .Value }}\r\n {{ end -}}\r\n {{ end -}}\r\n{{ end }}</body></html>',
"headers": {
"Subject": '[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }}\n {{- if gt (len .CommonLabels) (len .GroupLabels) -}}\n {{" "}}(\n {{- with .CommonLabels.Remove .GroupLabels.Names }}\n {{- range $index, $label := .SortedPairs -}}\n {{ if $index }}, {{ end }}\n {{- $label.Name }}="{{ $label.Value -}}"\n {{- end }}\n {{- end -}}\n )\n {{- end }}'
},

View File

@@ -429,7 +429,11 @@ def test_create_channel( # pylint: disable=too-many-arguments,too-many-position
"\n"
"{{ end }}{{ if .GeneratorURL }}[View in SigNoz]({{ .GeneratorURL }})\n"
"\n"
'{{ end }}{{ template "signoz.relatedLinks.markdown" . }}{{ end }}'
"{{ end }}{{ if .Annotations.related_logs }}[View related logs]({{ .Annotations.related_logs }})\n"
"\n"
"{{ end }}{{ if .Annotations.related_traces }}[View related traces]({{ .Annotations.related_traces }})\n"
"\n"
"{{ end }}{{ end }}"
),
"tags": "signoz",
},
@@ -456,7 +460,11 @@ def test_create_channel( # pylint: disable=too-many-arguments,too-many-position
"\n"
"{{ end }}{{ if .GeneratorURL }}[View in SigNoz]({{ .GeneratorURL }})\n"
"\n"
'{{ end }}{{ template "signoz.relatedLinks.markdown" . }}{{ end }}'
"{{ end }}{{ if .Annotations.related_logs }}[View related logs]({{ .Annotations.related_logs }})\n"
"\n"
"{{ end }}{{ if .Annotations.related_traces }}[View related traces]({{ .Annotations.related_traces }})\n"
"\n"
"{{ end }}{{ end }}"
),
},
[],

View File

@@ -66,7 +66,7 @@ INCIDENTIO_CASES = [
"count": 1,
"json_body": incidentio_event_subset(
"threshold_below_at_least_once",
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View Related Logs", r"/logs/logs-explorer\?")],
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View related logs", r"/logs/logs-explorer\?")],
),
},
),

View File

@@ -73,7 +73,7 @@ JSMOPS_CASES = [
"count": 1,
"json_body": jsmops_alert_subset(
"threshold_below_at_least_once",
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View Related Logs", r"/logs/logs-explorer\?")],
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View related logs", r"/logs/logs-explorer\?")],
),
},
),

View File

@@ -73,7 +73,7 @@ TELEGRAM_CASES = [
"count": 1,
"json_body": telegram_message_subset(
"threshold_below_at_least_once",
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View Related Logs", r"/logs/logs-explorer\?")],
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View related logs", r"/logs/logs-explorer\?")],
),
},
),
@@ -96,7 +96,7 @@ TELEGRAM_CASES = [
"count": 1,
"json_body": telegram_message_subset(
"threshold_above_average",
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View Related Traces", r"traces-explorer\?")],
[("View in SigNoz", r"/alerts/overview\?ruleId="), ("View related traces", r"traces-explorer\?")],
),
},
),

View File

@@ -1,6 +1,7 @@
from collections.abc import Callable
from http import HTTPStatus
import pytest
import requests
from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
@@ -154,6 +155,86 @@ COLLIDER_RULE = {
"schemaVersion": "v2alpha1",
}
SEVERITY_RULES = [
# this rule defines a severity label, so every notification it sends carries
# that severity whichever threshold fired; the list ignores the threshold
# names for it too
{
"alert": "api latency",
"alertType": "METRIC_BASED_ALERT",
"ruleType": "threshold_rule",
"condition": {
**METRIC_CONDITION,
"thresholds": {
"kind": "basic",
"spec": [
{"name": "critical", "target": 90, "matchType": "at_least_once", "op": "above", "channels": [SEED_CHANNEL_NAME]},
{"name": "info", "target": 50, "matchType": "at_least_once", "op": "above", "channels": [SEED_CHANNEL_NAME]},
],
},
},
"labels": {"severity": "warning"},
"annotations": {"summary": "s", "description": "d"},
"evaluation": EVALUATION,
"notificationSettings": NOTIFICATION_SETTINGS,
"version": "v5",
"schemaVersion": "v2alpha1",
},
# this rule has no severity label, so a notification carries the lowercased
# name of the threshold that fired as its severity; the list reports each
# distinct one, critical, error, warning and info first, then the rest
{
"alert": "queue depth",
"alertType": "METRIC_BASED_ALERT",
"ruleType": "threshold_rule",
"condition": {
**METRIC_CONDITION,
"thresholds": {
"kind": "basic",
"spec": [
{"name": "Warning", "target": 80, "matchType": "at_least_once", "op": "above", "channels": [SEED_CHANNEL_NAME]},
{"name": "critical", "target": 95, "matchType": "at_least_once", "op": "above", "channels": [SEED_CHANNEL_NAME]},
{"name": "warning", "target": 70, "matchType": "at_least_once", "op": "above", "channels": [SEED_CHANNEL_NAME]},
{"name": "p3", "target": 60, "matchType": "at_least_once", "op": "above", "channels": [SEED_CHANNEL_NAME]},
],
},
},
"labels": {"team": "messaging"},
"annotations": {"summary": "s", "description": "d"},
"evaluation": EVALUATION,
"notificationSettings": NOTIFICATION_SETTINGS,
"version": "v5",
"schemaVersion": "v2alpha1",
},
# this rule has no severity label and a single threshold, so its only
# severity is error, which ranks between the other two rules' highest
# severities and is the one both negative filters leave standing
{
"alert": "cache miss ratio",
"alertType": "METRIC_BASED_ALERT",
"ruleType": "threshold_rule",
"condition": {
**METRIC_CONDITION,
"thresholds": {
"kind": "basic",
"spec": [{"name": "Error", "target": 5, "matchType": "at_least_once", "op": "above", "channels": [SEED_CHANNEL_NAME]}],
},
},
"labels": {"team": "storage"},
"annotations": {"summary": "s", "description": "d"},
"evaluation": EVALUATION,
"notificationSettings": NOTIFICATION_SETTINGS,
"version": "v5",
"schemaVersion": "v2alpha1",
},
]
SEVERITIES_BY_NAME = {
"api latency": ["warning"],
"queue depth": ["critical", "warning", "p3"],
"cache miss ratio": ["error"],
}
RESERVED_KEYWORDS = [
"alert_type",
"created_at",
@@ -208,11 +289,20 @@ def test_envelope_and_slim_rows(
for rule in data["rules"]:
for forbidden_field in ("condition", "annotations", "notificationSettings", "evaluation", "source", "version", "schemaVersion"):
assert forbidden_field not in rule, f"slim row leaked {forbidden_field}"
for required_field in ("id", "state", "alert", "alertType", "ruleType", "createdAt", "updatedAt"):
for required_field in ("id", "state", "alert", "alertType", "ruleType", "severities", "createdAt", "updatedAt"):
assert required_field in rule, f"slim row missing {required_field}"
assert rule["createdBy"] == USER_ADMIN_EMAIL
assert rule["updatedBy"] == USER_ADMIN_EMAIL
# rules without a severity label take their threshold names
assert {name: rule["severities"] for name, rule in by_name.items()} == {
"payment latency high": ["critical"],
"payment gateway errors": ["warning"],
"checkout conversion drop": ["important"],
"infra cpu saturation": ["critical"],
"prom uptime probe": ["critical"],
}
assert by_name["checkout conversion drop"]["state"] == "disabled"
assert by_name["checkout conversion drop"]["disabled"] is True
assert by_name["payment latency high"]["state"] == "inactive"
@@ -318,15 +408,8 @@ def test_label_missing_semantics(
seed_alert_rules(SEED_CHANNEL_NAME, SEED_RULES)
# A missing label uniformly evaluates as the empty string for value
# operators; presence is expressed with EXISTS / NOT EXISTS.
# operators, so a negative term matches the rules without the label.
cases = [
("severity = ''", {"infra cpu saturation", "prom uptime probe"}),
("severity != ''", {"payment latency high", "payment gateway errors", "checkout conversion drop"}),
("severity != 'critical'", {"payment gateway errors", "checkout conversion drop", "infra cpu saturation", "prom uptime probe"}),
("severity EXISTS", {"payment latency high", "payment gateway errors", "checkout conversion drop"}),
("severity NOT EXISTS", {"infra cpu saturation", "prom uptime probe"}),
("severity = 'critical'", {"payment latency high"}),
("severity IN ['critical', 'warning']", {"payment latency high", "payment gateway errors"}),
("labels.team != 'payments'", {"checkout conversion drop", "infra cpu saturation", "prom uptime probe"}),
("labels.team NOT IN ['payments']", {"checkout conversion drop", "infra cpu saturation", "prom uptime probe"}),
("labels.team NOT CONTAINS 'pay'", {"checkout conversion drop", "infra cpu saturation", "prom uptime probe"}),
@@ -435,8 +518,9 @@ def test_sorting(
"prom uptime probe",
]
# severity: known ranks first (critical > warning), then custom values
# lexically, then rules without severity tie and break on name asc
# severity: known ranks first (critical > warning), then custom values;
# the two unlabelled rules are critical by threshold name, so the three
# critical rules tie and break on name asc
response = requests.get(
signoz.self.host_configs["8080"].get(BASE_URL),
params={"sort": "severity", "order": "desc"},
@@ -445,11 +529,11 @@ def test_sorting(
)
assert response.status_code == HTTPStatus.OK
assert [rule["alert"] for rule in response.json()["data"]["rules"]] == [
"infra cpu saturation",
"payment latency high",
"prom uptime probe",
"payment gateway errors",
"checkout conversion drop",
"infra cpu saturation",
"prom uptime probe",
]
for order in ("asc", "desc"):
@@ -547,6 +631,7 @@ def test_error_contract(
({"query": "team > 'infra'"}, "rule_list_filter_invalid", 'operator > is not allowed on the label filter "team"'),
({"query": "alert_type = 'bogus'"}, "rule_list_filter_invalid", "METRIC_BASED_ALERT"),
({"query": "name REGEXP 'x.*'"}, "rule_list_filter_invalid", "operator REGEXP is not allowed"),
({"query": "severity EXISTS"}, "rule_list_filter_invalid", 'operator EXISTS is not allowed for key "severity"'),
({"query": "created_at >= 'yesterday'"}, "rule_list_filter_invalid", "invalid RFC3339 timestamp"),
({"query": "name LIKE 'prod\\\\'"}, "rule_list_filter_invalid", "must not end with an unescaped backslash"),
({"states": ["bogus"]}, "rule_list_invalid", 'invalid state "bogus"'),
@@ -567,3 +652,101 @@ def test_error_contract(
error = response.json()["error"]
assert error["code"] == expected_code, f"params {params!r}"
assert expected_message_part in error["message"], f"params {params!r}: {error['message']}"
def test_severities(
signoz: SigNoz,
create_user_admin: Operation, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
seed_alert_rules: Callable[[str, list[dict]], None],
):
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
seed_alert_rules(SEED_CHANNEL_NAME, SEVERITY_RULES)
response = requests.get(
signoz.self.host_configs["8080"].get(BASE_URL),
headers={"Authorization": f"Bearer {token}"},
timeout=5,
)
assert response.status_code == HTTPStatus.OK
assert {rule["alert"]: rule["severities"] for rule in response.json()["data"]["rules"]} == SEVERITIES_BY_NAME
# a positive term matches any of the rule's severities, a negative term none of them
@pytest.mark.parametrize(
("query", "expected_names"),
[
("severity = 'warning'", {"api latency", "queue depth"}),
("severity != 'warning'", {"cache miss ratio"}),
("severity = 'critical'", {"queue depth"}),
("severity IN ['p3', 'error']", {"queue depth", "cache miss ratio"}),
("severity NOT IN ['critical', 'warning']", {"cache miss ratio"}),
("severity CONTAINS 'warn'", {"api latency", "queue depth"}),
("severity ILIKE 'P3'", {"queue depth"}),
# the labels. prefix reads the label alone
("labels.severity EXISTS", {"api latency"}),
("labels.severity = 'warning'", {"api latency"}),
],
ids=[
"equals_matches_any_severity",
"not_equals_matches_no_severity",
"equals_threshold_name",
"in_matches_any_severity",
"not_in_matches_no_severity",
"contains",
"ilike",
"labels_prefix_exists_reads_label_alone",
"labels_prefix_equals_reads_label_alone",
],
)
def test_severity_filter(
signoz: SigNoz,
create_user_admin: Operation, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
seed_alert_rules: Callable[[str, list[dict]], None],
query: str,
expected_names: set[str],
):
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
seed_alert_rules(SEED_CHANNEL_NAME, SEVERITY_RULES)
response = requests.get(
signoz.self.host_configs["8080"].get(BASE_URL),
params={"query": query},
headers={"Authorization": f"Bearer {token}"},
timeout=5,
)
assert response.status_code == HTTPStatus.OK, response.text
data = response.json()["data"]
assert {rule["alert"] for rule in data["rules"]} == expected_names
assert data["total"] == len(expected_names)
# a rule sorts by its highest severity
@pytest.mark.parametrize(
("order", "expected_names"),
[
("desc", ["queue depth", "cache miss ratio", "api latency"]),
("asc", ["api latency", "cache miss ratio", "queue depth"]),
],
ids=["desc", "asc"],
)
def test_severity_sort(
signoz: SigNoz,
create_user_admin: Operation, # pylint: disable=unused-argument
get_token: Callable[[str, str], str],
seed_alert_rules: Callable[[str, list[dict]], None],
order: str,
expected_names: list[str],
):
token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
seed_alert_rules(SEED_CHANNEL_NAME, SEVERITY_RULES)
response = requests.get(
signoz.self.host_configs["8080"].get(BASE_URL),
params={"sort": "severity", "order": order},
headers={"Authorization": f"Bearer {token}"},
timeout=5,
)
assert response.status_code == HTTPStatus.OK, response.text
assert [rule["alert"] for rule in response.json()["data"]["rules"]] == expected_names