2019-08-02 22:52:55 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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"
|
2019-08-02 22:52:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type tracer struct {
|
2019-11-14 20:50:20 +02:00
|
|
|
provider *Provider
|
|
|
|
name string
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ apitrace.Tracer = &tracer{}
|
|
|
|
|
2019-12-04 23:41:13 +02:00
|
|
|
func (tr *tracer) Start(ctx context.Context, name string, o ...apitrace.StartOption) (context.Context, apitrace.Span) {
|
|
|
|
var opts apitrace.StartConfig
|
2019-08-02 22:52:55 +02:00
|
|
|
|
|
|
|
for _, op := range o {
|
|
|
|
op(&opts)
|
|
|
|
}
|
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
parentSpanContext, remoteParent, links := parent.GetSpanContextAndLinks(ctx, opts.NewRoot)
|
|
|
|
|
|
|
|
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-02-04 18:55:03 +02:00
|
|
|
span := startSpanInternal(tr, name, parentSpanContext, remoteParent, opts)
|
|
|
|
for _, l := range links {
|
|
|
|
span.addLink(l)
|
|
|
|
}
|
2019-11-04 15:03:40 +02:00
|
|
|
for _, l := range opts.Links {
|
2019-11-26 23:03:07 +02:00
|
|
|
span.addLink(l)
|
2019-11-04 15:03:40 +02:00
|
|
|
}
|
2019-11-04 21:04:38 +02:00
|
|
|
span.SetAttributes(opts.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
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *tracer) WithSpan(ctx context.Context, name string, body func(ctx context.Context) error) error {
|
|
|
|
ctx, span := tr.Start(ctx, name)
|
2019-09-27 19:48:10 +02:00
|
|
|
defer span.End()
|
2019-08-02 22:52:55 +02:00
|
|
|
|
|
|
|
if err := body(ctx); err != nil {
|
|
|
|
// TODO: set event with boolean attribute for error.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|