mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Make the behavior configurable
Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
This commit is contained in:
parent
a53b48a912
commit
ffabc04829
|
@ -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"`
|
||||||
|
ServiceNameInTargetInfo bool `yaml:"service_name_in_target_info,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_service_name_in_target_info.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.Equal(t, true, got.OTLPConfig.ServiceNameInTargetInfo)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
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_service_name_in_target_info.good.yml
vendored
Normal file
2
config/testdata/otlp_allow_service_name_in_target_info.good.yml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
otlp:
|
||||||
|
service_name_in_target_info: true
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
@ -116,7 +117,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,
|
||||||
logOnOverwrite bool, extras ...string) []prompb.Label {
|
ignoreAttrs []string, 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)
|
||||||
|
@ -146,7 +147,9 @@ 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 {
|
||||||
labels = append(labels, prompb.Label{Name: key, Value: value.AsString()})
|
if !slices.Contains(ignoreAttrs, key) {
|
||||||
|
labels = append(labels, prompb.Label{Name: key, Value: value.AsString()})
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
sort.Stable(ByLabelName(labels))
|
sort.Stable(ByLabelName(labels))
|
||||||
|
@ -248,7 +251,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, false)
|
baseLabels := createAttributes(resource, pt.Attributes(), settings, nil, 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
|
||||||
|
@ -448,7 +451,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, false)
|
baseLabels := createAttributes(resource, pt.Attributes(), settings, nil, 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{
|
||||||
|
@ -597,7 +600,11 @@ func addResourceTargetInfo(resource pcommon.Resource, settings Settings, timesta
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.PromoteResourceAttributes = nil
|
settings.PromoteResourceAttributes = nil
|
||||||
labels := createAttributes(resource, attributes, settings, false, model.MetricNameLabel, name)
|
if settings.ServiceNameInTargetInfo {
|
||||||
|
// Do not pass identifying attributes as ignoreAttrs below.
|
||||||
|
identifyingAttrs = nil
|
||||||
|
}
|
||||||
|
labels := createAttributes(resource, attributes, settings, identifyingAttrs, 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 {
|
||||||
|
|
|
@ -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, 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)
|
||||||
})
|
})
|
||||||
|
|
|
@ -54,6 +54,7 @@ func (c *PrometheusConverter) addExponentialHistogramDataPoints(ctx context.Cont
|
||||||
resource,
|
resource,
|
||||||
pt.Attributes(),
|
pt.Attributes(),
|
||||||
settings,
|
settings,
|
||||||
|
nil,
|
||||||
true,
|
true,
|
||||||
model.MetricNameLabel,
|
model.MetricNameLabel,
|
||||||
promName,
|
promName,
|
||||||
|
|
|
@ -39,6 +39,7 @@ type Settings struct {
|
||||||
AddMetricSuffixes bool
|
AddMetricSuffixes bool
|
||||||
AllowUTF8 bool
|
AllowUTF8 bool
|
||||||
PromoteResourceAttributes []string
|
PromoteResourceAttributes []string
|
||||||
|
ServiceNameInTargetInfo 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,73 @@ 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) {
|
|
||||||
converter := NewPrometheusConverter()
|
for _, serviceNameInTargetInfo := range []bool{false, true} {
|
||||||
payload := createExportRequest(5, 128, 128, 2, 0)
|
t.Run(fmt.Sprintf("successful/keepIdentifyingAttributes=%v", serviceNameInTargetInfo), func(t *testing.T) {
|
||||||
var expMetadata []prompb.MetricMetadata
|
converter := NewPrometheusConverter()
|
||||||
resourceMetricsSlice := payload.Metrics().ResourceMetrics()
|
payload := createExportRequest(5, 128, 128, 2, 0)
|
||||||
for i := 0; i < resourceMetricsSlice.Len(); i++ {
|
var expMetadata []prompb.MetricMetadata
|
||||||
scopeMetricsSlice := resourceMetricsSlice.At(i).ScopeMetrics()
|
resourceMetricsSlice := payload.Metrics().ResourceMetrics()
|
||||||
for j := 0; j < scopeMetricsSlice.Len(); j++ {
|
for i := 0; i < resourceMetricsSlice.Len(); i++ {
|
||||||
metricSlice := scopeMetricsSlice.At(j).Metrics()
|
scopeMetricsSlice := resourceMetricsSlice.At(i).ScopeMetrics()
|
||||||
for k := 0; k < metricSlice.Len(); k++ {
|
for j := 0; j < scopeMetricsSlice.Len(); j++ {
|
||||||
metric := metricSlice.At(k)
|
metricSlice := scopeMetricsSlice.At(j).Metrics()
|
||||||
promName := prometheustranslator.BuildCompliantName(metric, "", false, false)
|
for k := 0; k < metricSlice.Len(); k++ {
|
||||||
expMetadata = append(expMetadata, prompb.MetricMetadata{
|
metric := metricSlice.At(k)
|
||||||
Type: otelMetricTypeToPromMetricType(metric),
|
promName := prometheustranslator.BuildCompliantName(metric, "", false, false)
|
||||||
MetricFamilyName: promName,
|
expMetadata = append(expMetadata, prompb.MetricMetadata{
|
||||||
Help: metric.Description(),
|
Type: otelMetricTypeToPromMetricType(metric),
|
||||||
Unit: metric.Unit(),
|
MetricFamilyName: promName,
|
||||||
})
|
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{ServiceNameInTargetInfo: serviceNameInTargetInfo},
|
||||||
|
)
|
||||||
|
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 serviceNameInTargetInfo {
|
||||||
|
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 +201,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())
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ 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,
|
||||||
|
@ -75,6 +76,7 @@ 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,
|
||||||
|
|
|
@ -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,
|
||||||
|
ServiceNameInTargetInfo: otlpCfg.ServiceNameInTargetInfo,
|
||||||
})
|
})
|
||||||
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