From 11396751f11905fe62df105f444cef9ab531def0 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Wed, 8 May 2024 04:36:53 -0400 Subject: [PATCH] chore: add constants for prometheus translation (#32830) **Description:** This is a cleanup to consolidate the constants used for prometheus translation in a single place. --------- Co-authored-by: Curtis Robert --- exporter/prometheusexporter/collector.go | 12 +++------ pkg/translator/prometheus/constants.go | 26 +++++++++++++++++++ .../prometheusremotewrite/helper.go | 15 ++++------- .../prometheusremotewrite/helper_test.go | 5 ++-- .../prometheusremotewrite/testutils_test.go | 4 ++- .../internal/metricfamily.go | 9 ++----- .../internal/transaction.go | 22 +++++++--------- 7 files changed, 52 insertions(+), 41 deletions(-) diff --git a/exporter/prometheusexporter/collector.go b/exporter/prometheusexporter/collector.go index 494a4e02b92d..8a243c01a921 100644 --- a/exporter/prometheusexporter/collector.go +++ b/exporter/prometheusexporter/collector.go @@ -18,10 +18,6 @@ import ( prometheustranslator "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus" ) -const ( - targetMetricName = "target_info" -) - var ( separatorString = string([]byte{model.SeparatorByte}) ) @@ -56,11 +52,11 @@ func convertExemplars(exemplars pmetric.ExemplarSlice) []prometheus.Exemplar { exemplarLabels := make(prometheus.Labels, 0) if traceID := e.TraceID(); !traceID.IsEmpty() { - exemplarLabels["trace_id"] = hex.EncodeToString(traceID[:]) + exemplarLabels[prometheustranslator.ExemplarTraceIDKey] = hex.EncodeToString(traceID[:]) } if spanID := e.SpanID(); !spanID.IsEmpty() { - exemplarLabels["span_id"] = hex.EncodeToString(spanID[:]) + exemplarLabels[prometheustranslator.ExemplarSpanIDKey] = hex.EncodeToString(spanID[:]) } var value float64 @@ -332,7 +328,7 @@ func (c *collector) createTargetInfoMetrics(resourceAttrs []pcommon.Map) ([]prom labels[model.InstanceLabel] = instance } - name := targetMetricName + name := prometheustranslator.TargetInfoMetricName if len(c.namespace) > 0 { name = c.namespace + "_" + name } @@ -370,7 +366,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { targetMetrics, err := c.createTargetInfoMetrics(resourceAttrs) if err != nil { - c.logger.Error(fmt.Sprintf("failed to convert metric %s: %s", targetMetricName, err.Error())) + c.logger.Error(fmt.Sprintf("failed to convert metric %s: %s", prometheustranslator.TargetInfoMetricName, err.Error())) } for _, m := range targetMetrics { ch <- m diff --git a/pkg/translator/prometheus/constants.go b/pkg/translator/prometheus/constants.go index 313f939a0a26..c6358681c23d 100644 --- a/pkg/translator/prometheus/constants.go +++ b/pkg/translator/prometheus/constants.go @@ -8,4 +8,30 @@ const ( // type in metric metadata: // https://github.com/open-telemetry/opentelemetry-specification/blob/e6eccba97ebaffbbfad6d4358408a2cead0ec2df/specification/compatibility/prometheus_and_openmetrics.md#metric-metadata MetricMetadataTypeKey = "prometheus.type" + // ExemplarTraceIDKey is the key used to store the trace ID in Prometheus + // exemplars: + // https://github.com/open-telemetry/opentelemetry-specification/blob/e6eccba97ebaffbbfad6d4358408a2cead0ec2df/specification/compatibility/prometheus_and_openmetrics.md#exemplars + ExemplarTraceIDKey = "trace_id" + // ExemplarSpanIDKey is the key used to store the Span ID in Prometheus + // exemplars: + // https://github.com/open-telemetry/opentelemetry-specification/blob/e6eccba97ebaffbbfad6d4358408a2cead0ec2df/specification/compatibility/prometheus_and_openmetrics.md#exemplars + ExemplarSpanIDKey = "span_id" + // ScopeInfoMetricName is the name of the metric used to preserve scope + // attributes in Prometheus format: + // https://github.com/open-telemetry/opentelemetry-specification/blob/e6eccba97ebaffbbfad6d4358408a2cead0ec2df/specification/compatibility/prometheus_and_openmetrics.md#instrumentation-scope + ScopeInfoMetricName = "otel_scope_info" + // ScopeNameLabelKey is the name of the label key used to identify the name + // of the OpenTelemetry scope which produced the metric: + // https://github.com/open-telemetry/opentelemetry-specification/blob/e6eccba97ebaffbbfad6d4358408a2cead0ec2df/specification/compatibility/prometheus_and_openmetrics.md#instrumentation-scope + ScopeNameLabelKey = "otel_scope_name" + // ScopeVersionLabelKey is the name of the label key used to identify the + // version of the OpenTelemetry scope which produced the metric: + // https://github.com/open-telemetry/opentelemetry-specification/blob/e6eccba97ebaffbbfad6d4358408a2cead0ec2df/specification/compatibility/prometheus_and_openmetrics.md#instrumentation-scope + ScopeVersionLabelKey = "otel_scope_version" + // TargetInfoMetricName is the name of the metric used to preserve resource + // attributes in Prometheus format: + // https://github.com/open-telemetry/opentelemetry-specification/blob/e6eccba97ebaffbbfad6d4358408a2cead0ec2df/specification/compatibility/prometheus_and_openmetrics.md#resource-attributes-1 + // It originates from OpenMetrics: + // https://github.com/OpenObservability/OpenMetrics/blob/1386544931307dff279688f332890c31b6c5de36/specification/OpenMetrics.md#supporting-target-metadata-in-both-push-based-and-pull-based-systems + TargetInfoMetricName = "target_info" ) diff --git a/pkg/translator/prometheusremotewrite/helper.go b/pkg/translator/prometheusremotewrite/helper.go index a5d1db09b189..99fabfb2da14 100644 --- a/pkg/translator/prometheusremotewrite/helper.go +++ b/pkg/translator/prometheusremotewrite/helper.go @@ -38,12 +38,7 @@ const ( // according to the prometheus specification // https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#exemplars maxExemplarRunes = 128 - // Trace and Span id keys are defined as part of the spec: - // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification%2Fmetrics%2Fdatamodel.md#exemplars-2 - traceIDKey = "trace_id" - spanIDKey = "span_id" infoType = "info" - targetMetricName = "target_info" ) type bucketBoundsData struct { @@ -303,18 +298,18 @@ func getPromExemplars[T exemplarType](pt T) []prompb.Exemplar { } if traceID := exemplar.TraceID(); !traceID.IsEmpty() { val := hex.EncodeToString(traceID[:]) - exemplarRunes += utf8.RuneCountInString(traceIDKey) + utf8.RuneCountInString(val) + exemplarRunes += utf8.RuneCountInString(prometheustranslator.ExemplarTraceIDKey) + utf8.RuneCountInString(val) promLabel := prompb.Label{ - Name: traceIDKey, + Name: prometheustranslator.ExemplarTraceIDKey, Value: val, } promExemplar.Labels = append(promExemplar.Labels, promLabel) } if spanID := exemplar.SpanID(); !spanID.IsEmpty() { val := hex.EncodeToString(spanID[:]) - exemplarRunes += utf8.RuneCountInString(spanIDKey) + utf8.RuneCountInString(val) + exemplarRunes += utf8.RuneCountInString(prometheustranslator.ExemplarSpanIDKey) + utf8.RuneCountInString(val) promLabel := prompb.Label{ - Name: spanIDKey, + Name: prometheustranslator.ExemplarSpanIDKey, Value: val, } promExemplar.Labels = append(promExemplar.Labels, promLabel) @@ -534,7 +529,7 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta return } - name := targetMetricName + name := prometheustranslator.TargetInfoMetricName if len(settings.Namespace) > 0 { name = settings.Namespace + "_" + name } diff --git a/pkg/translator/prometheusremotewrite/helper_test.go b/pkg/translator/prometheusremotewrite/helper_test.go index d0158e5204d8..5a09c418f71b 100644 --- a/pkg/translator/prometheusremotewrite/helper_test.go +++ b/pkg/translator/prometheusremotewrite/helper_test.go @@ -20,6 +20,7 @@ import ( conventions "go.opentelemetry.io/collector/semconv/v1.6.1" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata" + prometheustranslator "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus" ) func Test_isValidAggregationTemporality(t *testing.T) { @@ -462,7 +463,7 @@ func Test_getPromExemplars(t *testing.T) { { Value: floatVal1, Timestamp: timestamp.FromTime(tnow), - Labels: []prompb.Label{getLabel(traceIDKey, traceIDValue1), getLabel(spanIDKey, spanIDValue1), getLabel(label11, value11)}, + Labels: []prompb.Label{getLabel(prometheustranslator.ExemplarTraceIDKey, traceIDValue1), getLabel(prometheustranslator.ExemplarSpanIDKey, spanIDValue1), getLabel(label11, value11)}, }, }, }, @@ -505,7 +506,7 @@ func Test_getPromExemplars(t *testing.T) { { Value: floatVal1, Timestamp: timestamp.FromTime(tnow), - Labels: []prompb.Label{getLabel(traceIDKey, traceIDValue1), getLabel(spanIDKey, spanIDValue1)}, + Labels: []prompb.Label{getLabel(prometheustranslator.ExemplarTraceIDKey, traceIDValue1), getLabel(prometheustranslator.ExemplarSpanIDKey, spanIDValue1)}, }, }, }, diff --git a/pkg/translator/prometheusremotewrite/testutils_test.go b/pkg/translator/prometheusremotewrite/testutils_test.go index f0fa3df45358..49ef7a735081 100644 --- a/pkg/translator/prometheusremotewrite/testutils_test.go +++ b/pkg/translator/prometheusremotewrite/testutils_test.go @@ -14,6 +14,8 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" + + prometheustranslator "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus" ) var ( @@ -170,7 +172,7 @@ func getExemplar(v float64, t int64) prompb.Exemplar { return prompb.Exemplar{ Value: v, Timestamp: t, - Labels: []prompb.Label{getLabel(traceIDKey, traceIDValue1)}, + Labels: []prompb.Label{getLabel(prometheustranslator.ExemplarTraceIDKey, traceIDValue1)}, } } diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index cae8487e474a..1dc1b1432d9e 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -22,11 +22,6 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus" ) -const ( - traceIDKey = "trace_id" - spanIDKey = "span_id" -) - type metricFamily struct { mtype pmetric.MetricType // isMonotonic only applies to sums @@ -545,7 +540,7 @@ func convertExemplar(pe exemplar.Exemplar, e pmetric.Exemplar) { e.FilteredAttributes().EnsureCapacity(pe.Labels.Len()) pe.Labels.Range(func(lb labels.Label) { switch strings.ToLower(lb.Name) { - case traceIDKey: + case prometheus.ExemplarTraceIDKey: var tid [16]byte err := decodeAndCopyToLowerBytes(tid[:], []byte(lb.Value)) if err == nil { @@ -553,7 +548,7 @@ func convertExemplar(pe exemplar.Exemplar, e pmetric.Exemplar) { } else { e.FilteredAttributes().PutStr(lb.Name, lb.Value) } - case spanIDKey: + case prometheus.ExemplarSpanIDKey: var sid [8]byte err := decodeAndCopyToLowerBytes(sid[:], []byte(lb.Value)) if err == nil { diff --git a/receiver/prometheusreceiver/internal/transaction.go b/receiver/prometheusreceiver/internal/transaction.go index dc1bf78dba9e..4b29a5cd4534 100644 --- a/receiver/prometheusreceiver/internal/transaction.go +++ b/receiver/prometheusreceiver/internal/transaction.go @@ -24,16 +24,12 @@ import ( "go.opentelemetry.io/collector/receiver" "go.opentelemetry.io/collector/receiver/receiverhelper" "go.uber.org/zap" -) -const ( - targetMetricName = "target_info" - scopeMetricName = "otel_scope_info" - scopeNameLabel = "otel_scope_name" - scopeVersionLabel = "otel_scope_version" - receiverName = "otelcol/prometheusreceiver" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus" ) +const receiverName = "otelcol/prometheusreceiver" + type transaction struct { isNew bool trimSuffixes bool @@ -138,13 +134,13 @@ func (t *transaction) Append(_ storage.SeriesRef, ls labels.Labels, atMs int64, } // For the `target_info` metric we need to convert it to resource attributes. - if metricName == targetMetricName { + if metricName == prometheus.TargetInfoMetricName { t.AddTargetInfo(ls) return 0, nil } // For the `otel_scope_info` metric we need to convert it to scope attributes. - if metricName == scopeMetricName { + if metricName == prometheus.ScopeInfoMetricName { t.addScopeInfo(ls) return 0, nil } @@ -349,10 +345,10 @@ func (t *transaction) getMetrics(resource pcommon.Resource) (pmetric.Metrics, er func getScopeID(ls labels.Labels) scopeID { var scope scopeID ls.Range(func(lbl labels.Label) { - if lbl.Name == scopeNameLabel { + if lbl.Name == prometheus.ScopeNameLabelKey { scope.name = lbl.Value } - if lbl.Name == scopeVersionLabel { + if lbl.Name == prometheus.ScopeVersionLabelKey { scope.version = lbl.Value } }) @@ -431,11 +427,11 @@ func (t *transaction) addScopeInfo(ls labels.Labels) { if lbl.Name == model.JobLabel || lbl.Name == model.InstanceLabel || lbl.Name == model.MetricNameLabel { return } - if lbl.Name == scopeNameLabel { + if lbl.Name == prometheus.ScopeNameLabelKey { scope.name = lbl.Value return } - if lbl.Name == scopeVersionLabel { + if lbl.Name == prometheus.ScopeVersionLabelKey { scope.version = lbl.Value return }