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 (
|
|
|
|
"context"
|
2021-09-02 23:30:12 +02:00
|
|
|
"time"
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2020-06-09 20:47:54 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
2021-11-13 18:35:04 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-08-02 22:52:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type tracer struct {
|
2020-09-24 00:16:13 +02:00
|
|
|
provider *TracerProvider
|
2020-06-09 20:47:54 +02:00
|
|
|
instrumentationLibrary instrumentation.Library
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
var _ trace.Tracer = &tracer{}
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
// Start starts a Span and returns it along with a context containing it.
|
|
|
|
//
|
|
|
|
// The Span is created with the provided name and as a child of any existing
|
|
|
|
// span context found in the passed context. The created Span will be
|
2021-09-02 23:30:12 +02:00
|
|
|
// configured appropriately by any SpanOption passed.
|
2021-05-27 16:53:56 +02:00
|
|
|
func (tr *tracer) Start(ctx context.Context, name string, options ...trace.SpanStartOption) (context.Context, trace.Span) {
|
|
|
|
config := trace.NewSpanStartConfig(options...)
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2021-03-25 16:36:39 +02:00
|
|
|
// For local spans created by this SDK, track child span count.
|
2020-11-07 00:13:31 +02:00
|
|
|
if p := trace.SpanFromContext(ctx); p != nil {
|
2021-09-02 23:30:12 +02:00
|
|
|
if sdkSpan, ok := p.(*recordingSpan); ok {
|
2020-02-04 18:55:03 +02:00
|
|
|
sdkSpan.addChild()
|
2019-10-14 22:20:03 +02:00
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 23:30:12 +02:00
|
|
|
s := tr.newSpan(ctx, name, &config)
|
|
|
|
if rw, ok := s.(ReadWriteSpan); ok && s.IsRecording() {
|
2020-09-24 20:43:23 +02:00
|
|
|
sps, _ := tr.provider.spanProcessors.Load().(spanProcessorStates)
|
|
|
|
for _, sp := range sps {
|
2021-09-02 23:30:12 +02:00
|
|
|
sp.sp.OnStart(ctx, rw)
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 23:30:12 +02:00
|
|
|
if rtt, ok := s.(runtimeTracer); ok {
|
|
|
|
ctx = rtt.runtimeTrace(ctx)
|
|
|
|
}
|
2019-09-16 22:58:15 +02:00
|
|
|
|
2021-09-02 23:30:12 +02:00
|
|
|
return trace.ContextWithSpan(ctx, s), s
|
|
|
|
}
|
|
|
|
|
|
|
|
type runtimeTracer interface {
|
|
|
|
// runtimeTrace starts a "runtime/trace".Task for the span and
|
|
|
|
// returns a context containing the task.
|
|
|
|
runtimeTrace(ctx context.Context) context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSpan returns a new configured span.
|
|
|
|
func (tr *tracer) newSpan(ctx context.Context, name string, config *trace.SpanConfig) trace.Span {
|
|
|
|
// 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 config.NewRoot() {
|
|
|
|
ctx = trace.ContextWithSpanContext(ctx, psc)
|
|
|
|
} else {
|
|
|
|
psc = trace.SpanContextFromContext(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
var tid trace.TraceID
|
|
|
|
var sid trace.SpanID
|
|
|
|
if !psc.TraceID().IsValid() {
|
|
|
|
tid, sid = tr.provider.idGenerator.NewIDs(ctx)
|
|
|
|
} else {
|
|
|
|
tid = psc.TraceID()
|
|
|
|
sid = tr.provider.idGenerator.NewSpanID(ctx, tid)
|
|
|
|
}
|
|
|
|
|
|
|
|
samplingResult := tr.provider.sampler.ShouldSample(SamplingParameters{
|
|
|
|
ParentContext: ctx,
|
|
|
|
TraceID: tid,
|
|
|
|
Name: name,
|
|
|
|
Kind: config.SpanKind(),
|
|
|
|
Attributes: config.Attributes(),
|
|
|
|
Links: config.Links(),
|
|
|
|
})
|
|
|
|
|
|
|
|
scc := trace.SpanContextConfig{
|
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
|
|
|
TraceState: samplingResult.Tracestate,
|
|
|
|
}
|
|
|
|
if isSampled(samplingResult) {
|
|
|
|
scc.TraceFlags = psc.TraceFlags() | trace.FlagsSampled
|
|
|
|
} else {
|
|
|
|
scc.TraceFlags = psc.TraceFlags() &^ trace.FlagsSampled
|
|
|
|
}
|
|
|
|
sc := trace.NewSpanContext(scc)
|
|
|
|
|
|
|
|
if !isRecording(samplingResult) {
|
|
|
|
return tr.newNonRecordingSpan(sc)
|
|
|
|
}
|
|
|
|
return tr.newRecordingSpan(psc, sc, name, samplingResult, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRecordingSpan returns a new configured recordingSpan.
|
|
|
|
func (tr *tracer) newRecordingSpan(psc, sc trace.SpanContext, name string, sr SamplingResult, config *trace.SpanConfig) *recordingSpan {
|
|
|
|
startTime := config.Timestamp()
|
|
|
|
if startTime.IsZero() {
|
|
|
|
startTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &recordingSpan{
|
2022-02-02 01:20:35 +02:00
|
|
|
parent: psc,
|
|
|
|
spanContext: sc,
|
|
|
|
spanKind: trace.ValidateSpanKind(config.SpanKind()),
|
|
|
|
name: name,
|
|
|
|
startTime: startTime,
|
|
|
|
attributes: newAttributesMap(tr.provider.spanLimits.AttributeCountLimit),
|
|
|
|
events: newEvictedQueue(tr.provider.spanLimits.EventCountLimit),
|
|
|
|
links: newEvictedQueue(tr.provider.spanLimits.LinkCountLimit),
|
|
|
|
tracer: tr,
|
2021-09-02 23:30:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range config.Links() {
|
|
|
|
s.addLink(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.SetAttributes(sr.Attributes...)
|
|
|
|
s.SetAttributes(config.Attributes()...)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2021-01-29 22:41:49 +02:00
|
|
|
|
2021-09-02 23:30:12 +02:00
|
|
|
// newNonRecordingSpan returns a new configured nonRecordingSpan.
|
|
|
|
func (tr *tracer) newNonRecordingSpan(sc trace.SpanContext) nonRecordingSpan {
|
|
|
|
return nonRecordingSpan{tracer: tr, sc: sc}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|