-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestFixedSizeExemplarConcurrentSafe
This verifies this as a fix to #5814.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metric // import "go.opentelemetry.io/otel/sdk/metric" | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
) | ||
|
||
func TestFixedSizeExemplarConcurrentSafe(t *testing.T) { | ||
// Tests https://github.com/open-telemetry/opentelemetry-go/issues/5814 | ||
|
||
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "always_on") | ||
|
||
r := NewManualReader() | ||
m := NewMeterProvider(WithReader(r)).Meter("exemplar-concurrency") | ||
// Use two instruments to get concurrent access to any shared globals. | ||
i0, err := m.Int64Counter("counter.0") | ||
require.NoError(t, err) | ||
i1, err := m.Int64Counter("counter.1") | ||
require.NoError(t, err) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
add := func() { | ||
i0.Add(ctx, 1) | ||
i1.Add(ctx, 2) | ||
} | ||
|
||
goRoutines := max(10, runtime.NumCPU()) | ||
|
||
var wg sync.WaitGroup | ||
for n := 0; n < goRoutines; n++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
require.NotPanics(t, add) | ||
} | ||
} | ||
}() | ||
} | ||
|
||
const collections = 100 | ||
var rm metricdata.ResourceMetrics | ||
for c := 0; c < collections; c++ { | ||
require.NotPanics(t, func() { _ = r.Collect(ctx, &rm) }) | ||
} | ||
|
||
cancel() | ||
wg.Wait() | ||
} |