Debian Codegraph @

sdk/metric/internal/aggregate/aggregate.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
5
6 import (
7 "context"
8 "time"
9
10 "go.opentelemetry.io/otel/attribute"
11 "go.opentelemetry.io/otel/sdk/metric/metricdata"
12 )
13
14 // now is used to return the current local time while allowing tests to
15 // override the default time.Now function.
16 var now = time.Now
17
18 // Measure receives measurements to be aggregated.
19 type Measure[N int64 | float64] func(context.Context, N, attribute.Set)
20
21 // ComputeAggregation stores the aggregate of measurements into dest and
22 // returns the number of aggregate data-points output.
23 type ComputeAggregation func(dest *metricdata.Aggregation) int
24
25 // Builder builds an aggregate function.
26 type Builder[N int64 | float64] struct {
27 // Temporality is the temporality used for the returned aggregate function.
28 //
29 // If this is not provided a default of cumulative will be used (except for
30 // the last-value aggregate function where delta is the only appropriate
31 // temporality).
32 Temporality metricdata.Temporality
33 // Filter is the attribute filter the aggregate function will use on the
34 // input of measurements.
35 Filter attribute.Filter
36 // ReservoirFunc is the factory function used by aggregate functions to
37 // create new exemplar reservoirs for a new seen attribute set.
38 //
39 // If this is not provided a default factory function that returns an
40 // dropReservoir reservoir will be used.
41 ReservoirFunc func(attribute.Set) FilteredExemplarReservoir[N]
42 // AggregationLimit is the cardinality limit of measurement attributes. Any
43 // measurement for new attributes once the limit has been reached will be
44 // aggregated into a single aggregate for the "otel.metric.overflow"
45 // attribute.
46 //
47 // If AggregationLimit is less than or equal to zero there will not be an
48 // aggregation limit imposed (i.e. unlimited attribute sets).
49 AggregationLimit int
50 }
51
52 func (b Builder[N]) resFunc() func(attribute.Set) FilteredExemplarReservoir[N] {
53 if b.ReservoirFunc != nil {
54 return b.ReservoirFunc
55 }
56
57 return dropReservoir
58 }
59
60 type fltrMeasure[N int64 | float64] func(ctx context.Context, value N, fltrAttr attribute.Set, droppedAttr []attribute.KeyValue)
61
62 func (b Builder[N]) filter(f fltrMeasure[N]) Measure[N] {
63 if b.Filter != nil {
64 fltr := b.Filter // Copy to make it immutable after assignment.
65 return func(ctx context.Context, n N, a attribute.Set) {
66 fAttr, dropped := a.Filter(fltr)
67 f(ctx, n, fAttr, dropped)
68 }
69 }
70 return func(ctx context.Context, n N, a attribute.Set) {
71 f(ctx, n, a, nil)
72 }
73 }
74
75 // LastValue returns a last-value aggregate function input and output.
76 func (b Builder[N]) LastValue() (Measure[N], ComputeAggregation) {
77 switch b.Temporality {
78 case metricdata.DeltaTemporality:
79 lv := newDeltaLastValue[N](b.AggregationLimit, b.resFunc())
80 return b.filter(lv.measure), lv.collect
81 default:
82 lv := newCumulativeLastValue[N](b.AggregationLimit, b.resFunc())
83 return b.filter(lv.measure), lv.collect
84 }
85 }
86
87 // PrecomputedLastValue returns a last-value aggregate function input and
88 // output. The aggregation returned from the returned ComputeAggregation
89 // function will always only return values from the previous collection cycle.
90 func (b Builder[N]) PrecomputedLastValue() (Measure[N], ComputeAggregation) {
91 lv := newPrecomputedLastValue[N](b.AggregationLimit, b.resFunc())
92 switch b.Temporality {
93 case metricdata.DeltaTemporality:
94 return b.filter(lv.measure), lv.delta
95 default:
96 return b.filter(lv.measure), lv.cumulative
97 }
98 }
99
100 // PrecomputedSum returns a sum aggregate function input and output. The
101 // arguments passed to the input are expected to be the precomputed sum values.
102 func (b Builder[N]) PrecomputedSum(monotonic bool) (Measure[N], ComputeAggregation) {
103 s := newPrecomputedSum[N](monotonic, b.AggregationLimit, b.resFunc())
104 switch b.Temporality {
105 case metricdata.DeltaTemporality:
106 return b.filter(s.measure), s.delta
107 default:
108 return b.filter(s.measure), s.cumulative
109 }
110 }
111
112 // Sum returns a sum aggregate function input and output.
113 func (b Builder[N]) Sum(monotonic bool) (Measure[N], ComputeAggregation) {
114 switch b.Temporality {
115 case metricdata.DeltaTemporality:
116 s := newDeltaSum[N](monotonic, b.AggregationLimit, b.resFunc())
117 return b.filter(s.measure), s.collect
118 default:
119 s := newCumulativeSum[N](monotonic, b.AggregationLimit, b.resFunc())
120 return b.filter(s.measure), s.collect
121 }
122 }
123
124 // ExplicitBucketHistogram returns a histogram aggregate function input and
125 // output.
126 func (b Builder[N]) ExplicitBucketHistogram(
127 boundaries []float64,
128 noMinMax, noSum bool,
129 ) (Measure[N], ComputeAggregation) {
130 switch b.Temporality {
131 case metricdata.DeltaTemporality:
132 h := newDeltaHistogram[N](boundaries, noMinMax, noSum, b.AggregationLimit, b.resFunc())
133 return b.filter(h.measure), h.collect
134 default:
135 h := newCumulativeHistogram[N](boundaries, noMinMax, noSum, b.AggregationLimit, b.resFunc())
136 return b.filter(h.measure), h.collect
137 }
138 }
139
140 // ExponentialBucketHistogram returns a histogram aggregate function input and
141 // output.
142 func (b Builder[N]) ExponentialBucketHistogram(
143 maxSize, maxScale int32,
144 noMinMax, noSum bool,
145 ) (Measure[N], ComputeAggregation) {
146 h := newExponentialHistogram[N](maxSize, maxScale, noMinMax, noSum, b.AggregationLimit, b.resFunc())
147 switch b.Temporality {
148 case metricdata.DeltaTemporality:
149 return b.filter(h.measure), h.delta
150 default:
151 return b.filter(h.measure), h.cumulative
152 }
153 }
154
155 // reset ensures s has capacity and sets it length. If the capacity of s too
156 // small, a new slice is returned with the specified capacity and length.
157 func reset[T any](s []T, length, capacity int) []T {
158 if cap(s) < capacity {
159 return make([]T, length, capacity)
160 }
161 return s[:length]
162 }