Skip to content

Commit 2bc9904

Browse files
dashpoledmitryax
andauthored
prometheus exporters: Add add_metric_suffixes configuration option (#24260)
**Description:** Add add_metric_suffixes configuration option, which can disable the addition of type and unit suffixes. This is backwards-compatible, since the default is true. This is recommended by the specification for sum suffixes in https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/compatibility/prometheus_and_openmetrics.md#sums and allowed in metadata https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/compatibility/prometheus_and_openmetrics.md#metric-metadata-1: `Exporters SHOULD provide a configuration option to disable the addition of _total suffixes.` `The resulting unit SHOULD be added to the metric as OpenMetrics UNIT metadata and as a suffix to the metric name unless the metric name already contains the unit, or the unit MUST be omitted` This deprecates the BuildPromCompliantName function in-favor of BuildCompliantName, which includes the additional argument for configuring suffixes. **Link to tracking Issue:** Fixes #21743 Part of #8950 --------- Co-authored-by: Dmitrii Anoshin <[email protected]>
1 parent 227ed82 commit 2bc9904

File tree

19 files changed

+97
-37
lines changed

19 files changed

+97
-37
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: 'enhancement'
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: prometheusremotewriteexporter, prometheusexporter
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: "Add `add_metric_suffixes` configuration option, which can disable the addition of type and unit suffixes."
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [21743, 8950]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

exporter/prometheusexporter/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The following settings can be optionally configured:
3333
- `resource_to_telemetry_conversion`
3434
- `enabled` (default = false): If `enabled` is `true`, all the resource attributes will be converted to metric labels by default.
3535
- `enable_open_metrics`: (default = `false`): If true, metrics will be exported using the OpenMetrics format. Exemplars are only exported in the OpenMetrics format, and only for histogram and monotonic sum (i.e. counter) metrics.
36+
- `add_metric_suffixes`: (default = `true`): If false, addition of type and unit suffixes is disabled.
3637

3738
Example:
3839

@@ -51,6 +52,7 @@ exporters:
5152
send_timestamps: true
5253
metric_expiration: 180m
5354
enable_open_metrics: true
55+
add_metric_suffixes: false
5456
resource_to_telemetry_conversion:
5557
enabled: true
5658
```

exporter/prometheusexporter/collector.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ type collector struct {
3030
accumulator accumulator
3131
logger *zap.Logger
3232

33-
sendTimestamps bool
34-
namespace string
35-
constLabels prometheus.Labels
33+
sendTimestamps bool
34+
addMetricSuffixes bool
35+
namespace string
36+
constLabels prometheus.Labels
3637
}
3738

3839
func newCollector(config *Config, logger *zap.Logger) *collector {
3940
return &collector{
40-
accumulator: newAccumulator(logger, config.MetricExpiration),
41-
logger: logger,
42-
namespace: prometheustranslator.CleanUpString(config.Namespace),
43-
sendTimestamps: config.SendTimestamps,
44-
constLabels: config.ConstLabels,
41+
accumulator: newAccumulator(logger, config.MetricExpiration),
42+
logger: logger,
43+
namespace: prometheustranslator.CleanUpString(config.Namespace),
44+
sendTimestamps: config.SendTimestamps,
45+
constLabels: config.ConstLabels,
46+
addMetricSuffixes: config.AddMetricSuffixes,
4547
}
4648
}
4749

@@ -127,7 +129,7 @@ func (c *collector) getMetricMetadata(metric pmetric.Metric, attributes pcommon.
127129
}
128130

129131
return prometheus.NewDesc(
130-
prometheustranslator.BuildPromCompliantName(metric, c.namespace),
132+
prometheustranslator.BuildCompliantName(metric, c.namespace, c.addMetricSuffixes),
131133
metric.Description(),
132134
keys,
133135
c.constLabels,

exporter/prometheusexporter/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type Config struct {
3434

3535
// EnableOpenMetrics enables the use of the OpenMetrics encoding option for the prometheus exporter.
3636
EnableOpenMetrics bool `mapstructure:"enable_open_metrics"`
37+
38+
// AddMetricSuffixes controls whether suffixes are added to metric names. Defaults to true.
39+
AddMetricSuffixes bool `mapstructure:"add_metric_suffixes"`
3740
}
3841

3942
var _ component.Config = (*Config)(nil)

exporter/prometheusexporter/config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ func TestLoadConfig(t *testing.T) {
5050
"label1": "value1",
5151
"another label": "spaced value",
5252
},
53-
SendTimestamps: true,
54-
MetricExpiration: 60 * time.Minute,
53+
SendTimestamps: true,
54+
MetricExpiration: 60 * time.Minute,
55+
AddMetricSuffixes: false,
5556
},
5657
},
5758
}

exporter/prometheusexporter/factory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func createDefaultConfig() component.Config {
2929
SendTimestamps: false,
3030
MetricExpiration: time.Minute * 5,
3131
EnableOpenMetrics: false,
32+
AddMetricSuffixes: true,
3233
}
3334
}
3435

exporter/prometheusexporter/testdata/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ prometheus/2:
1111
"another label": spaced value
1212
send_timestamps: true
1313
metric_expiration: 60m
14+
add_metric_suffixes: false

exporter/prometheusremotewriteexporter/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The following settings can be optionally configured:
5050
- `headers`: additional headers attached to each HTTP request.
5151
- *Note the following headers cannot be changed: `Content-Encoding`, `Content-Type`, `X-Prometheus-Remote-Write-Version`, and `User-Agent`.*
5252
- `namespace`: prefix attached to each exported metric name.
53+
- `add_metric_suffixes`: If set to false, type and unit suffixes will not be added to metrics. Default: true.
5354
- `remote_write_queue`: fine tuning for queueing and sending of the outgoing remote writes.
5455
- `enabled`: enable the sending queue
5556
- `queue_size`: number of OTLP metrics that can be queued. Ignored if `enabled` is `false`

exporter/prometheusremotewriteexporter/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type Config struct {
4242

4343
// CreatedMetric allows customizing creation of _created metrics
4444
CreatedMetric *CreatedMetric `mapstructure:"export_created_metric,omitempty"`
45+
46+
// AddMetricSuffixes controls whether unit and type suffixes are added to metrics on export
47+
AddMetricSuffixes bool `mapstructure:"add_metric_suffixes"`
4548
}
4649

4750
type CreatedMetric struct {

exporter/prometheusremotewriteexporter/config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ func TestLoadConfig(t *testing.T) {
5454
QueueSize: 2000,
5555
NumConsumers: 10,
5656
},
57-
Namespace: "test-space",
58-
ExternalLabels: map[string]string{"key1": "value1", "key2": "value2"},
57+
AddMetricSuffixes: false,
58+
Namespace: "test-space",
59+
ExternalLabels: map[string]string{"key1": "value1", "key2": "value2"},
5960
HTTPClientSettings: confighttp.HTTPClientSettings{
6061
Endpoint: "localhost:8888",
6162
TLSSetting: configtls.TLSClientSetting{

0 commit comments

Comments
 (0)