Skip to content

Commit

Permalink
chore: add constants for prometheus translation (open-telemetry#32830)
Browse files Browse the repository at this point in the history
**Description:**

This is a cleanup to consolidate the constants used for prometheus
translation in a single place.

---------

Co-authored-by: Curtis Robert <[email protected]>
  • Loading branch information
dashpole and crobert-1 authored May 8, 2024
1 parent 984a185 commit 1139675
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 41 deletions.
12 changes: 4 additions & 8 deletions exporter/prometheusexporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions pkg/translator/prometheus/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
15 changes: 5 additions & 10 deletions pkg/translator/prometheusremotewrite/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/translator/prometheusremotewrite/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)},
},
},
},
Expand Down Expand Up @@ -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)},
},
},
},
Expand Down
4 changes: 3 additions & 1 deletion pkg/translator/prometheusremotewrite/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)},
}
}

Expand Down
9 changes: 2 additions & 7 deletions receiver/prometheusreceiver/internal/metricfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -545,15 +540,15 @@ 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 {
e.SetTraceID(tid)
} 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 {
Expand Down
22 changes: 9 additions & 13 deletions receiver/prometheusreceiver/internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
})
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1139675

Please sign in to comment.