diff --git a/.chloggen/fix_46984.yaml b/.chloggen/fix_46984.yaml new file mode 100644 index 0000000000000..ebf14b153f98e --- /dev/null +++ b/.chloggen/fix_46984.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog) +component: receiver/prometheus_remote_write + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add remaining metric metadata translations (Name, Unit) from Prometheus to OTLP as per compatibility spec + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [46984] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/prometheusremotewritereceiver/config.go b/receiver/prometheusremotewritereceiver/config.go index 8bfd4d9ac8ccd..fd910d4f9c9c2 100644 --- a/receiver/prometheusremotewritereceiver/config.go +++ b/receiver/prometheusremotewritereceiver/config.go @@ -11,6 +11,7 @@ import ( // Config holds common fields and embedded protocol-specific configurations type Config struct { confighttp.ServerConfig `mapstructure:",squash"` + TrimMetricSuffixes bool `mapstructure:"trim_metric_suffixes"` } var _ component.Config = (*Config)(nil) diff --git a/receiver/prometheusremotewritereceiver/config.schema.yaml b/receiver/prometheusremotewritereceiver/config.schema.yaml index 89cfb106503d7..fc2a7e4bfa408 100644 --- a/receiver/prometheusremotewritereceiver/config.schema.yaml +++ b/receiver/prometheusremotewritereceiver/config.schema.yaml @@ -1,4 +1,7 @@ description: Config holds common fields and embedded protocol-specific configurations type: object +properties: + trim_metric_suffixes: + type: boolean allOf: - $ref: go.opentelemetry.io/collector/config/confighttp.server_config diff --git a/receiver/prometheusremotewritereceiver/factory.go b/receiver/prometheusremotewritereceiver/factory.go index 65a670a68c502..dfa4256851c8a 100644 --- a/receiver/prometheusremotewritereceiver/factory.go +++ b/receiver/prometheusremotewritereceiver/factory.go @@ -34,6 +34,7 @@ func createDefaultConfig() component.Config { ServerConfig: confighttp.ServerConfig{ NetAddr: netAddr, }, + TrimMetricSuffixes: false, } } diff --git a/receiver/prometheusremotewritereceiver/receiver.go b/receiver/prometheusremotewritereceiver/receiver.go index bc34be2f5f77a..030c86efdcb8a 100644 --- a/receiver/prometheusremotewritereceiver/receiver.go +++ b/receiver/prometheusremotewritereceiver/receiver.go @@ -360,6 +360,27 @@ func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, req *wr unit := req.Symbols[ts.Metadata.UnitRef] description := req.Symbols[ts.Metadata.HelpRef] + if unit != "" { + if unit == "ratio" { + unit = "1" + } else { + unit = strings.ReplaceAll(unit, "_per_", "/") + unit = prometheus.UnitWordToUCUM(unit) + } + } + + if prw.config.TrimMetricSuffixes { + metricName = strings.TrimSuffix(metricName, "_total") + metricName = strings.TrimSuffix(metricName, "_sum") + metricName = strings.TrimSuffix(metricName, "_count") + metricName = strings.TrimSuffix(metricName, "_bucket") + + origUnit := req.Symbols[ts.Metadata.UnitRef] + if origUnit != "" { + metricName = strings.TrimSuffix(metricName, "_"+origUnit) + } + } + // Handle histograms separately due to their complex mixed-schema processing if ts.Metadata.Type == writev2.Metadata_METRIC_TYPE_HISTOGRAM || ts.Metadata.Type == writev2.Metadata_METRIC_TYPE_UNSPECIFIED && len(ts.Histograms) > 0 { diff --git a/receiver/prometheusremotewritereceiver/receiver_test.go b/receiver/prometheusremotewritereceiver/receiver_test.go index c2c075c79ae60..5168a17cae85e 100644 --- a/receiver/prometheusremotewritereceiver/receiver_test.go +++ b/receiver/prometheusremotewritereceiver/receiver_test.go @@ -579,7 +579,7 @@ func TestTranslateV2(t *testing.T) { // The second metric should have 1 data point. metrics1 := sm1.Metrics().AppendEmpty() metrics1.SetName("test_metric") - metrics1.SetUnit("seconds") + metrics1.SetUnit("s") metrics1.SetDescription("longer description") metrics1.Metadata().PutStr(prometheus.MetricMetadataTypeKey, "gauge") @@ -595,7 +595,7 @@ func TestTranslateV2(t *testing.T) { metrics2 := sm1.Metrics().AppendEmpty() metrics2.SetName("test_metric") - metrics2.SetUnit("milliseconds") + metrics2.SetUnit("ms") metrics2.SetDescription("small desc") metrics2.Metadata().PutStr(prometheus.MetricMetadataTypeKey, "gauge")