mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user