mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-26 03:52:03 +02:00
e3bf787c21
* Add agg limiting func * Add unit test for limitAttr * Add limiting to aggregate types * Add internal x pkg for experimental feature-flagging * Connect cardinality limit to metric SDK * Replace limitAttr fn with limiter type The Attribute method is still inlinable. * Use x.CardinalityLimit directly * Simplify limiter test * Add limiter benchmark * Document the AggregationLimit field * Test sum limits * Test limit for last value * Test histogram limit * Refactor expo hist test to use existing fixtures The tests for the exponential histogram create their own testing fixtures. There is nothing these new fixtures do that cannot already be done with the existing testing fixtures used by all the other aggregate functions. Unify the exponential histogram testing to use the existing fixtures. * Test the ExponentialHistogram limit * Fix lint * Add docs * Rename aggregation field to aggLimit --------- Co-authored-by: Robert Pająk <pellared@hotmail.com>
131 lines
3.0 KiB
Go
131 lines
3.0 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
)
|
|
|
|
func TestLastValue(t *testing.T) {
|
|
t.Cleanup(mockTime(now))
|
|
|
|
t.Run("Int64", testLastValue[int64]())
|
|
t.Run("Float64", testLastValue[float64]())
|
|
}
|
|
|
|
func testLastValue[N int64 | float64]() func(*testing.T) {
|
|
in, out := Builder[N]{
|
|
Filter: attrFltr,
|
|
AggregationLimit: 3,
|
|
}.LastValue()
|
|
ctx := context.Background()
|
|
return test[N](in, out, []teststep[N]{
|
|
{
|
|
// Empty output if nothing is measured.
|
|
input: []arg[N]{},
|
|
expect: output{n: 0, agg: metricdata.Gauge[N]{}},
|
|
}, {
|
|
input: []arg[N]{
|
|
{ctx, 1, alice},
|
|
{ctx, -1, bob},
|
|
{ctx, 1, fltrAlice},
|
|
{ctx, 2, alice},
|
|
{ctx, -10, bob},
|
|
},
|
|
expect: output{
|
|
n: 2,
|
|
agg: metricdata.Gauge[N]{
|
|
DataPoints: []metricdata.DataPoint[N]{
|
|
{
|
|
Attributes: fltrAlice,
|
|
Time: staticTime,
|
|
Value: 2,
|
|
},
|
|
{
|
|
Attributes: fltrBob,
|
|
Time: staticTime,
|
|
Value: -10,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, {
|
|
// Everything resets, do not report old measurements.
|
|
input: []arg[N]{},
|
|
expect: output{n: 0, agg: metricdata.Gauge[N]{}},
|
|
}, {
|
|
input: []arg[N]{
|
|
{ctx, 10, alice},
|
|
{ctx, 3, bob},
|
|
},
|
|
expect: output{
|
|
n: 2,
|
|
agg: metricdata.Gauge[N]{
|
|
DataPoints: []metricdata.DataPoint[N]{
|
|
{
|
|
Attributes: fltrAlice,
|
|
Time: staticTime,
|
|
Value: 10,
|
|
},
|
|
{
|
|
Attributes: fltrBob,
|
|
Time: staticTime,
|
|
Value: 3,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}, {
|
|
input: []arg[N]{
|
|
{ctx, 1, alice},
|
|
{ctx, 1, bob},
|
|
// These will exceed cardinality limit.
|
|
{ctx, 1, carol},
|
|
{ctx, 1, dave},
|
|
},
|
|
expect: output{
|
|
n: 3,
|
|
agg: metricdata.Gauge[N]{
|
|
DataPoints: []metricdata.DataPoint[N]{
|
|
{
|
|
Attributes: fltrAlice,
|
|
Time: staticTime,
|
|
Value: 1,
|
|
},
|
|
{
|
|
Attributes: fltrBob,
|
|
Time: staticTime,
|
|
Value: 1,
|
|
},
|
|
{
|
|
Attributes: overflowSet,
|
|
Time: staticTime,
|
|
Value: 1,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func BenchmarkLastValue(b *testing.B) {
|
|
b.Run("Int64", benchmarkAggregate(Builder[int64]{}.LastValue))
|
|
b.Run("Float64", benchmarkAggregate(Builder[float64]{}.LastValue))
|
|
}
|