@@ -19,6 +19,7 @@ import (
19
19
20
20
type options struct {
21
21
keepRunningOneInstance bool
22
+ startTransaction bool
22
23
}
23
24
24
25
// Option specifies how a routine is run.
@@ -57,6 +58,12 @@ func KeepRunningOneInstance() Option {
57
58
}
58
59
}
59
60
61
+ func StartAsTransaction () Option {
62
+ return func (o * options ) {
63
+ o .startTransaction = true
64
+ }
65
+ }
66
+
60
67
// RunNamed runs a routine like Run does. Additionally it assigns the routine a
61
68
// name and allows using options to control how the routine is run. Routines
62
69
// with the same name show consistent behaviour for the options, like mutual
@@ -73,19 +80,25 @@ func RunNamed(parentCtx context.Context, name string, routine func(context.Conte
73
80
74
81
if o .keepRunningOneInstance {
75
82
routine = (& routineThatKeepsRunningOneInstance {
76
- Name : name ,
77
- Routine : routine ,
83
+ Name : name ,
84
+ Routine : routine ,
85
+ startTransaction : o .startTransaction ,
78
86
}).Run
79
87
}
80
88
81
- return Run (parentCtx , routine )
89
+ return Run (parentCtx , routine , opts ... )
82
90
}
83
91
84
92
// Run runs the given function in a new background context. Panics
85
93
// thrown in the function are logged and sent to sentry. The routines context is
86
94
// canceled if the program receives a shutdown signal (SIGINT, SIGTERM), if the
87
95
// returned CancelFunc is called, or if the routine returned.
88
- func Run (ctx context.Context , routine func (context.Context )) (cancel context.CancelFunc ) {
96
+ func Run (ctx context.Context , routine func (context.Context ), opts ... Option ) (cancel context.CancelFunc ) {
97
+ o := options {}
98
+ for _ , opt := range opts {
99
+ opt (& o )
100
+ }
101
+
89
102
ctx = context .WithoutCancel (ctx )
90
103
91
104
// add routine number to context and logger
@@ -114,7 +127,14 @@ func Run(ctx context.Context, routine func(context.Context)) (cancel context.Can
114
127
defer errors .HandleWithCtx (ctx , fmt .Sprintf ("routine %d" , num )) // handle panics
115
128
defer cancel ()
116
129
117
- span := sentry .StartSpan (ctx , "function" , sentry .WithDescription (fmt .Sprintf ("routine %d" , num )))
130
+ var span * sentry.Span
131
+
132
+ if o .startTransaction {
133
+ span = sentry .StartTransaction (ctx , fmt .Sprintf ("routine %d" , num ), sentry .WithOpName ("function" ))
134
+ } else {
135
+ span = sentry .StartSpan (ctx , "function" , sentry .WithDescription (fmt .Sprintf ("routine %d" , num )))
136
+ }
137
+
118
138
defer span .Finish ()
119
139
120
140
routine (span .Context ())
0 commit comments