Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (grafana/v1-alpha) : add custom units for custom grafana dashboards #2965

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jkremser,

Could you please sort out the lint issue for we move forward?
See that you can run make lint-fix for this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"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])))