Debian Codegraph @

sdk/metric/exemplar.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package metric // import "go.opentelemetry.io/otel/sdk/metric"
5
6 import (
7 "runtime"
8
9 "go.opentelemetry.io/otel/attribute"
10 "go.opentelemetry.io/otel/sdk/metric/exemplar"
11 "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
12 )
13
14 // ExemplarReservoirProviderSelector selects the
15 // [exemplar.ReservoirProvider] to use
16 // based on the [Aggregation] of the metric.
17 type ExemplarReservoirProviderSelector func(Aggregation) exemplar.ReservoirProvider
18
19 // reservoirFunc returns the appropriately configured exemplar reservoir
20 // creation func based on the passed InstrumentKind and filter configuration.
21 func reservoirFunc[N int64 | float64](
22 provider exemplar.ReservoirProvider,
23 filter exemplar.Filter,
24 ) func(attribute.Set) aggregate.FilteredExemplarReservoir[N] {
25 return func(attrs attribute.Set) aggregate.FilteredExemplarReservoir[N] {
26 return aggregate.NewFilteredExemplarReservoir[N](filter, provider(attrs))
27 }
28 }
29
30 // DefaultExemplarReservoirProviderSelector returns the default
31 // [exemplar.ReservoirProvider] for the
32 // provided [Aggregation].
33 //
34 // For explicit bucket histograms with more than 1 bucket, it uses the
35 // [exemplar.HistogramReservoirProvider].
36 // For exponential histograms, it uses the
37 // [exemplar.FixedSizeReservoirProvider]
38 // with a size of min(20, max_buckets).
39 // For all other aggregations, it uses the
40 // [exemplar.FixedSizeReservoirProvider]
41 // with a size equal to the number of CPUs.
42 //
43 // Exemplar default reservoirs MAY change in a minor version bump. No
44 // guarantees are made on the shape or statistical properties of returned
45 // exemplars.
46 func DefaultExemplarReservoirProviderSelector(agg Aggregation) exemplar.ReservoirProvider {
47 // https://github.com/open-telemetry/opentelemetry-specification/blob/d4b241f451674e8f611bb589477680341006ad2b/specification/metrics/sdk.md#exemplar-defaults
48 // Explicit bucket histogram aggregation with more than 1 bucket will
49 // use AlignedHistogramBucketExemplarReservoir.
50 a, ok := agg.(AggregationExplicitBucketHistogram)
51 if ok && len(a.Boundaries) > 0 {
52 return exemplar.HistogramReservoirProvider(a.Boundaries)
53 }
54
55 var n int
56 if a, ok := agg.(AggregationBase2ExponentialHistogram); ok {
57 // Base2 Exponential Histogram Aggregation SHOULD use a
58 // SimpleFixedSizeExemplarReservoir with a reservoir equal to the
59 // smaller of the maximum number of buckets configured on the
60 // aggregation or twenty (e.g. min(20, max_buckets)).
61 n = min(int(a.MaxSize), 20)
62 } else {
63 // https://github.com/open-telemetry/opentelemetry-specification/blob/e94af89e3d0c01de30127a0f423e912f6cda7bed/specification/metrics/sdk.md#simplefixedsizeexemplarreservoir
64 // This Exemplar reservoir MAY take a configuration parameter for
65 // the size of the reservoir. If no size configuration is
66 // provided, the default size MAY be the number of possible
67 // concurrent threads (e.g. number of CPUs) to help reduce
68 // contention. Otherwise, a default size of 1 SHOULD be used.
69 //
70 // Use runtime.GOMAXPROCS instead of runtime.NumCPU to support
71 // containerized environments that may have less than the total number
72 // of logical CPUs available on the local machine allocated to it.
73 n = max(runtime.GOMAXPROCS(0), 1)
74 }
75
76 return exemplar.FixedSizeReservoirProvider(n)
77 }