Skip to content

Commit abd5a6e

Browse files
committed
pkg/ruler: Add external_labels option
This option can be used to add a set of labels to all alerts emitted by the ruler. Signed-off-by: Benoît Knecht <[email protected]>
1 parent fc862f9 commit abd5a6e

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master / unreleased
44

5+
* [FEATURE] Ruler: Add `external_labels` option to tag all alerts with a given set of labels.
6+
57
## 1.12.0 in pgoress
68

79
* [CHANGE] Changed default for `-ingester.min-ready-duration` from 1 minute to 15 seconds. #4539

pkg/ruler/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (r *DefaultMultiTenantManager) syncRulesToManager(ctx context.Context, user
148148
go manager.Run()
149149
r.userManagers[user] = manager
150150
}
151-
err = manager.Update(r.cfg.EvaluationInterval, files, nil, r.cfg.ExternalURL.String())
151+
err = manager.Update(r.cfg.EvaluationInterval, files, r.cfg.ExternalLabels, r.cfg.ExternalURL.String())
152152
if err != nil {
153153
r.lastReloadSuccessful.WithLabelValues(user).Set(0)
154154
level.Error(r.logger).Log("msg", "unable to update rule manager", "user", user, "err", err)

pkg/ruler/notifier.go

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ func buildNotifierConfig(rulerConfig *Config) (*config.Config, error) {
128128
}
129129

130130
promConfig := &config.Config{
131+
GlobalConfig: config.GlobalConfig{
132+
ExternalLabels: rulerConfig.ExternalLabels,
133+
},
131134
AlertingConfig: config.AlertingConfig{
132135
AlertmanagerConfigs: amConfigs,
133136
},

pkg/ruler/notifier_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/prometheus/prometheus/config"
1111
"github.com/prometheus/prometheus/discovery"
1212
"github.com/prometheus/prometheus/discovery/dns"
13+
"github.com/prometheus/prometheus/model/labels"
1314
"github.com/stretchr/testify/require"
1415

1516
"github.com/cortexproject/cortex/pkg/util"
@@ -220,6 +221,38 @@ func TestBuildNotifierConfig(t *testing.T) {
220221
},
221222
},
222223
},
224+
{
225+
name: "with external labels",
226+
cfg: &Config{
227+
AlertmanagerURL: "http://alertmanager.default.svc.cluster.local/alertmanager",
228+
ExternalLabels: []labels.Label{
229+
{Name: "region", Value: "us-east-1"},
230+
},
231+
},
232+
ncfg: &config.Config{
233+
AlertingConfig: config.AlertingConfig{
234+
AlertmanagerConfigs: []*config.AlertmanagerConfig{
235+
{
236+
APIVersion: "v1",
237+
Scheme: "http",
238+
PathPrefix: "/alertmanager",
239+
ServiceDiscoveryConfigs: discovery.Configs{
240+
discovery.StaticConfig{
241+
{
242+
Targets: []model.LabelSet{{"__address__": "alertmanager.default.svc.cluster.local"}},
243+
},
244+
},
245+
},
246+
},
247+
},
248+
},
249+
GlobalConfig: config.GlobalConfig{
250+
ExternalLabels: []labels.Label{
251+
{Name: "region", Value: "us-east-1"},
252+
},
253+
},
254+
},
255+
},
223256
}
224257

225258
for _, tt := range tests {

pkg/ruler/ruler.go

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/pkg/errors"
1818
"github.com/prometheus/client_golang/prometheus"
1919
"github.com/prometheus/client_golang/prometheus/promauto"
20+
"github.com/prometheus/prometheus/model/labels"
2021
"github.com/prometheus/prometheus/model/rulefmt"
2122
"github.com/prometheus/prometheus/notifier"
2223
promRules "github.com/prometheus/prometheus/rules"
@@ -71,6 +72,8 @@ const (
7172
type Config struct {
7273
// This is used for template expansion in alerts; must be a valid URL.
7374
ExternalURL flagext.URLValue `yaml:"external_url"`
75+
// Labels to add to all alerts
76+
ExternalLabels labels.Labels `yaml:"external_labels,omitempty" doc:"nocli|description=Labels to add to all alerts."`
7477
// GRPC Client configuration.
7578
ClientTLSConfig grpcclient.Config `yaml:"ruler_client"`
7679
// How frequently to evaluate rules by default.

0 commit comments

Comments
 (0)