...

Source file src/go.opentelemetry.io/otel/sdk/trace/tracetest/span.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  	"time"
    19  
    20  	"go.opentelemetry.io/otel/attribute"
    21  	"go.opentelemetry.io/otel/sdk/instrumentation"
    22  	"go.opentelemetry.io/otel/sdk/resource"
    23  	tracesdk "go.opentelemetry.io/otel/sdk/trace"
    24  	"go.opentelemetry.io/otel/trace"
    25  )
    26  
    27  // SpanStubs is a slice of SpanStub use for testing an SDK.
    28  type SpanStubs []SpanStub
    29  
    30  // SpanStubsFromReadOnlySpans returns SpanStubs populated from ro.
    31  func SpanStubsFromReadOnlySpans(ro []tracesdk.ReadOnlySpan) SpanStubs {
    32  	if len(ro) == 0 {
    33  		return nil
    34  	}
    35  
    36  	s := make(SpanStubs, 0, len(ro))
    37  	for _, r := range ro {
    38  		s = append(s, SpanStubFromReadOnlySpan(r))
    39  	}
    40  
    41  	return s
    42  }
    43  
    44  // Snapshots returns s as a slice of ReadOnlySpans.
    45  func (s SpanStubs) Snapshots() []tracesdk.ReadOnlySpan {
    46  	if len(s) == 0 {
    47  		return nil
    48  	}
    49  
    50  	ro := make([]tracesdk.ReadOnlySpan, len(s))
    51  	for i := 0; i < len(s); i++ {
    52  		ro[i] = s[i].Snapshot()
    53  	}
    54  	return ro
    55  }
    56  
    57  // SpanStub is a stand-in for a Span.
    58  type SpanStub struct {
    59  	Name                   string
    60  	SpanContext            trace.SpanContext
    61  	Parent                 trace.SpanContext
    62  	SpanKind               trace.SpanKind
    63  	StartTime              time.Time
    64  	EndTime                time.Time
    65  	Attributes             []attribute.KeyValue
    66  	Events                 []tracesdk.Event
    67  	Links                  []tracesdk.Link
    68  	Status                 tracesdk.Status
    69  	DroppedAttributes      int
    70  	DroppedEvents          int
    71  	DroppedLinks           int
    72  	ChildSpanCount         int
    73  	Resource               *resource.Resource
    74  	InstrumentationLibrary instrumentation.Library
    75  }
    76  
    77  // SpanStubFromReadOnlySpan returns a SpanStub populated from ro.
    78  func SpanStubFromReadOnlySpan(ro tracesdk.ReadOnlySpan) SpanStub {
    79  	if ro == nil {
    80  		return SpanStub{}
    81  	}
    82  
    83  	return SpanStub{
    84  		Name:                   ro.Name(),
    85  		SpanContext:            ro.SpanContext(),
    86  		Parent:                 ro.Parent(),
    87  		SpanKind:               ro.SpanKind(),
    88  		StartTime:              ro.StartTime(),
    89  		EndTime:                ro.EndTime(),
    90  		Attributes:             ro.Attributes(),
    91  		Events:                 ro.Events(),
    92  		Links:                  ro.Links(),
    93  		Status:                 ro.Status(),
    94  		DroppedAttributes:      ro.DroppedAttributes(),
    95  		DroppedEvents:          ro.DroppedEvents(),
    96  		DroppedLinks:           ro.DroppedLinks(),
    97  		ChildSpanCount:         ro.ChildSpanCount(),
    98  		Resource:               ro.Resource(),
    99  		InstrumentationLibrary: ro.InstrumentationScope(),
   100  	}
   101  }
   102  
   103  // Snapshot returns a read-only copy of the SpanStub.
   104  func (s SpanStub) Snapshot() tracesdk.ReadOnlySpan {
   105  	return spanSnapshot{
   106  		name:                 s.Name,
   107  		spanContext:          s.SpanContext,
   108  		parent:               s.Parent,
   109  		spanKind:             s.SpanKind,
   110  		startTime:            s.StartTime,
   111  		endTime:              s.EndTime,
   112  		attributes:           s.Attributes,
   113  		events:               s.Events,
   114  		links:                s.Links,
   115  		status:               s.Status,
   116  		droppedAttributes:    s.DroppedAttributes,
   117  		droppedEvents:        s.DroppedEvents,
   118  		droppedLinks:         s.DroppedLinks,
   119  		childSpanCount:       s.ChildSpanCount,
   120  		resource:             s.Resource,
   121  		instrumentationScope: s.InstrumentationLibrary,
   122  	}
   123  }
   124  
   125  type spanSnapshot struct {
   126  	// Embed the interface to implement the private method.
   127  	tracesdk.ReadOnlySpan
   128  
   129  	name                 string
   130  	spanContext          trace.SpanContext
   131  	parent               trace.SpanContext
   132  	spanKind             trace.SpanKind
   133  	startTime            time.Time
   134  	endTime              time.Time
   135  	attributes           []attribute.KeyValue
   136  	events               []tracesdk.Event
   137  	links                []tracesdk.Link
   138  	status               tracesdk.Status
   139  	droppedAttributes    int
   140  	droppedEvents        int
   141  	droppedLinks         int
   142  	childSpanCount       int
   143  	resource             *resource.Resource
   144  	instrumentationScope instrumentation.Scope
   145  }
   146  
   147  func (s spanSnapshot) Name() string                     { return s.name }
   148  func (s spanSnapshot) SpanContext() trace.SpanContext   { return s.spanContext }
   149  func (s spanSnapshot) Parent() trace.SpanContext        { return s.parent }
   150  func (s spanSnapshot) SpanKind() trace.SpanKind         { return s.spanKind }
   151  func (s spanSnapshot) StartTime() time.Time             { return s.startTime }
   152  func (s spanSnapshot) EndTime() time.Time               { return s.endTime }
   153  func (s spanSnapshot) Attributes() []attribute.KeyValue { return s.attributes }
   154  func (s spanSnapshot) Links() []tracesdk.Link           { return s.links }
   155  func (s spanSnapshot) Events() []tracesdk.Event         { return s.events }
   156  func (s spanSnapshot) Status() tracesdk.Status          { return s.status }
   157  func (s spanSnapshot) DroppedAttributes() int           { return s.droppedAttributes }
   158  func (s spanSnapshot) DroppedLinks() int                { return s.droppedLinks }
   159  func (s spanSnapshot) DroppedEvents() int               { return s.droppedEvents }
   160  func (s spanSnapshot) ChildSpanCount() int              { return s.childSpanCount }
   161  func (s spanSnapshot) Resource() *resource.Resource     { return s.resource }
   162  func (s spanSnapshot) InstrumentationScope() instrumentation.Scope {
   163  	return s.instrumentationScope
   164  }
   165  
   166  func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library {
   167  	return s.instrumentationScope
   168  }
   169  

View as plain text