| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlpmetrichttp // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 5 |
|
| 6 |
import ( |
| 7 |
"sync" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf" |
| 15 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest" |
| 16 |
"go.opentelemetry.io/otel/sdk/metric" |
| 17 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 18 |
) |
| 19 |
|
| 20 |
func TestExporterClientConcurrentSafe(t *testing.T) { |
| 21 |
const goroutines = 5 |
| 22 |
|
| 23 |
coll, err := otest.NewHTTPCollector("", nil) |
| 24 |
require.NoError(t, err) |
| 25 |
|
| 26 |
ctx := t.Context() |
| 27 |
addr := coll.Addr().String() |
| 28 |
opts := []Option{WithEndpoint(addr), WithInsecure()} |
| 29 |
cfg := oconf.NewHTTPConfig(asHTTPOptions(opts)...) |
| 30 |
client, err := newClient(cfg) |
| 31 |
require.NoError(t, err) |
| 32 |
|
| 33 |
exp, err := newExporter(client, oconf.Config{}) |
| 34 |
require.NoError(t, err) |
| 35 |
rm := new(metricdata.ResourceMetrics) |
| 36 |
|
| 37 |
done := make(chan struct{}) |
| 38 |
var wg, someWork sync.WaitGroup |
| 39 |
for range goroutines { |
| 40 |
wg.Add(1) |
| 41 |
someWork.Add(1) |
| 42 |
go func() { |
| 43 |
defer wg.Done() |
| 44 |
assert.NoError(t, exp.Export(ctx, rm)) |
| 45 |
assert.NoError(t, exp.ForceFlush(ctx)) |
| 46 |
|
| 47 |
// Ensure some work is done before shutting down. |
| 48 |
someWork.Done() |
| 49 |
|
| 50 |
for { |
| 51 |
_ = exp.Export(ctx, rm) |
| 52 |
_ = exp.ForceFlush(ctx) |
| 53 |
|
| 54 |
select { |
| 55 |
case <-done: |
| 56 |
return |
| 57 |
default: |
| 58 |
} |
| 59 |
} |
| 60 |
}() |
| 61 |
} |
| 62 |
|
| 63 |
someWork.Wait() |
| 64 |
assert.NoError(t, exp.Shutdown(ctx)) |
| 65 |
assert.ErrorIs(t, exp.Shutdown(ctx), errShutdown) |
| 66 |
|
| 67 |
close(done) |
| 68 |
wg.Wait() |
| 69 |
} |
| 70 |
|
| 71 |
func TestExporterDoesNotBlockTemporalityAndAggregation(t *testing.T) { |
| 72 |
rCh := make(chan otest.ExportResult, 1) |
| 73 |
coll, err := otest.NewHTTPCollector("", rCh) |
| 74 |
require.NoError(t, err) |
| 75 |
|
| 76 |
ctx := t.Context() |
| 77 |
addr := coll.Addr().String() |
| 78 |
opts := []Option{WithEndpoint(addr), WithInsecure()} |
| 79 |
cfg := oconf.NewHTTPConfig(asHTTPOptions(opts)...) |
| 80 |
client, err := newClient(cfg) |
| 81 |
require.NoError(t, err) |
| 82 |
|
| 83 |
exp, err := newExporter(client, oconf.Config{}) |
| 84 |
require.NoError(t, err) |
| 85 |
|
| 86 |
var wg sync.WaitGroup |
| 87 |
wg.Go(func() { |
| 88 |
rm := new(metricdata.ResourceMetrics) |
| 89 |
t.Log("starting export") |
| 90 |
require.NoError(t, exp.Export(ctx, rm)) |
| 91 |
t.Log("export complete") |
| 92 |
}) |
| 93 |
|
| 94 |
assert.Eventually(t, func() bool { |
| 95 |
const inst = metric.InstrumentKindCounter |
| 96 |
// These should not be blocked. |
| 97 |
t.Log("getting temporality") |
| 98 |
_ = exp.Temporality(inst) |
| 99 |
t.Log("getting aggregation") |
| 100 |
_ = exp.Aggregation(inst) |
| 101 |
return true |
| 102 |
}, time.Second, 10*time.Millisecond) |
| 103 |
|
| 104 |
// Clear the export. |
| 105 |
rCh <- otest.ExportResult{} |
| 106 |
close(rCh) |
| 107 |
wg.Wait() |
| 108 |
} |