Skip to content

Commit 7fc400f

Browse files
committed
Skip attaching unit and type label if these exist
Signed-off-by: SungJin1212 <[email protected]>
1 parent 56bc703 commit 7fc400f

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [FEATURE] StoreGateway: Introduces a new parquet mode. #7046
66
* [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
7+
* [ENHANCEMENT] Distributor: Skip attaching `__unit__` and `__type__` labels when `-distributor.enable-type-and-unit-labels` is enabled, as these are appended from metadata. #7145
78
* [ENHANCEMENT] StoreGateway: Add tracings to parquet mode. #7125
89
* [ENHANCEMENT] Alertmanager: Upgrade alertmanger to 0.29.0 and add a new incidentIO integration. #7092
910
* [ENHANCEMENT] Querier: Add a `-querier.parquet-queryable-shard-cache-ttl` flag to add TTL to parquet shard cache. #7098

pkg/util/push/push.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/go-kit/log/level"
1010
"github.com/prometheus/client_golang/exp/api/remote"
11+
"github.com/prometheus/common/model"
1112
"github.com/prometheus/prometheus/model/labels"
1213
"github.com/prometheus/prometheus/schema"
1314
"github.com/prometheus/prometheus/util/compression"
@@ -202,7 +203,11 @@ func convertV2RequestToV1(req *cortexpb.PreallocWriteRequestV2, enableTypeAndUni
202203
if shouldAttachTypeAndUnitLabels {
203204
slb := labels.NewScratchBuilder(lbs.Len() + 2) // for __type__ and __unit__
204205
lbs.Range(func(l labels.Label) {
205-
slb.Add(l.Name, l.Value)
206+
// Skip __type__ and __unit__ labels to prevent duplication,
207+
// We append these labels from metadata.
208+
if l.Name != model.MetricTypeLabel && l.Name != model.MetricUnitLabel {
209+
slb.Add(l.Name, l.Value)
210+
}
206211
})
207212
schema.Metadata{Type: cortexpb.MetadataV2MetricTypeToMetricType(metricType), Unit: unit}.AddToLabels(&slb)
208213
slb.Sort()

pkg/util/push/push_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func Benchmark_convertV2RequestToV1(b *testing.B) {
173173
}
174174

175175
func Test_convertV2RequestToV1_WithEnableTypeAndUnitLabels(t *testing.T) {
176-
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"}
176+
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"}
177177
samples := []cortexpb.Sample{
178178
{
179179
Value: 123,
@@ -233,6 +233,52 @@ func Test_convertV2RequestToV1_WithEnableTypeAndUnitLabels(t *testing.T) {
233233
},
234234
enableTypeAndUnitLabels: true,
235235
},
236+
{
237+
desc: "should be added from metadata when __type__ and __unit__ labels already exist.",
238+
v2Req: &cortexpb.PreallocWriteRequestV2{
239+
WriteRequestV2: cortexpb.WriteRequestV2{
240+
Symbols: symbols,
241+
Timeseries: []cortexpb.PreallocTimeseriesV2{
242+
{
243+
TimeSeriesV2: &cortexpb.TimeSeriesV2{
244+
LabelsRefs: []uint32{1, 2, 3, 4, 18, 19, 20, 21},
245+
Samples: samples,
246+
Metadata: cortexpb.MetadataV2{Type: cortexpb.METRIC_TYPE_COUNTER, HelpRef: 15, UnitRef: 16},
247+
Exemplars: []cortexpb.ExemplarV2{{LabelsRefs: []uint32{11, 12}, Value: 1, Timestamp: 1}},
248+
},
249+
},
250+
},
251+
},
252+
},
253+
expectedV1Req: cortexpb.PreallocWriteRequest{
254+
WriteRequest: cortexpb.WriteRequest{
255+
Timeseries: []cortexpb.PreallocTimeseries{
256+
{
257+
TimeSeries: &cortexpb.TimeSeries{
258+
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("__name__", "test_metric1", "__type__", "counter", "__unit__", "Maybe op/sec who knows (:", "b", "c")),
259+
Samples: samples,
260+
Exemplars: []cortexpb.Exemplar{
261+
{
262+
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromStrings("f", "g")),
263+
Value: 1,
264+
TimestampMs: 1,
265+
},
266+
},
267+
},
268+
},
269+
},
270+
Metadata: []*cortexpb.MetricMetadata{
271+
{
272+
Type: cortexpb.COUNTER,
273+
MetricFamilyName: "test_metric1",
274+
Help: "Test gauge for test purposes",
275+
Unit: "Maybe op/sec who knows (:",
276+
},
277+
},
278+
},
279+
},
280+
enableTypeAndUnitLabels: true,
281+
},
236282
{
237283
desc: "should not attach unit and type labels when the enableTypeAndUnitLabels is false",
238284
v2Req: &cortexpb.PreallocWriteRequestV2{

0 commit comments

Comments
 (0)