Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/otelcol/receiver/prometheus/internal/metricsutil_test.go
5414 views
1
// Copyright The OpenTelemetry Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
package internal
16
17
import (
18
"go.opentelemetry.io/collector/pdata/pcommon"
19
"go.opentelemetry.io/collector/pdata/pmetric"
20
)
21
22
type kv struct {
23
Key, Value string
24
}
25
26
func metrics(metrics ...pmetric.Metric) pmetric.Metrics {
27
md := pmetric.NewMetrics()
28
ms := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
29
for _, metric := range metrics {
30
destMetric := ms.AppendEmpty()
31
metric.CopyTo(destMetric)
32
}
33
34
return md
35
}
36
37
func histogramPointRaw(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.HistogramDataPoint {
38
hdp := pmetric.NewHistogramDataPoint()
39
hdp.SetStartTimestamp(startTimestamp)
40
hdp.SetTimestamp(timestamp)
41
42
attrs := hdp.Attributes()
43
for _, kv := range attributes {
44
attrs.PutStr(kv.Key, kv.Value)
45
}
46
47
return hdp
48
}
49
50
func histogramPoint(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp, bounds []float64, counts []uint64) pmetric.HistogramDataPoint {
51
hdp := histogramPointRaw(attributes, startTimestamp, timestamp)
52
hdp.ExplicitBounds().FromRaw(bounds)
53
hdp.BucketCounts().FromRaw(counts)
54
55
var sum float64
56
var count uint64
57
for i, bcount := range counts {
58
count += bcount
59
if i > 0 {
60
sum += float64(bcount) * bounds[i-1]
61
}
62
}
63
hdp.SetCount(count)
64
hdp.SetSum(sum)
65
66
return hdp
67
}
68
69
func histogramPointNoValue(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.HistogramDataPoint {
70
hdp := histogramPointRaw(attributes, startTimestamp, timestamp)
71
hdp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
72
73
return hdp
74
}
75
76
func histogramMetric(name string, points ...pmetric.HistogramDataPoint) pmetric.Metric {
77
metric := pmetric.NewMetric()
78
metric.SetName(name)
79
histogram := metric.SetEmptyHistogram()
80
histogram.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
81
82
destPointL := histogram.DataPoints()
83
// By default the AggregationTemporality is Cumulative until it'll be changed by the caller.
84
for _, point := range points {
85
destPoint := destPointL.AppendEmpty()
86
point.CopyTo(destPoint)
87
}
88
89
return metric
90
}
91
92
func doublePointRaw(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.NumberDataPoint {
93
ndp := pmetric.NewNumberDataPoint()
94
ndp.SetStartTimestamp(startTimestamp)
95
ndp.SetTimestamp(timestamp)
96
97
for _, kv := range attributes {
98
ndp.Attributes().PutStr(kv.Key, kv.Value)
99
}
100
101
return ndp
102
}
103
104
func doublePoint(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp, value float64) pmetric.NumberDataPoint {
105
ndp := doublePointRaw(attributes, startTimestamp, timestamp)
106
ndp.SetDoubleValue(value)
107
return ndp
108
}
109
110
func doublePointNoValue(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.NumberDataPoint {
111
ndp := doublePointRaw(attributes, startTimestamp, timestamp)
112
ndp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
113
return ndp
114
}
115
116
func gaugeMetric(name string, points ...pmetric.NumberDataPoint) pmetric.Metric {
117
metric := pmetric.NewMetric()
118
metric.SetName(name)
119
destPointL := metric.SetEmptyGauge().DataPoints()
120
for _, point := range points {
121
destPoint := destPointL.AppendEmpty()
122
point.CopyTo(destPoint)
123
}
124
125
return metric
126
}
127
128
func sumMetric(name string, points ...pmetric.NumberDataPoint) pmetric.Metric {
129
metric := pmetric.NewMetric()
130
metric.SetName(name)
131
sum := metric.SetEmptySum()
132
sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
133
sum.SetIsMonotonic(true)
134
135
destPointL := sum.DataPoints()
136
for _, point := range points {
137
destPoint := destPointL.AppendEmpty()
138
point.CopyTo(destPoint)
139
}
140
141
return metric
142
}
143
144
func summaryPointRaw(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.SummaryDataPoint {
145
sdp := pmetric.NewSummaryDataPoint()
146
sdp.SetStartTimestamp(startTimestamp)
147
sdp.SetTimestamp(timestamp)
148
149
for _, kv := range attributes {
150
sdp.Attributes().PutStr(kv.Key, kv.Value)
151
}
152
153
return sdp
154
}
155
156
func summaryPoint(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp, count uint64, sum float64, quantiles, values []float64) pmetric.SummaryDataPoint {
157
sdp := summaryPointRaw(attributes, startTimestamp, timestamp)
158
sdp.SetCount(count)
159
sdp.SetSum(sum)
160
161
qvL := sdp.QuantileValues()
162
for i := 0; i < len(quantiles); i++ {
163
qvi := qvL.AppendEmpty()
164
qvi.SetQuantile(quantiles[i])
165
qvi.SetValue(values[i])
166
}
167
168
return sdp
169
}
170
171
func summaryPointNoValue(attributes []*kv, startTimestamp, timestamp pcommon.Timestamp) pmetric.SummaryDataPoint {
172
sdp := summaryPointRaw(attributes, startTimestamp, timestamp)
173
sdp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
174
175
return sdp
176
}
177
178
func summaryMetric(name string, points ...pmetric.SummaryDataPoint) pmetric.Metric {
179
metric := pmetric.NewMetric()
180
metric.SetName(name)
181
destPointL := metric.SetEmptySummary().DataPoints()
182
for _, point := range points {
183
destPoint := destPointL.AppendEmpty()
184
point.CopyTo(destPoint)
185
}
186
187
return metric
188
}
189
190