| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metric // import "go.opentelemetry.io/otel/sdk/metric" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"runtime" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 15 |
) |
| 16 |
|
| 17 |
func TestFixedSizeExemplarConcurrentSafe(t *testing.T) { |
| 18 |
// Tests https://github.com/open-telemetry/opentelemetry-go/issues/5814 |
| 19 |
|
| 20 |
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "always_on") |
| 21 |
|
| 22 |
r := NewManualReader() |
| 23 |
m := NewMeterProvider(WithReader(r)).Meter("exemplar-concurrency") |
| 24 |
// Use two instruments to get concurrent access to any shared globals. |
| 25 |
i0, err := m.Int64Counter("counter.0") |
| 26 |
require.NoError(t, err) |
| 27 |
i1, err := m.Int64Counter("counter.1") |
| 28 |
require.NoError(t, err) |
| 29 |
|
| 30 |
ctx, cancel := context.WithCancel(t.Context()) |
| 31 |
|
| 32 |
add := func() { |
| 33 |
i0.Add(ctx, 1) |
| 34 |
i1.Add(ctx, 2) |
| 35 |
} |
| 36 |
|
| 37 |
goRoutines := max(10, runtime.NumCPU()) |
| 38 |
|
| 39 |
var wg sync.WaitGroup |
| 40 |
for range goRoutines { |
| 41 |
wg.Go(func() { |
| 42 |
for { |
| 43 |
select { |
| 44 |
case <-ctx.Done(): |
| 45 |
return |
| 46 |
default: |
| 47 |
require.NotPanics(t, add) |
| 48 |
} |
| 49 |
} |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
const collections = 100 |
| 54 |
var rm metricdata.ResourceMetrics |
| 55 |
for range collections { |
| 56 |
require.NotPanics(t, func() { _ = r.Collect(ctx, &rm) }) |
| 57 |
} |
| 58 |
|
| 59 |
cancel() |
| 60 |
wg.Wait() |
| 61 |
} |