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-07-17 22:59:53 +02:00
|
|
|
package sdk
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
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/trace"
|
2019-09-23 23:39:10 +02:00
|
|
|
"go.opentelemetry.io/experimental/streaming/exporter"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-06-27 22:34:57 +02:00
|
|
|
type tracer struct {
|
2019-09-23 23:39:10 +02:00
|
|
|
exporter *exporter.Exporter
|
|
|
|
resources exporter.EventID
|
2019-06-27 22:34:57 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
var (
|
2019-07-17 22:59:53 +02:00
|
|
|
// TODO These should move somewhere in the api, right?
|
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")
|
2019-09-19 20:20:02 +02:00
|
|
|
MessageKey = key.New("message")
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func New(observers ...exporter.Observer) trace.Tracer {
|
|
|
|
return &tracer{
|
|
|
|
exporter: exporter.NewExporter(observers...),
|
|
|
|
}
|
2019-06-27 07:03:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (t *tracer) WithResources(attributes ...core.KeyValue) trace.Tracer {
|
|
|
|
s := t.exporter.NewScope(exporter.ScopeID{
|
2019-07-17 22:59:53 +02:00
|
|
|
EventID: t.resources,
|
|
|
|
}, attributes...)
|
|
|
|
return &tracer{
|
2019-09-23 23:39:10 +02:00
|
|
|
exporter: t.exporter,
|
2019-07-17 22:59:53 +02:00
|
|
|
resources: s.EventID,
|
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (t *tracer) WithComponent(name string) trace.Tracer {
|
2019-06-27 07:03:09 +02:00
|
|
|
return t.WithResources(ComponentKey.String(name))
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (t *tracer) WithService(name string) trace.Tracer {
|
2019-06-27 07:03:09 +02:00
|
|
|
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-09-03 20:03:51 +02:00
|
|
|
span.AddEvent(ctx, "span error", MessageKey.String(err.Error()))
|
2019-06-14 20:37:05 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanOption) (context.Context, trace.Span) {
|
2019-06-14 20:37:05 +02:00
|
|
|
var child core.SpanContext
|
|
|
|
|
|
|
|
child.SpanID = rand.Uint64()
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
o := &trace.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-09-23 23:39:10 +02:00
|
|
|
var parentScope exporter.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-09-23 23:39:10 +02:00
|
|
|
parentScope.SpanContext = trace.CurrentSpan(ctx).SpanContext()
|
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-09-23 23:39:10 +02:00
|
|
|
childScope := exporter.ScopeID{
|
2019-06-14 20:37:05 +02:00
|
|
|
SpanContext: child,
|
|
|
|
EventID: t.resources,
|
|
|
|
}
|
|
|
|
|
|
|
|
span := &span{
|
2019-07-17 22:59:53 +02:00
|
|
|
tracer: t,
|
2019-09-23 23:39:10 +02:00
|
|
|
initial: exporter.ScopeID{
|
2019-07-17 22:59:53 +02:00
|
|
|
SpanContext: child,
|
2019-09-23 23:39:10 +02:00
|
|
|
EventID: t.exporter.Record(exporter.Event{
|
2019-07-17 22:59:53 +02:00
|
|
|
Time: o.StartTime,
|
2019-09-23 23:39:10 +02:00
|
|
|
Type: exporter.START_SPAN,
|
|
|
|
Scope: t.exporter.NewScope(childScope, o.Attributes...),
|
2019-07-17 22:59:53 +02:00
|
|
|
Context: ctx,
|
|
|
|
Parent: parentScope,
|
|
|
|
String: name,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
2019-07-12 00:28:38 +02:00
|
|
|
return trace.SetCurrentSpan(ctx, span), span
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|