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-10-16 19:24:38 +02:00
|
|
|
// TODO These should move somewhere in the api, right?
|
2019-06-14 20:37:05 +02:00
|
|
|
var (
|
2019-10-03 06:19:32 +02:00
|
|
|
ErrorKey = key.New("error")
|
|
|
|
SpanIDKey = key.New("span_id")
|
|
|
|
TraceIDKey = key.New("trace_id")
|
|
|
|
MessageKey = key.New("message")
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-10-09 00:45:49 +02:00
|
|
|
func (s *sdk) WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
|
|
|
|
// TODO: use runtime/trace.WithRegion for execution sdk support
|
2019-06-14 20:37:05 +02:00
|
|
|
// TODO: use runtime/pprof.Do for profile tags support
|
2019-10-09 00:45:49 +02:00
|
|
|
ctx, span := s.Start(ctx, name)
|
2019-09-27 19:48:10 +02:00
|
|
|
defer span.End()
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
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-10-09 00:45:49 +02:00
|
|
|
func (s *sdk) 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,
|
2019-10-09 00:45:49 +02:00
|
|
|
EventID: s.resources,
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
span := &span{
|
2019-10-09 00:45:49 +02:00
|
|
|
sdk: s,
|
2019-09-23 23:39:10 +02:00
|
|
|
initial: exporter.ScopeID{
|
2019-07-17 22:59:53 +02:00
|
|
|
SpanContext: child,
|
2019-10-09 00:45:49 +02:00
|
|
|
EventID: s.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,
|
2019-10-09 00:45:49 +02:00
|
|
|
Scope: s.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
|
|
|
}
|