Skip to content

pkg/ruler: Add external_labels option #4499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master / unreleased

* [FEATURE] Ruler: Add `external_labels` option to tag all alerts with a given set of labels.

## 1.12.0 in pgoress

* [CHANGE] Changed default for `-ingester.min-ready-duration` from 1 minute to 15 seconds. #4539
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,9 @@ The `ruler_config` configures the Cortex ruler.
# CLI flag: -ruler.external.url
[external_url: <url> | default = ]

# Labels to add to all alerts.
[external_labels: <map of string to string> | default = ]

ruler_client:
# gRPC client max receive message size (bytes).
# CLI flag: -ruler.client.grpc-max-recv-msg-size
Expand Down
2 changes: 1 addition & 1 deletion pkg/ruler/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/ruler/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
33 changes: 33 additions & 0 deletions pkg/ruler/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/cortexproject/cortex/pkg/util"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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"
Expand Down Expand Up @@ -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" doc:"nocli|description=Labels to add to all alerts."`
// GRPC Client configuration.
ClientTLSConfig grpcclient.Config `yaml:"ruler_client"`
// How frequently to evaluate rules by default.
Expand Down
2 changes: 2 additions & 0 deletions tools/doc-generator/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ func getFieldType(t reflect.Type) (string, error) {
return "string", nil
case "[]*relabel.Config":
return "relabel_config...", nil
case "labels.Labels":
return "map of string to string", nil
}

// Fallback to auto-detection of built-in data types
Expand Down