Skip to content

Commit

Permalink
feat(ruler): support alert relabeling (#6220)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityacs committed May 26, 2022
1 parent 0b947d9 commit 410c1a0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/sources/configuration/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ alertmanager_client:
# CLI flag: -ruler.alertmanager-use-v2
[enable_alertmanager_v2: <boolean> | default = false]

# List of alert relabel configs
alert_relabel_configs:
[- <relabel_config> ...]

# Capacity of the queue for notifications to be sent to the Alertmanager.
# CLI flag: -ruler.notification-queue-capacity
[notification_queue_capacity: <int> | default = 10000]
Expand Down
1 change: 1 addition & 0 deletions pkg/ruler/base/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func buildNotifierConfig(rulerConfig *Config) (*config.Config, error) {
ExternalLabels: rulerConfig.ExternalLabels,
},
AlertingConfig: config.AlertingConfig{
AlertRelabelConfigs: rulerConfig.AlertRelabelConfigs,
AlertmanagerConfigs: amConfigs,
},
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/ruler/base/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/prometheus/discovery"
"github.com/prometheus/prometheus/discovery/dns"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/stretchr/testify/require"

"github.com/grafana/loki/pkg/util"
Expand Down Expand Up @@ -325,6 +326,54 @@ func TestBuildNotifierConfig(t *testing.T) {
},
},
},
{
name: "with alert relabel config",
cfg: &Config{
AlertmanagerURL: "http://alertmanager.default.svc.cluster.local/alertmanager",
ExternalLabels: []labels.Label{
{Name: "region", Value: "us-east-1"},
},
AlertRelabelConfigs: []*relabel.Config{
{
SourceLabels: model.LabelNames{"severity"},
Regex: relabel.MustNewRegexp("high"),
TargetLabel: "priority",
Replacement: "p1",
},
},
},
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"}},
},
},
},
},
},
AlertRelabelConfigs: []*relabel.Config{
{
SourceLabels: model.LabelNames{"severity"},
Regex: relabel.MustNewRegexp("high"),
TargetLabel: "priority",
Replacement: "p1",
},
},
},
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/base/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/prometheus/prometheus/model/rulefmt"
"github.com/prometheus/prometheus/notifier"
promRules "github.com/prometheus/prometheus/rules"
Expand Down Expand Up @@ -94,6 +95,8 @@ type Config struct {
AlertmanagerRefreshInterval time.Duration `yaml:"alertmanager_refresh_interval"`
// Enables the ruler notifier to use the Alertmananger V2 API.
AlertmanangerEnableV2API bool `yaml:"enable_alertmanager_v2"`
// Configuration for alert relabeling.
AlertRelabelConfigs []*relabel.Config `yaml:"alert_relabel_configs,omitempty"`
// Capacity of the queue for notifications to be sent to the Alertmanager.
NotificationQueueCapacity int `yaml:"notification_queue_capacity"`
// HTTP timeout duration when sending notifications to the Alertmanager.
Expand Down

0 comments on commit 410c1a0

Please sign in to comment.