Skip to content

Commit 6b9d8f9

Browse files
committed
add WithContext
1 parent 6d3a0ad commit 6b9d8f9

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

internal/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,6 @@ func GetSpanContext(ctx Context) opentracing.SpanContext {
494494
return nil
495495
}
496496

497-
func contextWithSpan(ctx Context, spanContext opentracing.SpanContext) Context {
497+
func WithSpanContext(ctx Context, spanContext opentracing.SpanContext) Context {
498498
return WithValue(ctx, activeSpanContextKey, spanContext)
499499
}

internal/tracer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,5 @@ func (t *tracingContextPropagator) ExtractToWorkflow(
119119
// did not find a tracing span, just return the current context
120120
return ctx, nil
121121
}
122-
return contextWithSpan(ctx, spanContext), nil
122+
return WithSpanContext(ctx, spanContext), nil
123123
}

internal/tracer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestTracingContextPropagatorWorkflowContext(t *testing.T) {
8282

8383
span := tracer.StartSpan("test-operation")
8484
assert.NotNil(t, span.Context())
85-
ctx := contextWithSpan(Background(), span.Context())
85+
ctx := WithSpanContext(Background(), span.Context())
8686
header := &shared.Header{
8787
Fields: map[string][]byte{},
8888
}
@@ -130,7 +130,7 @@ func TestConsistentInjectionExtraction(t *testing.T) {
130130
var baggageVal = "e30="
131131
span.SetBaggageItem("request-tenancy", baggageVal)
132132
assert.NotNil(t, span.Context())
133-
ctx := contextWithSpan(Background(), span.Context())
133+
ctx := WithSpanContext(Background(), span.Context())
134134
header := &shared.Header{
135135
Fields: map[string][]byte{},
136136
}

workflow/context.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,26 @@ func NewDisconnectedContext(parent Context) (ctx Context, cancel CancelFunc) {
9393
func GetSpanContext(ctx Context) opentracing.SpanContext {
9494
return internal.GetSpanContext(ctx)
9595
}
96+
97+
// WithSpanContext returns [Context] with override [opentracing.SpanContext].
98+
// This is useful to modify baggage items of current workflow and pass it to activities and child workflows.
99+
//
100+
// Example Usage:
101+
// func myWorkflow(ctx Context) error {
102+
// // start a new workflow span
103+
// wSpan := opentracing.StartSpan("workflow-operation", opentracing.ChildOf(spanContext))
104+
// // pass the new span context to activity
105+
// aCtx := WithSpanContext(ctx, wSpan.Context())
106+
// var activityFooResult string
107+
// err := ExecuteActivity(aCtx, ActivityFoo).Get(aCtx, &activityFooResult)
108+
// if err != nil {
109+
// wSpan.SetTag("workflow-error", err)
110+
// } else {
111+
// wSpan.SetTag("workflow-result", activityFooResult)
112+
// }
113+
// wSpan.Finish()
114+
// return activityFooResult, err
115+
// }
116+
func WithSpanContext(ctx Context, spanContext opentracing.SpanContext) Context {
117+
return internal.WithSpanContext(ctx, spanContext)
118+
}

0 commit comments

Comments
 (0)