Debian Codegraph @

exporters/otlp/otlpmetric/otlpmetrichttp/internal/transform/metricdata.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/otlpmetric/transform/metricdata.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 // Package transform provides transformation functionality from the
8 // sdk/metric/metricdata data-types into OTLP data-types.
9 package transform // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/transform"
10
11 import (
12 "fmt"
13 "time"
14
15 "go.opentelemetry.io/otel/sdk/metric/metricdata"
16 cpb "go.opentelemetry.io/proto/otlp/common/v1"
17 mpb "go.opentelemetry.io/proto/otlp/metrics/v1"
18 rpb "go.opentelemetry.io/proto/otlp/resource/v1"
19 )
20
21 // ResourceMetrics returns an OTLP ResourceMetrics generated from rm. If rm
22 // contains invalid ScopeMetrics, an error will be returned along with an OTLP
23 // ResourceMetrics that contains partial OTLP ScopeMetrics.
24 func ResourceMetrics(rm *metricdata.ResourceMetrics) (*mpb.ResourceMetrics, error) {
25 sms, err := ScopeMetrics(rm.ScopeMetrics)
26 return &mpb.ResourceMetrics{
27 Resource: &rpb.Resource{
28 Attributes: AttrIter(rm.Resource.Iter()),
29 },
30 ScopeMetrics: sms,
31 SchemaUrl: rm.Resource.SchemaURL(),
32 }, err
33 }
34
35 // ScopeMetrics returns a slice of OTLP ScopeMetrics generated from sms. If
36 // sms contains invalid metric values, an error will be returned along with a
37 // slice that contains partial OTLP ScopeMetrics.
38 func ScopeMetrics(sms []metricdata.ScopeMetrics) ([]*mpb.ScopeMetrics, error) {
39 errs := &multiErr{datatype: "ScopeMetrics"}
40 out := make([]*mpb.ScopeMetrics, 0, len(sms))
41 for _, sm := range sms {
42 ms, err := Metrics(sm.Metrics)
43 if err != nil {
44 errs.append(err)
45 }
46
47 out = append(out, &mpb.ScopeMetrics{
48 Scope: &cpb.InstrumentationScope{
49 Name: sm.Scope.Name,
50 Version: sm.Scope.Version,
51 Attributes: AttrIter(sm.Scope.Attributes.Iter()),
52 },
53 Metrics: ms,
54 SchemaUrl: sm.Scope.SchemaURL,
55 })
56 }
57 return out, errs.errOrNil()
58 }
59
60 // Metrics returns a slice of OTLP Metric generated from ms. If ms contains
61 // invalid metric values, an error will be returned along with a slice that
62 // contains partial OTLP Metrics.
63 func Metrics(ms []metricdata.Metrics) ([]*mpb.Metric, error) {
64 errs := &multiErr{datatype: "Metrics"}
65 out := make([]*mpb.Metric, 0, len(ms))
66 for _, m := range ms {
67 o, err := metric(m)
68 if err != nil {
69 // Do not include invalid data. Drop the metric, report the error.
70 errs.append(errMetric{m: o, err: err})
71 continue
72 }
73 out = append(out, o)
74 }
75 return out, errs.errOrNil()
76 }
77
78 func metric(m metricdata.Metrics) (*mpb.Metric, error) {
79 var err error
80 out := &mpb.Metric{
81 Name: m.Name,
82 Description: m.Description,
83 Unit: m.Unit,
84 }
85 switch a := m.Data.(type) {
86 case metricdata.Gauge[int64]:
87 out.Data = Gauge(a)
88 case metricdata.Gauge[float64]:
89 out.Data = Gauge(a)
90 case metricdata.Sum[int64]:
91 out.Data, err = Sum(a)
92 case metricdata.Sum[float64]:
93 out.Data, err = Sum(a)
94 case metricdata.Histogram[int64]:
95 out.Data, err = Histogram(a)
96 case metricdata.Histogram[float64]:
97 out.Data, err = Histogram(a)
98 case metricdata.ExponentialHistogram[int64]:
99 out.Data, err = ExponentialHistogram(a)
100 case metricdata.ExponentialHistogram[float64]:
101 out.Data, err = ExponentialHistogram(a)
102 case metricdata.Summary:
103 out.Data = Summary(a)
104 default:
105 return out, fmt.Errorf("%w: %T", errUnknownAggregation, a)
106 }
107 return out, err
108 }
109
110 // Gauge returns an OTLP Metric_Gauge generated from g.
111 func Gauge[N int64 | float64](g metricdata.Gauge[N]) *mpb.Metric_Gauge {
112 return &mpb.Metric_Gauge{
113 Gauge: &mpb.Gauge{
114 DataPoints: DataPoints(g.DataPoints),
115 },
116 }
117 }
118
119 // Sum returns an OTLP Metric_Sum generated from s. An error is returned
120 // if the temporality of s is unknown.
121 func Sum[N int64 | float64](s metricdata.Sum[N]) (*mpb.Metric_Sum, error) {
122 t, err := Temporality(s.Temporality)
123 if err != nil {
124 return nil, err
125 }
126 return &mpb.Metric_Sum{
127 Sum: &mpb.Sum{
128 AggregationTemporality: t,
129 IsMonotonic: s.IsMonotonic,
130 DataPoints: DataPoints(s.DataPoints),
131 },
132 }, nil
133 }
134
135 // DataPoints returns a slice of OTLP NumberDataPoint generated from dPts.
136 func DataPoints[N int64 | float64](dPts []metricdata.DataPoint[N]) []*mpb.NumberDataPoint {
137 out := make([]*mpb.NumberDataPoint, 0, len(dPts))
138 for _, dPt := range dPts {
139 ndp := &mpb.NumberDataPoint{
140 Attributes: AttrIter(dPt.Attributes.Iter()),
141 StartTimeUnixNano: timeUnixNano(dPt.StartTime),
142 TimeUnixNano: timeUnixNano(dPt.Time),
143 Exemplars: Exemplars(dPt.Exemplars),
144 }
145 switch v := any(dPt.Value).(type) {
146 case int64:
147 ndp.Value = &mpb.NumberDataPoint_AsInt{
148 AsInt: v,
149 }
150 case float64:
151 ndp.Value = &mpb.NumberDataPoint_AsDouble{
152 AsDouble: v,
153 }
154 }
155 out = append(out, ndp)
156 }
157 return out
158 }
159
160 // Histogram returns an OTLP Metric_Histogram generated from h. An error is
161 // returned if the temporality of h is unknown.
162 func Histogram[N int64 | float64](h metricdata.Histogram[N]) (*mpb.Metric_Histogram, error) {
163 t, err := Temporality(h.Temporality)
164 if err != nil {
165 return nil, err
166 }
167 return &mpb.Metric_Histogram{
168 Histogram: &mpb.Histogram{
169 AggregationTemporality: t,
170 DataPoints: HistogramDataPoints(h.DataPoints),
171 },
172 }, nil
173 }
174
175 // HistogramDataPoints returns a slice of OTLP HistogramDataPoint generated
176 // from dPts.
177 func HistogramDataPoints[N int64 | float64](dPts []metricdata.HistogramDataPoint[N]) []*mpb.HistogramDataPoint {
178 out := make([]*mpb.HistogramDataPoint, 0, len(dPts))
179 for _, dPt := range dPts {
180 sum := float64(dPt.Sum)
181 hdp := &mpb.HistogramDataPoint{
182 Attributes: AttrIter(dPt.Attributes.Iter()),
183 StartTimeUnixNano: timeUnixNano(dPt.StartTime),
184 TimeUnixNano: timeUnixNano(dPt.Time),
185 Count: dPt.Count,
186 Sum: &sum,
187 BucketCounts: dPt.BucketCounts,
188 ExplicitBounds: dPt.Bounds,
189 Exemplars: Exemplars(dPt.Exemplars),
190 }
191 if v, ok := dPt.Min.Value(); ok {
192 vF64 := float64(v)
193 hdp.Min = &vF64
194 }
195 if v, ok := dPt.Max.Value(); ok {
196 vF64 := float64(v)
197 hdp.Max = &vF64
198 }
199 out = append(out, hdp)
200 }
201 return out
202 }
203
204 // ExponentialHistogram returns an OTLP Metric_ExponentialHistogram generated from h. An error is
205 // returned if the temporality of h is unknown.
206 func ExponentialHistogram[N int64 | float64](
207 h metricdata.ExponentialHistogram[N],
208 ) (*mpb.Metric_ExponentialHistogram, error) {
209 t, err := Temporality(h.Temporality)
210 if err != nil {
211 return nil, err
212 }
213 return &mpb.Metric_ExponentialHistogram{
214 ExponentialHistogram: &mpb.ExponentialHistogram{
215 AggregationTemporality: t,
216 DataPoints: ExponentialHistogramDataPoints(h.DataPoints),
217 },
218 }, nil
219 }
220
221 // ExponentialHistogramDataPoints returns a slice of OTLP ExponentialHistogramDataPoint generated
222 // from dPts.
223 func ExponentialHistogramDataPoints[N int64 | float64](
224 dPts []metricdata.ExponentialHistogramDataPoint[N],
225 ) []*mpb.ExponentialHistogramDataPoint {
226 out := make([]*mpb.ExponentialHistogramDataPoint, 0, len(dPts))
227 for _, dPt := range dPts {
228 sum := float64(dPt.Sum)
229 ehdp := &mpb.ExponentialHistogramDataPoint{
230 Attributes: AttrIter(dPt.Attributes.Iter()),
231 StartTimeUnixNano: timeUnixNano(dPt.StartTime),
232 TimeUnixNano: timeUnixNano(dPt.Time),
233 Count: dPt.Count,
234 Sum: &sum,
235 Scale: dPt.Scale,
236 ZeroCount: dPt.ZeroCount,
237 Exemplars: Exemplars(dPt.Exemplars),
238
239 Positive: ExponentialHistogramDataPointBuckets(dPt.PositiveBucket),
240 Negative: ExponentialHistogramDataPointBuckets(dPt.NegativeBucket),
241 }
242 if v, ok := dPt.Min.Value(); ok {
243 vF64 := float64(v)
244 ehdp.Min = &vF64
245 }
246 if v, ok := dPt.Max.Value(); ok {
247 vF64 := float64(v)
248 ehdp.Max = &vF64
249 }
250 out = append(out, ehdp)
251 }
252 return out
253 }
254
255 // ExponentialHistogramDataPointBuckets returns an OTLP ExponentialHistogramDataPoint_Buckets generated
256 // from bucket.
257 func ExponentialHistogramDataPointBuckets(
258 bucket metricdata.ExponentialBucket,
259 ) *mpb.ExponentialHistogramDataPoint_Buckets {
260 return &mpb.ExponentialHistogramDataPoint_Buckets{
261 Offset: bucket.Offset,
262 BucketCounts: bucket.Counts,
263 }
264 }
265
266 // Temporality returns an OTLP AggregationTemporality generated from t. If t
267 // is unknown, an error is returned along with the invalid
268 // AggregationTemporality_AGGREGATION_TEMPORALITY_UNSPECIFIED.
269 func Temporality(t metricdata.Temporality) (mpb.AggregationTemporality, error) {
270 switch t {
271 case metricdata.DeltaTemporality:
272 return mpb.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, nil
273 case metricdata.CumulativeTemporality:
274 return mpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, nil
275 default:
276 err := fmt.Errorf("%w: %s", errUnknownTemporality, t)
277 return mpb.AggregationTemporality_AGGREGATION_TEMPORALITY_UNSPECIFIED, err
278 }
279 }
280
281 // timeUnixNano returns t as a Unix time, the number of nanoseconds elapsed
282 // since January 1, 1970 UTC as uint64.
283 // The result is undefined if the Unix time
284 // in nanoseconds cannot be represented by an int64
285 // (a date before the year 1678 or after 2262).
286 // timeUnixNano on the zero Time returns 0.
287 // The result does not depend on the location associated with t.
288 func timeUnixNano(t time.Time) uint64 {
289 return uint64(max(0, t.UnixNano())) // nolint:gosec // Overflow checked.
290 }
291
292 // Exemplars returns a slice of OTLP Exemplars generated from exemplars.
293 func Exemplars[N int64 | float64](exemplars []metricdata.Exemplar[N]) []*mpb.Exemplar {
294 out := make([]*mpb.Exemplar, 0, len(exemplars))
295 for _, exemplar := range exemplars {
296 e := &mpb.Exemplar{
297 FilteredAttributes: KeyValues(exemplar.FilteredAttributes),
298 TimeUnixNano: timeUnixNano(exemplar.Time),
299 SpanId: exemplar.SpanID,
300 TraceId: exemplar.TraceID,
301 }
302 switch v := any(exemplar.Value).(type) {
303 case int64:
304 e.Value = &mpb.Exemplar_AsInt{
305 AsInt: v,
306 }
307 case float64:
308 e.Value = &mpb.Exemplar_AsDouble{
309 AsDouble: v,
310 }
311 }
312 out = append(out, e)
313 }
314 return out
315 }
316
317 // Summary returns an OTLP Metric_Summary generated from s.
318 func Summary(s metricdata.Summary) *mpb.Metric_Summary {
319 return &mpb.Metric_Summary{
320 Summary: &mpb.Summary{
321 DataPoints: SummaryDataPoints(s.DataPoints),
322 },
323 }
324 }
325
326 // SummaryDataPoints returns a slice of OTLP SummaryDataPoint generated from
327 // dPts.
328 func SummaryDataPoints(dPts []metricdata.SummaryDataPoint) []*mpb.SummaryDataPoint {
329 out := make([]*mpb.SummaryDataPoint, 0, len(dPts))
330 for _, dPt := range dPts {
331 sdp := &mpb.SummaryDataPoint{
332 Attributes: AttrIter(dPt.Attributes.Iter()),
333 StartTimeUnixNano: timeUnixNano(dPt.StartTime),
334 TimeUnixNano: timeUnixNano(dPt.Time),
335 Count: dPt.Count,
336 Sum: dPt.Sum,
337 QuantileValues: QuantileValues(dPt.QuantileValues),
338 }
339 out = append(out, sdp)
340 }
341 return out
342 }
343
344 // QuantileValues returns a slice of OTLP SummaryDataPoint_ValueAtQuantile
345 // generated from quantiles.
346 func QuantileValues(quantiles []metricdata.QuantileValue) []*mpb.SummaryDataPoint_ValueAtQuantile {
347 out := make([]*mpb.SummaryDataPoint_ValueAtQuantile, 0, len(quantiles))
348 for _, q := range quantiles {
349 quantile := &mpb.SummaryDataPoint_ValueAtQuantile{
350 Quantile: q.Quantile,
351 Value: q.Value,
352 }
353 out = append(out, quantile)
354 }
355 return out
356 }