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.
|
|
|
|
|
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
apitrace "go.opentelemetry.io/otel/api/trace"
|
2020-02-04 18:55:03 +02:00
|
|
|
"go.opentelemetry.io/otel/internal/trace/parent"
|
2020-06-09 20:47:54 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
2019-08-02 22:52:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type tracer struct {
|
2020-06-09 20:47:54 +02:00
|
|
|
provider *Provider
|
|
|
|
instrumentationLibrary instrumentation.Library
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ apitrace.Tracer = &tracer{}
|
|
|
|
|
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
|
|
|
|
// configured appropriately by any SpanOption passed. Any Timestamp option
|
|
|
|
// passed will be used as the start time of the Span's life-cycle.
|
|
|
|
func (tr *tracer) Start(ctx context.Context, name string, options ...apitrace.SpanOption) (context.Context, apitrace.Span) {
|
2020-09-10 21:15:17 +02:00
|
|
|
config := apitrace.NewSpanConfig(options...)
|
2019-08-02 22:52:55 +02:00
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
parentSpanContext, remoteParent, links := parent.GetSpanContextAndLinks(ctx, config.NewRoot)
|
2020-02-04 18:55:03 +02:00
|
|
|
|
|
|
|
if p := apitrace.SpanFromContext(ctx); p != nil {
|
|
|
|
if sdkSpan, ok := p.(*span); ok {
|
|
|
|
sdkSpan.addChild()
|
2019-10-14 22:20:03 +02:00
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
span := startSpanInternal(tr, name, parentSpanContext, remoteParent, config)
|
2020-02-04 18:55:03 +02:00
|
|
|
for _, l := range links {
|
|
|
|
span.addLink(l)
|
|
|
|
}
|
2020-09-03 16:34:36 +02:00
|
|
|
for _, l := range config.Links {
|
2019-11-26 23:03:07 +02:00
|
|
|
span.addLink(l)
|
2019-11-04 15:03:40 +02:00
|
|
|
}
|
2020-09-03 16:34:36 +02:00
|
|
|
span.SetAttributes(config.Attributes...)
|
2019-11-04 15:03:40 +02:00
|
|
|
|
2019-08-02 22:52:55 +02:00
|
|
|
span.tracer = tr
|
|
|
|
|
2019-10-11 03:07:35 +02:00
|
|
|
if span.IsRecording() {
|
2019-10-22 22:19:11 +02:00
|
|
|
sps, _ := tr.provider.spanProcessors.Load().(spanProcessorMap)
|
2019-09-16 22:58:15 +02:00
|
|
|
for sp := range sps {
|
|
|
|
sp.OnStart(span.data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 14:54:48 +02:00
|
|
|
ctx, end := startExecutionTracerTask(ctx, name)
|
2019-08-02 22:52:55 +02:00
|
|
|
span.executionTracerTaskEnd = end
|
2019-12-11 18:51:32 +02:00
|
|
|
return apitrace.ContextWithSpan(ctx, span), span
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|