mirror of
https://github.com/SigNoz/signoz.git
synced 2026-11-16 13:21:15 +00:00
Compare commits
4 Commits
main
...
nv/rules-l
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24e2c703d7 | ||
|
|
e20c197566 | ||
|
|
0215d7b50c | ||
|
|
60fb8397d0 |
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"`,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user