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"
|
|
|
|
|
2019-07-15 23:49:21 +02:00
|
|
|
"go.opentelemetry.io/api/core"
|
|
|
|
"go.opentelemetry.io/api/key"
|
|
|
|
"go.opentelemetry.io/api/tag"
|
|
|
|
"go.opentelemetry.io/api/trace"
|
|
|
|
apitrace "go.opentelemetry.io/api/trace"
|
|
|
|
"go.opentelemetry.io/experimental/streaming/exporter/observer"
|
|
|
|
"go.opentelemetry.io/experimental/streaming/sdk/event"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-06-27 22:34:57 +02:00
|
|
|
type tracer struct {
|
2019-07-12 00:28:38 +02:00
|
|
|
resources observer.EventID
|
2019-06-27 22:34:57 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
var (
|
2019-07-12 00:28:38 +02:00
|
|
|
ServiceKey = key.New("service")
|
|
|
|
ComponentKey = key.New("component")
|
|
|
|
ErrorKey = key.New("error")
|
|
|
|
SpanIDKey = key.New("span_id")
|
|
|
|
TraceIDKey = key.New("trace_id")
|
|
|
|
MessageKey = key.New("message",
|
|
|
|
key.WithDescription("message text: info, error, etc"),
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) WithResources(attributes ...core.KeyValue) apitrace.Tracer {
|
2019-07-12 00:28:38 +02:00
|
|
|
return t
|
|
|
|
// s := scope.New(t.resources.Scope(), attributes...)
|
|
|
|
// return &tracer{
|
|
|
|
// resources: s.ScopeID().EventID,
|
|
|
|
// }
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-12 00:28:38 +02:00
|
|
|
var parentScope observer.ScopeID
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
if o.Reference.HasTraceID() {
|
2019-07-12 00:28:38 +02:00
|
|
|
parentScope.SpanContext = o.Reference.SpanContext
|
2019-06-14 20:37:05 +02:00
|
|
|
} else {
|
2019-07-12 00:28:38 +02:00
|
|
|
parentSpan, _ := apitrace.CurrentSpan(ctx).(*span)
|
2019-06-27 07:03:09 +02:00
|
|
|
parentScope = parentSpan.ScopeID()
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if parentScope.HasTraceID() {
|
|
|
|
parent := parentScope.SpanContext
|
2019-07-03 01:21:24 +02:00
|
|
|
child.TraceID.High = parent.TraceID.High
|
|
|
|
child.TraceID.Low = parent.TraceID.Low
|
2019-06-14 20:37:05 +02:00
|
|
|
} else {
|
2019-07-03 01:21:24 +02:00
|
|
|
child.TraceID.High = rand.Uint64()
|
|
|
|
child.TraceID.Low = rand.Uint64()
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 00:28:38 +02:00
|
|
|
childScope := observer.ScopeID{
|
2019-06-14 20:37:05 +02:00
|
|
|
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-07-12 00:28:38 +02:00
|
|
|
Scope: observer.NewScope(childScope, o.Attributes...),
|
2019-06-14 20:37:05 +02:00
|
|
|
Context: ctx,
|
|
|
|
Parent: parentScope,
|
|
|
|
String: name,
|
|
|
|
}),
|
|
|
|
}
|
2019-07-12 00:28:38 +02:00
|
|
|
return trace.SetCurrentSpan(ctx, span), span
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
func (t *tracer) Inject(ctx context.Context, span apitrace.Span, injector apitrace.Injector) {
|
2019-07-12 00:28:38 +02:00
|
|
|
injector.Inject(span.SpanContext(), tag.FromContext(ctx))
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|