2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-08-02 22:52:55 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
2019-08-02 22:52:55 +02:00
|
|
|
|
|
|
|
import (
|
2020-12-10 03:30:32 +02:00
|
|
|
"context"
|
2020-02-28 23:44:53 +02:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-08-02 22:52:55 +02:00
|
|
|
"sync"
|
2021-02-18 21:31:35 +02:00
|
|
|
"sync/atomic"
|
2019-08-02 22:52:55 +02:00
|
|
|
"time"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-08-10 18:17:09 +02:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2020-12-11 07:15:44 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/internal"
|
2020-12-11 07:15:44 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-08-02 22:52:55 +02:00
|
|
|
)
|
|
|
|
|
2020-02-28 23:44:53 +02:00
|
|
|
const (
|
2021-02-18 19:59:37 +02:00
|
|
|
errorTypeKey = attribute.Key("error.type")
|
|
|
|
errorMessageKey = attribute.Key("error.message")
|
2020-02-28 23:44:53 +02:00
|
|
|
errorEventName = "error"
|
|
|
|
)
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
// ReadOnlySpan allows reading information from the data structure underlying a
|
|
|
|
// trace.Span. It is used in places where reading information from a span is
|
|
|
|
// necessary but changing the span isn't necessary or allowed.
|
|
|
|
// TODO: Should we make the methods unexported? The purpose of this interface
|
|
|
|
// is controlling access to `span` fields, not having multiple implementations.
|
|
|
|
type ReadOnlySpan interface {
|
|
|
|
Name() string
|
|
|
|
SpanContext() trace.SpanContext
|
|
|
|
Parent() trace.SpanContext
|
|
|
|
SpanKind() trace.SpanKind
|
|
|
|
StartTime() time.Time
|
|
|
|
EndTime() time.Time
|
2021-02-18 19:59:37 +02:00
|
|
|
Attributes() []attribute.KeyValue
|
2020-12-11 07:15:44 +02:00
|
|
|
Links() []trace.Link
|
2021-01-14 19:07:25 +02:00
|
|
|
Events() []trace.Event
|
2020-12-11 07:15:44 +02:00
|
|
|
StatusCode() codes.Code
|
|
|
|
StatusMessage() string
|
|
|
|
Tracer() trace.Tracer
|
|
|
|
IsRecording() bool
|
|
|
|
InstrumentationLibrary() instrumentation.Library
|
|
|
|
Resource() *resource.Resource
|
|
|
|
Snapshot() *export.SpanSnapshot
|
2021-02-24 20:03:35 +02:00
|
|
|
|
|
|
|
// A private method to prevent users implementing the
|
|
|
|
// interface and so future additions to it will not
|
|
|
|
// violate compatibility.
|
|
|
|
private()
|
2020-12-11 07:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadWriteSpan exposes the same methods as trace.Span and in addition allows
|
|
|
|
// reading information from the underlying data structure.
|
|
|
|
// This interface exposes the union of the methods of trace.Span (which is a
|
|
|
|
// "write-only" span) and ReadOnlySpan. New methods for writing or reading span
|
|
|
|
// information should be added under trace.Span or ReadOnlySpan, respectively.
|
|
|
|
type ReadWriteSpan interface {
|
|
|
|
trace.Span
|
|
|
|
ReadOnlySpan
|
|
|
|
}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
// span is an implementation of the OpenTelemetry Span API representing the
|
|
|
|
// individual component of a trace.
|
2019-08-02 22:52:55 +02:00
|
|
|
type span struct {
|
2021-02-18 21:31:35 +02:00
|
|
|
// droppedAttributeCount contains dropped attributes for the events and links.
|
|
|
|
droppedAttributeCount int64
|
|
|
|
|
2020-11-09 20:40:02 +02:00
|
|
|
// mu protects the contents of this span.
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
// parent holds the parent span of this span as a trace.SpanContext.
|
|
|
|
parent trace.SpanContext
|
|
|
|
|
|
|
|
// spanKind represents the kind of this span as a trace.SpanKind.
|
|
|
|
spanKind trace.SpanKind
|
|
|
|
|
|
|
|
// name is the name of this span.
|
|
|
|
name string
|
|
|
|
|
|
|
|
// startTime is the time at which this span was started.
|
|
|
|
startTime time.Time
|
|
|
|
|
|
|
|
// endTime is the time at which this span was ended. It contains the zero
|
|
|
|
// value of time.Time until the span is ended.
|
|
|
|
endTime time.Time
|
|
|
|
|
|
|
|
// statusCode represents the status of this span as a codes.Code value.
|
|
|
|
statusCode codes.Code
|
|
|
|
|
|
|
|
// statusMessage represents the status of this span as a string.
|
|
|
|
statusMessage string
|
|
|
|
|
|
|
|
// childSpanCount holds the number of child spans created for this span.
|
|
|
|
childSpanCount int
|
|
|
|
|
|
|
|
// resource contains attributes representing an entity that produced this
|
|
|
|
// span.
|
|
|
|
resource *resource.Resource
|
|
|
|
|
|
|
|
// instrumentationLibrary defines the instrumentation library used to
|
|
|
|
// provide instrumentation.
|
|
|
|
instrumentationLibrary instrumentation.Library
|
|
|
|
|
|
|
|
// spanContext holds the SpanContext of this span.
|
2020-11-07 00:13:31 +02:00
|
|
|
spanContext trace.SpanContext
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
// attributes are capped at configured limit. When the capacity is reached
|
|
|
|
// an oldest entry is removed to create room for a new entry.
|
2019-11-18 20:51:57 +02:00
|
|
|
attributes *attributesMap
|
2019-08-02 22:52:55 +02:00
|
|
|
|
|
|
|
// messageEvents are stored in FIFO queue capped by configured limit.
|
|
|
|
messageEvents *evictedQueue
|
|
|
|
|
|
|
|
// links are stored in FIFO queue capped by configured limit.
|
|
|
|
links *evictedQueue
|
|
|
|
|
2020-11-09 20:40:02 +02:00
|
|
|
// executionTracerTaskEnd ends the execution tracer span.
|
|
|
|
executionTracerTaskEnd func()
|
|
|
|
|
|
|
|
// tracer is the SDK tracer that created this span.
|
|
|
|
tracer *tracer
|
2021-02-18 21:31:35 +02:00
|
|
|
|
|
|
|
// spanLimits holds the limits to this span.
|
|
|
|
spanLimits SpanLimits
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
var _ trace.Span = &span{}
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// SpanContext returns the SpanContext of this span.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) SpanContext() trace.SpanContext {
|
2019-08-02 22:52:55 +02:00
|
|
|
if s == nil {
|
2020-11-07 00:13:31 +02:00
|
|
|
return trace.SpanContext{}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
return s.spanContext
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// IsRecording returns if this span is being recorded. If this span has ended
|
|
|
|
// this will return false.
|
2019-10-11 03:07:35 +02:00
|
|
|
func (s *span) IsRecording() bool {
|
2019-08-02 22:52:55 +02:00
|
|
|
if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-11 07:15:44 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-03-30 22:26:42 +02:00
|
|
|
|
|
|
|
return !s.startTime.IsZero() && s.endTime.IsZero()
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// SetStatus sets the status of this span in the form of a code and a
|
|
|
|
// message. This overrides the existing value of this span's status if one
|
2021-03-08 19:40:38 +02:00
|
|
|
// exists. Message will be set only if status is error. If this span is not being
|
|
|
|
// recorded than this method does nothing.
|
2020-03-07 20:38:59 +02:00
|
|
|
func (s *span) SetStatus(code codes.Code, msg string) {
|
2019-10-11 03:07:35 +02:00
|
|
|
if !s.IsRecording() {
|
2019-08-02 22:52:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
2020-12-11 07:15:44 +02:00
|
|
|
s.statusCode = code
|
2021-03-08 19:40:38 +02:00
|
|
|
if code == codes.Error {
|
|
|
|
s.statusMessage = msg
|
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// SetAttributes sets attributes of this span.
|
|
|
|
//
|
|
|
|
// If a key from attributes already exists the value associated with that key
|
|
|
|
// will be overwritten with the value contained in attributes.
|
|
|
|
//
|
|
|
|
// If this span is not being recorded than this method does nothing.
|
2021-02-18 19:59:37 +02:00
|
|
|
func (s *span) SetAttributes(attributes ...attribute.KeyValue) {
|
2019-10-11 03:07:35 +02:00
|
|
|
if !s.IsRecording() {
|
2019-08-02 22:52:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.copyToCappedAttributes(attributes...)
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// End ends the span. This method does nothing if the span is already ended or
|
|
|
|
// is not being recorded.
|
2020-09-03 16:34:36 +02:00
|
|
|
//
|
|
|
|
// The only SpanOption currently supported is WithTimestamp which will set the
|
|
|
|
// end time for a Span's life-cycle.
|
|
|
|
//
|
|
|
|
// If this method is called while panicking an error event is added to the
|
|
|
|
// Span before ending it and the panic is continued.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) End(options ...trace.SpanOption) {
|
2021-02-17 16:41:18 +02:00
|
|
|
// Do not start by checking if the span is being recorded which requires
|
|
|
|
// acquiring a lock. Make a minimal check that the span is not nil.
|
2019-08-02 22:52:55 +02:00
|
|
|
if s == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
// Store the end time as soon as possible to avoid artificially increasing
|
|
|
|
// the span's duration in case some operation below takes a while.
|
|
|
|
et := internal.MonotonicEndTime(s.startTime)
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Do relative expensive check now that we have an end time and see if we
|
|
|
|
// need to do any more processing.
|
|
|
|
if !s.IsRecording() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-08 21:10:36 +02:00
|
|
|
if recovered := recover(); recovered != nil {
|
|
|
|
// Record but don't stop the panic.
|
|
|
|
defer panic(recovered)
|
2020-10-16 17:09:27 +02:00
|
|
|
s.addEvent(
|
2020-08-08 21:10:36 +02:00
|
|
|
errorEventName,
|
2020-11-07 00:13:31 +02:00
|
|
|
trace.WithAttributes(
|
2020-10-16 17:09:27 +02:00
|
|
|
errorTypeKey.String(typeStr(recovered)),
|
|
|
|
errorMessageKey.String(fmt.Sprint(recovered)),
|
|
|
|
),
|
2020-08-08 21:10:36 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-02 22:52:55 +02:00
|
|
|
if s.executionTracerTaskEnd != nil {
|
|
|
|
s.executionTracerTaskEnd()
|
|
|
|
}
|
2020-12-11 07:15:44 +02:00
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
config := trace.NewSpanConfig(options...)
|
2020-12-11 07:15:44 +02:00
|
|
|
|
|
|
|
s.mu.Lock()
|
2021-02-17 16:41:18 +02:00
|
|
|
// Setting endTime to non-zero marks the span as ended and not recording.
|
2020-12-11 07:15:44 +02:00
|
|
|
if config.Timestamp.IsZero() {
|
|
|
|
s.endTime = et
|
|
|
|
} else {
|
|
|
|
s.endTime = config.Timestamp
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
sps, ok := s.tracer.provider.spanProcessors.Load().(spanProcessorStates)
|
|
|
|
mustExportOrProcess := ok && len(sps) > 0
|
|
|
|
if mustExportOrProcess {
|
|
|
|
for _, sp := range sps {
|
|
|
|
sp.sp.OnEnd(s)
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
2020-12-11 07:15:44 +02:00
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 21:31:01 +02:00
|
|
|
// RecordError will record err as a span event for this span. An additional call to
|
|
|
|
// SetStatus is required if the Status of the Span should be set to Error, this method
|
|
|
|
// does not change the Span status. If this span is not being recorded or err is nil
|
|
|
|
// than this method does nothing.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) RecordError(err error, opts ...trace.EventOption) {
|
2020-10-16 17:09:27 +02:00
|
|
|
if s == nil || err == nil || !s.IsRecording() {
|
2020-02-28 23:44:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
opts = append(opts, trace.WithAttributes(
|
2020-08-08 21:10:36 +02:00
|
|
|
errorTypeKey.String(typeStr(err)),
|
2020-02-28 23:44:53 +02:00
|
|
|
errorMessageKey.String(err.Error()),
|
2020-10-16 17:09:27 +02:00
|
|
|
))
|
|
|
|
s.addEvent(errorEventName, opts...)
|
2020-02-28 23:44:53 +02:00
|
|
|
}
|
|
|
|
|
2020-08-08 21:10:36 +02:00
|
|
|
func typeStr(i interface{}) string {
|
|
|
|
t := reflect.TypeOf(i)
|
|
|
|
if t.PkgPath() == "" && t.Name() == "" {
|
|
|
|
// Likely a builtin type.
|
|
|
|
return t.String()
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Tracer returns the Tracer that created this span.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) Tracer() trace.Tracer {
|
2019-08-02 22:52:55 +02:00
|
|
|
return s.tracer
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// AddEvent adds an event with the provided name and options. If this span is
|
|
|
|
// not being recorded than this method does nothing.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) AddEvent(name string, o ...trace.EventOption) {
|
2019-10-11 03:07:35 +02:00
|
|
|
if !s.IsRecording() {
|
2019-08-02 22:52:55 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 17:09:27 +02:00
|
|
|
s.addEvent(name, o...)
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) addEvent(name string, o ...trace.EventOption) {
|
|
|
|
c := trace.NewEventConfig(o...)
|
2019-09-25 08:12:22 +02:00
|
|
|
|
2021-02-18 21:31:35 +02:00
|
|
|
// Discard over limited attributes
|
|
|
|
if len(c.Attributes) > s.spanLimits.AttributePerEventCountLimit {
|
|
|
|
s.addDroppedAttributeCount(len(c.Attributes) - s.spanLimits.AttributePerEventCountLimit)
|
|
|
|
c.Attributes = c.Attributes[:s.spanLimits.AttributePerEventCountLimit]
|
|
|
|
}
|
|
|
|
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Lock()
|
2019-09-03 20:03:51 +02:00
|
|
|
defer s.mu.Unlock()
|
2021-01-14 19:07:25 +02:00
|
|
|
s.messageEvents.add(trace.Event{
|
2019-12-18 20:13:05 +02:00
|
|
|
Name: name,
|
2020-10-16 17:09:27 +02:00
|
|
|
Attributes: c.Attributes,
|
|
|
|
Time: c.Timestamp,
|
2019-08-02 22:52:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// SetName sets the name of this span. If this span is not being recorded than
|
|
|
|
// this method does nothing.
|
2019-08-26 20:53:12 +02:00
|
|
|
func (s *span) SetName(name string) {
|
2021-02-17 16:41:18 +02:00
|
|
|
if !s.IsRecording() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-23 00:25:33 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2020-12-11 07:15:44 +02:00
|
|
|
s.name = name
|
2019-08-26 20:53:12 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Name returns the name of this span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) Name() string {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Name returns the SpanContext of this span's parent span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) Parent() trace.SpanContext {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.parent
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// SpanKind returns the SpanKind of this span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) SpanKind() trace.SpanKind {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.spanKind
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// StartTime returns the time this span started.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) StartTime() time.Time {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.startTime
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// EndTime returns the time this span ended. For spans that have not yet
|
|
|
|
// ended, the returned value will be the zero value of time.Time.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) EndTime() time.Time {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.endTime
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Attributes returns the attributes of this span.
|
2021-02-18 19:59:37 +02:00
|
|
|
func (s *span) Attributes() []attribute.KeyValue {
|
2020-12-11 07:15:44 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.attributes.evictList.Len() == 0 {
|
2021-02-18 19:59:37 +02:00
|
|
|
return []attribute.KeyValue{}
|
2020-12-11 07:15:44 +02:00
|
|
|
}
|
|
|
|
return s.attributes.toKeyValue()
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Events returns the links of this span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) Links() []trace.Link {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if len(s.links.queue) == 0 {
|
|
|
|
return []trace.Link{}
|
|
|
|
}
|
|
|
|
return s.interfaceArrayToLinksArray()
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Events returns the events of this span.
|
2021-01-14 19:07:25 +02:00
|
|
|
func (s *span) Events() []trace.Event {
|
2020-12-11 07:15:44 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if len(s.messageEvents.queue) == 0 {
|
2021-01-14 19:07:25 +02:00
|
|
|
return []trace.Event{}
|
2020-12-11 07:15:44 +02:00
|
|
|
}
|
|
|
|
return s.interfaceArrayToMessageEventArray()
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// StatusCode returns the status code of this span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) StatusCode() codes.Code {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.statusCode
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// StatusMessage returns the status message of this span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) StatusMessage() string {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.statusMessage
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// InstrumentationLibrary returns the instrumentation.Library associated with
|
|
|
|
// the Tracer that created this span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) InstrumentationLibrary() instrumentation.Library {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.instrumentationLibrary
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:41:18 +02:00
|
|
|
// Resource returns the Resource associated with the Tracer that created this
|
|
|
|
// span.
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) Resource() *resource.Resource {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.resource
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) addLink(link trace.Link) {
|
2019-10-11 03:07:35 +02:00
|
|
|
if !s.IsRecording() {
|
2019-09-21 09:26:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-02-18 21:31:35 +02:00
|
|
|
|
|
|
|
// Discard over limited attributes
|
|
|
|
if len(link.Attributes) > s.spanLimits.AttributePerLinkCountLimit {
|
|
|
|
s.addDroppedAttributeCount(len(link.Attributes) - s.spanLimits.AttributePerLinkCountLimit)
|
|
|
|
link.Attributes = link.Attributes[:s.spanLimits.AttributePerLinkCountLimit]
|
|
|
|
}
|
|
|
|
|
2019-09-21 09:26:20 +02:00
|
|
|
s.links.add(link)
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
// Snapshot creates a snapshot representing the current state of the span as an
|
|
|
|
// export.SpanSnapshot and returns a pointer to it.
|
|
|
|
func (s *span) Snapshot() *export.SpanSnapshot {
|
|
|
|
var sd export.SpanSnapshot
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-11-18 20:51:57 +02:00
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
sd.ChildSpanCount = s.childSpanCount
|
|
|
|
sd.EndTime = s.endTime
|
|
|
|
sd.InstrumentationLibrary = s.instrumentationLibrary
|
|
|
|
sd.Name = s.name
|
2021-03-30 17:59:54 +02:00
|
|
|
sd.Parent = s.parent
|
2020-12-11 07:15:44 +02:00
|
|
|
sd.Resource = s.resource
|
|
|
|
sd.SpanContext = s.spanContext
|
|
|
|
sd.SpanKind = s.spanKind
|
|
|
|
sd.StartTime = s.startTime
|
|
|
|
sd.StatusCode = s.statusCode
|
|
|
|
sd.StatusMessage = s.statusMessage
|
|
|
|
|
2021-02-18 21:31:35 +02:00
|
|
|
sd.DroppedAttributeCount = int(s.droppedAttributeCount)
|
2020-12-11 07:15:44 +02:00
|
|
|
if s.attributes.evictList.Len() > 0 {
|
|
|
|
sd.Attributes = s.attributes.toKeyValue()
|
2021-02-18 21:31:35 +02:00
|
|
|
sd.DroppedAttributeCount += s.attributes.droppedCount
|
2020-12-11 07:15:44 +02:00
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
if len(s.messageEvents.queue) > 0 {
|
|
|
|
sd.MessageEvents = s.interfaceArrayToMessageEventArray()
|
|
|
|
sd.DroppedMessageEventCount = s.messageEvents.droppedCount
|
|
|
|
}
|
2019-09-21 09:26:20 +02:00
|
|
|
if len(s.links.queue) > 0 {
|
|
|
|
sd.Links = s.interfaceArrayToLinksArray()
|
|
|
|
sd.DroppedLinkCount = s.links.droppedCount
|
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
return &sd
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
func (s *span) interfaceArrayToLinksArray() []trace.Link {
|
|
|
|
linkArr := make([]trace.Link, 0)
|
2019-09-21 09:26:20 +02:00
|
|
|
for _, value := range s.links.queue {
|
2020-11-07 00:13:31 +02:00
|
|
|
linkArr = append(linkArr, value.(trace.Link))
|
2019-09-21 09:26:20 +02:00
|
|
|
}
|
|
|
|
return linkArr
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:07:25 +02:00
|
|
|
func (s *span) interfaceArrayToMessageEventArray() []trace.Event {
|
|
|
|
messageEventArr := make([]trace.Event, 0)
|
2019-08-02 22:52:55 +02:00
|
|
|
for _, value := range s.messageEvents.queue {
|
2021-01-14 19:07:25 +02:00
|
|
|
messageEventArr = append(messageEventArr, value.(trace.Event))
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
return messageEventArr
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
func (s *span) copyToCappedAttributes(attributes ...attribute.KeyValue) {
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, a := range attributes {
|
2021-03-08 19:24:29 +02:00
|
|
|
// Ensure attributes conform to the specification:
|
|
|
|
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.0.1/specification/common/common.md#attributes
|
2021-03-18 02:48:43 +02:00
|
|
|
if a.Valid() {
|
2020-06-25 00:58:08 +02:00
|
|
|
s.attributes.add(a)
|
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) addChild() {
|
2019-10-11 03:07:35 +02:00
|
|
|
if !s.IsRecording() {
|
2019-08-02 22:52:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
2020-12-11 07:15:44 +02:00
|
|
|
s.childSpanCount++
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-02-18 21:31:35 +02:00
|
|
|
func (s *span) addDroppedAttributeCount(delta int) {
|
|
|
|
atomic.AddInt64(&s.droppedAttributeCount, int64(delta))
|
|
|
|
}
|
|
|
|
|
2021-02-24 20:03:35 +02:00
|
|
|
func (*span) private() {}
|
|
|
|
|
2021-03-25 16:36:39 +02:00
|
|
|
func startSpanInternal(ctx context.Context, tr *tracer, name string, o *trace.SpanConfig) *span {
|
2019-08-02 22:52:55 +02:00
|
|
|
span := &span{}
|
|
|
|
|
2021-03-18 19:48:13 +02:00
|
|
|
provider := tr.provider
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2021-03-25 16:36:39 +02:00
|
|
|
// If told explicitly to make this a new root use a zero value SpanContext
|
|
|
|
// as a parent which contains an invalid trace ID and is not remote.
|
|
|
|
var psc trace.SpanContext
|
|
|
|
if !o.NewRoot {
|
2021-03-29 17:55:44 +02:00
|
|
|
psc = trace.SpanContextFromContext(ctx)
|
2021-03-25 16:36:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a valid parent trace ID, use it to ensure the continuity of
|
|
|
|
// the trace. Always generate a new span ID so other components can rely
|
|
|
|
// on a unique span ID, even if the Span is non-recording.
|
2021-03-09 18:17:29 +02:00
|
|
|
var tid trace.TraceID
|
|
|
|
var sid trace.SpanID
|
2021-03-25 16:36:39 +02:00
|
|
|
if !psc.TraceID().IsValid() {
|
2021-03-18 19:48:13 +02:00
|
|
|
tid, sid = provider.idGenerator.NewIDs(ctx)
|
2020-12-10 03:30:32 +02:00
|
|
|
} else {
|
2021-03-25 16:36:39 +02:00
|
|
|
tid = psc.TraceID()
|
2021-03-18 19:48:13 +02:00
|
|
|
sid = provider.idGenerator.NewSpanID(ctx, tid)
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
2020-12-11 07:15:44 +02:00
|
|
|
|
2021-03-18 19:48:13 +02:00
|
|
|
spanLimits := provider.spanLimits
|
|
|
|
span.attributes = newAttributesMap(spanLimits.AttributeCountLimit)
|
|
|
|
span.messageEvents = newEvictedQueue(spanLimits.EventCountLimit)
|
|
|
|
span.links = newEvictedQueue(spanLimits.LinkCountLimit)
|
|
|
|
span.spanLimits = spanLimits
|
2020-12-11 07:15:44 +02:00
|
|
|
|
2021-03-20 18:19:03 +02:00
|
|
|
samplingResult := provider.sampler.ShouldSample(SamplingParameters{
|
2021-03-30 19:34:40 +02:00
|
|
|
ParentContext: ctx,
|
|
|
|
TraceID: tid,
|
|
|
|
Name: name,
|
|
|
|
Kind: o.SpanKind,
|
|
|
|
Attributes: o.Attributes,
|
|
|
|
Links: o.Links,
|
2021-03-20 18:19:03 +02:00
|
|
|
})
|
2021-03-25 16:36:39 +02:00
|
|
|
|
|
|
|
scc := trace.SpanContextConfig{
|
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
|
|
|
TraceState: samplingResult.Tracestate,
|
|
|
|
}
|
2021-03-09 19:45:09 +02:00
|
|
|
if isSampled(samplingResult) {
|
2021-03-25 16:36:39 +02:00
|
|
|
scc.TraceFlags = psc.TraceFlags() | trace.FlagsSampled
|
2021-03-09 19:45:09 +02:00
|
|
|
} else {
|
2021-03-25 16:36:39 +02:00
|
|
|
scc.TraceFlags = psc.TraceFlags() &^ trace.FlagsSampled
|
2021-03-09 19:45:09 +02:00
|
|
|
}
|
2021-03-25 16:36:39 +02:00
|
|
|
span.spanContext = trace.NewSpanContext(scc)
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2021-03-09 19:45:09 +02:00
|
|
|
if !isRecording(samplingResult) {
|
2019-08-02 22:52:55 +02:00
|
|
|
return span
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
startTime := o.Timestamp
|
2019-09-03 20:03:51 +02:00
|
|
|
if startTime.IsZero() {
|
|
|
|
startTime = time.Now()
|
|
|
|
}
|
2020-12-11 07:15:44 +02:00
|
|
|
span.startTime = startTime
|
|
|
|
|
|
|
|
span.spanKind = trace.ValidateSpanKind(o.SpanKind)
|
|
|
|
span.name = name
|
2021-03-25 19:51:54 +02:00
|
|
|
span.parent = psc
|
2021-03-18 19:48:13 +02:00
|
|
|
span.resource = provider.resource
|
2020-12-11 07:15:44 +02:00
|
|
|
span.instrumentationLibrary = tr.instrumentationLibrary
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2021-03-09 19:45:09 +02:00
|
|
|
span.SetAttributes(samplingResult.Attributes...)
|
2020-03-10 17:25:11 +02:00
|
|
|
|
2019-08-02 22:52:55 +02:00
|
|
|
return span
|
|
|
|
}
|
2019-08-26 20:53:12 +02:00
|
|
|
|
2021-03-09 19:45:09 +02:00
|
|
|
func isRecording(s SamplingResult) bool {
|
|
|
|
return s.Decision == RecordOnly || s.Decision == RecordAndSample
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSampled(s SamplingResult) bool {
|
|
|
|
return s.Decision == RecordAndSample
|
2019-08-26 20:53:12 +02:00
|
|
|
}
|