Skip to content

Commit

Permalink
Replace filter aggregator with direct filter on measure (#4342)
Browse files Browse the repository at this point in the history
* Replace filter aggregator with direct filter on measure

Part of #4220

Instead of using an aggregator to filter measured attributes, directly
filter the attributes in the constructed Measure function the Builder
creates.

Include unit and integration testing of new filtering.

* Update sdk/metric/internal/aggregate/aggregate.go

Co-authored-by: David Ashpole <[email protected]>

---------

Co-authored-by: David Ashpole <[email protected]>
  • Loading branch information
MrAlias and dashpole authored Jul 20, 2023
1 parent a5c82bf commit a37cb05
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 258 deletions.
6 changes: 5 additions & 1 deletion sdk/metric/internal/aggregate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ type Builder[N int64 | float64] struct {

func (b Builder[N]) input(agg aggregator[N]) Measure[N] {
if b.Filter != nil {
agg = newFilter[N](agg, b.Filter)
fltr := b.Filter // Copy to make it immutable after assignment.
return func(_ context.Context, n N, a attribute.Set) {
fAttr, _ := a.Filter(fltr)
agg.Aggregate(n, fAttr)
}
}
return func(_ context.Context, n N, a attribute.Set) {
agg.Aggregate(n, a)
Expand Down
75 changes: 75 additions & 0 deletions sdk/metric/internal/aggregate/aggregate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/attribute"
)

var (
keyUser = "user"
userAlice = attribute.String(keyUser, "Alice")
adminTrue = attribute.Bool("admin", true)

alice = attribute.NewSet(userAlice, adminTrue)

// Filtered.
attrFltr = func(kv attribute.KeyValue) bool {
return kv.Key == attribute.Key(keyUser)
}
fltrAlice = attribute.NewSet(userAlice)
)

type inputTester[N int64 | float64] struct {
aggregator[N]

value N
attr attribute.Set
}

func (it *inputTester[N]) Aggregate(v N, a attribute.Set) { it.value, it.attr = v, a }

func TestBuilderInput(t *testing.T) {
t.Run("Int64", testBuilderInput[int64]())
t.Run("Float64", testBuilderInput[float64]())
}

func testBuilderInput[N int64 | float64]() func(t *testing.T) {
return func(t *testing.T) {
t.Helper()

value, attr := N(1), alice
run := func(b Builder[N], wantA attribute.Set) func(*testing.T) {
return func(t *testing.T) {
t.Helper()

it := &inputTester[N]{}
meas := b.input(it)
meas(context.Background(), value, attr)

assert.Equal(t, value, it.value, "measured incorrect value")
assert.Equal(t, wantA, it.attr, "measured incorrect attributes")
}
}

t.Run("NoFilter", run(Builder[N]{}, attr))
t.Run("Filter", run(Builder[N]{Filter: attrFltr}, fltrAlice))
}
}
5 changes: 2 additions & 3 deletions sdk/metric/internal/aggregate/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ const (
)

var (
alice = attribute.NewSet(attribute.String("user", "alice"), attribute.Bool("admin", true))
bob = attribute.NewSet(attribute.String("user", "bob"), attribute.Bool("admin", false))
carol = attribute.NewSet(attribute.String("user", "carol"), attribute.Bool("admin", false))
bob = attribute.NewSet(attribute.String(keyUser, "bob"), attribute.Bool("admin", false))
carol = attribute.NewSet(attribute.String(keyUser, "carol"), attribute.Bool("admin", false))

// Sat Jan 01 2000 00:00:00 GMT+0000.
staticTime = time.Unix(946684800, 0)
Expand Down
58 changes: 0 additions & 58 deletions sdk/metric/internal/aggregate/filter.go

This file was deleted.

196 changes: 0 additions & 196 deletions sdk/metric/internal/aggregate/filter_test.go

This file was deleted.

0 comments on commit a37cb05

Please sign in to comment.