Skip to content

Commit

Permalink
Fix up tracing propagator
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyassrivatsan committed May 3, 2019
1 parent f96ff9d commit 5593f81
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 92 deletions.
38 changes: 38 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ ignored = ["go.uber.org/cadence/.tmp"]
[[constraint]]
name = "github.com/robfig/cron"
version = "1.1.0"

[[constraint]]
name = "github.com/uber/jaeger-client-go"
version = "2.15.0"
66 changes: 0 additions & 66 deletions internal/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package internal
import (
"context"

"github.com/opentracing/opentracing-go"
"go.uber.org/cadence/.gen/go/shared"
)

Expand Down Expand Up @@ -55,71 +54,6 @@ type ContextPropagator interface {
ExtractToWorkflow(Context, HeaderReader) (Context, error)
}

// NewTracingContextPropagator returns new tracing context propagator object
func NewTracingContextPropagator() ContextPropagator {
return &tracingContextPropagator{}
}

const tracingKey = "tracingContextKey"

type tracingContextPropagator struct {
tracer opentracing.Tracer
}

func (t *tracingContextPropagator) Inject(
ctx context.Context,
hw HeaderWriter,
) error {
// retrieve span from context object
span := opentracing.SpanFromContext(ctx)

t.tracer.Inject(span.Context(), opentracing.HTTPHeaders, hw)
return nil
}

func (t *tracingContextPropagator) Extract(
ctx context.Context,
hr HeaderReader,
) (context.Context, error) {
err := hr.ForEachKey(func(key string, value []byte) error {
if key == tracingKey {
return nil
}
return nil
})
if err != nil {
return nil, err
}
return ctx, nil
}

func (t *tracingContextPropagator) InjectFromWorkflow(
ctx Context,
hw HeaderWriter,
) error {
// retrieve span from context object
span := spanFromContext(ctx)

t.tracer.Inject(span.Context(), opentracing.HTTPHeaders, hw)
return nil
}

func (t *tracingContextPropagator) ExtractToWorkflow(
ctx Context,
hr HeaderReader,
) (Context, error) {
err := hr.ForEachKey(func(key string, value []byte) error {
if key == tracingKey {
return nil
}
return nil
})
if err != nil {
return nil, err
}
return ctx, nil
}

type headerReader struct {
header *shared.Header
}
Expand Down
26 changes: 0 additions & 26 deletions internal/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,29 +139,3 @@ func TestHeaderReader(t *testing.T) {
})
}
}

func TestTracingContextPropagator(t *testing.T) {
/*
ctxProp := NewTracingContextPropagator()
tracer := opentracing.NoopTracer{}
span := tracer.StartSpan("test-operation")
ctx := context.Background()
ctx = opentracing.ContextWithSpan(ctx, span)
header := &shared.Header{
Fields: map[string][]byte{},
}
err := ctxProp.Inject(ctx, NewHeaderWriter(header))
assert.NoError(t, err)
_, ok := header.Fields[tracingKey]
assert.True(t, ok)
returnCtx := context.Background()
returnCtx, err = ctxProp.Extract(returnCtx, NewHeaderReader(header))
assert.NoError(t, err)
newSpan := opentracing.SpanFromContext(returnCtx)
assert.Equal(t, span, newSpan)
*/
}
100 changes: 100 additions & 0 deletions internal/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package internal

import (
"context"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)

// NewTracingContextPropagator returns new tracing context propagator object
func NewTracingContextPropagator(tracer opentracing.Tracer) ContextPropagator {
return &tracingContextPropagator{tracer}
}

type tracingReader struct {
reader HeaderReader
}

func (t tracingReader) ForeachKey(handler func(key, val string) error) error {
return t.reader.ForEachKey(func(k string, v []byte) error {
return handler(k, string(v))
})
}

type tracingWriter struct {
writer HeaderWriter
}

func (t tracingWriter) Set(key, val string) {
t.writer.Set(key, []byte(val))
}

type tracingContextPropagator struct {
tracer opentracing.Tracer
}

func (t *tracingContextPropagator) Inject(
ctx context.Context,
hw HeaderWriter,
) error {
// retrieve span from context object
span := opentracing.SpanFromContext(ctx)

return t.tracer.Inject(span.Context(), opentracing.TextMap, tracingWriter{hw})
}

func (t *tracingContextPropagator) Extract(
ctx context.Context,
hr HeaderReader,
) (context.Context, error) {
spanContext, err := t.tracer.Extract(opentracing.TextMap, tracingReader{hr})
if err != nil {
return nil, err
}
span := t.tracer.StartSpan("test-operation", ext.RPCServerOption(spanContext))
return opentracing.ContextWithSpan(ctx, span), nil
}

func (t *tracingContextPropagator) InjectFromWorkflow(
ctx Context,
hw HeaderWriter,
) error {
// retrieve span from context object
span := spanFromContext(ctx)

t.tracer.Inject(span.Context(), opentracing.HTTPHeaders, hw)
return nil
}

func (t *tracingContextPropagator) ExtractToWorkflow(
ctx Context,
hr HeaderReader,
) (Context, error) {
spanContext, err := t.tracer.Extract(opentracing.TextMap, hr)
if err != nil {
return nil, err
}
span := t.tracer.StartSpan("test-operation", ext.RPCServerOption(spanContext))
return contextWithSpan(ctx, span), nil
}
57 changes: 57 additions & 0 deletions internal/tracer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package internal

import (
"context"
"testing"

"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
jaeger_config "github.com/uber/jaeger-client-go/config"
"go.uber.org/cadence/.gen/go/shared"
)

func TestTracingContextPropagator(t *testing.T) {
config := jaeger_config.Configuration{}
closer, err := config.InitGlobalTracer("test-service")
assert.NoError(t, err)
defer closer.Close()
tracer := opentracing.GlobalTracer()
ctxProp := NewTracingContextPropagator(tracer)

span := tracer.StartSpan("test-operation")
ctx := context.Background()
ctx = opentracing.ContextWithSpan(ctx, span)
header := &shared.Header{
Fields: map[string][]byte{},
}

err = ctxProp.Inject(ctx, NewHeaderWriter(header))
assert.NoError(t, err)

returnCtx := context.Background()
returnCtx, err = ctxProp.Extract(returnCtx, NewHeaderReader(header))
assert.NoError(t, err)

span = opentracing.SpanFromContext(returnCtx)
assert.NotNil(t, span)
}

0 comments on commit 5593f81

Please sign in to comment.