-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78c970f
commit 160b00e
Showing
6 changed files
with
199 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |