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

@@ -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)
}