Skip to content

Commit 6d3a0ad

Browse files
committed
Expose Workflow.GetSpanContext API
1 parent a061890 commit 6d3a0ad

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

internal/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ func (c *valueCtx) Value(key interface{}) interface{} {
486486
return c.Context.Value(key)
487487
}
488488

489-
func spanFromContext(ctx Context) opentracing.SpanContext {
489+
func GetSpanContext(ctx Context) opentracing.SpanContext {
490490
val := ctx.Value(activeSpanContextKey)
491491
if sp, ok := val.(opentracing.SpanContext); ok {
492492
return sp

internal/tracer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (t *tracingContextPropagator) InjectFromWorkflow(
103103
hw HeaderWriter,
104104
) error {
105105
// retrieve span from context object
106-
spanContext := spanFromContext(ctx)
106+
spanContext := GetSpanContext(ctx)
107107
if spanContext == nil {
108108
return nil
109109
}

internal/tracer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func TestTracingContextPropagatorWorkflowContext(t *testing.T) {
9696
returnCtx2, err := ctxProp.ExtractToWorkflow(Background(), NewHeaderReader(header))
9797
require.NoError(t, err)
9898

99-
newSpanContext := spanFromContext(returnCtx)
99+
newSpanContext := GetSpanContext(returnCtx)
100100
assert.NotNil(t, newSpanContext)
101-
newSpanContext2 := spanFromContext(returnCtx2)
101+
newSpanContext2 := GetSpanContext(returnCtx2)
102102
assert.NotNil(t, newSpanContext2)
103103
assert.Equal(t, newSpanContext2, newSpanContext)
104104
}
@@ -140,7 +140,7 @@ func TestConsistentInjectionExtraction(t *testing.T) {
140140
extractedCtx, err := ctxProp.ExtractToWorkflow(Background(), NewHeaderReader(header))
141141
require.NoError(t, err)
142142

143-
extractedSpanContext := spanFromContext(extractedCtx)
143+
extractedSpanContext := GetSpanContext(extractedCtx)
144144
extractedSpanContext.ForeachBaggageItem(func(k, v string) bool {
145145
if k == "request-tenancy" {
146146
assert.Equal(t, v, baggageVal)

workflow/context.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package workflow
2222

2323
import (
24+
"github.com/opentracing/opentracing-go"
2425
"go.uber.org/cadence/internal"
2526
)
2627

@@ -76,3 +77,19 @@ func WithValue(parent Context, key interface{}, val interface{}) Context {
7677
func NewDisconnectedContext(parent Context) (ctx Context, cancel CancelFunc) {
7778
return internal.NewDisconnectedContext(parent)
7879
}
80+
81+
// GetSpanContext returns the [opentracing.SpanContext] from [Context].
82+
// Returns nil if tracer is not set in [go.uber.org/cadence/worker.Options].
83+
//
84+
// Note: If tracer is set, we already activate a span for each workflow.
85+
// This SpanContext will be passed to the activities and child workflows to start new spans.
86+
//
87+
// Example Usage:
88+
//
89+
// span := GetSpanContext(ctx)
90+
// if span != nil {
91+
// span.SetTag("foo", "bar")
92+
// }
93+
func GetSpanContext(ctx Context) opentracing.SpanContext {
94+
return internal.GetSpanContext(ctx)
95+
}

0 commit comments

Comments
 (0)