Merge pull request #15448 from prometheus/no-ignore-identifying-attributes

OTLP translate: optionally keep identifying attributes in target_info
This commit is contained in:
George Krajcsovits 2024-12-02 17:33:48 +01:00 committed by GitHub
commit 92fdf65187
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 147 additions and 39 deletions

View file

@ -1431,6 +1431,7 @@ var (
type OTLPConfig struct { type OTLPConfig struct {
PromoteResourceAttributes []string `yaml:"promote_resource_attributes,omitempty"` PromoteResourceAttributes []string `yaml:"promote_resource_attributes,omitempty"`
TranslationStrategy translationStrategyOption `yaml:"translation_strategy,omitempty"` TranslationStrategy translationStrategyOption `yaml:"translation_strategy,omitempty"`
KeepIdentifyingResourceAttributes bool `yaml:"keep_identifying_resource_attributes,omitempty"`
} }
// UnmarshalYAML implements the yaml.Unmarshaler interface. // UnmarshalYAML implements the yaml.Unmarshaler interface.

View file

@ -1554,6 +1554,20 @@ func TestOTLPSanitizeResourceAttributes(t *testing.T) {
}) })
} }
func TestOTLPAllowServiceNameInTargetInfo(t *testing.T) {
t.Run("good config", func(t *testing.T) {
want, err := LoadFile(filepath.Join("testdata", "otlp_allow_keep_identifying_resource_attributes.good.yml"), false, promslog.NewNopLogger())
require.NoError(t, err)
out, err := yaml.Marshal(want)
require.NoError(t, err)
var got Config
require.NoError(t, yaml.UnmarshalStrict(out, &got))
require.True(t, got.OTLPConfig.KeepIdentifyingResourceAttributes)
})
}
func TestOTLPAllowUTF8(t *testing.T) { func TestOTLPAllowUTF8(t *testing.T) {
t.Run("good config", func(t *testing.T) { t.Run("good config", func(t *testing.T) {
fpath := filepath.Join("testdata", "otlp_allow_utf8.good.yml") fpath := filepath.Join("testdata", "otlp_allow_utf8.good.yml")

View file

@ -0,0 +1,2 @@
otlp:
keep_identifying_resource_attributes: true

View file

@ -182,6 +182,10 @@ otlp:
# It preserves all special characters like dots, but it still add required suffixes # It preserves all special characters like dots, but it still add required suffixes
# for units and _total like in UnderscoreEscapingWithSuffixes. # for units and _total like in UnderscoreEscapingWithSuffixes.
[ translation_strategy: <string> | default = "UnderscoreEscapingWithSuffixes" ] [ translation_strategy: <string> | default = "UnderscoreEscapingWithSuffixes" ]
# Enables adding "service.name", "service.namespace" and "service.instance.id"
# resource attributes to the "target_info" metric, on top of converting
# them into the "instance" and "job" labels.
[ keep_identifying_resource_attributes: <boolean> | default = false]
# Settings related to the remote read feature. # Settings related to the remote read feature.
remote_read: remote_read:

View file

@ -600,6 +600,10 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta
} }
settings.PromoteResourceAttributes = nil settings.PromoteResourceAttributes = nil
if settings.KeepIdentifyingResourceAttributes {
// Do not pass identifying attributes as ignoreAttrs below.
identifyingAttrs = nil
}
labels := createAttributes(resource, attributes, settings, identifyingAttrs, false, model.MetricNameLabel, name) labels := createAttributes(resource, attributes, settings, identifyingAttrs, false, model.MetricNameLabel, name)
haveIdentifier := false haveIdentifier := false
for _, l := range labels { for _, l := range labels {

View file

@ -49,15 +49,44 @@ func TestCreateAttributes(t *testing.T) {
} }
attrs := pcommon.NewMap() attrs := pcommon.NewMap()
attrs.PutStr("metric-attr", "metric value") attrs.PutStr("metric-attr", "metric value")
attrs.PutStr("metric-attr-other", "metric value other")
testCases := []struct { testCases := []struct {
name string name string
promoteResourceAttributes []string promoteResourceAttributes []string
ignoreAttrs []string
expectedLabels []prompb.Label expectedLabels []prompb.Label
}{ }{
{ {
name: "Successful conversion without resource attribute promotion", name: "Successful conversion without resource attribute promotion",
promoteResourceAttributes: nil, promoteResourceAttributes: nil,
expectedLabels: []prompb.Label{
{
Name: "__name__",
Value: "test_metric",
},
{
Name: "instance",
Value: "service ID",
},
{
Name: "job",
Value: "service name",
},
{
Name: "metric_attr",
Value: "metric value",
},
{
Name: "metric_attr_other",
Value: "metric value other",
},
},
},
{
name: "Successful conversion with some attributes ignored",
promoteResourceAttributes: nil,
ignoreAttrs: []string{"metric-attr-other"},
expectedLabels: []prompb.Label{ expectedLabels: []prompb.Label{
{ {
Name: "__name__", Name: "__name__",
@ -97,6 +126,10 @@ func TestCreateAttributes(t *testing.T) {
Name: "metric_attr", Name: "metric_attr",
Value: "metric value", Value: "metric value",
}, },
{
Name: "metric_attr_other",
Value: "metric value other",
},
{ {
Name: "existent_attr", Name: "existent_attr",
Value: "resource value", Value: "resource value",
@ -127,6 +160,10 @@ func TestCreateAttributes(t *testing.T) {
Name: "metric_attr", Name: "metric_attr",
Value: "metric value", Value: "metric value",
}, },
{
Name: "metric_attr_other",
Value: "metric value other",
},
}, },
}, },
{ {
@ -153,6 +190,10 @@ func TestCreateAttributes(t *testing.T) {
Name: "metric_attr", Name: "metric_attr",
Value: "metric value", Value: "metric value",
}, },
{
Name: "metric_attr_other",
Value: "metric value other",
},
}, },
}, },
} }
@ -161,7 +202,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, tc.ignoreAttrs, false, model.MetricNameLabel, "test_metric")
assert.ElementsMatch(t, lbls, tc.expectedLabels) assert.ElementsMatch(t, lbls, tc.expectedLabels)
}) })

View file

@ -39,6 +39,7 @@ type Settings struct {
AddMetricSuffixes bool AddMetricSuffixes bool
AllowUTF8 bool AllowUTF8 bool
PromoteResourceAttributes []string PromoteResourceAttributes []string
KeepIdentifyingResourceAttributes bool
} }
// PrometheusConverter converts from OTel write format to Prometheus remote write format. // PrometheusConverter converts from OTel write format to Prometheus remote write format.

View file

@ -28,12 +28,14 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" "go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/prompb"
prometheustranslator "github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheus" prometheustranslator "github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheus"
) )
func TestFromMetrics(t *testing.T) { func TestFromMetrics(t *testing.T) {
t.Run("successful", func(t *testing.T) { for _, keepIdentifyingResourceAttributes := range []bool{false, true} {
t.Run(fmt.Sprintf("successful/keepIdentifyingAttributes=%v", keepIdentifyingResourceAttributes), func(t *testing.T) {
converter := NewPrometheusConverter() converter := NewPrometheusConverter()
payload := createExportRequest(5, 128, 128, 2, 0) payload := createExportRequest(5, 128, 128, 2, 0)
var expMetadata []prompb.MetricMetadata var expMetadata []prompb.MetricMetadata
@ -55,14 +57,43 @@ func TestFromMetrics(t *testing.T) {
} }
} }
annots, err := converter.FromMetrics(context.Background(), payload.Metrics(), Settings{}) annots, err := converter.FromMetrics(
context.Background(),
payload.Metrics(),
Settings{KeepIdentifyingResourceAttributes: keepIdentifyingResourceAttributes},
)
require.NoError(t, err) require.NoError(t, err)
require.Empty(t, annots) require.Empty(t, annots)
if diff := cmp.Diff(expMetadata, converter.Metadata()); diff != "" { if diff := cmp.Diff(expMetadata, converter.Metadata()); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff) t.Errorf("mismatch (-want +got):\n%s", diff)
} }
ts := converter.TimeSeries()
require.Len(t, ts, 1408+1) // +1 for the target_info.
target_info_count := 0
for _, s := range ts {
b := labels.NewScratchBuilder(2)
lbls := s.ToLabels(&b, nil)
if lbls.Get(labels.MetricName) == "target_info" {
target_info_count++
require.Equal(t, "test-namespace/test-service", lbls.Get("job"))
require.Equal(t, "id1234", lbls.Get("instance"))
if keepIdentifyingResourceAttributes {
require.Equal(t, "test-service", lbls.Get("service_name"))
require.Equal(t, "test-namespace", lbls.Get("service_namespace"))
require.Equal(t, "id1234", lbls.Get("service_instance_id"))
} else {
require.False(t, lbls.Has("service_name"))
require.False(t, lbls.Has("service_namespace"))
require.False(t, lbls.Has("service_instance_id"))
}
}
}
require.Equal(t, 1, target_info_count)
}) })
}
t.Run("context cancellation", func(t *testing.T) { t.Run("context cancellation", func(t *testing.T) {
converter := NewPrometheusConverter() converter := NewPrometheusConverter()
@ -169,6 +200,15 @@ func createExportRequest(resourceAttributeCount, histogramCount, nonHistogramCou
rm := request.Metrics().ResourceMetrics().AppendEmpty() rm := request.Metrics().ResourceMetrics().AppendEmpty()
generateAttributes(rm.Resource().Attributes(), "resource", resourceAttributeCount) generateAttributes(rm.Resource().Attributes(), "resource", resourceAttributeCount)
// Fake some resource attributes.
for k, v := range map[string]string{
"service.name": "test-service",
"service.namespace": "test-namespace",
"service.instance.id": "id1234",
} {
rm.Resource().Attributes().PutStr(k, v)
}
metrics := rm.ScopeMetrics().AppendEmpty().Metrics() metrics := rm.ScopeMetrics().AppendEmpty().Metrics()
ts := pcommon.NewTimestampFromTime(time.Now()) ts := pcommon.NewTimestampFromTime(time.Now())

View file

@ -515,6 +515,7 @@ func (h *otlpWriteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
AddMetricSuffixes: true, AddMetricSuffixes: true,
AllowUTF8: otlpCfg.TranslationStrategy == config.NoUTF8EscapingWithSuffixes, AllowUTF8: otlpCfg.TranslationStrategy == config.NoUTF8EscapingWithSuffixes,
PromoteResourceAttributes: otlpCfg.PromoteResourceAttributes, PromoteResourceAttributes: otlpCfg.PromoteResourceAttributes,
KeepIdentifyingResourceAttributes: otlpCfg.KeepIdentifyingResourceAttributes,
}) })
if err != nil { if err != nil {
h.logger.Warn("Error translating OTLP metrics to Prometheus write request", "err", err) h.logger.Warn("Error translating OTLP metrics to Prometheus write request", "err", err)