Debian Codegraph @

pdata/testdata/metric.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package testdata
5
6 import (
7 "time"
8
9 "go.opentelemetry.io/collector/pdata/pcommon"
10 "go.opentelemetry.io/collector/pdata/pmetric"
11 )
12
13 var (
14 metricStartTimestamp = pcommon.NewTimestampFromTime(time.Date(2020, 2, 11, 20, 26, 12, 321, time.UTC))
15 metricExemplarTimestamp = pcommon.NewTimestampFromTime(time.Date(2020, 2, 11, 20, 26, 13, 123, time.UTC))
16 metricTimestamp = pcommon.NewTimestampFromTime(time.Date(2020, 2, 11, 20, 26, 13, 789, time.UTC))
17 )
18
19 const (
20 TestGaugeDoubleMetricName = "gauge-double"
21 TestGaugeIntMetricName = "gauge-int"
22 TestSumDoubleMetricName = "sum-double"
23 TestSumIntMetricName = "sum-int"
24 TestHistogramMetricName = "histogram"
25 TestExponentialHistogramMetricName = "exponential-histogram"
26 TestSummaryMetricName = "summary"
27 )
28
29 func generateMetricsOneEmptyInstrumentationScope() pmetric.Metrics {
30 md := pmetric.NewMetrics()
31 initResource(md.ResourceMetrics().AppendEmpty().Resource())
32 md.ResourceMetrics().At(0).ScopeMetrics().AppendEmpty()
33 return md
34 }
35
36 func GenerateMetricsAllTypesEmpty() pmetric.Metrics {
37 md := generateMetricsOneEmptyInstrumentationScope()
38 ms := md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics()
39
40 doubleGauge := ms.AppendEmpty()
41 initMetric(doubleGauge, TestGaugeDoubleMetricName, pmetric.MetricTypeGauge)
42 doubleGauge.Gauge().DataPoints().AppendEmpty()
43 intGauge := ms.AppendEmpty()
44 initMetric(intGauge, TestGaugeIntMetricName, pmetric.MetricTypeGauge)
45 intGauge.Gauge().DataPoints().AppendEmpty()
46 doubleSum := ms.AppendEmpty()
47 initMetric(doubleSum, TestSumDoubleMetricName, pmetric.MetricTypeSum)
48 doubleSum.Sum().DataPoints().AppendEmpty()
49 intSum := ms.AppendEmpty()
50 initMetric(intSum, TestSumIntMetricName, pmetric.MetricTypeSum)
51 intSum.Sum().DataPoints().AppendEmpty()
52 histogram := ms.AppendEmpty()
53 initMetric(histogram, TestHistogramMetricName, pmetric.MetricTypeHistogram)
54 histogram.Histogram().DataPoints().AppendEmpty()
55 summary := ms.AppendEmpty()
56 initMetric(summary, TestSummaryMetricName, pmetric.MetricTypeSummary)
57 summary.Summary().DataPoints().AppendEmpty()
58 return md
59 }
60
61 func GenerateMetricsMetricTypeInvalid() pmetric.Metrics {
62 md := generateMetricsOneEmptyInstrumentationScope()
63 initMetric(md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().AppendEmpty(), TestSumIntMetricName, pmetric.MetricTypeEmpty)
64 return md
65 }
66
67 func GenerateMetricsAllTypes() pmetric.Metrics {
68 md := generateMetricsOneEmptyInstrumentationScope()
69 ms := md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics()
70 initGaugeIntMetric(ms.AppendEmpty())
71 initGaugeDoubleMetric(ms.AppendEmpty())
72 initSumIntMetric(ms.AppendEmpty())
73 initSumDoubleMetric(ms.AppendEmpty())
74 initHistogramMetric(ms.AppendEmpty())
75 initExponentialHistogramMetric(ms.AppendEmpty())
76 initSummaryMetric(ms.AppendEmpty())
77 return md
78 }
79
80 func GenerateMetrics(count int) pmetric.Metrics {
81 md := generateMetricsOneEmptyInstrumentationScope()
82 ms := md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics()
83 ms.EnsureCapacity(count)
84 for i := range count {
85 switch i % 7 {
86 case 0:
87 initGaugeIntMetric(ms.AppendEmpty())
88 case 1:
89 initGaugeDoubleMetric(ms.AppendEmpty())
90 case 2:
91 initSumIntMetric(ms.AppendEmpty())
92 case 3:
93 initSumDoubleMetric(ms.AppendEmpty())
94 case 4:
95 initHistogramMetric(ms.AppendEmpty())
96 case 5:
97 initExponentialHistogramMetric(ms.AppendEmpty())
98 case 6:
99 initSummaryMetric(ms.AppendEmpty())
100 }
101 }
102 return md
103 }
104
105 func initGaugeIntMetric(im pmetric.Metric) {
106 initMetric(im, TestGaugeIntMetricName, pmetric.MetricTypeGauge)
107
108 idps := im.Gauge().DataPoints()
109 idp0 := idps.AppendEmpty()
110 initMetricAttributes1(idp0.Attributes())
111 idp0.SetStartTimestamp(metricStartTimestamp)
112 idp0.SetTimestamp(metricTimestamp)
113 idp0.SetIntValue(123)
114 idp1 := idps.AppendEmpty()
115 initMetricAttributes2(idp1.Attributes())
116 idp1.SetStartTimestamp(metricStartTimestamp)
117 idp1.SetTimestamp(metricTimestamp)
118 idp1.SetIntValue(456)
119 }
120
121 func initGaugeDoubleMetric(im pmetric.Metric) {
122 initMetric(im, TestGaugeDoubleMetricName, pmetric.MetricTypeGauge)
123
124 idps := im.Gauge().DataPoints()
125 idp0 := idps.AppendEmpty()
126 initMetricAttributes12(idp0.Attributes())
127 idp0.SetStartTimestamp(metricStartTimestamp)
128 idp0.SetTimestamp(metricTimestamp)
129 idp0.SetDoubleValue(1.23)
130 idp1 := idps.AppendEmpty()
131 initMetricAttributes13(idp1.Attributes())
132 idp1.SetStartTimestamp(metricStartTimestamp)
133 idp1.SetTimestamp(metricTimestamp)
134 idp1.SetDoubleValue(4.56)
135 }
136
137 func initSumIntMetric(im pmetric.Metric) {
138 initMetric(im, TestSumIntMetricName, pmetric.MetricTypeSum)
139
140 idps := im.Sum().DataPoints()
141 idp0 := idps.AppendEmpty()
142 initMetricAttributes1(idp0.Attributes())
143 idp0.SetStartTimestamp(metricStartTimestamp)
144 idp0.SetTimestamp(metricTimestamp)
145 idp0.SetIntValue(123)
146 idp1 := idps.AppendEmpty()
147 initMetricAttributes2(idp1.Attributes())
148 idp1.SetStartTimestamp(metricStartTimestamp)
149 idp1.SetTimestamp(metricTimestamp)
150 idp1.SetIntValue(456)
151 }
152
153 func initSumDoubleMetric(dm pmetric.Metric) {
154 initMetric(dm, TestSumDoubleMetricName, pmetric.MetricTypeSum)
155
156 ddps := dm.Sum().DataPoints()
157 ddp0 := ddps.AppendEmpty()
158 initMetricAttributes12(ddp0.Attributes())
159 ddp0.SetStartTimestamp(metricStartTimestamp)
160 ddp0.SetTimestamp(metricTimestamp)
161 ddp0.SetDoubleValue(1.23)
162
163 ddp1 := ddps.AppendEmpty()
164 initMetricAttributes13(ddp1.Attributes())
165 ddp1.SetStartTimestamp(metricStartTimestamp)
166 ddp1.SetTimestamp(metricTimestamp)
167 ddp1.SetDoubleValue(4.56)
168 }
169
170 func initHistogramMetric(hm pmetric.Metric) {
171 initMetric(hm, TestHistogramMetricName, pmetric.MetricTypeHistogram)
172
173 hdps := hm.Histogram().DataPoints()
174 hdp0 := hdps.AppendEmpty()
175 initMetricAttributes13(hdp0.Attributes())
176 hdp0.SetStartTimestamp(metricStartTimestamp)
177 hdp0.SetTimestamp(metricTimestamp)
178 hdp0.SetCount(1)
179 hdp0.SetSum(15)
180
181 hdp1 := hdps.AppendEmpty()
182 initMetricAttributes2(hdp1.Attributes())
183 hdp1.SetStartTimestamp(metricStartTimestamp)
184 hdp1.SetTimestamp(metricTimestamp)
185 hdp1.SetCount(1)
186 hdp1.SetSum(15)
187 hdp1.SetMin(15)
188 hdp1.SetMax(15)
189 hdp1.BucketCounts().FromRaw([]uint64{0, 1})
190 exemplar := hdp1.Exemplars().AppendEmpty()
191 exemplar.SetTraceID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10})
192 exemplar.SetSpanID([8]byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18})
193 exemplar.SetTimestamp(metricExemplarTimestamp)
194 exemplar.SetDoubleValue(15)
195 initMetricExemplarAttributes(exemplar.FilteredAttributes())
196 hdp1.ExplicitBounds().FromRaw([]float64{1})
197 }
198
199 func initExponentialHistogramMetric(hm pmetric.Metric) {
200 initMetric(hm, TestExponentialHistogramMetricName, pmetric.MetricTypeExponentialHistogram)
201
202 hdps := hm.ExponentialHistogram().DataPoints()
203 hdp0 := hdps.AppendEmpty()
204 initMetricAttributes13(hdp0.Attributes())
205 hdp0.SetStartTimestamp(metricStartTimestamp)
206 hdp0.SetTimestamp(metricTimestamp)
207 hdp0.SetCount(5)
208 hdp0.SetSum(0.15)
209 hdp0.SetZeroCount(1)
210 hdp0.SetScale(1)
211
212 // positive index 1 and 2 are values sqrt(2), 2 at scale 1
213 hdp0.Positive().SetOffset(1)
214 hdp0.Positive().BucketCounts().FromRaw([]uint64{1, 1})
215 // negative index -1 and 0 are values -1/sqrt(2), -1 at scale 1
216 hdp0.Negative().SetOffset(-1)
217 hdp0.Negative().BucketCounts().FromRaw([]uint64{1, 1})
218
219 // The above will print:
220 // Bucket (-1.414214, -1.000000], Count: 1
221 // Bucket (-1.000000, -0.707107], Count: 1
222 // Bucket [0, 0], Count: 1
223 // Bucket [0.707107, 1.000000), Count: 1
224 // Bucket [1.000000, 1.414214), Count: 1
225
226 hdp1 := hdps.AppendEmpty()
227 initMetricAttributes2(hdp1.Attributes())
228 hdp1.SetStartTimestamp(metricStartTimestamp)
229 hdp1.SetTimestamp(metricTimestamp)
230 hdp1.SetCount(3)
231 hdp1.SetSum(1.25)
232 hdp1.SetMin(0)
233 hdp1.SetMax(1)
234 hdp1.SetZeroCount(1)
235 hdp1.SetScale(-1)
236
237 // index -1 and 0 are values 0.25, 1 at scale -1
238 hdp1.Positive().SetOffset(-1)
239 hdp1.Positive().BucketCounts().FromRaw([]uint64{1, 1})
240
241 // The above will print:
242 // Bucket [0, 0], Count: 1
243 // Bucket [0.250000, 1.000000), Count: 1
244 // Bucket [1.000000, 4.000000), Count: 1
245
246 exemplar := hdp1.Exemplars().AppendEmpty()
247 exemplar.SetTraceID([16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10})
248 exemplar.SetSpanID([8]byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18})
249 exemplar.SetTimestamp(metricExemplarTimestamp)
250 exemplar.SetIntValue(15)
251 initMetricExemplarAttributes(exemplar.FilteredAttributes())
252 }
253
254 func initSummaryMetric(sm pmetric.Metric) {
255 initMetric(sm, TestSummaryMetricName, pmetric.MetricTypeSummary)
256
257 sdps := sm.Summary().DataPoints()
258 sdp0 := sdps.AppendEmpty()
259 initMetricAttributes13(sdp0.Attributes())
260 sdp0.SetStartTimestamp(metricStartTimestamp)
261 sdp0.SetTimestamp(metricTimestamp)
262 sdp0.SetCount(1)
263 sdp0.SetSum(15)
264
265 sdp1 := sdps.AppendEmpty()
266 initMetricAttributes2(sdp1.Attributes())
267 sdp1.SetStartTimestamp(metricStartTimestamp)
268 sdp1.SetTimestamp(metricTimestamp)
269 sdp1.SetCount(1)
270 sdp1.SetSum(15)
271
272 quantile := sdp1.QuantileValues().AppendEmpty()
273 quantile.SetQuantile(0.01)
274 quantile.SetValue(15)
275 }
276
277 func initMetric(m pmetric.Metric, name string, ty pmetric.MetricType) {
278 m.SetName(name)
279 m.SetDescription("")
280 m.SetUnit("1")
281 switch ty {
282 case pmetric.MetricTypeGauge:
283 m.SetEmptyGauge()
284 case pmetric.MetricTypeSum:
285 sum := m.SetEmptySum()
286 sum.SetIsMonotonic(true)
287 sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
288 case pmetric.MetricTypeHistogram:
289 histo := m.SetEmptyHistogram()
290 histo.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
291 case pmetric.MetricTypeExponentialHistogram:
292 histo := m.SetEmptyExponentialHistogram()
293 histo.SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
294 case pmetric.MetricTypeSummary:
295 m.SetEmptySummary()
296 }
297 }