Add KEEP_LABELS option for whitelisting labels

This allows to provide a list of labels that are the only labels allowed in the UI, reversing the logic of STRIP_LABELS.
Fixes #170
This commit is contained in:
Łukasz Mierzwa
2017-08-10 22:08:12 -07:00
parent 067bd2e645
commit 549ff9eefd
5 changed files with 64 additions and 4 deletions

View File

@@ -174,7 +174,7 @@ func (am *Alertmanager) pullAlerts(version string) error {
}
alert.Annotations, alert.Links = transform.DetectLinks(alert.Annotations)
alert.Labels = transform.StripLables(config.Config.StripLabels, alert.Labels)
alert.Labels = transform.StripLables(config.Config.KeepLabels, config.Config.StripLabels, alert.Labels)
transform.ColorLabel(colors, "@receiver", alert.Receiver)
for k, v := range alert.Labels {

View File

@@ -36,6 +36,7 @@ type configEnvs struct {
SentryDSN string `envconfig:"SENTRY_DSN" help:"Sentry DSN for Go exceptions"`
SentryPublicDSN string `envconfig:"SENTRY_PUBLIC_DSN" help:"Sentry DSN for javascript exceptions"`
StripLabels spaceSeparatedList `envconfig:"STRIP_LABELS" help:"List of labels to ignore"`
KeepLabels spaceSeparatedList `envconfig:"KEEP_LABELS" help:"List of labels to keep, all other labels will be stripped"`
WebPrefix string `envconfig:"WEB_PREFIX" default:"/" help:"URL prefix"`
}

View File

@@ -9,10 +9,16 @@ import (
// StripLables allows filtering out some labels from alerts
// it takes the list of label keys to ignore and alert label map
// it will return label map without labels found on the ignore list
func StripLables(ignoredLabels []string, sourceLabels map[string]string) map[string]string {
func StripLables(keptLabels, ignoredLabels []string, sourceLabels map[string]string) map[string]string {
// empty keep list means keep everything by default
keepAll := len(keptLabels) == 0
labels := map[string]string{}
for label, value := range sourceLabels {
if !slices.StringInSlice(ignoredLabels, label) {
// is explicitly marked to be kept
inKeep := slices.StringInSlice(keptLabels, label)
// is explicitly marked to be stripped
inStrip := slices.StringInSlice(ignoredLabels, label)
if (keepAll || inKeep) && !inStrip {
// strip leading and trailung space in label value
// this is to normalize values in case space is added by Alertmanager rules
labels[label] = strings.TrimSpace(value)

View File

@@ -9,6 +9,7 @@ import (
type stripTest struct {
strip []string
keep []string
before map[string]string
after map[string]string
}
@@ -16,6 +17,7 @@ type stripTest struct {
var stripTests = []stripTest{
stripTest{
strip: []string{"env"},
keep: []string{},
before: map[string]string{
"host": "localhost",
"env": "production",
@@ -28,6 +30,7 @@ var stripTests = []stripTest{
},
stripTest{
strip: []string{"server"},
keep: []string{},
before: map[string]string{
"host": "localhost",
"env": "production",
@@ -41,6 +44,7 @@ var stripTests = []stripTest{
},
stripTest{
strip: []string{},
keep: []string{},
before: map[string]string{
"host": "localhost",
"env": "production",
@@ -54,16 +58,50 @@ var stripTests = []stripTest{
},
stripTest{
strip: []string{"host"},
keep: []string{},
before: map[string]string{
"host": "localhost",
},
after: map[string]string{},
},
stripTest{
strip: []string{},
keep: []string{"env"},
before: map[string]string{
"host": "localhost",
"env": "production",
"level": "info",
},
after: map[string]string{
"env": "production",
},
},
stripTest{
strip: []string{"env"},
keep: []string{"host"},
before: map[string]string{
"host": "localhost",
"env": "production",
"level": "info",
},
after: map[string]string{
"host": "localhost",
},
},
stripTest{
strip: []string{},
keep: []string{"env"},
before: map[string]string{
"host": "localhost",
"level": "info",
},
after: map[string]string{},
},
}
func TestStripLables(t *testing.T) {
for _, testCase := range stripTests {
labels := transform.StripLables(testCase.strip, testCase.before)
labels := transform.StripLables(testCase.keep, testCase.strip, testCase.before)
if !reflect.DeepEqual(labels, testCase.after) {
t.Errorf("StripLables failed, expected %v, got %v", testCase.after, labels)
}