feat(api): allow using wildcards with custom colors

Fixes #537
This commit is contained in:
Łukasz Mierzwa
2019-03-20 21:15:04 +00:00
parent 23f51f9f7b
commit 4ef9052a98
3 changed files with 62 additions and 24 deletions

View File

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

View File

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

View File

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