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

Adds attribute filter logic #3396

Merged
merged 7 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
319 changes: 319 additions & 0 deletions sdk/metric/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"go.opentelemetry.io/otel/sdk/metric/view"
"go.opentelemetry.io/otel/sdk/resource"
)

Expand Down Expand Up @@ -474,3 +475,321 @@ func TestMetersProvideScope(t *testing.T) {
assert.NoError(t, err)
metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp())
}

func TestAttributeFilter(t *testing.T) {
one := 1.0
two := 2.0
testcases := []struct {
name string
register func(t *testing.T, mtr metric.Meter) error
wantMetric metricdata.Metrics
}{
{
name: "AsyncFloat64Counter",
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.AsyncFloat64().Counter("afcounter")
if err != nil {
return err
}
return mtr.RegisterCallback([]instrument.Asynchronous{ctr}, func(ctx context.Context) {
ctr.Observe(ctx, 1.0, attribute.Int("version", 1))
ctr.Observe(ctx, 2.0, attribute.Int("version", 2))
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
})
},
wantMetric: metricdata.Metrics{
Name: "afcounter",
Data: metricdata.Sum[float64]{
DataPoints: []metricdata.DataPoint[float64]{
{Value: 2.0}, // TODO: This should be 3.0. It is a bug in the sum aggregator
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
},
},
},
{
name: "AsyncFloat64UpDownCounter",
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.AsyncFloat64().UpDownCounter("afupdowncounter")
if err != nil {
return err
}
return mtr.RegisterCallback([]instrument.Asynchronous{ctr}, func(ctx context.Context) {
ctr.Observe(ctx, 1.0, attribute.Int("version", 1))
ctr.Observe(ctx, 2.0, attribute.Int("version", 2))
})
},
wantMetric: metricdata.Metrics{
Name: "afupdowncounter",
Data: metricdata.Sum[float64]{
DataPoints: []metricdata.DataPoint[float64]{
{Value: 2.0}, // TODO: This should be 3.0. It is a bug in the sum aggregator
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: false,
},
},
},
{
name: "AsyncFloat64Gauge",
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.AsyncFloat64().Gauge("afgauge")
if err != nil {
return err
}
return mtr.RegisterCallback([]instrument.Asynchronous{ctr}, func(ctx context.Context) {
ctr.Observe(ctx, 1.0, attribute.Int("version", 1))
ctr.Observe(ctx, 2.0, attribute.Int("version", 2))
})
},
wantMetric: metricdata.Metrics{
Name: "afgauge",
Data: metricdata.Gauge[float64]{
DataPoints: []metricdata.DataPoint[float64]{
{Value: 2.0},
},
},
},
},
{
name: "AsyncFloat64Counter",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.AsyncInt64().Counter("aicounter")
if err != nil {
return err
}
return mtr.RegisterCallback([]instrument.Asynchronous{ctr}, func(ctx context.Context) {
ctr.Observe(ctx, 10, attribute.Int("version", 1))
ctr.Observe(ctx, 20, attribute.Int("version", 2))
})
},
wantMetric: metricdata.Metrics{
Name: "aicounter",
Data: metricdata.Sum[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{Value: 20}, // TODO: This should be 3.0. It is a bug in the sum aggregator
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
},
},
},
{
name: "AsyncFloat64UpDownCounter",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.AsyncInt64().UpDownCounter("aiupdowncounter")
if err != nil {
return err
}
return mtr.RegisterCallback([]instrument.Asynchronous{ctr}, func(ctx context.Context) {
ctr.Observe(ctx, 10, attribute.Int("version", 1))
ctr.Observe(ctx, 20, attribute.Int("version", 2))
})
},
wantMetric: metricdata.Metrics{
Name: "aiupdowncounter",
Data: metricdata.Sum[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{Value: 20}, // TODO: This should be 3.0. It is a bug in the sum aggregator
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: false,
},
},
},
{
name: "AsyncInt64Gauge",
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.AsyncInt64().Gauge("aigauge")
if err != nil {
return err
}
return mtr.RegisterCallback([]instrument.Asynchronous{ctr}, func(ctx context.Context) {
ctr.Observe(ctx, 10, attribute.Int("version", 1))
ctr.Observe(ctx, 20, attribute.Int("version", 2))
})
},
wantMetric: metricdata.Metrics{
Name: "aigauge",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{Value: 20},
},
},
},
},
{
name: "SyncFloatCounter",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.SyncFloat64().Counter("sfcounter")
if err != nil {
return err
}

ctr.Add(context.Background(), 1.0, attribute.Int("version", 1))
ctr.Add(context.Background(), 2.0, attribute.Int("version", 2))
return nil
},
wantMetric: metricdata.Metrics{
Name: "sfcounter",
Data: metricdata.Sum[float64]{
DataPoints: []metricdata.DataPoint[float64]{
{Value: 3.0},
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
},
},
},
{
name: "SyncFloatUpDownCounter",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.SyncFloat64().UpDownCounter("sfupdowncounter")
if err != nil {
return err
}

ctr.Add(context.Background(), 1.0, attribute.Int("version", 1))
ctr.Add(context.Background(), 2.0, attribute.Int("version", 2))
return nil
},
wantMetric: metricdata.Metrics{
Name: "sfupdowncounter",
Data: metricdata.Sum[float64]{
DataPoints: []metricdata.DataPoint[float64]{
{Value: 3.0},
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: false,
},
},
},
{
name: "SyncFloatHistogram",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.SyncFloat64().Histogram("sfhistogram")
if err != nil {
return err
}

ctr.Record(context.Background(), 1.0, attribute.Int("version", 1))
ctr.Record(context.Background(), 2.0, attribute.Int("version", 2))
return nil
},
wantMetric: metricdata.Metrics{
Name: "sfhistogram",
Data: metricdata.Histogram{
DataPoints: []metricdata.HistogramDataPoint{
{
Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000},
BucketCounts: []uint64{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Count: 2,
Min: &one,
Max: &two,
Sum: 3.0,
},
},
Temporality: metricdata.CumulativeTemporality,
},
},
},
{
name: "SyncIntCounter",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.SyncInt64().Counter("sicounter")
if err != nil {
return err
}

ctr.Add(context.Background(), 10, attribute.Int("version", 1))
ctr.Add(context.Background(), 20, attribute.Int("version", 2))
return nil
},
wantMetric: metricdata.Metrics{
Name: "sicounter",
Data: metricdata.Sum[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{Value: 30},
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
},
},
},
{
name: "SyncIntUpDownCounter",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.SyncInt64().UpDownCounter("siupdowncounter")
if err != nil {
return err
}

ctr.Add(context.Background(), 10, attribute.Int("version", 1))
ctr.Add(context.Background(), 20, attribute.Int("version", 2))
return nil
},
wantMetric: metricdata.Metrics{
Name: "siupdowncounter",
Data: metricdata.Sum[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{Value: 30},
},
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: false,
},
},
},
{
name: "SyncIntHistogram",
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
register: func(t *testing.T, mtr metric.Meter) error {
ctr, err := mtr.SyncInt64().Histogram("sihistogram")
if err != nil {
return err
}

ctr.Record(context.Background(), 1, attribute.Int("version", 1))
ctr.Record(context.Background(), 2, attribute.Int("version", 2))
return nil
},
wantMetric: metricdata.Metrics{
Name: "sihistogram",
Data: metricdata.Histogram{
DataPoints: []metricdata.HistogramDataPoint{
{
Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000},
BucketCounts: []uint64{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Count: 2,
Min: &one,
Max: &two,
Sum: 3.0,
},
},
Temporality: metricdata.CumulativeTemporality,
},
},
},
}

for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
v, err := view.New(
view.MatchInstrumentName("*"),
view.WithFilterAttributes(attribute.Key("foo")),
)
require.NoError(t, err)
rdr := NewManualReader()
mtr := NewMeterProvider(WithReader(rdr, v)).Meter("TestAttributeFilter")

err = tt.register(t, mtr)
require.NoError(t, err)

m, err := rdr.Collect(context.Background())
assert.NoError(t, err)

require.Len(t, m.ScopeMetrics, 1)
require.Len(t, m.ScopeMetrics[0].Metrics, 1)
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved

metricdatatest.AssertEqual(t, tt.wantMetric, m.ScopeMetrics[0].Metrics[0], metricdatatest.IgnoreTimestamp())
})
}
}
11 changes: 8 additions & 3 deletions sdk/metric/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"sync"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/instrumentation"
Expand Down Expand Up @@ -203,7 +204,7 @@ func (i *inserter[N]) Instrument(inst view.Instrument, instUnit unit.Unit) ([]in
}
matched = true

agg, err := i.cachedAggregator(inst, instUnit)
agg, err := i.cachedAggregator(inst, instUnit, v.AttributeFilter())
if err != nil {
errs.append(err)
}
Expand All @@ -223,7 +224,7 @@ func (i *inserter[N]) Instrument(inst view.Instrument, instUnit unit.Unit) ([]in
}

// Apply implicit default view if no explicit matched.
agg, err := i.cachedAggregator(inst, instUnit)
agg, err := i.cachedAggregator(inst, instUnit, nil)
if err != nil {
errs.append(err)
}
Expand All @@ -247,7 +248,7 @@ func (i *inserter[N]) Instrument(inst view.Instrument, instUnit unit.Unit) ([]in
//
// If the instrument defines an unknown or incompatible aggregation, an error
// is returned.
func (i *inserter[N]) cachedAggregator(inst view.Instrument, u unit.Unit) (internal.Aggregator[N], error) {
func (i *inserter[N]) cachedAggregator(inst view.Instrument, u unit.Unit, filter func(attribute.Set) attribute.Set) (internal.Aggregator[N], error) {
switch inst.Aggregation.(type) {
case nil, aggregation.Default:
// Undefined, nil, means to use the default from the reader.
Expand All @@ -273,6 +274,10 @@ func (i *inserter[N]) cachedAggregator(inst view.Instrument, u unit.Unit) (inter
if agg == nil { // Drop aggregator.
return nil, nil
}
if filter != nil {
agg = internal.NewFilter(agg, filter)
}

i.pipeline.addSync(inst.Scope, instrumentSync{
name: inst.Name,
description: inst.Description,
Expand Down