go.d/prometheus: add label_prefix config option (#18559)
Ilya Mashchenko committed
Sep 15, 2024 at 02:05 UTC
eb52fe4c250912e067f86551072ab3202aafdae8
6 files changed
+23
-4
src/go/plugin/go.d/modules/prometheus/charts.go
+11
-4
@@ -41,7 +41,7 @@ func (p *Prometheus) addGaugeChart(id, name, help string, labels labels.Labels)
41
for _, lbl := range labels {
42
chart.Labels = append(chart.Labels,
43
module.Label{
44
- Key: lbl.Name,
44
+ Key: p.labelName(lbl.Name),
45
Value: apostropheReplacer.Replace(lbl.Value),
46
},
47
)
@@ -84,7 +84,7 @@ func (p *Prometheus) addCounterChart(id, name, help string, labels labels.Labels
84
for _, lbl := range labels {
85
chart.Labels = append(chart.Labels,
86
module.Label{
87
- Key: lbl.Name,
87
+ Key: p.labelName(lbl.Name),
88
Value: apostropheReplacer.Replace(lbl.Value),
89
},
90
)
@@ -154,7 +154,7 @@ func (p *Prometheus) addSummaryCharts(id, name, help string, labels labels.Label
154
for _, chart := range charts {
155
for _, lbl := range labels {
156
chart.Labels = append(chart.Labels, module.Label{
157
- Key: lbl.Name,
157
+ Key: p.labelName(lbl.Name),
158
Value: apostropheReplacer.Replace(lbl.Value),
159
})
160
}
@@ -222,7 +222,7 @@ func (p *Prometheus) addHistogramCharts(id, name, help string, labels labels.Lab
222
for _, chart := range charts {
223
for _, lbl := range labels {
224
chart.Labels = append(chart.Labels, module.Label{
225
- Key: lbl.Name,
225
+ Key: p.labelName(lbl.Name),
226
Value: apostropheReplacer.Replace(lbl.Value),
227
})
228
}
@@ -241,6 +241,13 @@ func (p *Prometheus) application() string {
241
return p.Name
242
}
243
244
+func (p *Prometheus) labelName(lblName string) string {
245
+ if p.LabelPrefix == "" {
246
+ return lblName
247
+ }
248
+ return p.LabelPrefix + "_" + lblName
249
+}
250
+
251
func getChartTitle(name, help string) string {
252
if help == "" {
253
return fmt.Sprintf("Metric \"%s\"", name)
src/go/plugin/go.d/modules/prometheus/config_schema.json
+5
@@ -34,6 +34,11 @@
34
"description": "If an endpoint does not return at least one metric with the specified prefix, the data is not processed.",
35
"type": "string"
36
},
37
+ "label_prefix": {
38
+ "title": "Label prefix",
39
+ "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).",
40
+ "type": "string"
41
+ },
42
"app": {
43
"title": "Application",
44
"description": "If set, this value will be used in the chart context as 'prometheus.{app}.{metric_name}'.",
src/go/plugin/go.d/modules/prometheus/metadata.yaml
+4
@@ -113,6 +113,10 @@ modules:
113
description: Time series per metric (metric name) limit. Metrics with number of time series > limit are skipped.
114
default_value: 200
115
required: false
116
+ - name: label_prefix
117
+ 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)."
118
+ default_value: ""
119
+ required: false
120
- name: timeout
121
description: HTTP request timeout.
122
default_value: 10
src/go/plugin/go.d/modules/prometheus/prometheus.go
+1
@@ -50,6 +50,7 @@ type Config struct {
50
web.HTTPConfig `yaml:",inline" json:""`
51
Name string `yaml:"name,omitempty" json:"name"`
52
Application string `yaml:"app,omitempty" json:"app"`
53
+ LabelPrefix string `yaml:"label_prefix,omitempty" json:"label_prefix"`
54
BearerTokenFile string `yaml:"bearer_token_file,omitempty" json:"bearer_token_file"`
55
Selector selector.Expr `yaml:"selector,omitempty" json:"selector"`
56
ExpectedPrefix string `yaml:"expected_prefix,omitempty" json:"expected_prefix"`
src/go/plugin/go.d/modules/prometheus/testdata/config.json
+1
@@ -19,6 +19,7 @@
19
"tls_skip_verify": true,
20
"name": "ok",
21
"app": "ok",
22
+ "label_prefix": "ok",
23
"bearer_token_file": "ok",
24
"selector": {
25
"allow": [
src/go/plugin/go.d/modules/prometheus/testdata/config.yaml
+1
@@ -17,6 +17,7 @@ tls_key: "ok"
17
tls_skip_verify: yes
18
name: "ok"
19
app: "ok"
20
+label_prefix: "ok"
21
bearer_token_file: "ok"
22
selector:
23
allow: