Skip to content

Commit

Permalink
go.d/prometheus: add label_prefix config option (netdata#18559)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Sep 14, 2024
1 parent c48203d commit eb52fe4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/go/plugin/go.d/modules/prometheus/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
)
Expand Down Expand Up @@ -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),
},
)
Expand Down Expand Up @@ -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),
})
}
Expand Down Expand Up @@ -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),
})
}
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/go/plugin/go.d/modules/prometheus/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.",
Expand Down
4 changes: 4 additions & 0 deletions src/go/plugin/go.d/modules/prometheus/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/go/plugin/go.d/modules/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions src/go/plugin/go.d/modules/prometheus/testdata/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"tls_skip_verify": true,
"name": "ok",
"app": "ok",
"label_prefix": "ok",
"bearer_token_file": "ok",
"selector": {
"allow": [
Expand Down
1 change: 1 addition & 0 deletions src/go/plugin/go.d/modules/prometheus/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tls_key: "ok"
tls_skip_verify: yes
name: "ok"
app: "ok"
label_prefix: "ok"
bearer_token_file: "ok"
selector:
allow:
Expand Down

0 comments on commit eb52fe4

Please sign in to comment.