mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Merge pull request #15448 from prometheus/no-ignore-identifying-attributes
OTLP translate: optionally keep identifying attributes in target_info
This commit is contained in:
commit
92fdf65187
|
@ -1429,8 +1429,9 @@ var (
|
||||||
|
|
||||||
// OTLPConfig is the configuration for writing to the OTLP endpoint.
|
// OTLPConfig is the configuration for writing to the OTLP endpoint.
|
||||||
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.
|
||||||
|
|
|
@ -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")
|
||||||
|
|
2
config/testdata/otlp_allow_keep_identifying_resource_attributes.good.yml
vendored
Normal file
2
config/testdata/otlp_allow_keep_identifying_resource_attributes.good.yml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
otlp:
|
||||||
|
keep_identifying_resource_attributes: true
|
|
@ -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:
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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)
|
||||||
})
|
})
|
||||||
|
|
|
@ -32,13 +32,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
Namespace string
|
Namespace string
|
||||||
ExternalLabels map[string]string
|
ExternalLabels map[string]string
|
||||||
DisableTargetInfo bool
|
DisableTargetInfo bool
|
||||||
ExportCreatedMetric bool
|
ExportCreatedMetric bool
|
||||||
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.
|
||||||
|
|
|
@ -28,41 +28,72 @@ 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} {
|
||||||
converter := NewPrometheusConverter()
|
t.Run(fmt.Sprintf("successful/keepIdentifyingAttributes=%v", keepIdentifyingResourceAttributes), func(t *testing.T) {
|
||||||
payload := createExportRequest(5, 128, 128, 2, 0)
|
converter := NewPrometheusConverter()
|
||||||
var expMetadata []prompb.MetricMetadata
|
payload := createExportRequest(5, 128, 128, 2, 0)
|
||||||
resourceMetricsSlice := payload.Metrics().ResourceMetrics()
|
var expMetadata []prompb.MetricMetadata
|
||||||
for i := 0; i < resourceMetricsSlice.Len(); i++ {
|
resourceMetricsSlice := payload.Metrics().ResourceMetrics()
|
||||||
scopeMetricsSlice := resourceMetricsSlice.At(i).ScopeMetrics()
|
for i := 0; i < resourceMetricsSlice.Len(); i++ {
|
||||||
for j := 0; j < scopeMetricsSlice.Len(); j++ {
|
scopeMetricsSlice := resourceMetricsSlice.At(i).ScopeMetrics()
|
||||||
metricSlice := scopeMetricsSlice.At(j).Metrics()
|
for j := 0; j < scopeMetricsSlice.Len(); j++ {
|
||||||
for k := 0; k < metricSlice.Len(); k++ {
|
metricSlice := scopeMetricsSlice.At(j).Metrics()
|
||||||
metric := metricSlice.At(k)
|
for k := 0; k < metricSlice.Len(); k++ {
|
||||||
promName := prometheustranslator.BuildCompliantName(metric, "", false, false)
|
metric := metricSlice.At(k)
|
||||||
expMetadata = append(expMetadata, prompb.MetricMetadata{
|
promName := prometheustranslator.BuildCompliantName(metric, "", false, false)
|
||||||
Type: otelMetricTypeToPromMetricType(metric),
|
expMetadata = append(expMetadata, prompb.MetricMetadata{
|
||||||
MetricFamilyName: promName,
|
Type: otelMetricTypeToPromMetricType(metric),
|
||||||
Help: metric.Description(),
|
MetricFamilyName: promName,
|
||||||
Unit: metric.Unit(),
|
Help: metric.Description(),
|
||||||
})
|
Unit: metric.Unit(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
annots, err := converter.FromMetrics(context.Background(), payload.Metrics(), Settings{})
|
annots, err := converter.FromMetrics(
|
||||||
require.NoError(t, err)
|
context.Background(),
|
||||||
require.Empty(t, annots)
|
payload.Metrics(),
|
||||||
|
Settings{KeepIdentifyingResourceAttributes: keepIdentifyingResourceAttributes},
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
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())
|
||||||
|
|
||||||
|
|
|
@ -512,9 +512,10 @@ func (h *otlpWriteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
converter := otlptranslator.NewPrometheusConverter()
|
converter := otlptranslator.NewPrometheusConverter()
|
||||||
annots, err := converter.FromMetrics(r.Context(), req.Metrics(), otlptranslator.Settings{
|
annots, err := converter.FromMetrics(r.Context(), req.Metrics(), otlptranslator.Settings{
|
||||||
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)
|
||||||
|
|
Loading…
Reference in a new issue