2019-06-14 22:09:41 +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.
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/open-telemetry/opentelemetry-go/api/core"
|
|
|
|
"github.com/open-telemetry/opentelemetry-go/api/scope"
|
|
|
|
"github.com/open-telemetry/opentelemetry-go/api/tag"
|
2019-06-27 07:03:09 +02:00
|
|
|
apitrace "github.com/open-telemetry/opentelemetry-go/api/trace"
|
2019-06-14 20:37:05 +02:00
|
|
|
"github.com/open-telemetry/opentelemetry-go/exporter/observer"
|
2019-06-27 07:03:09 +02:00
|
|
|
"github.com/open-telemetry/opentelemetry-go/sdk/event"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-06-27 22:34:57 +02:00
|
|
|
type tracer struct {
|
|
|
|
resources core.EventID
|
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
var (
|
2019-06-27 07:03:09 +02:00
|
|
|
ServiceKey = tag.New("service")
|
|
|
|
ComponentKey = tag.New("component")
|
|
|
|
ErrorKey = tag.New("error")
|
|
|
|
SpanIDKey = tag.New("span_id")
|
|
|
|
TraceIDKey = tag.New("trace_id")
|
|
|
|
MessageKey = tag.New("message",
|
2019-06-14 20:37:05 +02:00
|
|
|
tag.WithDescription("message text: info, error, etc"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
var t = &tracer{}
|
|
|
|
|
|
|
|
// Register registers tracer to global registry and returns the registered tracer.
|
|
|
|
func Register() apitrace.Tracer {
|
|
|
|
apitrace.SetGlobalTracer(t)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
func (t *tracer) ScopeID() core.ScopeID {
|
|
|
|
return t.resources.Scope()
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
func (t *tracer) WithResources(attributes ...core.KeyValue) apitrace.Tracer {
|
2019-06-14 20:37:05 +02:00
|
|
|
s := scope.New(t.resources.Scope(), attributes...)
|
|
|
|
return &tracer{
|
|
|
|
resources: s.ScopeID().EventID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
func (t *tracer) WithComponent(name string) apitrace.Tracer {
|
|
|
|
return t.WithResources(ComponentKey.String(name))
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
func (t *tracer) WithService(name string) apitrace.Tracer {
|
|
|
|
return t.WithResources(ServiceKey.String(name))
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
|
|
|
|
// TODO: use runtime/trace.WithRegion for execution tracer support
|
|
|
|
// TODO: use runtime/pprof.Do for profile tags support
|
|
|
|
ctx, span := t.Start(ctx, name)
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
if err := body(ctx); err != nil {
|
|
|
|
span.SetAttribute(ErrorKey.Bool(true))
|
2019-06-25 22:50:46 +02:00
|
|
|
span.AddEvent(ctx, event.WithAttr("span error", MessageKey.String(err.Error())))
|
2019-06-14 20:37:05 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// Start starts a new span with provided name and span options.
|
|
|
|
// If parent span reference is provided in the span option then it is used as as parent.
|
|
|
|
// Otherwise, parent span reference is retrieved from current context.
|
|
|
|
// The new span uses the same TraceID as parent.
|
|
|
|
// If no parent is found then a root span is created and started with random TraceID.
|
|
|
|
// TODO: Add sampling logic.
|
|
|
|
func (t *tracer) Start(ctx context.Context, name string, opts ...apitrace.SpanOption) (context.Context, apitrace.Span) {
|
2019-06-14 20:37:05 +02:00
|
|
|
var child core.SpanContext
|
|
|
|
|
|
|
|
child.SpanID = rand.Uint64()
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
o := &apitrace.SpanOptions{}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
2019-06-19 19:44:46 +02:00
|
|
|
opt(o)
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var parentScope core.ScopeID
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
if o.Reference.HasTraceID() {
|
|
|
|
parentScope = o.Reference.Scope()
|
2019-06-14 20:37:05 +02:00
|
|
|
} else {
|
2019-06-27 07:03:09 +02:00
|
|
|
parentSpan, _ := apitrace.Active(ctx).(*span)
|
|
|
|
parentScope = parentSpan.ScopeID()
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if parentScope.HasTraceID() {
|
|
|
|
parent := parentScope.SpanContext
|
|
|
|
child.TraceIDHigh = parent.TraceIDHigh
|
|
|
|
child.TraceIDLow = parent.TraceIDLow
|
|
|
|
} else {
|
|
|
|
child.TraceIDHigh = rand.Uint64()
|
|
|
|
child.TraceIDLow = rand.Uint64()
|
|
|
|
}
|
|
|
|
|
|
|
|
childScope := core.ScopeID{
|
|
|
|
SpanContext: child,
|
|
|
|
EventID: t.resources,
|
|
|
|
}
|
|
|
|
|
|
|
|
span := &span{
|
|
|
|
spanContext: child,
|
|
|
|
tracer: t,
|
2019-06-27 07:03:09 +02:00
|
|
|
recordEvent: o.RecordEvent,
|
2019-06-14 20:37:05 +02:00
|
|
|
eventID: observer.Record(observer.Event{
|
2019-06-27 07:03:09 +02:00
|
|
|
Time: o.StartTime,
|
2019-06-14 20:37:05 +02:00
|
|
|
Type: observer.START_SPAN,
|
2019-06-27 07:03:09 +02:00
|
|
|
Scope: scope.New(childScope, o.Attributes...).ScopeID(),
|
2019-06-14 20:37:05 +02:00
|
|
|
Context: ctx,
|
|
|
|
Parent: parentScope,
|
|
|
|
String: name,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
return scope.SetActive(ctx, span), span
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
func (t *tracer) Inject(ctx context.Context, span apitrace.Span, injector apitrace.Injector) {
|
2019-06-14 20:37:05 +02:00
|
|
|
injector.Inject(span.ScopeID().SpanContext, tag.FromContext(ctx))
|
|
|
|
}
|