Skip to content

Commit

Permalink
Add the SampledFilter exemplar Reservoir impl (#4851)
Browse files Browse the repository at this point in the history
This Reservoir implementaiton is used at the MeterProvider level to
pre-filter measurements offered to a wrapped Reservoir.

Co-authored-by: Robert Pająk <[email protected]>
  • Loading branch information
MrAlias and pellared authored Jan 25, 2024
1 parent 08beb8b commit 79371c1
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
40 changes: 40 additions & 0 deletions sdk/metric/internal/exemplar/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar"

import (
"context"
"time"

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

// SampledFilter returns a [Reservoir] wrapping r that will only offer measurements
// to r if the passed context associated with the measurement contains a sampled
// [go.opentelemetry.io/otel/trace.SpanContext].
func SampledFilter[N int64 | float64](r Reservoir[N]) Reservoir[N] {
return filtered[N]{Reservoir: r}
}

type filtered[N int64 | float64] struct {
Reservoir[N]
}

func (f filtered[N]) Offer(ctx context.Context, t time.Time, n N, a []attribute.KeyValue) {
if trace.SpanContextFromContext(ctx).IsSampled() {
f.Reservoir.Offer(ctx, t, n, a)
}
}
77 changes: 77 additions & 0 deletions sdk/metric/internal/exemplar/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar"

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/trace"
)

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

func testSampledFiltered[N int64 | float64](t *testing.T) {
under := &res[N]{}

r := SampledFilter[N](under)

ctx := context.Background()
r.Offer(ctx, staticTime, 0, nil)
assert.False(t, under.OfferCalled, "underlying Reservoir Offer called")
r.Offer(sample(ctx), staticTime, 0, nil)
assert.True(t, under.OfferCalled, "underlying Reservoir Offer not called")

r.Collect(nil)
assert.True(t, under.CollectCalled, "underlying Reservoir Collect not called")

r.Flush(nil)
assert.True(t, under.FlushCalled, "underlying Reservoir Flush not called")
}

func sample(parent context.Context) context.Context {
sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{0x01},
SpanID: trace.SpanID{0x01},
TraceFlags: trace.FlagsSampled,
})
return trace.ContextWithSpanContext(parent, sc)
}

type res[N int64 | float64] struct {
OfferCalled bool
CollectCalled bool
FlushCalled bool
}

func (r *res[N]) Offer(context.Context, time.Time, N, []attribute.KeyValue) {
r.OfferCalled = true
}

func (r *res[N]) Collect(*[]metricdata.Exemplar[N]) {
r.CollectCalled = true
}

func (r *res[N]) Flush(*[]metricdata.Exemplar[N]) {
r.FlushCalled = true
}

0 comments on commit 79371c1

Please sign in to comment.