OTLP translate: keep identifying attributes in target_info

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
This commit is contained in:
György Krajcsovits 2024-11-25 09:27:50 +01:00
parent 872e2db2a9
commit a53b48a912
4 changed files with 6 additions and 12 deletions

View file

@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"log" "log"
"math" "math"
"slices"
"sort" "sort"
"strconv" "strconv"
"unicode/utf8" "unicode/utf8"
@ -117,7 +116,7 @@ var seps = []byte{'\xff'}
// if logOnOverwrite is true, the overwrite is logged. Resulting label names are sanitized. // if logOnOverwrite is true, the overwrite is logged. Resulting label names are sanitized.
// If settings.PromoteResourceAttributes is not empty, it's a set of resource attributes that should be promoted to labels. // If settings.PromoteResourceAttributes is not empty, it's a set of resource attributes that should be promoted to labels.
func createAttributes(resource pcommon.Resource, attributes pcommon.Map, settings Settings, func createAttributes(resource pcommon.Resource, attributes pcommon.Map, settings Settings,
ignoreAttrs []string, logOnOverwrite bool, extras ...string) []prompb.Label { logOnOverwrite bool, extras ...string) []prompb.Label {
resourceAttrs := resource.Attributes() resourceAttrs := resource.Attributes()
serviceName, haveServiceName := resourceAttrs.Get(conventions.AttributeServiceName) serviceName, haveServiceName := resourceAttrs.Get(conventions.AttributeServiceName)
instance, haveInstanceID := resourceAttrs.Get(conventions.AttributeServiceInstanceID) instance, haveInstanceID := resourceAttrs.Get(conventions.AttributeServiceInstanceID)
@ -147,9 +146,7 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, setting
// XXX: Should we always drop service namespace/service name/service instance ID from the labels // XXX: Should we always drop service namespace/service name/service instance ID from the labels
// (as they get mapped to other Prometheus labels)? // (as they get mapped to other Prometheus labels)?
attributes.Range(func(key string, value pcommon.Value) bool { attributes.Range(func(key string, value pcommon.Value) bool {
if !slices.Contains(ignoreAttrs, key) { labels = append(labels, prompb.Label{Name: key, Value: value.AsString()})
labels = append(labels, prompb.Label{Name: key, Value: value.AsString()})
}
return true return true
}) })
sort.Stable(ByLabelName(labels)) sort.Stable(ByLabelName(labels))
@ -251,7 +248,7 @@ func (c *PrometheusConverter) addHistogramDataPoints(ctx context.Context, dataPo
pt := dataPoints.At(x) pt := dataPoints.At(x)
timestamp := convertTimeStamp(pt.Timestamp()) timestamp := convertTimeStamp(pt.Timestamp())
baseLabels := createAttributes(resource, pt.Attributes(), settings, nil, false) baseLabels := createAttributes(resource, pt.Attributes(), settings, false)
// If the sum is unset, it indicates the _sum metric point should be // If the sum is unset, it indicates the _sum metric point should be
// omitted // omitted
@ -451,7 +448,7 @@ func (c *PrometheusConverter) addSummaryDataPoints(ctx context.Context, dataPoin
pt := dataPoints.At(x) pt := dataPoints.At(x)
timestamp := convertTimeStamp(pt.Timestamp()) timestamp := convertTimeStamp(pt.Timestamp())
baseLabels := createAttributes(resource, pt.Attributes(), settings, nil, false) baseLabels := createAttributes(resource, pt.Attributes(), settings, false)
// treat sum as a sample in an individual TimeSeries // treat sum as a sample in an individual TimeSeries
sum := &prompb.Sample{ sum := &prompb.Sample{
@ -600,7 +597,7 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta
} }
settings.PromoteResourceAttributes = nil settings.PromoteResourceAttributes = nil
labels := createAttributes(resource, attributes, settings, identifyingAttrs, false, model.MetricNameLabel, name) labels := createAttributes(resource, attributes, settings, false, model.MetricNameLabel, name)
haveIdentifier := false haveIdentifier := false
for _, l := range labels { for _, l := range labels {
if l.Name == model.JobLabel || l.Name == model.InstanceLabel { if l.Name == model.JobLabel || l.Name == model.InstanceLabel {

View file

@ -161,7 +161,7 @@ func TestCreateAttributes(t *testing.T) {
settings := Settings{ settings := Settings{
PromoteResourceAttributes: tc.promoteResourceAttributes, PromoteResourceAttributes: tc.promoteResourceAttributes,
} }
lbls := createAttributes(resource, attrs, settings, nil, false, model.MetricNameLabel, "test_metric") lbls := createAttributes(resource, attrs, settings, false, model.MetricNameLabel, "test_metric")
assert.ElementsMatch(t, lbls, tc.expectedLabels) assert.ElementsMatch(t, lbls, tc.expectedLabels)
}) })

View file

@ -54,7 +54,6 @@ func (c *PrometheusConverter) addExponentialHistogramDataPoints(ctx context.Cont
resource, resource,
pt.Attributes(), pt.Attributes(),
settings, settings,
nil,
true, true,
model.MetricNameLabel, model.MetricNameLabel,
promName, promName,

View file

@ -40,7 +40,6 @@ func (c *PrometheusConverter) addGaugeNumberDataPoints(ctx context.Context, data
resource, resource,
pt.Attributes(), pt.Attributes(),
settings, settings,
nil,
true, true,
model.MetricNameLabel, model.MetricNameLabel,
name, name,
@ -76,7 +75,6 @@ func (c *PrometheusConverter) addSumNumberDataPoints(ctx context.Context, dataPo
resource, resource,
pt.Attributes(), pt.Attributes(),
settings, settings,
nil,
true, true,
model.MetricNameLabel, model.MetricNameLabel,
name, name,