@@ -46,6 +46,11 @@ type TracerOptions struct {
46
46
47
47
// DisableQueryTracing can be set to disable query tracing.
48
48
DisableQueryTracing bool
49
+
50
+ // OnFinish sets finish options.
51
+ // If unset, this will use [tracer.WithError]
52
+ // in case [interceptor.TracerFinishSpanOptions.Error] is non-nil and not [workflow.IsContinueAsNewError].
53
+ OnFinish func (options * interceptor.TracerFinishSpanOptions ) []tracer.FinishOption
49
54
}
50
55
51
56
// NewTracingInterceptor convenience method that wraps a NeTracer() with a tracing interceptor
@@ -57,10 +62,23 @@ func NewTracingInterceptor(opts TracerOptions) interceptor.Interceptor {
57
62
// NewTracer creates an interceptor for setting on client options
58
63
// that implements Datadog tracing for workflows.
59
64
func NewTracer (opts TracerOptions ) interceptor.Tracer {
65
+ if opts .OnFinish == nil {
66
+ opts .OnFinish = func (options * interceptor.TracerFinishSpanOptions ) []tracer.FinishOption {
67
+ var finishOpts []tracer.FinishOption
68
+
69
+ if err := options .Error ; err != nil && ! workflow .IsContinueAsNewError (err ) {
70
+ finishOpts = append (finishOpts , tracer .WithError (err ))
71
+ }
72
+
73
+ return finishOpts
74
+ }
75
+ }
76
+
60
77
return & tracerImpl {
61
78
opts : TracerOptions {
62
79
DisableSignalTracing : opts .DisableSignalTracing ,
63
80
DisableQueryTracing : opts .DisableQueryTracing ,
81
+ OnFinish : opts .OnFinish ,
64
82
},
65
83
}
66
84
}
@@ -114,7 +132,7 @@ func (t *tracerImpl) SpanFromContext(ctx context.Context) interceptor.TracerSpan
114
132
if ! ok {
115
133
return nil
116
134
}
117
- return & tracerSpan {Span : span }
135
+ return & tracerSpan {OnFinish : t . opts . OnFinish , Span : span }
118
136
}
119
137
120
138
func (t * tracerImpl ) ContextWithSpan (ctx context.Context , span interceptor.TracerSpan ) context.Context {
@@ -174,7 +192,7 @@ func (t *tracerImpl) StartSpan(options *interceptor.TracerStartSpanOptions) (int
174
192
175
193
// Start and return span
176
194
s := tracer .StartSpan (t .SpanName (options ), startOpts ... )
177
- return & tracerSpan {Span : s }, nil
195
+ return & tracerSpan {OnFinish : t . opts . OnFinish , Span : s }, nil
178
196
}
179
197
180
198
func (t * tracerImpl ) GetLogger (logger log.Logger , ref interceptor.TracerSpanRef ) log.Logger {
@@ -223,6 +241,7 @@ func (r spanContextReader) ForeachKey(handler func(key string, value string) err
223
241
224
242
type tracerSpan struct {
225
243
ddtrace.Span
244
+ OnFinish func (options * interceptor.TracerFinishSpanOptions ) []tracer.FinishOption
226
245
}
227
246
type tracerSpanCtx struct {
228
247
ddtrace.SpanContext
@@ -241,9 +260,7 @@ func (t *tracerSpan) ForeachBaggageItem(handler func(k string, v string) bool) {
241
260
}
242
261
243
262
func (t * tracerSpan ) Finish (options * interceptor.TracerFinishSpanOptions ) {
244
- var opts []tracer.FinishOption
245
- if err := options .Error ; err != nil && ! workflow .IsContinueAsNewError (err ) {
246
- opts = append (opts , tracer .WithError (err ))
247
- }
263
+ opts := t .OnFinish (options )
264
+
248
265
t .Span .Finish (opts ... )
249
266
}
0 commit comments