From 4ef9052a98d9cd886239e7e65b82a15ec6efa1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Wed, 20 Mar 2019 21:15:04 +0000 Subject: [PATCH] feat(api): allow using wildcards with custom colors Fixes #537 --- docs/CONFIGURATION.md | 19 ++++++++++- internal/transform/color_test.go | 9 +++++ internal/transform/colors.go | 58 +++++++++++++++++++------------- 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 6b39c1304..8f18b4f6d 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -345,7 +345,10 @@ labels: to configure a set of labels with custom predefined colors applied to them rather than generated. Colors can be defined as RGB or HEX values. Value is a mapping with `label name` -> `label value` -> `color`, see examples - below. + below. Wildcard values (`*`) are allowed to provide a fallback custom color, + which can also be used instead of `color:static` option. Wildcard options + are evaluated after exact matches, so it's possible to mix explicit and + wildcard values. Note: this option is not available via environment variables, you can only set it via the config file. - `keep` - list of allowed labels, if empty all labels are allowed. @@ -398,6 +401,20 @@ labels: critical: "#ff220c" ``` +Example with a wildcard value, `info`, `warning` and `critical` will get colors +as below, but any value not matching those 3 values will use the color from `*`: + +```YAML +labels: + color: + custom: + severity: + "*": "#736598" + info: "#87c4e0" + warning: "#ffae42" + critical: "#ff220c" +``` + Defaults: ```YAML diff --git a/internal/transform/color_test.go b/internal/transform/color_test.go index d0a993891..85b17a4c7 100644 --- a/internal/transform/color_test.go +++ b/internal/transform/color_test.go @@ -75,6 +75,15 @@ var colorTests = []colorTest{ }, colors: map[string]string{}, }, + { + customLabels: map[string]map[string]string{ + "node": map[string]string{"*": "#123"}, + }, + labels: map[string]string{ + "node": "localhost", + }, + colors: map[string]string{"node": "localhost"}, + }, } func TestColorLabel(t *testing.T) { diff --git a/internal/transform/colors.go b/internal/transform/colors.go index 9800a1f22..32c48718e 100644 --- a/internal/transform/colors.go +++ b/internal/transform/colors.go @@ -39,6 +39,29 @@ func rgbToBrightness(r, g, b uint8) int32 { return ((int32(r) * 299) + (int32(g) * 587) + (int32(b) * 114)) / 1000 } +func parseCustomColor(colorStore models.LabelsColorMap, key, val, customColor string) { + color, err := plcolors.Parse(customColor) + if err != nil { + log.Warningf("Failed to parse custom color for %s=%s: %s", key, val, err) + return + } + rgb := color.ToRGB() + bc := models.Color{ + Red: rgb.R, + Green: rgb.G, + Blue: rgb.B, + Alpha: 255, + } + brightness := rgbToBrightness(bc.Red, bc.Green, bc.Blue) + if _, found := colorStore[key]; !found { + colorStore[key] = make(map[string]models.LabelColors) + } + colorStore[key][val] = models.LabelColors{ + Brightness: brightness, + Background: bc, + } +} + // ColorLabel update karmaColorMap object with a color object generated // from label key and value passed here // It's used to generate unique colors for configured labels @@ -46,30 +69,19 @@ func ColorLabel(colorStore models.LabelsColorMap, key string, val string) { // first handle custom colors _, ok := config.Config.Labels.Color.Custom[key] if ok { - c, ol := config.Config.Labels.Color.Custom[key][val] - if ol { - color, err := plcolors.Parse(c) - if err != nil { - log.Warningf("Failed to parse custom color for %s=%s: %s", key, val, err) - return - } - rgb := color.ToRGB() - bc := models.Color{ - Red: rgb.R, - Green: rgb.G, - Blue: rgb.B, - Alpha: 255, - } - brightness := rgbToBrightness(bc.Red, bc.Green, bc.Blue) - if _, found := colorStore[key]; !found { - colorStore[key] = make(map[string]models.LabelColors) - } - colorStore[key][val] = models.LabelColors{ - Brightness: brightness, - Background: bc, - } + // try matching both key and value + customColor, found := config.Config.Labels.Color.Custom[key][val] + if found { + parseCustomColor(colorStore, key, val, customColor) + return + } + + // if not found try matching key and wildcard (*) + customColor, found = config.Config.Labels.Color.Custom[key]["*"] + if found { + parseCustomColor(colorStore, key, val, customColor) + return } - return } // if no custom color is found then generate unique colors if needed