From eb52fe4c250912e067f86551072ab3202aafdae8 Mon Sep 17 00:00:00 2001 From: Ilya Mashchenko Date: Sun, 15 Sep 2024 02:05:06 +0300 Subject: [PATCH] go.d/prometheus: add label_prefix config option (#18559) --- src/go/plugin/go.d/modules/prometheus/charts.go | 15 +++++++++++---- .../go.d/modules/prometheus/config_schema.json | 5 +++++ .../plugin/go.d/modules/prometheus/metadata.yaml | 4 ++++ .../plugin/go.d/modules/prometheus/prometheus.go | 1 + .../go.d/modules/prometheus/testdata/config.json | 1 + .../go.d/modules/prometheus/testdata/config.yaml | 1 + 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/go/plugin/go.d/modules/prometheus/charts.go b/src/go/plugin/go.d/modules/prometheus/charts.go index c78f9b1b01ef35..bdddc623d970bd 100644 --- a/src/go/plugin/go.d/modules/prometheus/charts.go +++ b/src/go/plugin/go.d/modules/prometheus/charts.go @@ -41,7 +41,7 @@ func (p *Prometheus) addGaugeChart(id, name, help string, labels labels.Labels) for _, lbl := range labels { chart.Labels = append(chart.Labels, module.Label{ - Key: lbl.Name, + Key: p.labelName(lbl.Name), Value: apostropheReplacer.Replace(lbl.Value), }, ) @@ -84,7 +84,7 @@ func (p *Prometheus) addCounterChart(id, name, help string, labels labels.Labels for _, lbl := range labels { chart.Labels = append(chart.Labels, module.Label{ - Key: lbl.Name, + Key: p.labelName(lbl.Name), Value: apostropheReplacer.Replace(lbl.Value), }, ) @@ -154,7 +154,7 @@ func (p *Prometheus) addSummaryCharts(id, name, help string, labels labels.Label for _, chart := range charts { for _, lbl := range labels { chart.Labels = append(chart.Labels, module.Label{ - Key: lbl.Name, + Key: p.labelName(lbl.Name), Value: apostropheReplacer.Replace(lbl.Value), }) } @@ -222,7 +222,7 @@ func (p *Prometheus) addHistogramCharts(id, name, help string, labels labels.Lab for _, chart := range charts { for _, lbl := range labels { chart.Labels = append(chart.Labels, module.Label{ - Key: lbl.Name, + Key: p.labelName(lbl.Name), Value: apostropheReplacer.Replace(lbl.Value), }) } @@ -241,6 +241,13 @@ func (p *Prometheus) application() string { return p.Name } +func (p *Prometheus) labelName(lblName string) string { + if p.LabelPrefix == "" { + return lblName + } + return p.LabelPrefix + "_" + lblName +} + func getChartTitle(name, help string) string { if help == "" { return fmt.Sprintf("Metric \"%s\"", name) diff --git a/src/go/plugin/go.d/modules/prometheus/config_schema.json b/src/go/plugin/go.d/modules/prometheus/config_schema.json index 2df96b049deef7..df843109ed48e2 100644 --- a/src/go/plugin/go.d/modules/prometheus/config_schema.json +++ b/src/go/plugin/go.d/modules/prometheus/config_schema.json @@ -34,6 +34,11 @@ "description": "If an endpoint does not return at least one metric with the specified prefix, the data is not processed.", "type": "string" }, + "label_prefix": { + "title": "Label prefix", + "description": "An optional prefix that will be added to all labels of all charts. If set, the label names will be automatically formatted as `prefix_name` (the prefix followed by an underscore and the original name).", + "type": "string" + }, "app": { "title": "Application", "description": "If set, this value will be used in the chart context as 'prometheus.{app}.{metric_name}'.", diff --git a/src/go/plugin/go.d/modules/prometheus/metadata.yaml b/src/go/plugin/go.d/modules/prometheus/metadata.yaml index fee2b820bbd57d..1a2a88c36bbef1 100644 --- a/src/go/plugin/go.d/modules/prometheus/metadata.yaml +++ b/src/go/plugin/go.d/modules/prometheus/metadata.yaml @@ -113,6 +113,10 @@ modules: description: Time series per metric (metric name) limit. Metrics with number of time series > limit are skipped. default_value: 200 required: false + - name: label_prefix + description: "An optional prefix that will be added to all labels of all charts. If set, the label names will be automatically formatted as `prefix_name` (the prefix followed by an underscore and the original name)." + default_value: "" + required: false - name: timeout description: HTTP request timeout. default_value: 10 diff --git a/src/go/plugin/go.d/modules/prometheus/prometheus.go b/src/go/plugin/go.d/modules/prometheus/prometheus.go index ee5939154339cb..b7a1d26b0a3696 100644 --- a/src/go/plugin/go.d/modules/prometheus/prometheus.go +++ b/src/go/plugin/go.d/modules/prometheus/prometheus.go @@ -50,6 +50,7 @@ type Config struct { web.HTTPConfig `yaml:",inline" json:""` Name string `yaml:"name,omitempty" json:"name"` Application string `yaml:"app,omitempty" json:"app"` + LabelPrefix string `yaml:"label_prefix,omitempty" json:"label_prefix"` BearerTokenFile string `yaml:"bearer_token_file,omitempty" json:"bearer_token_file"` Selector selector.Expr `yaml:"selector,omitempty" json:"selector"` ExpectedPrefix string `yaml:"expected_prefix,omitempty" json:"expected_prefix"` diff --git a/src/go/plugin/go.d/modules/prometheus/testdata/config.json b/src/go/plugin/go.d/modules/prometheus/testdata/config.json index 2e9b2e138b8043..75d7e9ba3cd38c 100644 --- a/src/go/plugin/go.d/modules/prometheus/testdata/config.json +++ b/src/go/plugin/go.d/modules/prometheus/testdata/config.json @@ -19,6 +19,7 @@ "tls_skip_verify": true, "name": "ok", "app": "ok", + "label_prefix": "ok", "bearer_token_file": "ok", "selector": { "allow": [ diff --git a/src/go/plugin/go.d/modules/prometheus/testdata/config.yaml b/src/go/plugin/go.d/modules/prometheus/testdata/config.yaml index 37a411b9af3336..d7ab417ecdde8e 100644 --- a/src/go/plugin/go.d/modules/prometheus/testdata/config.yaml +++ b/src/go/plugin/go.d/modules/prometheus/testdata/config.yaml @@ -17,6 +17,7 @@ tls_key: "ok" tls_skip_verify: yes name: "ok" app: "ok" +label_prefix: "ok" bearer_token_file: "ok" selector: allow: