...

Source file src/go.opentelemetry.io/otel/sdk/trace/tracetest/exporter.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 is a testing helper package for the SDK. User can
    16  // configure no-op or in-memory exporters to verify different SDK behaviors or
    17  // custom instrumentation.
    18  package tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest"
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  
    24  	"go.opentelemetry.io/otel/sdk/trace"
    25  )
    26  
    27  var _ trace.SpanExporter = (*NoopExporter)(nil)
    28  
    29  // NewNoopExporter returns a new no-op exporter.
    30  func NewNoopExporter() *NoopExporter {
    31  	return new(NoopExporter)
    32  }
    33  
    34  // NoopExporter is an exporter that drops all received spans and performs no
    35  // action.
    36  type NoopExporter struct{}
    37  
    38  // ExportSpans handles export of spans by dropping them.
    39  func (nsb *NoopExporter) ExportSpans(context.Context, []trace.ReadOnlySpan) error { return nil }
    40  
    41  // Shutdown stops the exporter by doing nothing.
    42  func (nsb *NoopExporter) Shutdown(context.Context) error { return nil }
    43  
    44  var _ trace.SpanExporter = (*InMemoryExporter)(nil)
    45  
    46  // NewInMemoryExporter returns a new InMemoryExporter.
    47  func NewInMemoryExporter() *InMemoryExporter {
    48  	return new(InMemoryExporter)
    49  }
    50  
    51  // InMemoryExporter is an exporter that stores all received spans in-memory.
    52  type InMemoryExporter struct {
    53  	mu sync.Mutex
    54  	ss SpanStubs
    55  }
    56  
    57  // ExportSpans handles export of spans by storing them in memory.
    58  func (imsb *InMemoryExporter) ExportSpans(_ context.Context, spans []trace.ReadOnlySpan) error {
    59  	imsb.mu.Lock()
    60  	defer imsb.mu.Unlock()
    61  	imsb.ss = append(imsb.ss, SpanStubsFromReadOnlySpans(spans)...)
    62  	return nil
    63  }
    64  
    65  // Shutdown stops the exporter by clearing spans held in memory.
    66  func (imsb *InMemoryExporter) Shutdown(context.Context) error {
    67  	imsb.Reset()
    68  	return nil
    69  }
    70  
    71  // Reset the current in-memory storage.
    72  func (imsb *InMemoryExporter) Reset() {
    73  	imsb.mu.Lock()
    74  	defer imsb.mu.Unlock()
    75  	imsb.ss = nil
    76  }
    77  
    78  // GetSpans returns the current in-memory stored spans.
    79  func (imsb *InMemoryExporter) GetSpans() SpanStubs {
    80  	imsb.mu.Lock()
    81  	defer imsb.mu.Unlock()
    82  	ret := make(SpanStubs, len(imsb.ss))
    83  	copy(ret, imsb.ss)
    84  	return ret
    85  }
    86  

View as plain text