From a0ff6d819f30c1747dd97848f97dceb8f7fa7747 Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Tue, 25 Oct 2022 18:37:50 +0000 Subject: [PATCH 1/4] Adds attribute filter logic --- sdk/metric/meter_test.go | 319 +++++++++++++++++++++++++++++++++++++++ sdk/metric/pipeline.go | 11 +- 2 files changed, 327 insertions(+), 3 deletions(-) diff --git a/sdk/metric/meter_test.go b/sdk/metric/meter_test.go index 8f6448f757e..0a221debe86 100644 --- a/sdk/metric/meter_test.go +++ b/sdk/metric/meter_test.go @@ -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" ) @@ -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)) + }) + }, + 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 + }, + 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", + 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 + }, + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + }, + }, + }, + { + name: "AsyncFloat64UpDownCounter", + 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 + }, + 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", + 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", + 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", + 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", + 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", + 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", + 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) + + metricdatatest.AssertEqual(t, tt.wantMetric, m.ScopeMetrics[0].Metrics[0], metricdatatest.IgnoreTimestamp()) + }) + } +} diff --git a/sdk/metric/pipeline.go b/sdk/metric/pipeline.go index db70003b1d5..5d36e867727 100644 --- a/sdk/metric/pipeline.go +++ b/sdk/metric/pipeline.go @@ -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" @@ -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) } @@ -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) } @@ -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. @@ -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, From f034fb4e113b68181fea1b5452322375a6c4ffa1 Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Tue, 1 Nov 2022 15:42:27 +0000 Subject: [PATCH 2/4] Apply PR feedback --- sdk/metric/meter_test.go | 116 +++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 42 deletions(-) diff --git a/sdk/metric/meter_test.go b/sdk/metric/meter_test.go index c30da384760..912992b3956 100644 --- a/sdk/metric/meter_test.go +++ b/sdk/metric/meter_test.go @@ -492,15 +492,18 @@ func TestAttributeFilter(t *testing.T) { 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)) + ctr.Observe(ctx, 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Observe(ctx, 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2)) }) }, 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 + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 2.0, // TODO: This should be 3.0. It is a bug in the sum aggregator + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: true, @@ -515,15 +518,18 @@ func TestAttributeFilter(t *testing.T) { 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)) + ctr.Observe(ctx, 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Observe(ctx, 2.0, attribute.String("foo", "bar"), 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 + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 2.0, // TODO: This should be 3.0. It is a bug in the sum aggregator + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: false, @@ -538,36 +544,42 @@ func TestAttributeFilter(t *testing.T) { 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)) + ctr.Observe(ctx, 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Observe(ctx, 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2)) }) }, wantMetric: metricdata.Metrics{ Name: "afgauge", Data: metricdata.Gauge[float64]{ DataPoints: []metricdata.DataPoint[float64]{ - {Value: 2.0}, + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 2.0, + }, }, }, }, }, { - name: "AsyncFloat64Counter", + name: "AsyncInt64Counter", 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)) + ctr.Observe(ctx, 10, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Observe(ctx, 20, attribute.String("foo", "bar"), 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 + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 20, // TODO: This should be 30. It is a bug in the sum aggregator + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: true, @@ -575,22 +587,25 @@ func TestAttributeFilter(t *testing.T) { }, }, { - name: "AsyncFloat64UpDownCounter", + name: "AsyncInt64UpDownCounter", 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)) + ctr.Observe(ctx, 10, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Observe(ctx, 20, attribute.String("foo", "bar"), 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 + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 20, // TODO: This should be 30. It is a bug in the sum aggregator + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: false, @@ -605,36 +620,42 @@ func TestAttributeFilter(t *testing.T) { 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)) + ctr.Observe(ctx, 10, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Observe(ctx, 20, attribute.String("foo", "bar"), attribute.Int("version", 2)) }) }, wantMetric: metricdata.Metrics{ Name: "aigauge", Data: metricdata.Gauge[int64]{ DataPoints: []metricdata.DataPoint[int64]{ - {Value: 20}, + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 20, + }, }, }, }, }, { - name: "SyncFloatCounter", + name: "SyncFloat64Counter", 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)) + ctr.Add(context.Background(), 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Add(context.Background(), 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2)) return nil }, wantMetric: metricdata.Metrics{ Name: "sfcounter", Data: metricdata.Sum[float64]{ DataPoints: []metricdata.DataPoint[float64]{ - {Value: 3.0}, + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 3.0, + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: true, @@ -642,22 +663,25 @@ func TestAttributeFilter(t *testing.T) { }, }, { - name: "SyncFloatUpDownCounter", + name: "SyncFloat64UpDownCounter", 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)) + ctr.Add(context.Background(), 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Add(context.Background(), 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2)) return nil }, wantMetric: metricdata.Metrics{ Name: "sfupdowncounter", Data: metricdata.Sum[float64]{ DataPoints: []metricdata.DataPoint[float64]{ - {Value: 3.0}, + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 3.0, + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: false, @@ -665,15 +689,15 @@ func TestAttributeFilter(t *testing.T) { }, }, { - name: "SyncFloatHistogram", + name: "SyncFloat64Histogram", 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)) + ctr.Record(context.Background(), 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Record(context.Background(), 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2)) return nil }, wantMetric: metricdata.Metrics{ @@ -681,6 +705,7 @@ func TestAttributeFilter(t *testing.T) { Data: metricdata.Histogram{ DataPoints: []metricdata.HistogramDataPoint{ { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), 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, @@ -694,22 +719,25 @@ func TestAttributeFilter(t *testing.T) { }, }, { - name: "SyncIntCounter", + name: "SyncInt64Counter", 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)) + ctr.Add(context.Background(), 10, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Add(context.Background(), 20, attribute.String("foo", "bar"), attribute.Int("version", 2)) return nil }, wantMetric: metricdata.Metrics{ Name: "sicounter", Data: metricdata.Sum[int64]{ DataPoints: []metricdata.DataPoint[int64]{ - {Value: 30}, + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 30, + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: true, @@ -717,22 +745,25 @@ func TestAttributeFilter(t *testing.T) { }, }, { - name: "SyncIntUpDownCounter", + name: "SyncInt64UpDownCounter", 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)) + ctr.Add(context.Background(), 10, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Add(context.Background(), 20, attribute.String("foo", "bar"), attribute.Int("version", 2)) return nil }, wantMetric: metricdata.Metrics{ Name: "siupdowncounter", Data: metricdata.Sum[int64]{ DataPoints: []metricdata.DataPoint[int64]{ - {Value: 30}, + { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), + Value: 30, + }, }, Temporality: metricdata.CumulativeTemporality, IsMonotonic: false, @@ -740,15 +771,15 @@ func TestAttributeFilter(t *testing.T) { }, }, { - name: "SyncIntHistogram", + name: "SyncInt64Histogram", 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)) + ctr.Record(context.Background(), 1, attribute.String("foo", "bar"), attribute.Int("version", 1)) + ctr.Record(context.Background(), 2, attribute.String("foo", "bar"), attribute.Int("version", 2)) return nil }, wantMetric: metricdata.Metrics{ @@ -756,6 +787,7 @@ func TestAttributeFilter(t *testing.T) { Data: metricdata.Histogram{ DataPoints: []metricdata.HistogramDataPoint{ { + Attributes: attribute.NewSet(attribute.String("foo", "bar")), 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, From f9676b5f0b2ec4ca584dfa9f4b1e3b75273a3598 Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Tue, 1 Nov 2022 15:50:04 +0000 Subject: [PATCH 3/4] Use updated MP options --- sdk/metric/meter_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/metric/meter_test.go b/sdk/metric/meter_test.go index 912992b3956..858d2a1d9dc 100644 --- a/sdk/metric/meter_test.go +++ b/sdk/metric/meter_test.go @@ -810,7 +810,10 @@ func TestAttributeFilter(t *testing.T) { ) require.NoError(t, err) rdr := NewManualReader() - mtr := NewMeterProvider(WithReader(rdr, v)).Meter("TestAttributeFilter") + mtr := NewMeterProvider( + WithReader(rdr), + WithView(v), + ).Meter("TestAttributeFilter") err = tt.register(t, mtr) require.NoError(t, err) From ec165d635a98288c744659425242e9c363dee7d8 Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Thu, 3 Nov 2022 20:57:30 +0000 Subject: [PATCH 4/4] Update Changelog and TODO numbers --- CHANGELOG.md | 1 + sdk/metric/meter_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43c083fba4d..99b77f41d80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Cumulative metrics from the OpenCensus bridge (`go.opentelemetry.io/otel/bridge/opencensus`) are defined as monotonic sums, instead of non-monotonic. (#3389) - Asynchronous counters (`Counter` and `UpDownCounter`) from the metric SDK now produce delta sums when configured with delta temporality. (#3398) - Exported `Status` codes in the `go.opentelemetry.io/otel/exporters/zipkin` exporter are now exported as all upper case values. (#3340) +- Reenabled Attribute Filters in the Metric SDK. (#3396) ## [1.11.1/0.33.0] 2022-10-19 diff --git a/sdk/metric/meter_test.go b/sdk/metric/meter_test.go index 858d2a1d9dc..7cc7e307466 100644 --- a/sdk/metric/meter_test.go +++ b/sdk/metric/meter_test.go @@ -502,7 +502,7 @@ func TestAttributeFilter(t *testing.T) { DataPoints: []metricdata.DataPoint[float64]{ { Attributes: attribute.NewSet(attribute.String("foo", "bar")), - Value: 2.0, // TODO: This should be 3.0. It is a bug in the sum aggregator + Value: 2.0, // TODO (#3439): This should be 3.0. }, }, Temporality: metricdata.CumulativeTemporality, @@ -528,7 +528,7 @@ func TestAttributeFilter(t *testing.T) { DataPoints: []metricdata.DataPoint[float64]{ { Attributes: attribute.NewSet(attribute.String("foo", "bar")), - Value: 2.0, // TODO: This should be 3.0. It is a bug in the sum aggregator + Value: 2.0, // TODO (#3439): This should be 3.0. }, }, Temporality: metricdata.CumulativeTemporality, @@ -578,7 +578,7 @@ func TestAttributeFilter(t *testing.T) { DataPoints: []metricdata.DataPoint[int64]{ { Attributes: attribute.NewSet(attribute.String("foo", "bar")), - Value: 20, // TODO: This should be 30. It is a bug in the sum aggregator + Value: 20, // TODO (#3439): This should be 30. }, }, Temporality: metricdata.CumulativeTemporality, @@ -604,7 +604,7 @@ func TestAttributeFilter(t *testing.T) { DataPoints: []metricdata.DataPoint[int64]{ { Attributes: attribute.NewSet(attribute.String("foo", "bar")), - Value: 20, // TODO: This should be 30. It is a bug in the sum aggregator + Value: 20, // TODO (#3439): This should be 30. }, }, Temporality: metricdata.CumulativeTemporality,