Skip to content

Commit

Permalink
chore(otel_test): rework test to prep for keep id resource attributes (
Browse files Browse the repository at this point in the history
…#10212)

* chore(otel_test): rework test to prep for keep id resource attributes

Rework the test to be more explicit and support testing for
prometheus/prometheus#15448
that's coming next.

Signed-off-by: György Krajcsovits <[email protected]>
  • Loading branch information
krajorama authored Dec 11, 2024
1 parent d598978 commit c7ac884
Showing 1 changed file with 72 additions and 31 deletions.
103 changes: 72 additions & 31 deletions pkg/distributor/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb"
prometheustranslator "github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand All @@ -50,6 +49,7 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
}, []string{tenantID, "group"})
resourceAttrs := map[string]string{
"service.name": "service name",
"service.namespace": "service namespace",
"service.instance.id": "service ID",
"existent-attr": "resource value",
// This one is for testing conflict with metric attribute.
Expand All @@ -59,32 +59,6 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
// This one is for testing conflict with auto-generated instance attribute.
"instance": "resource value",
}
expTargetInfoLabels := []mimirpb.LabelAdapter{
{
Name: labels.MetricName,
Value: "target_info",
},
}
for k, v := range resourceAttrs {
switch k {
case "service.name":
k = "job"
case "service.instance.id":
k = "instance"
case "job", "instance":
// Ignore, as these labels are generated from service.name and service.instance.id
continue
default:
k = prometheustranslator.NormalizeLabel(k, false)
}
expTargetInfoLabels = append(expTargetInfoLabels, mimirpb.LabelAdapter{
Name: k,
Value: v,
})
}
slices.SortStableFunc(expTargetInfoLabels, func(a, b mimirpb.LabelAdapter) int {
return strings.Compare(a.Name, b.Name)
})

md := pmetric.NewMetrics()
{
Expand All @@ -105,6 +79,7 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
name string
promoteResourceAttributes []string
expectedLabels []mimirpb.LabelAdapter
expectedInfoLabels []mimirpb.LabelAdapter
}{
{
name: "Successful conversion without resource attribute promotion",
Expand All @@ -120,13 +95,35 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
},
{
Name: "job",
Value: "service name",
Value: "service namespace/service name",
},
{
Name: "metric_attr",
Value: "metric value",
},
},
expectedInfoLabels: []mimirpb.LabelAdapter{
{
Name: "__name__",
Value: "target_info",
},
{
Name: "existent_attr",
Value: "resource value",
},
{
Name: "metric_attr",
Value: "resource value",
},
{
Name: "job",
Value: "service namespace/service name",
},
{
Name: "instance",
Value: "service ID",
},
},
},
{
name: "Successful conversion with resource attribute promotion",
Expand All @@ -142,7 +139,7 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
},
{
Name: "job",
Value: "service name",
Value: "service namespace/service name",
},
{
Name: "metric_attr",
Expand All @@ -153,6 +150,28 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
Value: "resource value",
},
},
expectedInfoLabels: []mimirpb.LabelAdapter{
{
Name: "__name__",
Value: "target_info",
},
{
Name: "existent_attr",
Value: "resource value",
},
{
Name: "metric_attr",
Value: "resource value",
},
{
Name: "job",
Value: "service namespace/service name",
},
{
Name: "instance",
Value: "service ID",
},
},
},
{
name: "Successful conversion with resource attribute promotion, conflicting resource attributes are ignored",
Expand All @@ -168,7 +187,7 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
},
{
Name: "job",
Value: "service name",
Value: "service namespace/service name",
},
{
Name: "existent_attr",
Expand All @@ -179,6 +198,28 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
Value: "metric value",
},
},
expectedInfoLabels: []mimirpb.LabelAdapter{
{
Name: "__name__",
Value: "target_info",
},
{
Name: "existent_attr",
Value: "resource value",
},
{
Name: "instance",
Value: "service ID",
},
{
Name: "job",
Value: "service namespace/service name",
},
{
Name: "metric_attr",
Value: "resource value",
},
},
},
}
for _, tc := range testCases {
Expand All @@ -205,7 +246,7 @@ func TestOTelMetricsToTimeSeries(t *testing.T) {
}

assert.ElementsMatch(t, ts.Labels, tc.expectedLabels)
assert.ElementsMatch(t, targetInfo.Labels, expTargetInfoLabels)
assert.ElementsMatch(t, targetInfo.Labels, tc.expectedInfoLabels)
})
}
}
Expand Down

0 comments on commit c7ac884

Please sign in to comment.