Skip to content

Commit

Permalink
add some migration api and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
krnowak committed Sep 9, 2019
1 parent 0c2be8b commit b499dbd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
13 changes: 11 additions & 2 deletions experimental/bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
otelkey "go.opentelemetry.io/api/key"
oteltag "go.opentelemetry.io/api/tag"
oteltrace "go.opentelemetry.io/api/trace"

migration "go.opentelemetry.io/experimental/bridge/opentracing/migration"
)

var (
Expand All @@ -38,6 +40,9 @@ type MockTracer struct {
rand *rand.Rand
}

var _ oteltrace.Tracer = &MockTracer{}
var _ migration.DeferredContextSetupExtension = &MockTracer{}

func NewMockTracer() *MockTracer {
return &MockTracer{
Resources: oteltag.NewEmptyMap(),
Expand Down Expand Up @@ -94,7 +99,9 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...oteltrace.S
Events: nil,
}
ctx = oteltrace.SetCurrentSpan(ctx, span)
ctx = t.addSpareContextValue(ctx)
if !migration.SkipContextSetup(ctx) {
ctx = t.addSpareContextValue(ctx)
}
return ctx, span
}

Expand Down Expand Up @@ -186,7 +193,9 @@ func (t *MockTracer) Inject(ctx context.Context, span oteltrace.Span, injector o
}))
}

var _ oteltrace.Tracer = &MockTracer{}
func (t *MockTracer) DeferredContextSetupHook(ctx context.Context, span oteltrace.Span) context.Context {
return t.addSpareContextValue(ctx)
}

type MockEvent struct {
CtxAttributes oteltag.Map
Expand Down
18 changes: 17 additions & 1 deletion experimental/bridge/opentracing/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"context"
"fmt"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"sync"

"google.golang.org/grpc/codes"

Expand All @@ -31,6 +33,8 @@ import (
otelcore "go.opentelemetry.io/api/core"
otelregistry "go.opentelemetry.io/api/registry"
oteltrace "go.opentelemetry.io/api/trace"

migration "go.opentelemetry.io/experimental/bridge/opentracing/migration"
)

type bridgeSpanContext struct {
Expand Down Expand Up @@ -233,6 +237,8 @@ func (s *bridgeSpan) Log(data ot.LogData) {

type BridgeTracer struct {
otelTracer oteltrace.Tracer

warnOnce sync.Once
}

var _ ot.Tracer = &BridgeTracer{}
Expand All @@ -253,12 +259,19 @@ func (t *BridgeTracer) StartSpan(operationName string, opts ...ot.StartSpanOptio
bReference, _ := otSpanReferencesToBridgeReferenceAndLinks(sso.References)
// TODO: handle span kind, needs SpanData to be in the API first?
attributes, _, hadTrueErrorTag := otTagsToOtelAttributesKindAndError(sso.Tags)
_, otelSpan := t.otelTracer.Start(context.Background(), operationName, func(opts *oteltrace.SpanOptions) {
checkCtx := migration.WithDeferredSetup(context.Background())
checkCtx2, otelSpan := t.otelTracer.Start(checkCtx, operationName, func(opts *oteltrace.SpanOptions) {
opts.Attributes = attributes
opts.StartTime = sso.StartTime
opts.Reference = bReference.ToOtelReference()
opts.RecordEvent = true
})
if checkCtx != checkCtx2 {
t.warnOnce.Do(func() {
// TODO: what to use for logging?
fmt.Fprintf(os.Stderr, "SDK should have deferred the context setup, see the documentation of go.opentelemetry.io/experimental/bridge/opentracing/migration\n")
})
}
if hadTrueErrorTag {
otelSpan.SetStatus(codes.Unknown)
}
Expand All @@ -282,6 +295,9 @@ func (t *BridgeTracer) StartSpan(operationName string, opts ...ot.StartSpanOptio

func (t *BridgeTracer) ContextWithSpanHook(ctx context.Context, span ot.Span) context.Context {
if bSpan, ok := span.(*bridgeSpan); ok {
if tracerWithExtension, ok := bSpan.tracer.otelTracer.(migration.DeferredContextSetupExtension); ok {
ctx = tracerWithExtension.DeferredContextSetupHook(ctx, bSpan.otelSpan)
}
ctx = oteltrace.SetCurrentSpan(ctx, bSpan.otelSpan)
}
return ctx
Expand Down
11 changes: 11 additions & 0 deletions experimental/bridge/opentracing/migration/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package migration // import "go.opentelemetry.io/experimental/bridge/opentracing/migration"

import (
"context"

oteltrace "go.opentelemetry.io/api/trace"
)

type DeferredContextSetupExtension interface {
DeferredContextSetupHook(ctx context.Context, span oteltrace.Span) context.Context
}
16 changes: 16 additions & 0 deletions experimental/bridge/opentracing/migration/defer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package migration

import (
"context"
)

type doDeferredContextSetupType struct{}

func WithDeferredSetup(ctx context.Context) context.Context {
return context.WithValue(ctx, doDeferredContextSetupType{}, doDeferredContextSetupType{})
}

func SkipContextSetup(ctx context.Context) bool {
_, ok := ctx.Value(doDeferredContextSetupType{}).(doDeferredContextSetupType)
return ok
}

0 comments on commit b499dbd

Please sign in to comment.