| 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 "go.opentelemetry.io/otel/attribute" |
| 7 |
|
| 8 |
// overflowSet is the attribute set used to record a measurement when adding |
| 9 |
// another distinct attribute set to the aggregate would exceed the aggregate |
| 10 |
// limit. |
| 11 |
var overflowSet = attribute.NewSet(attribute.Bool("otel.metric.overflow", true)) |
| 12 |
|
| 13 |
// limiter limits aggregate values. |
| 14 |
type limiter[V any] struct { |
| 15 |
// aggLimit is the maximum number of metric streams that can be aggregated. |
| 16 |
// |
| 17 |
// Any metric stream with attributes distinct from any set already |
| 18 |
// aggregated once the aggLimit will be meet will instead be aggregated |
| 19 |
// into an "overflow" metric stream. That stream will only contain the |
| 20 |
// "otel.metric.overflow"=true attribute. |
| 21 |
aggLimit int |
| 22 |
} |
| 23 |
|
| 24 |
// newLimiter returns a new Limiter with the provided aggregation limit. |
| 25 |
func newLimiter[V any](aggregation int) limiter[V] { |
| 26 |
return limiter[V]{aggLimit: aggregation} |
| 27 |
} |
| 28 |
|
| 29 |
// Attributes checks if adding a measurement for attrs will exceed the |
| 30 |
// aggregation cardinality limit for the existing measurements. If it will, |
| 31 |
// overflowSet is returned. Otherwise, if it will not exceed the limit, or the |
| 32 |
// limit is not set (limit <= 0), attr is returned. |
| 33 |
func (l limiter[V]) Attributes(attrs attribute.Set, measurements map[attribute.Distinct]*V) attribute.Set { |
| 34 |
if l.aggLimit > 0 { |
| 35 |
_, exists := measurements[attrs.Equivalent()] |
| 36 |
if !exists && len(measurements) >= l.aggLimit-1 { |
| 37 |
return overflowSet |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return attrs |
| 42 |
} |