diff --git a/CHANGELOG.md b/CHANGELOG.md index 6232398eacc..5ad2969a54b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * [FEATURE] StoreGateway: Introduces a new parquet mode. #7046 * [FEATURE] Distributor: Add a per-tenant flag `-distributor.enable-type-and-unit-labels` that enables adding `__unit__` and `__type__` labels for remote write v2 and OTLP requests. This is a breaking change; the `-distributor.otlp.enable-type-and-unit-labels` flag is now deprecated, operates as a no-op, and has been consolidated into this new flag. #7077 +* [ENHANCEMENT] Distributor: Skip attaching `__unit__` and `__type__` labels when `-distributor.enable-type-and-unit-labels` is enabled, as these are appended from metadata. #7145 * [ENHANCEMENT] StoreGateway: Add tracings to parquet mode. #7125 * [ENHANCEMENT] Alertmanager: Upgrade alertmanger to 0.29.0 and add a new incidentIO integration. #7092 * [ENHANCEMENT] Querier: Add a `-querier.parquet-queryable-shard-cache-ttl` flag to add TTL to parquet shard cache. #7098 diff --git a/pkg/util/push/push.go b/pkg/util/push/push.go index 7534b63f342..d15c5e51e2b 100644 --- a/pkg/util/push/push.go +++ b/pkg/util/push/push.go @@ -8,6 +8,7 @@ import ( "github.com/go-kit/log/level" "github.com/prometheus/client_golang/exp/api/remote" + "github.com/prometheus/common/model" "github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/schema" "github.com/prometheus/prometheus/util/compression" @@ -202,7 +203,11 @@ func convertV2RequestToV1(req *cortexpb.PreallocWriteRequestV2, enableTypeAndUni if shouldAttachTypeAndUnitLabels { slb := labels.NewScratchBuilder(lbs.Len() + 2) // for __type__ and __unit__ lbs.Range(func(l labels.Label) { - slb.Add(l.Name, l.Value) + // Skip __type__ and __unit__ labels to prevent duplication, + // We append these labels from metadata. + if l.Name != model.MetricTypeLabel && l.Name != model.MetricUnitLabel { + slb.Add(l.Name, l.Value) + } }) schema.Metadata{Type: cortexpb.MetadataV2MetricTypeToMetricType(metricType), Unit: unit}.AddToLabels(&slb) slb.Sort() diff --git a/pkg/util/push/push_test.go b/pkg/util/push/push_test.go index 5b31f3544eb..bf21863eea4 100644 --- a/pkg/util/push/push_test.go +++ b/pkg/util/push/push_test.go @@ -173,7 +173,7 @@ func Benchmark_convertV2RequestToV1(b *testing.B) { } func Test_convertV2RequestToV1_WithEnableTypeAndUnitLabels(t *testing.T) { - symbols := []string{"", "__name__", "test_metric1", "b", "c", "baz", "qux", "d", "e", "foo", "bar", "f", "g", "h", "i", "Test gauge for test purposes", "Maybe op/sec who knows (:", "Test counter for test purposes"} + symbols := []string{"", "__name__", "test_metric1", "b", "c", "baz", "qux", "d", "e", "foo", "bar", "f", "g", "h", "i", "Test gauge for test purposes", "Maybe op/sec who knows (:", "Test counter for test purposes", "__type__", "exist type", "__unit__", "exist unit"} samples := []cortexpb.Sample{ { Value: 123, @@ -233,6 +233,52 @@ func Test_convertV2RequestToV1_WithEnableTypeAndUnitLabels(t *testing.T) { }, enableTypeAndUnitLabels: true, }, + { + desc: "should be added from metadata when __type__ and __unit__ labels already exist.", + v2Req: &cortexpb.PreallocWriteRequestV2{ + WriteRequestV2: cortexpb.WriteRequestV2{ + Symbols: symbols, + Timeseries: []cortexpb.PreallocTimeseriesV2{ + { + TimeSeriesV2: &cortexpb.TimeSeriesV2{ + LabelsRefs: []uint32{1, 2, 3, 4, 18, 19, 20, 21}, + Samples: samples, + Metadata: cortexpb.MetadataV2{Type: cortexpb.METRIC_TYPE_COUNTER, HelpRef: 15, UnitRef: 16}, + Exemplars: []cortexpb.ExemplarV2{{LabelsRefs: []uint32{11, 12}, Value: 1, Timestamp: 1}}, + }, + }, + }, + }, + }, + expectedV1Req: cortexpb.PreallocWriteRequest{ + WriteRequest: cortexpb.WriteRequest{ + Timeseries: []cortexpb.PreallocTimeseries{ + { + TimeSeries: &cortexpb.TimeSeries{ + Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("__name__", "test_metric1", "__type__", "counter", "__unit__", "Maybe op/sec who knows (:", "b", "c")), + Samples: samples, + Exemplars: []cortexpb.Exemplar{ + { + Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("f", "g")), + Value: 1, + TimestampMs: 1, + }, + }, + }, + }, + }, + Metadata: []*cortexpb.MetricMetadata{ + { + Type: cortexpb.COUNTER, + MetricFamilyName: "test_metric1", + Help: "Test gauge for test purposes", + Unit: "Maybe op/sec who knows (:", + }, + }, + }, + }, + enableTypeAndUnitLabels: true, + }, { desc: "should not attach unit and type labels when the enableTypeAndUnitLabels is false", v2Req: &cortexpb.PreallocWriteRequestV2{