Skip to content

Commit

Permalink
✨ add custom units for custom grafana dashboards
Browse files Browse the repository at this point in the history
Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com>
  • Loading branch information
jkremser committed Oct 6, 2022
1 parent eec9f37 commit 2e10ba8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/book/src/plugins/grafana-v1-alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ customMetrics:
# - metric: # Raw custom metric (required)
# type: # Metric type: counter/gauge/histogram (required)
# expr: # Prom_ql for the metric (optional)
# unit: # Unit of measurement, examples: s,none,bytes,percent,etc. (optional)
```

#### Add Custom Metrics to Config
Expand All @@ -159,6 +160,7 @@ Alternatively, you can provide `expr` and the plugin will use the specified one
customMetrics:
- metric: memcached_operator_reconcile_total # Raw custom metric (required)
type: counter # Metric type: counter/gauge/histogram (required)
unit: none
- metric: memcached_operator_reconcile_time_seconds_bucket
type: histogram
```
Expand Down
20 changes: 18 additions & 2 deletions pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ func validateCustomMetricItems(rawItems []templates.CustomMetricItem) []template
}
}

// 2. Fill Expr if missing
// 2. Fill Expr and Unit if missing
validatedItems := make([]templates.CustomMetricItem, len(filterResult))
for i, item := range filterResult {
validatedItems[i] = fillMissingExpr(item)
item = fillMissingExpr(item)
validatedItems[i] = fillMissingUnit(item)
}

return validatedItems
Expand Down Expand Up @@ -142,6 +143,21 @@ func fillMissingExpr(item templates.CustomMetricItem) templates.CustomMetricItem
return item
}

func fillMissingUnit(item templates.CustomMetricItem) templates.CustomMetricItem {
if item.Unit == "" {
name := strings.ToLower(item.Metric)
item.Unit = "none"
if strings.Contains(name, "second") || strings.Contains(name, "duration") {
item.Unit = "s"
} else if strings.Contains(name, "byte") {
item.Unit = "bytes"
} else if strings.Contains(name, "ratio") {
item.Unit = "percent"
}
}
return item
}

// Scaffold implements cmdutil.Scaffolder
func (s *editScaffolder) Scaffold() error {
fmt.Println("Generating Grafana manifests to visualize controller status...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ customMetrics:
# - metric: # Raw custom metric (required)
# type: # Metric type: counter/gauge/histogram (required)
# expr: # Prom_ql for the metric (optional)
# unit: # Unit of measurement, examples: s,none,bytes,percent,etc. (optional)
#
#
# Example:
# ---
# customMetrics:
# - metric: foo_bar
# unit: none
# type: histogram
# expr: histogram_quantile(0.90, sum by(instance, le) (rate(foo_bar{job=\"$job\", namespace=\"$namespace\"}[5m])))
`
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"path/filepath"
"strings"
"text/template"

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
Expand All @@ -33,6 +34,7 @@ type CustomMetricItem struct {
Metric string `json:"metric"`
Type string `json:"type"`
Expr string `json:"expr,omitempty"`
Unit string `json:"unit,omitempty"`
}

var _ machinery.Template = &CustomMetricsDashManifest{}
Expand Down Expand Up @@ -66,6 +68,7 @@ var fns = template.FuncMap{
"plus1": func(x int) int {
return x + 1
},
"hasSuffix": strings.HasSuffix,
}

func (f *CustomMetricsDashManifest) createTemplate() (string, error) {
Expand Down Expand Up @@ -173,7 +176,7 @@ const customMetricsDashTemplate = `{
}
]
},
"unit": "s"
"unit": "{{ .Unit }}"
},
"overrides": []
},
Expand Down Expand Up @@ -208,7 +211,19 @@ const customMetricsDashTemplate = `{
}
],
"title": "{{ .Metric }} ({{ .Type }})",
{{- if hasSuffix .Metric "_info" }}
"transformations": [
{
"id": "labelsToFields",
"options": {
"mode": "rows"
}
}
],
"type": "table"
{{- else }}
"type": "timeseries"
{{- end }}
}{{ if ne (plus1 $i) $n }},
{{end}}{{end}}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ customMetrics:
# - metric: # Raw custom metric (required)
# type: # Metric type: counter/gauge/histogram (required)
# expr: # Prom_ql for the metric (optional)
# unit: # Unit of measurement, examples: s,none,bytes,percent,etc. (optional)
#
#
# Example:
# ---
# customMetrics:
# - metric: foo_bar
# unit: none
# type: histogram
# expr: histogram_quantile(0.90, sum by(instance, le) (rate(foo_bar{job=\"$job\", namespace=\"$namespace\"}[5m])))
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ customMetrics:
# - metric: # Raw custom metric (required)
# type: # Metric type: counter/gauge/histogram (required)
# expr: # Prom_ql for the metric (optional)
# unit: # Unit of measurement, examples: s,none,bytes,percent,etc. (optional)
#
#
# Example:
# ---
# customMetrics:
# - metric: foo_bar
# unit: none
# type: histogram
# expr: histogram_quantile(0.90, sum by(instance, le) (rate(foo_bar{job=\"$job\", namespace=\"$namespace\"}[5m])))

0 comments on commit 2e10ba8

Please sign in to comment.