...

Source file src/go.opentelemetry.io/otel/sdk/trace/tracetest/recorder.go

Documentation: go.opentelemetry.io/otel/sdk/trace/tracetest

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest"
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  
    21  	sdktrace "go.opentelemetry.io/otel/sdk/trace"
    22  )
    23  
    24  // SpanRecorder records started and ended spans.
    25  type SpanRecorder struct {
    26  	startedMu sync.RWMutex
    27  	started   []sdktrace.ReadWriteSpan
    28  
    29  	endedMu sync.RWMutex
    30  	ended   []sdktrace.ReadOnlySpan
    31  }
    32  
    33  var _ sdktrace.SpanProcessor = (*SpanRecorder)(nil)
    34  
    35  // NewSpanRecorder returns a new initialized SpanRecorder.
    36  func NewSpanRecorder() *SpanRecorder {
    37  	return new(SpanRecorder)
    38  }
    39  
    40  // OnStart records started spans.
    41  //
    42  // This method is safe to be called concurrently.
    43  func (sr *SpanRecorder) OnStart(_ context.Context, s sdktrace.ReadWriteSpan) {
    44  	sr.startedMu.Lock()
    45  	defer sr.startedMu.Unlock()
    46  	sr.started = append(sr.started, s)
    47  }
    48  
    49  // OnEnd records completed spans.
    50  //
    51  // This method is safe to be called concurrently.
    52  func (sr *SpanRecorder) OnEnd(s sdktrace.ReadOnlySpan) {
    53  	sr.endedMu.Lock()
    54  	defer sr.endedMu.Unlock()
    55  	sr.ended = append(sr.ended, s)
    56  }
    57  
    58  // Shutdown does nothing.
    59  //
    60  // This method is safe to be called concurrently.
    61  func (sr *SpanRecorder) Shutdown(context.Context) error {
    62  	return nil
    63  }
    64  
    65  // ForceFlush does nothing.
    66  //
    67  // This method is safe to be called concurrently.
    68  func (sr *SpanRecorder) ForceFlush(context.Context) error {
    69  	return nil
    70  }
    71  
    72  // Started returns a copy of all started spans that have been recorded.
    73  //
    74  // This method is safe to be called concurrently.
    75  func (sr *SpanRecorder) Started() []sdktrace.ReadWriteSpan {
    76  	sr.startedMu.RLock()
    77  	defer sr.startedMu.RUnlock()
    78  	dst := make([]sdktrace.ReadWriteSpan, len(sr.started))
    79  	copy(dst, sr.started)
    80  	return dst
    81  }
    82  
    83  // Ended returns a copy of all ended spans that have been recorded.
    84  //
    85  // This method is safe to be called concurrently.
    86  func (sr *SpanRecorder) Ended() []sdktrace.ReadOnlySpan {
    87  	sr.endedMu.RLock()
    88  	defer sr.endedMu.RUnlock()
    89  	dst := make([]sdktrace.ReadOnlySpan, len(sr.ended))
    90  	copy(dst, sr.ended)
    91  	return dst
    92  }
    93  

View as plain text