app/vmalert: add debug to rule.Group (#8658)

### Describe Your Changes

app/vmalert feat: add Debug to rule.Group

This is to enable/disable debug logs for the rules at group level.
Default is false.

Rule specific `debug` configuration takes priority over group level
configuration.

Relates to [app/vmalert: chore: log when response is
partial](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/8522)

### Checklist

The following checks are **mandatory**:

- [ ] My change adheres [VictoriaMetrics contributing
guidelines](https://docs.victoriametrics.com/contributing/).

---------

Signed-off-by: emreya <e.yazici1990@gmail.com>
Signed-off-by: Emre Yazici <e.yazici1990@gmail.com>
Signed-off-by: emreya <emre.yazici@adyen.com>
Co-authored-by: Hui Wang <haley@victoriametrics.com>
Co-authored-by: Roman Khavronenko <roman@victoriametrics.com>
This commit is contained in:
Emre Yazıcı
2025-05-01 20:26:15 +02:00
committed by GitHub
parent c586eeb569
commit 90d2a6efed
7 changed files with 59 additions and 9 deletions

View File

@@ -50,6 +50,8 @@ type Group struct {
NotifierHeaders []Header `yaml:"notifier_headers,omitempty"`
// EvalAlignment will make the timestamp of group query requests be aligned with interval
EvalAlignment *bool `yaml:"eval_alignment,omitempty"`
// Debug enables debug logs for the group
Debug bool `yaml:"debug,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]any `yaml:",inline"`
}
@@ -143,7 +145,7 @@ type Rule struct {
KeepFiringFor *promutil.Duration `yaml:"keep_firing_for,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
Annotations map[string]string `yaml:"annotations,omitempty"`
Debug bool `yaml:"debug,omitempty"`
Debug *bool `yaml:"debug,omitempty"`
// UpdateEntriesLimit defines max number of rule's state updates stored in memory.
// Overrides `-rule.updateEntriesLimit`.
UpdateEntriesLimit *int `yaml:"update_entries_limit,omitempty"`

View File

@@ -124,6 +124,10 @@ func (arm *alertingRuleMetrics) close() {
// NewAlertingRule creates a new AlertingRule
func NewAlertingRule(qb datasource.QuerierBuilder, group *Group, cfg config.Rule) *AlertingRule {
debug := group.Debug
if cfg.Debug != nil {
debug = *cfg.Debug
}
ar := &AlertingRule{
Type: group.Type,
RuleID: cfg.ID,
@@ -137,14 +141,14 @@ func NewAlertingRule(qb datasource.QuerierBuilder, group *Group, cfg config.Rule
GroupName: group.Name,
File: group.File,
EvalInterval: group.Interval,
Debug: cfg.Debug,
Debug: debug,
q: qb.BuildWithParams(datasource.QuerierParams{
DataSourceType: group.Type.String(),
ApplyIntervalAsTimeFilter: setIntervalAsTimeFilter(group.Type.String(), cfg.Expr),
EvaluationInterval: group.Interval,
QueryParams: group.Params,
Headers: group.Headers,
Debug: cfg.Debug,
Debug: debug,
}),
alerts: make(map[uint64]*notifier.Alert),
}

View File

@@ -57,6 +57,7 @@ type Group struct {
// checksum stores the hash of yaml definition for this group.
checksum string
LastEvaluation time.Time
Debug bool
Labels map[string]string
Params url.Values
@@ -118,6 +119,7 @@ func NewGroup(cfg config.Group, qb datasource.QuerierBuilder, defaultInterval ti
Headers: make(map[string]string),
NotifierHeaders: make(map[string]string),
Labels: cfg.Labels,
Debug: cfg.Debug,
evalAlignment: cfg.EvalAlignment,
doneCh: make(chan struct{}),
@@ -271,6 +273,7 @@ func (g *Group) updateWith(newGroup *Group) error {
g.Limit = newGroup.Limit
g.checksum = newGroup.checksum
g.Rules = newRules
g.Debug = newGroup.Debug
return nil
}

View File

@@ -75,6 +75,9 @@ func TestUpdateWith(t *testing.T) {
t.Fatalf("comparison1 error: %s", err)
}
}
if g.Debug != expect.Debug {
t.Fatalf("expected to have debug %v; got %v", expect.Debug, g.Debug)
}
}
// new rule
@@ -131,6 +134,7 @@ func TestUpdateWith(t *testing.T) {
}})
// update recording rule
debug := true
f(config.Group{
Rules: []config.Rule{{
Record: "foo",
@@ -142,7 +146,7 @@ func TestUpdateWith(t *testing.T) {
Rules: []config.Rule{{
Record: "foo",
Expr: "min(up)",
Debug: true,
Debug: &debug,
Labels: map[string]string{
"baz": "bar",
},
@@ -158,7 +162,7 @@ func TestUpdateWith(t *testing.T) {
{
Alert: "foo",
Expr: "up > 0",
Debug: true,
Debug: &debug,
For: promutil.NewDuration(time.Second),
},
}}, config.Group{
@@ -166,7 +170,7 @@ func TestUpdateWith(t *testing.T) {
{
Record: "foo",
Expr: "max(up)",
Debug: true,
Debug: &debug,
},
{
Alert: "foo",
@@ -208,6 +212,31 @@ func TestUpdateWith(t *testing.T) {
{Alert: "foo4"},
{Record: "foo5"},
}})
f(config.Group{Debug: false}, config.Group{Debug: true})
f(config.Group{
Debug: false,
Rules: []config.Rule{
{Alert: "foo1"},
},
}, config.Group{
Debug: true,
Rules: []config.Rule{
{Alert: "foo1"},
},
})
f(config.Group{
Debug: false,
Rules: []config.Rule{
{Alert: "foo1"},
},
}, config.Group{
Debug: false,
Rules: []config.Rule{
{Alert: "foo1", Debug: &debug},
},
})
}
func TestUpdateDuringRandSleep(t *testing.T) {
@@ -309,6 +338,7 @@ func TestGroupStart(t *testing.T) {
summary: "{{ $value }}"
`
)
var groups []config.Group
err := yaml.Unmarshal([]byte(rules), &groups)
if err != nil {
@@ -509,6 +539,7 @@ func TestCloseWithEvalInterruption(t *testing.T) {
summary: "{{ $value }}"
`
)
var groups []config.Group
err := yaml.Unmarshal([]byte(rules), &groups)
if err != nil {

View File

@@ -83,6 +83,10 @@ func (rr *RecordingRule) ID() uint64 {
// NewRecordingRule creates a new RecordingRule
func NewRecordingRule(qb datasource.QuerierBuilder, group *Group, cfg config.Rule) *RecordingRule {
debug := group.Debug
if cfg.Debug != nil {
debug = *cfg.Debug
}
rr := &RecordingRule{
Type: group.Type,
RuleID: cfg.ID,
@@ -92,14 +96,14 @@ func NewRecordingRule(qb datasource.QuerierBuilder, group *Group, cfg config.Rul
GroupID: group.GetID(),
GroupName: group.Name,
File: group.File,
Debug: cfg.Debug,
Debug: debug,
q: qb.BuildWithParams(datasource.QuerierParams{
DataSourceType: group.Type.String(),
ApplyIntervalAsTimeFilter: setIntervalAsTimeFilter(group.Type.String(), cfg.Expr),
EvaluationInterval: group.Interval,
QueryParams: group.Params,
Headers: group.Headers,
Debug: cfg.Debug,
Debug: debug,
}),
}

View File

@@ -18,6 +18,8 @@ See also [LTS releases](https://docs.victoriametrics.com/victoriametrics/lts-rel
## tip
* FEATURE: [vmalert](https://docs.victoriametrics.com/victoriametrics/vmalert/): add `debug` option for [group](https://docs.victoriametrics.com/vmalert/#groups) for enabling [debug mode](https://docs.victoriametrics.com/victoriametrics/vmalert/#debug-mode) for all rules within the group.
* BUGFIX: [vmui](https://docs.victoriametrics.com/victoriametrics/single-server-victoriametrics/#vmui): use `retentionFilter` flag name in debugging interface to make it consistent with flag definition. Previously, flag name in debugging interface was different from command-line configuration so copying command-line flags for debugging produced an error. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/8697).
* BUGFIX: [vmalert](https://docs.victoriametrics.com/victoriametrics/vmalert/): fix various UI glitches and tidy up the visual styles. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/8773) for details.

View File

@@ -208,7 +208,7 @@ headers:
# notifier_headers:
# - "TenantID: foo"
notifier_headers:
[ <string>, ...]
[ <string>, ...]
# Optional list of labels added to every rule within a group.
# It has priority over the external labels.
@@ -219,6 +219,10 @@ labels:
rules:
[ - <rule> ... ]
# Enable debug mode for all rules in the group.
# This can be overridden by the `debug` field in rule.
[ debug: <bool> | default = false ]
```
### Rules