|
21 | 21 | package workflow |
22 | 22 |
|
23 | 23 | import ( |
| 24 | + "fmt" |
| 25 | + |
24 | 26 | "github.com/opentracing/opentracing-go" |
25 | 27 | "go.uber.org/cadence/internal" |
26 | 28 | ) |
@@ -98,21 +100,42 @@ func GetSpanContext(ctx Context) opentracing.SpanContext { |
98 | 100 | // This is useful to modify baggage items of current workflow and pass it to activities and child workflows. |
99 | 101 | // |
100 | 102 | // 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) |
115 | 115 | // } |
| 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 | +// } |
116 | 139 | func WithSpanContext(ctx Context, spanContext opentracing.SpanContext) Context { |
117 | 140 | return internal.WithSpanContext(ctx, spanContext) |
118 | 141 | } |
0 commit comments