Debian Codegraph @

storage/remote/otlptranslator/prometheusremotewrite/metrics_to_prw_test.go

raw
1 // Copyright 2024 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 // Provenance-includes-location: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/95e8f8fdc2a9dc87230406c9a3cf02be4fd68bea/pkg/translator/prometheusremotewrite/metrics_to_prw_test.go
14 // Provenance-includes-license: Apache-2.0
15 // Provenance-includes-copyright: Copyright The OpenTelemetry Authors.
16
17 package prometheusremotewrite
18
19 import (
20 "fmt"
21 "testing"
22 "time"
23
24 "github.com/stretchr/testify/require"
25 "go.opentelemetry.io/collector/pdata/pcommon"
26 "go.opentelemetry.io/collector/pdata/pmetric"
27 "go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
28 )
29
30 func BenchmarkPrometheusConverter_FromMetrics(b *testing.B) {
31 for _, resourceAttributeCount := range []int{0, 5, 50} {
32 b.Run(fmt.Sprintf("resource attribute count: %v", resourceAttributeCount), func(b *testing.B) {
33 for _, histogramCount := range []int{0, 1000} {
34 b.Run(fmt.Sprintf("histogram count: %v", histogramCount), func(b *testing.B) {
35 nonHistogramCounts := []int{0, 1000}
36
37 if resourceAttributeCount == 0 && histogramCount == 0 {
38 // Don't bother running a scenario where we'll generate no series.
39 nonHistogramCounts = []int{1000}
40 }
41
42 for _, nonHistogramCount := range nonHistogramCounts {
43 b.Run(fmt.Sprintf("non-histogram count: %v", nonHistogramCount), func(b *testing.B) {
44 for _, labelsPerMetric := range []int{2, 20} {
45 b.Run(fmt.Sprintf("labels per metric: %v", labelsPerMetric), func(b *testing.B) {
46 for _, exemplarsPerSeries := range []int{0, 5, 10} {
47 b.Run(fmt.Sprintf("exemplars per series: %v", exemplarsPerSeries), func(b *testing.B) {
48 payload := createExportRequest(resourceAttributeCount, histogramCount, nonHistogramCount, labelsPerMetric, exemplarsPerSeries)
49
50 for i := 0; i < b.N; i++ {
51 converter := NewPrometheusConverter()
52 require.NoError(b, converter.FromMetrics(payload.Metrics(), Settings{}))
53 require.NotNil(b, converter.TimeSeries())
54 }
55 })
56 }
57 })
58 }
59 })
60 }
61 })
62 }
63 })
64 }
65 }
66
67 func createExportRequest(resourceAttributeCount, histogramCount, nonHistogramCount, labelsPerMetric, exemplarsPerSeries int) pmetricotlp.ExportRequest {
68 request := pmetricotlp.NewExportRequest()
69
70 rm := request.Metrics().ResourceMetrics().AppendEmpty()
71 generateAttributes(rm.Resource().Attributes(), "resource", resourceAttributeCount)
72
73 metrics := rm.ScopeMetrics().AppendEmpty().Metrics()
74 ts := pcommon.NewTimestampFromTime(time.Now())
75
76 for i := 1; i <= histogramCount; i++ {
77 m := metrics.AppendEmpty()
78 m.SetEmptyHistogram()
79 m.SetName(fmt.Sprintf("histogram-%v", i))
80 m.Histogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
81 h := m.Histogram().DataPoints().AppendEmpty()
82 h.SetTimestamp(ts)
83
84 // Set 50 samples, 10 each with values 0.5, 1, 2, 4, and 8
85 h.SetCount(50)
86 h.SetSum(155)
87 h.BucketCounts().FromRaw([]uint64{10, 10, 10, 10, 10, 0})
88 h.ExplicitBounds().FromRaw([]float64{.5, 1, 2, 4, 8, 16}) // Bucket boundaries include the upper limit (ie. each sample is on the upper limit of its bucket)
89
90 generateAttributes(h.Attributes(), "series", labelsPerMetric)
91 generateExemplars(h.Exemplars(), exemplarsPerSeries, ts)
92 }
93
94 for i := 1; i <= nonHistogramCount; i++ {
95 m := metrics.AppendEmpty()
96 m.SetEmptySum()
97 m.SetName(fmt.Sprintf("sum-%v", i))
98 m.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
99 point := m.Sum().DataPoints().AppendEmpty()
100 point.SetTimestamp(ts)
101 point.SetDoubleValue(1.23)
102 generateAttributes(point.Attributes(), "series", labelsPerMetric)
103 generateExemplars(point.Exemplars(), exemplarsPerSeries, ts)
104 }
105
106 for i := 1; i <= nonHistogramCount; i++ {
107 m := metrics.AppendEmpty()
108 m.SetEmptyGauge()
109 m.SetName(fmt.Sprintf("gauge-%v", i))
110 point := m.Gauge().DataPoints().AppendEmpty()
111 point.SetTimestamp(ts)
112 point.SetDoubleValue(1.23)
113 generateAttributes(point.Attributes(), "series", labelsPerMetric)
114 generateExemplars(point.Exemplars(), exemplarsPerSeries, ts)
115 }
116
117 return request
118 }
119
120 func generateAttributes(m pcommon.Map, prefix string, count int) {
121 for i := 1; i <= count; i++ {
122 m.PutStr(fmt.Sprintf("%v-name-%v", prefix, i), fmt.Sprintf("value-%v", i))
123 }
124 }
125
126 func generateExemplars(exemplars pmetric.ExemplarSlice, count int, ts pcommon.Timestamp) {
127 for i := 1; i <= count; i++ {
128 e := exemplars.AppendEmpty()
129 e.SetTimestamp(ts)
130 e.SetDoubleValue(2.22)
131 e.SetSpanID(pcommon.SpanID{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08})
132 e.SetTraceID(pcommon.TraceID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f})
133 }
134 }