pkg/ruler/base: Add external_labels option (#5450)

This option can be used to add a set of labels to all alerts emitted by the
ruler.

Signed-off-by: Benoît Knecht <bknecht@protonmail.ch>
pull/5457/head
Benoît Knecht 3 years ago committed by GitHub
parent 6d0ca24251
commit 776e229e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 4
      docs/sources/configuration/_index.md
  3. 2
      pkg/ruler/base/manager.go
  4. 3
      pkg/ruler/base/notifier.go
  5. 33
      pkg/ruler/base/notifier_test.go
  6. 3
      pkg/ruler/base/ruler.go

@ -59,6 +59,7 @@
* [5253](https://github.com/grafana/loki/pull/5253) **Juneezee**: refactor: use `T.TempDir` to create temporary test directory
* [5315](https://github.com/grafana/loki/pull/5315) **bboreham**: filters: use faster regexp package
* [5393](https://github.com/grafana/loki/pull/5393) **sandeepsukhani**: jsonnet: move boltdb-shipper configs set as compactor args to yaml config
* [5450](https://github.com/grafana/loki/pull/5450) **BenoitKnecht**: pkg/ruler/base: Add external_labels option
# 2.4.1 (2021/11/07)

@ -427,6 +427,10 @@ The `ruler` block configures the Loki ruler.
# CLI flag: -ruler.external.url
[external_url: <url> | default = ]
# Labels to add to all alerts
external_labels:
[<labelname>: <labelvalue> ...]
ruler_client:
# Path to the client certificate file, which will be used for authenticating
# with the server. Also requires the key path to be configured.

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

@ -128,6 +128,9 @@ func buildNotifierConfig(rulerConfig *Config) (*config.Config, error) {
}
promConfig := &config.Config{
GlobalConfig: config.GlobalConfig{
ExternalLabels: rulerConfig.ExternalLabels,
},
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: amConfigs,
},

@ -10,6 +10,7 @@ import (
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/dns"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/pkg/util"
@ -220,6 +221,38 @@ func TestBuildNotifierConfig(t *testing.T) {
},
},
},
{
name: "with external labels",
cfg: &Config{
AlertmanagerURL: "http://alertmanager.default.svc.cluster.local/alertmanager",
ExternalLabels: []labels.Label{
{Name: "region", Value: "us-east-1"},
},
},
ncfg: &config.Config{
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{
{
APIVersion: "v1",
Scheme: "http",
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
discovery.StaticConfig{
{
Targets: []model.LabelSet{{"__address__": "alertmanager.default.svc.cluster.local"}},
},
},
},
},
},
},
GlobalConfig: config.GlobalConfig{
ExternalLabels: []labels.Label{
{Name: "region", Value: "us-east-1"},
},
},
},
},
}
for _, tt := range tests {

@ -23,6 +23,7 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/rulefmt"
"github.com/prometheus/prometheus/notifier"
promRules "github.com/prometheus/prometheus/rules"
@ -71,6 +72,8 @@ const (
type Config struct {
// This is used for template expansion in alerts; must be a valid URL.
ExternalURL flagext.URLValue `yaml:"external_url"`
// Labels to add to all alerts
ExternalLabels labels.Labels `yaml:"external_labels,omitempty"`
// GRPC Client configuration.
ClientTLSConfig grpcclient.Config `yaml:"ruler_client"`
// How frequently to evaluate rules by default.

Loading…
Cancel
Save