2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 07:05:28 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-09-16 13:58:15 -07:00
|
|
|
|
2025-03-04 21:29:11 -05:00
|
|
|
package trace
|
2019-09-16 13:58:15 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-04-28 17:15:00 +00:00
|
|
|
"errors"
|
2025-10-18 04:27:35 +05:30
|
|
|
"strconv"
|
2023-04-14 19:53:47 +05:30
|
|
|
"sync"
|
2019-09-16 13:58:15 -07:00
|
|
|
"testing"
|
2021-04-28 17:15:00 +00:00
|
|
|
"time"
|
2019-09-16 13:58:15 -07:00
|
|
|
|
2023-04-14 19:53:47 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
2025-10-18 04:27:35 +05:30
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
|
"go.opentelemetry.io/otel/sdk"
|
|
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric"
|
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
|
|
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
|
|
|
|
|
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
|
2021-03-08 17:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
2025-03-04 21:29:11 -05:00
|
|
|
type simpleTestExporter struct {
|
|
|
|
|
spans []ReadOnlySpan
|
2021-03-08 17:50:15 +00:00
|
|
|
shutdown bool
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 21:48:04 +02:00
|
|
|
func (t *simpleTestExporter) ExportSpans(_ context.Context, spans []ReadOnlySpan) error {
|
2021-05-04 23:45:13 +00:00
|
|
|
t.spans = append(t.spans, spans...)
|
2020-09-09 10:19:03 -07:00
|
|
|
return nil
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-04 21:29:11 -05:00
|
|
|
func (t *simpleTestExporter) Shutdown(ctx context.Context) error {
|
2021-03-08 17:50:15 +00:00
|
|
|
t.shutdown = true
|
2021-10-19 08:13:37 -07:00
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
// Ensure context deadline tests receive the expected error.
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
default:
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-03-08 17:50:15 +00:00
|
|
|
}
|
2020-09-09 10:19:03 -07:00
|
|
|
|
2025-10-18 04:27:35 +05:30
|
|
|
var _ SpanExporter = (*failingTestExporter)(nil)
|
|
|
|
|
|
|
|
|
|
type failingTestExporter struct {
|
|
|
|
|
simpleTestExporter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *failingTestExporter) ExportSpans(ctx context.Context, spans []ReadOnlySpan) error {
|
|
|
|
|
_ = f.simpleTestExporter.ExportSpans(ctx, spans)
|
|
|
|
|
return errors.New("failed to export spans")
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 21:29:11 -05:00
|
|
|
var _ SpanExporter = (*simpleTestExporter)(nil)
|
2019-09-16 13:58:15 -07:00
|
|
|
|
|
|
|
|
func TestNewSimpleSpanProcessor(t *testing.T) {
|
2025-03-04 21:29:11 -05:00
|
|
|
if ssp := NewSimpleSpanProcessor(&simpleTestExporter{}); ssp == nil {
|
2021-03-08 17:50:15 +00:00
|
|
|
t.Error("failed to create new SimpleSpanProcessor")
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewSimpleSpanProcessorWithNilExporter(t *testing.T) {
|
2025-03-04 21:29:11 -05:00
|
|
|
if ssp := NewSimpleSpanProcessor(nil); ssp == nil {
|
2021-03-08 17:50:15 +00:00
|
|
|
t.Error("failed to create new SimpleSpanProcessor with nil exporter")
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 17:50:15 +00:00
|
|
|
func TestSimpleSpanProcessorOnEnd(t *testing.T) {
|
|
|
|
|
tp := basicTracerProvider(t)
|
2025-03-04 21:29:11 -05:00
|
|
|
te := simpleTestExporter{}
|
|
|
|
|
ssp := NewSimpleSpanProcessor(&te)
|
2021-03-08 17:50:15 +00:00
|
|
|
|
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
2025-03-04 21:29:11 -05:00
|
|
|
startSpan(tp, "TestSimpleSpanProcessorOnEnd").End()
|
2019-09-16 13:58:15 -07:00
|
|
|
|
|
|
|
|
wantTraceID := tid
|
2021-05-04 23:45:13 +00:00
|
|
|
gotTraceID := te.spans[0].SpanContext().TraceID()
|
2019-09-16 13:58:15 -07:00
|
|
|
if wantTraceID != gotTraceID {
|
|
|
|
|
t.Errorf("SimplerSpanProcessor OnEnd() check: got %+v, want %+v\n", gotTraceID, wantTraceID)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-09 00:34:19 +07:00
|
|
|
|
|
|
|
|
func TestSimpleSpanProcessorShutdown(t *testing.T) {
|
2025-03-04 21:29:11 -05:00
|
|
|
exporter := &simpleTestExporter{}
|
|
|
|
|
ssp := NewSimpleSpanProcessor(exporter)
|
2021-03-08 17:50:15 +00:00
|
|
|
|
|
|
|
|
// Ensure we can export a span before we test we cannot after shutdown.
|
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
2025-03-04 21:29:11 -05:00
|
|
|
startSpan(tp, "TestSimpleSpanProcessorShutdown").End()
|
2021-03-08 17:50:15 +00:00
|
|
|
nExported := len(exporter.spans)
|
|
|
|
|
if nExported != 1 {
|
|
|
|
|
t.Error("failed to verify span export")
|
2019-10-09 00:34:19 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 15:52:45 +08:00
|
|
|
if err := ssp.Shutdown(t.Context()); err != nil {
|
2021-03-08 17:50:15 +00:00
|
|
|
t.Errorf("shutting the SimpleSpanProcessor down: %v", err)
|
2020-10-27 05:06:55 +03:00
|
|
|
}
|
2021-03-08 17:50:15 +00:00
|
|
|
if !exporter.shutdown {
|
|
|
|
|
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter")
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 21:29:11 -05:00
|
|
|
startSpan(tp, "TestSimpleSpanProcessorShutdown").End()
|
2021-03-08 17:50:15 +00:00
|
|
|
if len(exporter.spans) > nExported {
|
|
|
|
|
t.Error("exported span to shutdown exporter")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 09:35:06 +02:00
|
|
|
func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe(t *testing.T) {
|
2025-03-04 21:29:11 -05:00
|
|
|
exporter := &simpleTestExporter{}
|
|
|
|
|
ssp := NewSimpleSpanProcessor(exporter)
|
2021-03-08 17:50:15 +00:00
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
|
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
go func() {
|
|
|
|
|
defer func() {
|
|
|
|
|
done <- struct{}{}
|
|
|
|
|
}()
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-stop:
|
|
|
|
|
return
|
|
|
|
|
default:
|
2025-03-04 21:29:11 -05:00
|
|
|
startSpan(tp, "TestSimpleSpanProcessorShutdownOnEndConcurrentSafe").End()
|
2021-03-08 17:50:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2025-09-23 15:52:45 +08:00
|
|
|
if err := ssp.Shutdown(t.Context()); err != nil {
|
2021-03-08 17:50:15 +00:00
|
|
|
t.Errorf("shutting the SimpleSpanProcessor down: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !exporter.shutdown {
|
|
|
|
|
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stop <- struct{}{}
|
|
|
|
|
<-done
|
2019-10-09 00:34:19 +07:00
|
|
|
}
|
2021-04-28 17:15:00 +00:00
|
|
|
|
2023-07-24 09:35:06 +02:00
|
|
|
func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe2(t *testing.T) {
|
2025-03-04 21:29:11 -05:00
|
|
|
exporter := &simpleTestExporter{}
|
|
|
|
|
ssp := NewSimpleSpanProcessor(exporter)
|
2023-04-14 19:53:47 +05:30
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
|
|
span := func(spanName string) {
|
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
|
defer wg.Done()
|
2025-09-23 15:52:45 +08:00
|
|
|
_, span := tp.Tracer("test").Start(t.Context(), spanName)
|
2023-04-14 19:53:47 +05:30
|
|
|
span.End()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go span("test-span-1")
|
|
|
|
|
go span("test-span-2")
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
2025-09-23 15:52:45 +08:00
|
|
|
assert.NoError(t, ssp.Shutdown(t.Context()))
|
2023-04-14 19:53:47 +05:30
|
|
|
assert.True(t, exporter.shutdown, "exporter shutdown")
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 17:15:00 +00:00
|
|
|
func TestSimpleSpanProcessorShutdownHonorsContextDeadline(t *testing.T) {
|
2025-09-23 15:52:45 +08:00
|
|
|
ctx, cancel := context.WithTimeout(t.Context(), time.Nanosecond)
|
2021-04-28 17:15:00 +00:00
|
|
|
defer cancel()
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
2025-03-04 21:29:11 -05:00
|
|
|
ssp := NewSimpleSpanProcessor(&simpleTestExporter{})
|
2021-04-28 17:15:00 +00:00
|
|
|
if got, want := ssp.Shutdown(ctx), context.DeadlineExceeded; !errors.Is(got, want) {
|
|
|
|
|
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSimpleSpanProcessorShutdownHonorsContextCancel(t *testing.T) {
|
2025-09-23 15:52:45 +08:00
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
2021-04-28 17:15:00 +00:00
|
|
|
cancel()
|
|
|
|
|
|
2025-03-04 21:29:11 -05:00
|
|
|
ssp := NewSimpleSpanProcessor(&simpleTestExporter{})
|
2021-04-28 17:15:00 +00:00
|
|
|
if got, want := ssp.Shutdown(ctx), context.Canceled; !errors.Is(got, want) {
|
|
|
|
|
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-18 04:27:35 +05:30
|
|
|
|
|
|
|
|
func TestSimpleSpanProcessorObservability(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
enabled bool
|
|
|
|
|
exporter SpanExporter
|
|
|
|
|
assertMetrics func(t *testing.T, rm metricdata.ResourceMetrics)
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "Disabled",
|
|
|
|
|
enabled: false,
|
|
|
|
|
exporter: &simpleTestExporter{},
|
|
|
|
|
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) {
|
|
|
|
|
assert.Empty(t, rm.ScopeMetrics)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Enabled",
|
|
|
|
|
enabled: true,
|
|
|
|
|
exporter: &simpleTestExporter{},
|
|
|
|
|
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) {
|
|
|
|
|
assert.Len(t, rm.ScopeMetrics, 1)
|
|
|
|
|
sm := rm.ScopeMetrics[0]
|
|
|
|
|
|
|
|
|
|
want := metricdata.ScopeMetrics{
|
|
|
|
|
Scope: instrumentation.Scope{
|
|
|
|
|
Name: "go.opentelemetry.io/otel/sdk/trace/internal/observ",
|
|
|
|
|
Version: sdk.Version(),
|
|
|
|
|
SchemaURL: semconv.SchemaURL,
|
|
|
|
|
},
|
|
|
|
|
Metrics: []metricdata.Metrics{
|
|
|
|
|
{
|
|
|
|
|
Name: otelconv.SDKProcessorSpanProcessed{}.Name(),
|
|
|
|
|
Description: otelconv.SDKProcessorSpanProcessed{}.Description(),
|
|
|
|
|
Unit: otelconv.SDKProcessorSpanProcessed{}.Unit(),
|
|
|
|
|
Data: metricdata.Sum[int64]{
|
|
|
|
|
DataPoints: []metricdata.DataPoint[int64]{
|
|
|
|
|
{
|
|
|
|
|
Value: 1,
|
|
|
|
|
Attributes: attribute.NewSet(
|
|
|
|
|
semconv.OTelComponentName("simple_span_processor/0"),
|
|
|
|
|
semconv.OTelComponentTypeKey.String("simple_span_processor"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Temporality: metricdata.CumulativeTemporality,
|
|
|
|
|
IsMonotonic: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricdatatest.AssertEqual(
|
|
|
|
|
t,
|
|
|
|
|
want,
|
|
|
|
|
sm,
|
|
|
|
|
metricdatatest.IgnoreTimestamp(),
|
|
|
|
|
metricdatatest.IgnoreExemplars(),
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Enabled, Exporter error",
|
|
|
|
|
enabled: true,
|
|
|
|
|
exporter: &failingTestExporter{
|
|
|
|
|
simpleTestExporter: simpleTestExporter{},
|
|
|
|
|
},
|
|
|
|
|
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) {
|
|
|
|
|
assert.Len(t, rm.ScopeMetrics, 1)
|
|
|
|
|
sm := rm.ScopeMetrics[0]
|
|
|
|
|
|
|
|
|
|
want := metricdata.ScopeMetrics{
|
|
|
|
|
Scope: instrumentation.Scope{
|
|
|
|
|
Name: "go.opentelemetry.io/otel/sdk/trace/internal/observ",
|
|
|
|
|
Version: sdk.Version(),
|
|
|
|
|
SchemaURL: semconv.SchemaURL,
|
|
|
|
|
},
|
|
|
|
|
Metrics: []metricdata.Metrics{
|
|
|
|
|
{
|
|
|
|
|
Name: otelconv.SDKProcessorSpanProcessed{}.Name(),
|
|
|
|
|
Description: otelconv.SDKProcessorSpanProcessed{}.Description(),
|
|
|
|
|
Unit: otelconv.SDKProcessorSpanProcessed{}.Unit(),
|
|
|
|
|
Data: metricdata.Sum[int64]{
|
|
|
|
|
DataPoints: []metricdata.DataPoint[int64]{
|
|
|
|
|
{
|
|
|
|
|
Value: 1,
|
|
|
|
|
Attributes: attribute.NewSet(
|
|
|
|
|
semconv.OTelComponentName("simple_span_processor/0"),
|
|
|
|
|
semconv.OTelComponentTypeKey.String("simple_span_processor"),
|
|
|
|
|
semconv.ErrorTypeKey.String("*errors.errorString"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Temporality: metricdata.CumulativeTemporality,
|
|
|
|
|
IsMonotonic: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricdatatest.AssertEqual(
|
|
|
|
|
t,
|
|
|
|
|
want,
|
|
|
|
|
sm,
|
|
|
|
|
metricdatatest.IgnoreTimestamp(),
|
|
|
|
|
metricdatatest.IgnoreExemplars(),
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
t.Setenv("OTEL_GO_X_OBSERVABILITY", strconv.FormatBool(test.enabled))
|
|
|
|
|
|
|
|
|
|
original := otel.GetMeterProvider()
|
|
|
|
|
t.Cleanup(func() { otel.SetMeterProvider(original) })
|
|
|
|
|
|
|
|
|
|
r := metric.NewManualReader()
|
|
|
|
|
mp := metric.NewMeterProvider(
|
|
|
|
|
metric.WithReader(r),
|
|
|
|
|
metric.WithView(dropSpanMetricsView),
|
|
|
|
|
)
|
|
|
|
|
otel.SetMeterProvider(mp)
|
|
|
|
|
|
|
|
|
|
ssp := NewSimpleSpanProcessor(test.exporter)
|
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
|
startSpan(tp, test.name).End()
|
|
|
|
|
|
|
|
|
|
var rm metricdata.ResourceMetrics
|
|
|
|
|
require.NoError(t, r.Collect(t.Context(), &rm))
|
|
|
|
|
test.assertMetrics(t, rm)
|
|
|
|
|
simpleProcessorIDCounter.Store(0) // reset simpleProcessorIDCounter
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|