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
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
apitrace "go.opentelemetry.io/otel/api/trace"
|
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
|
|
|
var parent core.SpanContext
|
|
|
|
var remoteParent bool
|
|
|
|
|
|
|
|
//TODO [rghetia] : Add new option for parent. If parent is configured then use that parent.
|
|
|
|
for _, op := range o {
|
|
|
|
op(&opts)
|
|
|
|
}
|
|
|
|
|
2019-10-21 19:15:49 +02:00
|
|
|
if relation := opts.Relation; relation.SpanContext != core.EmptySpanContext() {
|
|
|
|
switch relation.RelationshipType {
|
2019-10-14 22:20:03 +02:00
|
|
|
case apitrace.ChildOfRelationship, apitrace.FollowsFromRelationship:
|
2019-10-21 19:15:49 +02:00
|
|
|
parent = relation.SpanContext
|
2019-10-14 22:20:03 +02:00
|
|
|
remoteParent = true
|
|
|
|
default:
|
|
|
|
// Future relationship types may have different behavior,
|
|
|
|
// e.g., adding a `Link` instead of setting the `parent`
|
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
} else {
|
2019-09-19 21:23:07 +02:00
|
|
|
if p := apitrace.CurrentSpan(ctx); p != nil {
|
|
|
|
if sdkSpan, ok := p.(*span); ok {
|
|
|
|
sdkSpan.addChild()
|
|
|
|
parent = sdkSpan.spanContext
|
|
|
|
}
|
2019-08-02 22:52:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
spanName := tr.spanNameWithPrefix(name)
|
|
|
|
span := startSpanInternal(tr, spanName, parent, remoteParent, opts)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
ctx, end := startExecutionTracerTask(ctx, spanName)
|
2019-08-02 22:52:55 +02:00
|
|
|
span.executionTracerTaskEnd = end
|
2019-09-19 21:23:07 +02:00
|
|
|
return apitrace.SetCurrentSpan(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
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
func (tr *tracer) spanNameWithPrefix(name string) string {
|
|
|
|
if tr.name != "" {
|
|
|
|
return tr.name + "/" + name
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|