Skip to content

Commit

Permalink
Merge pull request #744 from grafana/arve/fix-rules-logger
Browse files Browse the repository at this point in the history
rules.NewGroup: Fix when no logger is passed
  • Loading branch information
aknuds1 authored Nov 7, 2024
2 parents 7ef8b5b + b0e1b4a commit 279fac9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 11 additions & 7 deletions rules/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,13 @@ type GroupOptions struct {

// NewGroup makes a new Group with the given name, options, and rules.
func NewGroup(o GroupOptions) *Group {
metrics := o.Opts.Metrics
opts := o.Opts
if opts == nil {
opts = &ManagerOptions{}
}
metrics := opts.Metrics
if metrics == nil {
metrics = NewGroupMetrics(o.Opts.Registerer)
metrics = NewGroupMetrics(opts.Registerer)
}

key := GroupKey(o.File, o.Name)
Expand All @@ -124,13 +128,13 @@ func NewGroup(o GroupOptions) *Group {
evalIterationFunc = DefaultEvalIterationFunc
}

concurrencyController := o.Opts.RuleConcurrencyController
concurrencyController := opts.RuleConcurrencyController
if concurrencyController == nil {
concurrencyController = sequentialRuleEvalController{}
}

if o.Opts.Logger == nil {
promslog.NewNopLogger()
if opts.Logger == nil {
opts.Logger = promslog.NewNopLogger()
}

return &Group{
Expand All @@ -141,13 +145,13 @@ func NewGroup(o GroupOptions) *Group {
limit: o.Limit,
rules: o.Rules,
shouldRestore: o.ShouldRestore,
opts: o.Opts,
opts: opts,
sourceTenants: o.SourceTenants,
seriesInPreviousEval: make([]map[string]labels.Labels, len(o.Rules)),
done: make(chan struct{}),
managerDone: o.done,
terminated: make(chan struct{}),
logger: o.Opts.Logger.With("file", o.File, "group", o.Name),
logger: opts.Logger.With("file", o.File, "group", o.Name),
metrics: metrics,
evalIterationFunc: evalIterationFunc,
concurrencyController: concurrencyController,
Expand Down
9 changes: 9 additions & 0 deletions rules/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ import (
"testing"
"time"

"github.com/prometheus/common/promslog"
"github.com/stretchr/testify/require"

"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
)

func TestNewGroup(t *testing.T) {
g := NewGroup(GroupOptions{
File: "test-file",
Name: "test-name",
})
require.Equal(t, promslog.NewNopLogger().With("file", "test-file", "group", "test-name"), g.logger)
}

func TestGroup_Equals(t *testing.T) {
testExpression, err := parser.ParseExpr("up")
require.NoError(t, err)
Expand Down

0 comments on commit 279fac9

Please sign in to comment.