Skip to content

Commit b72d63f

Browse files
committed
Merge remote-tracking branch 'open-telemetry/main' into arve/xxhash
Signed-off-by: Arve Knudsen <[email protected]>
2 parents 6ab5bef + b3ffb62 commit b72d63f

File tree

2 files changed

+120
-37
lines changed

2 files changed

+120
-37
lines changed

pkg/translator/prometheusremotewrite/helper.go

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,6 @@ func isValidAggregationTemporality(metric pmetric.Metric) bool {
207207

208208
func (c *PrometheusConverter) addHistogramDataPoints(dataPoints pmetric.HistogramDataPointSlice,
209209
resource pcommon.Resource, settings Settings, baseName string) {
210-
createLabels := func(nameSuffix string, baseLabels []prompb.Label, extras ...string) []prompb.Label {
211-
extraLabelCount := len(extras) / 2
212-
labels := make([]prompb.Label, len(baseLabels), len(baseLabels)+extraLabelCount+1) // +1 for name
213-
copy(labels, baseLabels)
214-
215-
for extrasIdx := 0; extrasIdx < extraLabelCount; extrasIdx++ {
216-
labels = append(labels, prompb.Label{Name: extras[extrasIdx], Value: extras[extrasIdx+1]})
217-
}
218-
219-
// sum, count, and buckets of the histogram should append suffix to baseName
220-
labels = append(labels, prompb.Label{Name: model.MetricNameLabel, Value: baseName + nameSuffix})
221-
222-
return labels
223-
}
224-
225210
for x := 0; x < dataPoints.Len(); x++ {
226211
pt := dataPoints.At(x)
227212
timestamp := convertTimeStamp(pt.Timestamp())
@@ -239,7 +224,7 @@ func (c *PrometheusConverter) addHistogramDataPoints(dataPoints pmetric.Histogra
239224
sum.Value = math.Float64frombits(value.StaleNaN)
240225
}
241226

242-
sumlabels := createLabels(sumStr, baseLabels)
227+
sumlabels := createLabels(baseName+sumStr, baseLabels)
243228
c.addSample(sum, sumlabels)
244229

245230
}
@@ -253,7 +238,7 @@ func (c *PrometheusConverter) addHistogramDataPoints(dataPoints pmetric.Histogra
253238
count.Value = math.Float64frombits(value.StaleNaN)
254239
}
255240

256-
countlabels := createLabels(countStr, baseLabels)
241+
countlabels := createLabels(baseName+countStr, baseLabels)
257242
c.addSample(count, countlabels)
258243

259244
// cumulative count for conversion to cumulative histogram
@@ -273,7 +258,7 @@ func (c *PrometheusConverter) addHistogramDataPoints(dataPoints pmetric.Histogra
273258
bucket.Value = math.Float64frombits(value.StaleNaN)
274259
}
275260
boundStr := strconv.FormatFloat(bound, 'f', -1, 64)
276-
labels := createLabels(bucketStr, baseLabels, leStr, boundStr)
261+
labels := createLabels(baseName+bucketStr, baseLabels, leStr, boundStr)
277262
ts := c.addSample(bucket, labels)
278263

279264
bucketBounds = append(bucketBounds, bucketBoundsData{ts: ts, bound: bound})
@@ -287,15 +272,15 @@ func (c *PrometheusConverter) addHistogramDataPoints(dataPoints pmetric.Histogra
287272
} else {
288273
infBucket.Value = float64(pt.Count())
289274
}
290-
infLabels := createLabels(bucketStr, baseLabels, leStr, pInfStr)
275+
infLabels := createLabels(baseName+bucketStr, baseLabels, leStr, pInfStr)
291276
ts := c.addSample(infBucket, infLabels)
292277

293278
bucketBounds = append(bucketBounds, bucketBoundsData{ts: ts, bound: math.Inf(1)})
294279
c.addExemplars(pt, bucketBounds)
295280

296281
startTimestamp := pt.StartTimestamp()
297282
if settings.ExportCreatedMetric && startTimestamp != 0 {
298-
labels := createLabels(createdSuffix, baseLabels)
283+
labels := createLabels(baseName+createdSuffix, baseLabels)
299284
c.addTimeSeriesIfNeeded(labels, startTimestamp, pt.Timestamp())
300285
}
301286
}
@@ -405,20 +390,6 @@ func maxTimestamp(a, b pcommon.Timestamp) pcommon.Timestamp {
405390

406391
func (c *PrometheusConverter) addSummaryDataPoints(dataPoints pmetric.SummaryDataPointSlice, resource pcommon.Resource,
407392
settings Settings, baseName string) {
408-
createLabels := func(name string, baseLabels []prompb.Label, extras ...string) []prompb.Label {
409-
extraLabelCount := len(extras) / 2
410-
labels := make([]prompb.Label, len(baseLabels), len(baseLabels)+extraLabelCount+1) // +1 for name
411-
copy(labels, baseLabels)
412-
413-
for extrasIdx := 0; extrasIdx < extraLabelCount; extrasIdx++ {
414-
labels = append(labels, prompb.Label{Name: extras[extrasIdx], Value: extras[extrasIdx+1]})
415-
}
416-
417-
labels = append(labels, prompb.Label{Name: model.MetricNameLabel, Value: name})
418-
419-
return labels
420-
}
421-
422393
for x := 0; x < dataPoints.Len(); x++ {
423394
pt := dataPoints.At(x)
424395
timestamp := convertTimeStamp(pt.Timestamp())
@@ -470,6 +441,24 @@ func (c *PrometheusConverter) addSummaryDataPoints(dataPoints pmetric.SummaryDat
470441
}
471442
}
472443

444+
// createLabels returns a copy of baseLabels, adding to it the pair model.MetricNameLabel=name.
445+
// If extras are provided, corresponding label pairs are also added to the returned slice.
446+
// If extras is uneven length, the last (unpaired) extra will be ignored.
447+
func createLabels(name string, baseLabels []prompb.Label, extras ...string) []prompb.Label {
448+
extraLabelCount := len(extras) / 2
449+
labels := make([]prompb.Label, len(baseLabels), len(baseLabels)+extraLabelCount+1) // +1 for name
450+
copy(labels, baseLabels)
451+
452+
n := len(extras)
453+
n -= n % 2
454+
for extrasIdx := 0; extrasIdx < n; extrasIdx += 2 {
455+
labels = append(labels, prompb.Label{Name: extras[extrasIdx], Value: extras[extrasIdx+1]})
456+
}
457+
458+
labels = append(labels, prompb.Label{Name: model.MetricNameLabel, Value: name})
459+
return labels
460+
}
461+
473462
// getOrCreateTimeSeries returns the time series corresponding to the label set if existent, and false.
474463
// Otherwise it creates a new one and returns that, and true.
475464
func (c *PrometheusConverter) getOrCreateTimeSeries(lbls []prompb.Label) (*prompb.TimeSeries, bool) {

pkg/translator/prometheusremotewrite/helper_test.go

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ func TestPrometheusConverter_getOrCreateTimeSeries(t *testing.T) {
927927
require.NotNil(t, cTS1)
928928
require.True(t, created)
929929
require.Equal(t, map[uint64][]*prompb.TimeSeries{
930-
h: []*prompb.TimeSeries{cTS1},
930+
h: {cTS1},
931931
}, converter.conflicts)
932932

933933
// Fake a hash collision, by making this not equal to the next series with the same hash
@@ -938,18 +938,112 @@ func TestPrometheusConverter_getOrCreateTimeSeries(t *testing.T) {
938938
require.NotNil(t, cTS2)
939939
require.True(t, created)
940940
require.Equal(t, map[uint64][]*prompb.TimeSeries{
941-
h: []*prompb.TimeSeries{cTS1, cTS2},
941+
h: {cTS1, cTS2},
942942
}, converter.conflicts)
943943

944944
// Now, get (not create) the second colliding time series
945945
gotCTS2, created := converter.getOrCreateTimeSeries(lbls)
946946
require.Same(t, cTS2, gotCTS2)
947947
require.False(t, created)
948948
require.Equal(t, map[uint64][]*prompb.TimeSeries{
949-
h: []*prompb.TimeSeries{cTS1, cTS2},
949+
h: {cTS1, cTS2},
950950
}, converter.conflicts)
951951

952952
require.Equal(t, map[uint64]*prompb.TimeSeries{
953953
h: ts,
954954
}, converter.unique)
955955
}
956+
957+
func TestCreateLabels(t *testing.T) {
958+
testCases := []struct {
959+
name string
960+
metricName string
961+
baseLabels []prompb.Label
962+
extras []string
963+
expected []prompb.Label
964+
}{
965+
{
966+
name: "no base labels, no extras",
967+
metricName: "test",
968+
baseLabels: nil,
969+
expected: []prompb.Label{
970+
{Name: model.MetricNameLabel, Value: "test"},
971+
},
972+
},
973+
{
974+
name: "base labels, no extras",
975+
metricName: "test",
976+
baseLabels: []prompb.Label{
977+
{Name: "base1", Value: "value1"},
978+
{Name: "base2", Value: "value2"},
979+
},
980+
expected: []prompb.Label{
981+
{Name: "base1", Value: "value1"},
982+
{Name: "base2", Value: "value2"},
983+
{Name: model.MetricNameLabel, Value: "test"},
984+
},
985+
},
986+
{
987+
name: "base labels, 1 extra",
988+
metricName: "test",
989+
baseLabels: []prompb.Label{
990+
{Name: "base1", Value: "value1"},
991+
{Name: "base2", Value: "value2"},
992+
},
993+
extras: []string{"extra1", "extraValue1"},
994+
expected: []prompb.Label{
995+
{Name: "base1", Value: "value1"},
996+
{Name: "base2", Value: "value2"},
997+
{Name: "extra1", Value: "extraValue1"},
998+
{Name: model.MetricNameLabel, Value: "test"},
999+
},
1000+
},
1001+
{
1002+
name: "base labels, 2 extras",
1003+
metricName: "test",
1004+
baseLabels: []prompb.Label{
1005+
{Name: "base1", Value: "value1"},
1006+
{Name: "base2", Value: "value2"},
1007+
},
1008+
extras: []string{"extra1", "extraValue1", "extra2", "extraValue2"},
1009+
expected: []prompb.Label{
1010+
{Name: "base1", Value: "value1"},
1011+
{Name: "base2", Value: "value2"},
1012+
{Name: "extra1", Value: "extraValue1"},
1013+
{Name: "extra2", Value: "extraValue2"},
1014+
{Name: model.MetricNameLabel, Value: "test"},
1015+
},
1016+
},
1017+
{
1018+
name: "base labels, unpaired extra",
1019+
metricName: "test",
1020+
baseLabels: []prompb.Label{
1021+
{Name: "base1", Value: "value1"},
1022+
{Name: "base2", Value: "value2"},
1023+
},
1024+
extras: []string{"extra1", "extraValue1", "extra2"},
1025+
expected: []prompb.Label{
1026+
{Name: "base1", Value: "value1"},
1027+
{Name: "base2", Value: "value2"},
1028+
{Name: "extra1", Value: "extraValue1"},
1029+
{Name: model.MetricNameLabel, Value: "test"},
1030+
},
1031+
},
1032+
{
1033+
name: "no base labels, 1 extra",
1034+
metricName: "test",
1035+
baseLabels: nil,
1036+
extras: []string{"extra1", "extraValue1"},
1037+
expected: []prompb.Label{
1038+
{Name: "extra1", Value: "extraValue1"},
1039+
{Name: model.MetricNameLabel, Value: "test"},
1040+
},
1041+
},
1042+
}
1043+
for _, tc := range testCases {
1044+
t.Run(tc.name, func(t *testing.T) {
1045+
lbls := createLabels(tc.metricName, tc.baseLabels, tc.extras...)
1046+
assert.Equal(t, lbls, tc.expected)
1047+
})
1048+
}
1049+
}

0 commit comments

Comments
 (0)