Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaochao Dong (@damnever) <[email protected]>
  • Loading branch information
damnever committed Nov 29, 2024
1 parent b35b10c commit dfe7331
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 37 deletions.
6 changes: 3 additions & 3 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3519,6 +3519,9 @@ query_rejection:
# CLI flag: -ruler.query-offset
[ruler_query_offset: <duration> | default = 0s]

# external labels for alerting rules
[ruler_external_labels: <map of string (labelName) to string (labelValue)> | default = []]

# The default tenant's shard size when the shuffle-sharding strategy is used.
# Must be set when the store-gateway sharding is enabled with the
# shuffle-sharding strategy. When this setting is specified in the per-tenant
Expand Down Expand Up @@ -3620,9 +3623,6 @@ query_rejection:

# list of rule groups to disable
[disabled_rule_groups: <list of DisabledRuleGroup> | default = []]

# external labels for alerting rules
[external_labels: <map of string (labelName) to string (labelValue)> | default = []]
```
### `memberlist_config`
Expand Down
4 changes: 2 additions & 2 deletions pkg/ruler/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func TestRuler_LimitsPerGroup(t *testing.T) {
r := newTestRuler(t, cfg, store, nil)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

r.limits = ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1}
r.limits = &ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1}

a := NewAPI(r, r.store, log.NewNopLogger())

Expand Down Expand Up @@ -508,7 +508,7 @@ func TestRuler_RulerGroupLimits(t *testing.T) {
r := newTestRuler(t, cfg, store, nil)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

r.limits = ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1}
r.limits = &ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1}

a := NewAPI(r, r.store, log.NewNopLogger())

Expand Down
2 changes: 1 addition & 1 deletion pkg/ruler/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ type RulesLimits interface {
RulerMaxRulesPerRuleGroup(userID string) int
RulerQueryOffset(userID string) time.Duration
DisabledRuleGroups(userID string) validation.DisabledRuleGroups
ExternalLabels(userID string) labels.Labels
RulerExternalLabels(userID string) labels.Labels
}

// EngineQueryFunc returns a new engine query function validating max queryLength.
Expand Down
2 changes: 1 addition & 1 deletion pkg/ruler/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestPusherErrors(t *testing.T) {
writes := prometheus.NewCounter(prometheus.CounterOpts{})
failures := prometheus.NewCounter(prometheus.CounterOpts{})

pa := NewPusherAppendable(pusher, "user-1", ruleLimits{}, writes, failures)
pa := NewPusherAppendable(pusher, "user-1", &ruleLimits{}, writes, failures)

lbls, err := parser.ParseMetric("foo_bar")
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ruler/external_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (e *userExternalLabels) get(userID string) (labels.Labels, bool) {
}

func (e *userExternalLabels) update(userID string) (labels.Labels, bool) {
lset := e.limits.ExternalLabels(userID)
lset := e.limits.RulerExternalLabels(userID)

e.mtx.Lock()
defer e.mtx.Unlock()
Expand Down
69 changes: 69 additions & 0 deletions pkg/ruler/external_labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ruler

import (
"testing"

"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
)

func TestUserExternalLabels(t *testing.T) {
limits := ruleLimits{}
e := newUserExternalLabels(labels.FromStrings("from", "cortex"), &limits)

tests := []struct {
name string
removeBeforeTest bool
exists bool
userExternalLabels labels.Labels
expectedExternalLabels labels.Labels
}{
{
name: "global labels only",
removeBeforeTest: false,
exists: false,
userExternalLabels: nil,
expectedExternalLabels: labels.FromStrings("from", "cortex"),
},
{
name: "local labels without overriding",
removeBeforeTest: true,
exists: false,
userExternalLabels: labels.FromStrings("tag", "local"),
expectedExternalLabels: labels.FromStrings("from", "cortex", "tag", "local"),
},
{
name: "local labels that override globals",
removeBeforeTest: false,
exists: true,
userExternalLabels: labels.FromStrings("from", "cloud", "tag", "local"),
expectedExternalLabels: labels.FromStrings("from", "cloud", "tag", "local"),
},
}

const userID = "test-user"
for _, data := range tests {
data := data
t.Run(data.name, func(t *testing.T) {
if data.removeBeforeTest {
e.remove(userID)
}
_, exists := e.get(userID)
require.Equal(t, data.exists, exists)

limits.externalLabels = data.userExternalLabels
lset, ok := e.update(userID)
require.True(t, ok)
require.Equal(t, data.expectedExternalLabels, lset)
lset1, ok := e.update(userID)
require.False(t, ok) // Not updated.
require.Equal(t, data.expectedExternalLabels, lset1)
})
}

_, ok := e.get(userID)
require.True(t, ok)
e.cleanup()
_, ok = e.get(userID)
require.False(t, ok)
}
10 changes: 5 additions & 5 deletions pkg/ruler/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSyncRuleGroups(t *testing.T) {
}

ruleManagerFactory := RuleManagerFactory(nil, waitDurations)
limits := ruleLimits{externalLabels: labels.FromStrings("from", "cortex")}
limits := &ruleLimits{externalLabels: labels.FromStrings("from", "cortex")}

m, err := NewDefaultMultiTenantManager(Config{RulePath: dir}, limits, ruleManagerFactory, nil, nil, log.NewNopLogger())
require.NoError(t, err)
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestSyncRuleGroups(t *testing.T) {
require.True(t, ok)
lset, ok := m.userExternalLabels.get(user)
require.True(t, ok)
require.Equal(t, limits.externalLabels, lset)
require.Equal(t, limits.RulerExternalLabels(user), lset)
}

// Passing empty map / nil stops all managers.
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestSlowRuleGroupSyncDoesNotSlowdownListRules(t *testing.T) {
}

ruleManagerFactory := RuleManagerFactory(groupsToReturn, waitDurations)
m, err := NewDefaultMultiTenantManager(Config{RulePath: dir}, ruleLimits{}, ruleManagerFactory, nil, prometheus.NewRegistry(), log.NewNopLogger())
m, err := NewDefaultMultiTenantManager(Config{RulePath: dir}, &ruleLimits{}, ruleManagerFactory, nil, prometheus.NewRegistry(), log.NewNopLogger())
require.NoError(t, err)

m.SyncRuleGroups(context.Background(), userRules)
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestSyncRuleGroupsCleanUpPerUserMetrics(t *testing.T) {

ruleManagerFactory := RuleManagerFactory(nil, waitDurations)

m, err := NewDefaultMultiTenantManager(Config{RulePath: dir}, ruleLimits{}, ruleManagerFactory, evalMetrics, reg, log.NewNopLogger())
m, err := NewDefaultMultiTenantManager(Config{RulePath: dir}, &ruleLimits{}, ruleManagerFactory, evalMetrics, reg, log.NewNopLogger())
require.NoError(t, err)

const user = "testUser"
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestBackupRules(t *testing.T) {
ruleManagerFactory := RuleManagerFactory(nil, waitDurations)
config := Config{RulePath: dir}
config.Ring.ReplicationFactor = 3
m, err := NewDefaultMultiTenantManager(config, ruleLimits{}, ruleManagerFactory, evalMetrics, reg, log.NewNopLogger())
m, err := NewDefaultMultiTenantManager(config, &ruleLimits{}, ruleManagerFactory, evalMetrics, reg, log.NewNopLogger())
require.NoError(t, err)

const user1 = "testUser"
Expand Down
2 changes: 1 addition & 1 deletion pkg/ruler/ruler_ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestGetReplicationSetForListRule(t *testing.T) {
}

r, _ := buildRuler(t, cfg, nil, store, nil)
r.limits = ruleLimits{}
r.limits = &ruleLimits{}

rulerRing := r.ring
// We start ruler's ring, but nothing else (not even lifecycler).
Expand Down
Loading

0 comments on commit dfe7331

Please sign in to comment.