| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"sync" |
| 9 |
|
| 10 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 11 |
) |
| 12 |
|
| 13 |
// SpanRecorder records started and ended spans. |
| 14 |
type SpanRecorder struct { |
| 15 |
startedMu sync.RWMutex |
| 16 |
started []sdktrace.ReadWriteSpan |
| 17 |
|
| 18 |
endedMu sync.RWMutex |
| 19 |
ended []sdktrace.ReadOnlySpan |
| 20 |
} |
| 21 |
|
| 22 |
var _ sdktrace.SpanProcessor = (*SpanRecorder)(nil) |
| 23 |
|
| 24 |
// NewSpanRecorder returns a new initialized SpanRecorder. |
| 25 |
func NewSpanRecorder() *SpanRecorder { |
| 26 |
return new(SpanRecorder) |
| 27 |
} |
| 28 |
|
| 29 |
// OnStart records started spans. |
| 30 |
// |
| 31 |
// This method is safe to be called concurrently. |
| 32 |
func (sr *SpanRecorder) OnStart(_ context.Context, s sdktrace.ReadWriteSpan) { |
| 33 |
sr.startedMu.Lock() |
| 34 |
defer sr.startedMu.Unlock() |
| 35 |
sr.started = append(sr.started, s) |
| 36 |
} |
| 37 |
|
| 38 |
// OnEnd records completed spans. |
| 39 |
// |
| 40 |
// This method is safe to be called concurrently. |
| 41 |
func (sr *SpanRecorder) OnEnd(s sdktrace.ReadOnlySpan) { |
| 42 |
sr.endedMu.Lock() |
| 43 |
defer sr.endedMu.Unlock() |
| 44 |
sr.ended = append(sr.ended, s) |
| 45 |
} |
| 46 |
|
| 47 |
// Shutdown does nothing. |
| 48 |
// |
| 49 |
// This method is safe to be called concurrently. |
| 50 |
func (*SpanRecorder) Shutdown(context.Context) error { |
| 51 |
return nil |
| 52 |
} |
| 53 |
|
| 54 |
// ForceFlush does nothing. |
| 55 |
// |
| 56 |
// This method is safe to be called concurrently. |
| 57 |
func (*SpanRecorder) ForceFlush(context.Context) error { |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
// Started returns a copy of all started spans that have been recorded. |
| 62 |
// |
| 63 |
// This method is safe to be called concurrently. |
| 64 |
func (sr *SpanRecorder) Started() []sdktrace.ReadWriteSpan { |
| 65 |
sr.startedMu.RLock() |
| 66 |
defer sr.startedMu.RUnlock() |
| 67 |
dst := make([]sdktrace.ReadWriteSpan, len(sr.started)) |
| 68 |
copy(dst, sr.started) |
| 69 |
return dst |
| 70 |
} |
| 71 |
|
| 72 |
// Reset clears the recorded spans. |
| 73 |
// |
| 74 |
// This method is safe to be called concurrently. |
| 75 |
func (sr *SpanRecorder) Reset() { |
| 76 |
sr.startedMu.Lock() |
| 77 |
sr.endedMu.Lock() |
| 78 |
defer sr.startedMu.Unlock() |
| 79 |
defer sr.endedMu.Unlock() |
| 80 |
|
| 81 |
sr.started = nil |
| 82 |
sr.ended = nil |
| 83 |
} |
| 84 |
|
| 85 |
// Ended returns a copy of all ended spans that have been recorded. |
| 86 |
// |
| 87 |
// This method is safe to be called concurrently. |
| 88 |
func (sr *SpanRecorder) Ended() []sdktrace.ReadOnlySpan { |
| 89 |
sr.endedMu.RLock() |
| 90 |
defer sr.endedMu.RUnlock() |
| 91 |
dst := make([]sdktrace.ReadOnlySpan, len(sr.ended)) |
| 92 |
copy(dst, sr.ended) |
| 93 |
return dst |
| 94 |
} |