| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"strconv" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel" |
| 18 |
"go.opentelemetry.io/otel/attribute" |
| 19 |
"go.opentelemetry.io/otel/sdk" |
| 20 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 21 |
"go.opentelemetry.io/otel/sdk/metric" |
| 22 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 23 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 24 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 25 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 26 |
) |
| 27 |
|
| 28 |
type simpleTestExporter struct { |
| 29 |
spans []ReadOnlySpan |
| 30 |
shutdown bool |
| 31 |
} |
| 32 |
|
| 33 |
func (t *simpleTestExporter) ExportSpans(_ context.Context, spans []ReadOnlySpan) error { |
| 34 |
t.spans = append(t.spans, spans...) |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
func (t *simpleTestExporter) Shutdown(ctx context.Context) error { |
| 39 |
t.shutdown = true |
| 40 |
select { |
| 41 |
case <-ctx.Done(): |
| 42 |
// Ensure context deadline tests receive the expected error. |
| 43 |
return ctx.Err() |
| 44 |
default: |
| 45 |
return nil |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
var _ SpanExporter = (*failingTestExporter)(nil) |
| 50 |
|
| 51 |
type failingTestExporter struct { |
| 52 |
simpleTestExporter |
| 53 |
} |
| 54 |
|
| 55 |
func (f *failingTestExporter) ExportSpans(ctx context.Context, spans []ReadOnlySpan) error { |
| 56 |
_ = f.simpleTestExporter.ExportSpans(ctx, spans) |
| 57 |
return errors.New("failed to export spans") |
| 58 |
} |
| 59 |
|
| 60 |
var _ SpanExporter = (*simpleTestExporter)(nil) |
| 61 |
|
| 62 |
func TestNewSimpleSpanProcessor(t *testing.T) { |
| 63 |
if ssp := NewSimpleSpanProcessor(&simpleTestExporter{}); ssp == nil { |
| 64 |
t.Error("failed to create new SimpleSpanProcessor") |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestNewSimpleSpanProcessorWithNilExporter(t *testing.T) { |
| 69 |
if ssp := NewSimpleSpanProcessor(nil); ssp == nil { |
| 70 |
t.Error("failed to create new SimpleSpanProcessor with nil exporter") |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestSimpleSpanProcessorOnEnd(t *testing.T) { |
| 75 |
tp := basicTracerProvider(t) |
| 76 |
te := simpleTestExporter{} |
| 77 |
ssp := NewSimpleSpanProcessor(&te) |
| 78 |
|
| 79 |
tp.RegisterSpanProcessor(ssp) |
| 80 |
startSpan(tp, "TestSimpleSpanProcessorOnEnd").End() |
| 81 |
|
| 82 |
wantTraceID := tid |
| 83 |
gotTraceID := te.spans[0].SpanContext().TraceID() |
| 84 |
if wantTraceID != gotTraceID { |
| 85 |
t.Errorf("SimplerSpanProcessor OnEnd() check: got %+v, want %+v\n", gotTraceID, wantTraceID) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestSimpleSpanProcessorShutdown(t *testing.T) { |
| 90 |
exporter := &simpleTestExporter{} |
| 91 |
ssp := NewSimpleSpanProcessor(exporter) |
| 92 |
|
| 93 |
// Ensure we can export a span before we test we cannot after shutdown. |
| 94 |
tp := basicTracerProvider(t) |
| 95 |
tp.RegisterSpanProcessor(ssp) |
| 96 |
startSpan(tp, "TestSimpleSpanProcessorShutdown").End() |
| 97 |
nExported := len(exporter.spans) |
| 98 |
if nExported != 1 { |
| 99 |
t.Error("failed to verify span export") |
| 100 |
} |
| 101 |
|
| 102 |
if err := ssp.Shutdown(t.Context()); err != nil { |
| 103 |
t.Errorf("shutting the SimpleSpanProcessor down: %v", err) |
| 104 |
} |
| 105 |
if !exporter.shutdown { |
| 106 |
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter") |
| 107 |
} |
| 108 |
|
| 109 |
startSpan(tp, "TestSimpleSpanProcessorShutdown").End() |
| 110 |
if len(exporter.spans) > nExported { |
| 111 |
t.Error("exported span to shutdown exporter") |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe(t *testing.T) { |
| 116 |
exporter := &simpleTestExporter{} |
| 117 |
ssp := NewSimpleSpanProcessor(exporter) |
| 118 |
tp := basicTracerProvider(t) |
| 119 |
tp.RegisterSpanProcessor(ssp) |
| 120 |
|
| 121 |
stop := make(chan struct{}) |
| 122 |
done := make(chan struct{}) |
| 123 |
go func() { |
| 124 |
defer func() { |
| 125 |
done <- struct{}{} |
| 126 |
}() |
| 127 |
for { |
| 128 |
select { |
| 129 |
case <-stop: |
| 130 |
return |
| 131 |
default: |
| 132 |
startSpan(tp, "TestSimpleSpanProcessorShutdownOnEndConcurrentSafe").End() |
| 133 |
} |
| 134 |
} |
| 135 |
}() |
| 136 |
|
| 137 |
if err := ssp.Shutdown(t.Context()); err != nil { |
| 138 |
t.Errorf("shutting the SimpleSpanProcessor down: %v", err) |
| 139 |
} |
| 140 |
if !exporter.shutdown { |
| 141 |
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter") |
| 142 |
} |
| 143 |
|
| 144 |
stop <- struct{}{} |
| 145 |
<-done |
| 146 |
} |
| 147 |
|
| 148 |
func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe2(t *testing.T) { |
| 149 |
exporter := &simpleTestExporter{} |
| 150 |
ssp := NewSimpleSpanProcessor(exporter) |
| 151 |
tp := basicTracerProvider(t) |
| 152 |
tp.RegisterSpanProcessor(ssp) |
| 153 |
|
| 154 |
var wg sync.WaitGroup |
| 155 |
wg.Add(2) |
| 156 |
|
| 157 |
span := func(spanName string) { |
| 158 |
assert.NotPanics(t, func() { |
| 159 |
defer wg.Done() |
| 160 |
_, span := tp.Tracer("test").Start(t.Context(), spanName) |
| 161 |
span.End() |
| 162 |
}) |
| 163 |
} |
| 164 |
|
| 165 |
go span("test-span-1") |
| 166 |
go span("test-span-2") |
| 167 |
|
| 168 |
wg.Wait() |
| 169 |
|
| 170 |
assert.NoError(t, ssp.Shutdown(t.Context())) |
| 171 |
assert.True(t, exporter.shutdown, "exporter shutdown") |
| 172 |
} |
| 173 |
|
| 174 |
func TestSimpleSpanProcessorShutdownHonorsContextDeadline(t *testing.T) { |
| 175 |
ctx, cancel := context.WithTimeout(t.Context(), time.Nanosecond) |
| 176 |
defer cancel() |
| 177 |
<-ctx.Done() |
| 178 |
|
| 179 |
ssp := NewSimpleSpanProcessor(&simpleTestExporter{}) |
| 180 |
if got, want := ssp.Shutdown(ctx), context.DeadlineExceeded; !errors.Is(got, want) { |
| 181 |
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func TestSimpleSpanProcessorShutdownHonorsContextCancel(t *testing.T) { |
| 186 |
ctx, cancel := context.WithCancel(t.Context()) |
| 187 |
cancel() |
| 188 |
|
| 189 |
ssp := NewSimpleSpanProcessor(&simpleTestExporter{}) |
| 190 |
if got, want := ssp.Shutdown(ctx), context.Canceled; !errors.Is(got, want) { |
| 191 |
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func TestSimpleSpanProcessorObservability(t *testing.T) { |
| 196 |
tests := []struct { |
| 197 |
name string |
| 198 |
enabled bool |
| 199 |
exporter SpanExporter |
| 200 |
assertMetrics func(t *testing.T, rm metricdata.ResourceMetrics) |
| 201 |
}{ |
| 202 |
{ |
| 203 |
name: "Disabled", |
| 204 |
enabled: false, |
| 205 |
exporter: &simpleTestExporter{}, |
| 206 |
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 207 |
assert.Empty(t, rm.ScopeMetrics) |
| 208 |
}, |
| 209 |
}, |
| 210 |
{ |
| 211 |
name: "Enabled", |
| 212 |
enabled: true, |
| 213 |
exporter: &simpleTestExporter{}, |
| 214 |
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 215 |
assert.Len(t, rm.ScopeMetrics, 1) |
| 216 |
sm := rm.ScopeMetrics[0] |
| 217 |
|
| 218 |
want := metricdata.ScopeMetrics{ |
| 219 |
Scope: instrumentation.Scope{ |
| 220 |
Name: "go.opentelemetry.io/otel/sdk/trace/internal/observ", |
| 221 |
Version: sdk.Version(), |
| 222 |
SchemaURL: semconv.SchemaURL, |
| 223 |
}, |
| 224 |
Metrics: []metricdata.Metrics{ |
| 225 |
{ |
| 226 |
Name: otelconv.SDKProcessorSpanProcessed{}.Name(), |
| 227 |
Description: otelconv.SDKProcessorSpanProcessed{}.Description(), |
| 228 |
Unit: otelconv.SDKProcessorSpanProcessed{}.Unit(), |
| 229 |
Data: metricdata.Sum[int64]{ |
| 230 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 231 |
{ |
| 232 |
Value: 1, |
| 233 |
Attributes: attribute.NewSet( |
| 234 |
semconv.OTelComponentName("simple_span_processor/0"), |
| 235 |
semconv.OTelComponentTypeKey.String("simple_span_processor"), |
| 236 |
), |
| 237 |
}, |
| 238 |
}, |
| 239 |
Temporality: metricdata.CumulativeTemporality, |
| 240 |
IsMonotonic: true, |
| 241 |
}, |
| 242 |
}, |
| 243 |
}, |
| 244 |
} |
| 245 |
|
| 246 |
metricdatatest.AssertEqual( |
| 247 |
t, |
| 248 |
want, |
| 249 |
sm, |
| 250 |
metricdatatest.IgnoreTimestamp(), |
| 251 |
metricdatatest.IgnoreExemplars(), |
| 252 |
) |
| 253 |
}, |
| 254 |
}, |
| 255 |
{ |
| 256 |
name: "Enabled, Exporter error", |
| 257 |
enabled: true, |
| 258 |
exporter: &failingTestExporter{ |
| 259 |
simpleTestExporter: simpleTestExporter{}, |
| 260 |
}, |
| 261 |
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 262 |
assert.Len(t, rm.ScopeMetrics, 1) |
| 263 |
sm := rm.ScopeMetrics[0] |
| 264 |
|
| 265 |
want := metricdata.ScopeMetrics{ |
| 266 |
Scope: instrumentation.Scope{ |
| 267 |
Name: "go.opentelemetry.io/otel/sdk/trace/internal/observ", |
| 268 |
Version: sdk.Version(), |
| 269 |
SchemaURL: semconv.SchemaURL, |
| 270 |
}, |
| 271 |
Metrics: []metricdata.Metrics{ |
| 272 |
{ |
| 273 |
Name: otelconv.SDKProcessorSpanProcessed{}.Name(), |
| 274 |
Description: otelconv.SDKProcessorSpanProcessed{}.Description(), |
| 275 |
Unit: otelconv.SDKProcessorSpanProcessed{}.Unit(), |
| 276 |
Data: metricdata.Sum[int64]{ |
| 277 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 278 |
{ |
| 279 |
Value: 1, |
| 280 |
Attributes: attribute.NewSet( |
| 281 |
semconv.OTelComponentName("simple_span_processor/0"), |
| 282 |
semconv.OTelComponentTypeKey.String("simple_span_processor"), |
| 283 |
semconv.ErrorTypeKey.String("*errors.errorString"), |
| 284 |
), |
| 285 |
}, |
| 286 |
}, |
| 287 |
Temporality: metricdata.CumulativeTemporality, |
| 288 |
IsMonotonic: true, |
| 289 |
}, |
| 290 |
}, |
| 291 |
}, |
| 292 |
} |
| 293 |
|
| 294 |
metricdatatest.AssertEqual( |
| 295 |
t, |
| 296 |
want, |
| 297 |
sm, |
| 298 |
metricdatatest.IgnoreTimestamp(), |
| 299 |
metricdatatest.IgnoreExemplars(), |
| 300 |
) |
| 301 |
}, |
| 302 |
}, |
| 303 |
} |
| 304 |
|
| 305 |
for _, test := range tests { |
| 306 |
t.Run(test.name, func(t *testing.T) { |
| 307 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", strconv.FormatBool(test.enabled)) |
| 308 |
|
| 309 |
original := otel.GetMeterProvider() |
| 310 |
t.Cleanup(func() { otel.SetMeterProvider(original) }) |
| 311 |
|
| 312 |
r := metric.NewManualReader() |
| 313 |
mp := metric.NewMeterProvider( |
| 314 |
metric.WithReader(r), |
| 315 |
metric.WithView(dropSpanMetricsView), |
| 316 |
) |
| 317 |
otel.SetMeterProvider(mp) |
| 318 |
|
| 319 |
ssp := NewSimpleSpanProcessor(test.exporter) |
| 320 |
tp := basicTracerProvider(t) |
| 321 |
tp.RegisterSpanProcessor(ssp) |
| 322 |
startSpan(tp, test.name).End() |
| 323 |
|
| 324 |
var rm metricdata.ResourceMetrics |
| 325 |
require.NoError(t, r.Collect(t.Context(), &rm)) |
| 326 |
test.assertMetrics(t, rm) |
| 327 |
simpleProcessorIDCounter.Store(0) // reset simpleProcessorIDCounter |
| 328 |
}) |
| 329 |
} |
| 330 |
} |