Skip to content

Commit

Permalink
main: add test case to reproduce the behaviour, it will fail as of no…
Browse files Browse the repository at this point in the history
…w, once the fix goes it will pass
  • Loading branch information
pree-dew committed Oct 19, 2024
1 parent 2578acc commit 04a0ff9
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions sdk/metric/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"sync"
"testing"
"time"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
Expand Down Expand Up @@ -518,3 +519,45 @@ func TestExemplars(t *testing.T) {
check(t, r, 2, 2, 2)
})
}

func TestPipelineWithMultipleReaders(t *testing.T) {
exp := &fnExporter{}
r1 := NewPeriodicReader(exp, WithInterval(1*time.Second))
r2 := NewPeriodicReader(exp, WithInterval(600*time.Second))

mp := NewMeterProvider(WithReader(r1), WithReader(r2))
m := mp.Meter("test")

var val int64 = 1
measure := func(_ context.Context, m metric.Meter) {
oc, err := m.Int64ObservableCounter("int64-observable-counter")
require.NoError(t, err)
_, err = m.RegisterCallback(
// SDK periodically calls this function to collect data.
func(_ context.Context, o metric.Observer) error {
o.ObserveInt64(oc, val)
return nil
}, oc)
require.NoError(t, err)
}
ctx := context.Background()
measure(ctx, m)
rm := new(metricdata.ResourceMetrics)
val++

// adding sleep deliberately so that the callback get triggered
time.Sleep(2 * time.Second)
err := r1.Collect(ctx, rm)
require.NoError(t, err)
assert.Len(t, rm.ScopeMetrics, 1)
assert.Len(t, rm.ScopeMetrics[0].Metrics, 1)
assert.Equal(t, int64(2), rm.ScopeMetrics[0].Metrics[0].Data.(metricdata.Sum[int64]).DataPoints[0].Value)

val++
time.Sleep(1 * time.Second)
err = r2.Collect(ctx, rm)
require.NoError(t, err)
assert.Len(t, rm.ScopeMetrics, 1)
assert.Len(t, rm.ScopeMetrics[0].Metrics, 1)
assert.Equal(t, int64(3), rm.ScopeMetrics[0].Metrics[0].Data.(metricdata.Sum[int64]).DataPoints[0].Value)
}

0 comments on commit 04a0ff9

Please sign in to comment.