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"
|
|
|
|
"time"
|
|
|
|
|
2020-08-10 18:17:09 +02:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
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 (
|
2020-08-18 05:25:03 +02:00
|
|
|
errorTypeKey = label.Key("error.type")
|
|
|
|
errorMessageKey = label.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
|
|
|
|
Attributes() []label.KeyValue
|
|
|
|
Links() []trace.Link
|
|
|
|
Events() []export.Event
|
|
|
|
StatusCode() codes.Code
|
|
|
|
StatusMessage() string
|
|
|
|
Tracer() trace.Tracer
|
|
|
|
IsRecording() bool
|
|
|
|
InstrumentationLibrary() instrumentation.Library
|
|
|
|
Resource() *resource.Resource
|
|
|
|
Snapshot() *export.SpanSnapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-11-07 00:13:31 +02:00
|
|
|
var emptySpanContext = trace.SpanContext{}
|
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 {
|
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
|
|
|
|
|
|
|
|
// hasRemoteParent is true when this span has a remote parent span.
|
|
|
|
hasRemoteParent bool
|
|
|
|
|
|
|
|
// 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
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
return s.endTime.IsZero()
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 20:38:59 +02:00
|
|
|
func (s *span) SetStatus(code codes.Code, msg string) {
|
2019-08-02 22:52:55 +02:00
|
|
|
if s == nil {
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
s.statusMessage = msg
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
func (s *span) SetAttributes(attributes ...label.KeyValue) {
|
2019-10-11 03:07:35 +02:00
|
|
|
if !s.IsRecording() {
|
2019-08-02 22:52:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.copyToCappedAttributes(attributes...)
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
// End ends the span.
|
|
|
|
//
|
|
|
|
// 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) {
|
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)
|
|
|
|
|
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
|
|
|
|
2019-10-11 03:07:35 +02:00
|
|
|
if !s.IsRecording() {
|
2019-08-02 22:52:55 +02:00
|
|
|
return
|
|
|
|
}
|
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()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-10-16 17:09:27 +02:00
|
|
|
s.SetStatus(codes.Error, "")
|
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())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Lock()
|
2019-09-03 20:03:51 +02:00
|
|
|
defer s.mu.Unlock()
|
2019-10-08 20:56:58 +02:00
|
|
|
s.messageEvents.add(export.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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:53:12 +02:00
|
|
|
func (s *span) SetName(name string) {
|
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
|
|
|
// SAMPLING
|
2020-12-11 07:15:44 +02:00
|
|
|
noParent := !s.parent.SpanID.IsValid()
|
2020-11-07 00:13:31 +02:00
|
|
|
var ctx trace.SpanContext
|
2019-08-26 20:53:12 +02:00
|
|
|
if noParent {
|
2020-11-07 00:13:31 +02:00
|
|
|
ctx = trace.SpanContext{}
|
2019-08-26 20:53:12 +02:00
|
|
|
} else {
|
|
|
|
// FIXME: Where do we get the parent context from?
|
2020-12-11 07:15:44 +02:00
|
|
|
ctx = s.spanContext
|
2019-08-26 20:53:12 +02:00
|
|
|
}
|
|
|
|
data := samplingData{
|
|
|
|
noParent: noParent,
|
2020-12-11 07:15:44 +02:00
|
|
|
remoteParent: s.hasRemoteParent,
|
2019-08-26 20:53:12 +02:00
|
|
|
parent: ctx,
|
2020-01-14 14:54:48 +02:00
|
|
|
name: name,
|
2019-10-22 22:19:11 +02:00
|
|
|
cfg: s.tracer.provider.config.Load().(*Config),
|
2019-08-26 20:53:12 +02:00
|
|
|
span: s,
|
2020-12-11 07:15:44 +02:00
|
|
|
attributes: s.attributes.toKeyValue(),
|
|
|
|
links: s.interfaceArrayToLinksArray(),
|
|
|
|
kind: s.spanKind,
|
2020-03-10 17:25:11 +02:00
|
|
|
}
|
|
|
|
sampled := makeSamplingDecision(data)
|
|
|
|
|
|
|
|
// Adding attributes directly rather than using s.SetAttributes()
|
|
|
|
// as s.mu is already locked and attempting to do so would deadlock.
|
|
|
|
for _, a := range sampled.Attributes {
|
|
|
|
s.attributes.add(a)
|
2019-08-26 20:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
func (s *span) Name() string {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) Parent() trace.SpanContext {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) SpanKind() trace.SpanKind {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.spanKind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) StartTime() time.Time {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.startTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) EndTime() time.Time {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.endTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) Attributes() []label.KeyValue {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.attributes.evictList.Len() == 0 {
|
|
|
|
return []label.KeyValue{}
|
|
|
|
}
|
|
|
|
return s.attributes.toKeyValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) Events() []export.Event {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if len(s.messageEvents.queue) == 0 {
|
|
|
|
return []export.Event{}
|
|
|
|
}
|
|
|
|
return s.interfaceArrayToMessageEventArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) StatusCode() codes.Code {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) StatusMessage() string {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.statusMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *span) InstrumentationLibrary() instrumentation.Library {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.instrumentationLibrary
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
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.HasRemoteParent = s.hasRemoteParent
|
|
|
|
sd.InstrumentationLibrary = s.instrumentationLibrary
|
|
|
|
sd.Name = s.name
|
|
|
|
sd.ParentSpanID = s.parent.SpanID
|
|
|
|
sd.Resource = s.resource
|
|
|
|
sd.SpanContext = s.spanContext
|
|
|
|
sd.SpanKind = s.spanKind
|
|
|
|
sd.StartTime = s.startTime
|
|
|
|
sd.StatusCode = s.statusCode
|
|
|
|
sd.StatusMessage = s.statusMessage
|
|
|
|
|
|
|
|
if s.attributes.evictList.Len() > 0 {
|
|
|
|
sd.Attributes = s.attributes.toKeyValue()
|
|
|
|
sd.DroppedAttributeCount = s.attributes.droppedCount
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:56:58 +02:00
|
|
|
func (s *span) interfaceArrayToMessageEventArray() []export.Event {
|
|
|
|
messageEventArr := make([]export.Event, 0)
|
2019-08-02 22:52:55 +02:00
|
|
|
for _, value := range s.messageEvents.queue {
|
2019-10-08 20:56:58 +02:00
|
|
|
messageEventArr = append(messageEventArr, value.(export.Event))
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
return messageEventArr
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
func (s *span) copyToCappedAttributes(attributes ...label.KeyValue) {
|
2019-08-02 22:52:55 +02:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, a := range attributes {
|
2020-08-18 05:25:03 +02:00
|
|
|
if a.Value.Type() != label.INVALID {
|
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()
|
|
|
|
}
|
|
|
|
|
2020-12-10 03:30:32 +02:00
|
|
|
func startSpanInternal(ctx context.Context, tr *tracer, name string, parent trace.SpanContext, remoteParent bool, o *trace.SpanConfig) *span {
|
2019-08-02 22:52:55 +02:00
|
|
|
span := &span{}
|
|
|
|
span.spanContext = parent
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
cfg := tr.provider.config.Load().(*Config)
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
if parent == emptySpanContext {
|
2020-12-10 03:30:32 +02:00
|
|
|
// Generate both TraceID and SpanID
|
|
|
|
span.spanContext.TraceID, span.spanContext.SpanID = cfg.IDGenerator.NewIDs(ctx)
|
|
|
|
} else {
|
|
|
|
// TraceID already exists, just generate a SpanID
|
|
|
|
span.spanContext.SpanID = cfg.IDGenerator.NewSpanID(ctx, parent.TraceID)
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
2020-12-11 07:15:44 +02:00
|
|
|
|
|
|
|
span.attributes = newAttributesMap(cfg.MaxAttributesPerSpan)
|
|
|
|
span.messageEvents = newEvictedQueue(cfg.MaxEventsPerSpan)
|
|
|
|
span.links = newEvictedQueue(cfg.MaxLinksPerSpan)
|
|
|
|
|
2019-08-26 20:53:12 +02:00
|
|
|
data := samplingData{
|
2020-12-11 07:15:44 +02:00
|
|
|
noParent: parent == emptySpanContext,
|
2019-08-26 20:53:12 +02:00
|
|
|
remoteParent: remoteParent,
|
|
|
|
parent: parent,
|
|
|
|
name: name,
|
|
|
|
cfg: cfg,
|
|
|
|
span: span,
|
2020-03-10 17:25:11 +02:00
|
|
|
attributes: o.Attributes,
|
|
|
|
links: o.Links,
|
|
|
|
kind: o.SpanKind,
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
2020-03-10 17:25:11 +02:00
|
|
|
sampled := makeSamplingDecision(data)
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2019-10-11 03:07:35 +02:00
|
|
|
if !span.spanContext.IsSampled() && !o.Record {
|
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
|
|
|
|
span.hasRemoteParent = remoteParent
|
|
|
|
span.resource = cfg.Resource
|
|
|
|
span.instrumentationLibrary = tr.instrumentationLibrary
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2020-03-10 17:25:11 +02:00
|
|
|
span.SetAttributes(sampled.Attributes...)
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
span.parent = parent
|
2019-08-02 22:52:55 +02:00
|
|
|
|
|
|
|
return span
|
|
|
|
}
|
2019-08-26 20:53:12 +02:00
|
|
|
|
|
|
|
type samplingData struct {
|
|
|
|
noParent bool
|
|
|
|
remoteParent bool
|
2020-11-07 00:13:31 +02:00
|
|
|
parent trace.SpanContext
|
2019-08-26 20:53:12 +02:00
|
|
|
name string
|
|
|
|
cfg *Config
|
|
|
|
span *span
|
2020-08-18 05:25:03 +02:00
|
|
|
attributes []label.KeyValue
|
2020-11-07 00:13:31 +02:00
|
|
|
links []trace.Link
|
|
|
|
kind trace.SpanKind
|
2019-08-26 20:53:12 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:25:11 +02:00
|
|
|
func makeSamplingDecision(data samplingData) SamplingResult {
|
2020-10-10 00:48:34 +02:00
|
|
|
sampler := data.cfg.DefaultSampler
|
|
|
|
spanContext := &data.span.spanContext
|
|
|
|
sampled := sampler.ShouldSample(SamplingParameters{
|
|
|
|
ParentContext: data.parent,
|
|
|
|
TraceID: spanContext.TraceID,
|
|
|
|
Name: data.name,
|
|
|
|
HasRemoteParent: data.remoteParent,
|
|
|
|
Kind: data.kind,
|
|
|
|
Attributes: data.attributes,
|
|
|
|
Links: data.links,
|
|
|
|
})
|
|
|
|
if sampled.Decision == RecordAndSample {
|
2020-11-07 00:13:31 +02:00
|
|
|
spanContext.TraceFlags |= trace.FlagsSampled
|
2020-10-10 00:48:34 +02:00
|
|
|
} else {
|
2020-11-07 00:13:31 +02:00
|
|
|
spanContext.TraceFlags &^= trace.FlagsSampled
|
2019-08-26 20:53:12 +02:00
|
|
|
}
|
2020-10-10 00:48:34 +02:00
|
|
|
return sampled
|
2019-08-26 20:53:12 +02:00
|
|
|
}
|