Skip to content

Commit 3f8b8cc

Browse files
committed
Populate scope attributes from 'otel_scope_' labels. Deprecate otel_scope_info
Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 445a986 commit 3f8b8cc

File tree

5 files changed

+477
-53
lines changed

5 files changed

+477
-53
lines changed

receiver/prometheusreceiver/internal/metricfamily.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ func populateAttributes(mType pmetric.MetricType, ls labels.Labels, dest pcommon
360360
if j < len(names) && l.Name == names[j] {
361361
return
362362
}
363+
364+
// When removeScopeInfo feature gate is enabled, filter out otel_scope_ prefixed labels
365+
// as they are now extracted as scope attributes instead of datapoint attributes
366+
if RemoveScopeInfoGate.IsEnabled() && strings.HasPrefix(l.Name, "otel_scope_") {
367+
return
368+
}
369+
363370
if l.Value == "" {
364371
// empty label values should be omitted
365372
return

receiver/prometheusreceiver/internal/metricfamily_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package internal
55

66
import (
77
"math"
8+
"strings"
89
"testing"
910
"time"
1011

@@ -17,6 +18,8 @@ import (
1718
"go.opentelemetry.io/collector/pdata/pcommon"
1819
"go.opentelemetry.io/collector/pdata/pmetric"
1920
"go.uber.org/zap"
21+
22+
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
2023
)
2124

2225
type testMetadataStore map[string]scrape.MetricMetadata
@@ -773,6 +776,93 @@ func TestMetricGroupData_toSummaryUnitTest(t *testing.T) {
773776
}
774777
}
775778

779+
func TestPopulateAttributes_ScopeLabelFiltering(t *testing.T) {
780+
tests := []struct {
781+
name string
782+
labels labels.Labels
783+
featureEnabled bool
784+
wantAttrs map[string]string
785+
}{
786+
{
787+
name: "feature_disabled_keeps_scope_labels",
788+
labels: labels.FromStrings(
789+
"__name__", "test_metric",
790+
"job", "test-job",
791+
"instance", "localhost:8080",
792+
"method", "GET",
793+
"otel_scope_animal", "bear",
794+
"otel_scope_color", "blue",
795+
),
796+
featureEnabled: false,
797+
wantAttrs: map[string]string{
798+
"method": "GET",
799+
"otel_scope_animal": "bear",
800+
"otel_scope_color": "blue",
801+
},
802+
},
803+
{
804+
name: "feature_enabled_filters_scope_labels",
805+
labels: labels.FromStrings(
806+
"__name__", "test_metric",
807+
"job", "test-job",
808+
"instance", "localhost:8080",
809+
"method", "GET",
810+
"otel_scope_animal", "bear",
811+
"otel_scope_color", "blue",
812+
),
813+
featureEnabled: true,
814+
wantAttrs: map[string]string{
815+
"method": "GET",
816+
// otel_scope_* labels should be filtered out
817+
},
818+
},
819+
{
820+
name: "feature_enabled_keeps_standard_scope_labels_filtered",
821+
labels: labels.FromStrings(
822+
"__name__", "test_metric",
823+
"job", "test-job",
824+
"instance", "localhost:8080",
825+
"method", "GET",
826+
"otel_scope_name", "my-scope",
827+
"otel_scope_version", "1.0.0",
828+
"otel_scope_animal", "bear",
829+
),
830+
featureEnabled: true,
831+
wantAttrs: map[string]string{
832+
"method": "GET",
833+
// All otel_scope_* labels should be filtered out
834+
},
835+
},
836+
}
837+
838+
for _, tt := range tests {
839+
t.Run(tt.name, func(t *testing.T) {
840+
defer testutil.SetFeatureGateForTest(t, RemoveScopeInfoGate, tt.featureEnabled)()
841+
842+
attrs := pcommon.NewMap()
843+
populateAttributes(pmetric.MetricTypeGauge, tt.labels, attrs)
844+
845+
// Verify expected attributes
846+
require.Equal(t, len(tt.wantAttrs), attrs.Len(), "Unexpected number of attributes")
847+
848+
for key, expectedValue := range tt.wantAttrs {
849+
actualValue, exists := attrs.Get(key)
850+
require.True(t, exists, "Expected attribute %s not found", key)
851+
require.Equal(t, expectedValue, actualValue.AsString(), "Unexpected value for attribute %s", key)
852+
}
853+
854+
// Verify no otel_scope_ attributes when feature is enabled
855+
if tt.featureEnabled {
856+
attrs.Range(func(k string, _ pcommon.Value) bool {
857+
require.False(t, strings.HasPrefix(k, "otel_scope_"),
858+
"Found otel_scope_ prefixed attribute %s when feature gate is enabled", k)
859+
return true
860+
})
861+
}
862+
})
863+
}
864+
}
865+
776866
func TestMetricGroupData_toNumberDataUnitTest(t *testing.T) {
777867
type scrape struct {
778868
at int64

0 commit comments

Comments
 (0)