Skip to content

Commit 0c09201

Browse files
committed
add bad example
1 parent 6b9d8f9 commit 0c09201

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

workflow/context.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package workflow
2222

2323
import (
24+
"fmt"
25+
2426
"github.com/opentracing/opentracing-go"
2527
"go.uber.org/cadence/internal"
2628
)
@@ -98,21 +100,42 @@ func GetSpanContext(ctx Context) opentracing.SpanContext {
98100
// This is useful to modify baggage items of current workflow and pass it to activities and child workflows.
99101
//
100102
// 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
103+
// func goodWorkflow(ctx Context) (string, error) {
104+
// // start a short lived new workflow span within SideEffect to avoid duplicate span creation during replay
105+
// spanContextValue := SideEffect(ctx, func(ctx Context) interface{} {
106+
// wSpan := opentracing.StartSpan("workflow-operation-with-new-span", opentracing.ChildOf(GetSpanContext(ctx)))
107+
// defer wSpan.Finish()
108+
// wSpan.SetTag("some-key", "some-value")
109+
// return wSpan.Context()
110+
// })
111+
// var spanContext opentracing.SpanContext
112+
// err := spanContextValue.Get(&spanContext)
113+
// if err != nil {
114+
// return "",fmt.Errorf("failed to get span context: %w", err)
115115
// }
116+
//
117+
// aCtx := WithSpanContext(ctx, spanContext)
118+
// var activityFooResult string
119+
// err = ExecuteActivity(aCtx, activityFoo).Get(aCtx, &activityFooResult)
120+
// return activityFooResult, err
121+
// }
122+
//
123+
// Bad Example:
124+
// func badWorkflow(ctx Context) (string, error) {
125+
// // start a new workflow span for EVERY REPLAY
126+
// wSpan := opentracing.StartSpan("workflow-operation", opentracing.ChildOf(GetSpanContext(ctx)))
127+
// wSpan.SetBaggageItem("some-key", "some-value")
128+
// // pass the new span context to activity
129+
// aCtx := WithSpanContext(ctx, wSpan.Context())
130+
// var activityFooResult string
131+
// err := ExecuteActivity(aCtx, activityFoo).Get(aCtx, &activityFooResult)
132+
// wSpan.Finish()
133+
// return activityFooResult, err
134+
// }
135+
//
136+
// func activityFoo(ctx Context) (string, error) {
137+
// return "activity-foo-result", nil
138+
// }
116139
func WithSpanContext(ctx Context, spanContext opentracing.SpanContext) Context {
117140
return internal.WithSpanContext(ctx, spanContext)
118141
}

0 commit comments

Comments
 (0)