Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change View Attribute Filter to detect if not set. #3039

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions sdk/metric/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ func (v View) TransformInstrument(inst Instrument) (transformed Instrument, matc
return inst, true
}

// TransformAttributes filters an attribute set to the keys in the View. If no
// filter was provided the original set is returned.
func (v View) TransformAttributes(input attribute.Set) attribute.Set {
// AttributeFilter returns a function that returns only attributes specified by
// WithFilterAttributes.
// If no filter was provided nil is returned.
func (v View) AttributeFilter() func(attribute.Set) attribute.Set {
if v.filter == nil {
return input
return nil
}
return func(input attribute.Set) attribute.Set {
out, _ := input.Filter(v.filter)
return out
}
out, _ := input.Filter(v.filter)
return out
}

// TODO: Provide Transform* for AggregationKind (#2816)
Expand Down
36 changes: 29 additions & 7 deletions sdk/metric/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,32 @@ func TestViewMatchName(t *testing.T) {
}
}

func TestViewTransformAttributes(t *testing.T) {
func TestViewAttributeFilterNoFilter(t *testing.T) {
v, err := New(
MatchInstrumentName("*"),
)
require.NoError(t, err)
filter := v.AttributeFilter()
assert.Nil(t, filter)

v, err = New(
MatchInstrumentName("*"),
WithFilterAttributes(),
)
require.NoError(t, err)
filter = v.AttributeFilter()
assert.Nil(t, filter)

v, err = New(
MatchInstrumentName("*"),
WithFilterAttributes([]attribute.Key{}...),
)
require.NoError(t, err)
filter = v.AttributeFilter()
assert.Nil(t, filter)
}

func TestViewAttributeFilter(t *testing.T) {
inputSet := attribute.NewSet(
attribute.String("foo", "bar"),
attribute.Int("power-level", 9001),
Expand All @@ -321,11 +346,6 @@ func TestViewTransformAttributes(t *testing.T) {
filter []attribute.Key
want attribute.Set
}{
{
name: "empty should match all",
filter: []attribute.Key{},
want: inputSet,
},
{
name: "Match 1",
filter: []attribute.Key{
Expand Down Expand Up @@ -371,8 +391,10 @@ func TestViewTransformAttributes(t *testing.T) {
WithFilterAttributes(tt.filter...),
)
require.NoError(t, err)
filter := v.AttributeFilter()
require.NotNil(t, filter)

got := v.TransformAttributes(inputSet)
got := filter(inputSet)
assert.Equal(t, got.Equivalent(), tt.want.Equivalent())
})
}
Expand Down