Skip to content

Commit 40af4bc

Browse files
pkg/routine: Allow starting transaction
1 parent 237565b commit 40af4bc

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

pkg/routine/instance.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ type routineThatKeepsRunningOneInstance struct {
1818
Name string
1919
Routine func(context.Context)
2020

21-
lockTTL time.Duration
22-
retryInterval time.Duration
23-
backoff combinedExponentialBackoff
24-
num int64
21+
lockTTL time.Duration
22+
retryInterval time.Duration
23+
backoff combinedExponentialBackoff
24+
num int64
25+
startTransaction bool
2526
}
2627

2728
func (r *routineThatKeepsRunningOneInstance) Run(ctx context.Context) {
@@ -70,7 +71,14 @@ func (r *routineThatKeepsRunningOneInstance) singleRun(ctx context.Context) time
7071
func() {
7172
defer errors.HandleWithCtx(ctx, fmt.Sprintf("routine %d", r.num)) // handle panics
7273

73-
span := sentry.StartSpan(lockCtx, "function", sentry.WithDescription(fmt.Sprintf("routine %d", r.num)))
74+
var span *sentry.Span
75+
76+
if r.startTransaction {
77+
span = sentry.StartTransaction(lockCtx, fmt.Sprintf("routine %d", r.num), sentry.WithOpName("function"))
78+
} else {
79+
span = sentry.StartSpan(lockCtx, "function", sentry.WithDescription(fmt.Sprintf("routine %d", r.num)))
80+
}
81+
7482
defer span.Finish()
7583

7684
r.Routine(span.Context())

pkg/routine/routine.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
type options struct {
2121
keepRunningOneInstance bool
22+
startTransaction bool
2223
}
2324

2425
// Option specifies how a routine is run.
@@ -57,6 +58,12 @@ func KeepRunningOneInstance() Option {
5758
}
5859
}
5960

61+
func StartTransaction() Option {
62+
return func(o *options) {
63+
o.startTransaction = true
64+
}
65+
}
66+
6067
// RunNamed runs a routine like Run does. Additionally it assigns the routine a
6168
// name and allows using options to control how the routine is run. Routines
6269
// 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
7380

7481
if o.keepRunningOneInstance {
7582
routine = (&routineThatKeepsRunningOneInstance{
76-
Name: name,
77-
Routine: routine,
83+
Name: name,
84+
Routine: routine,
85+
startTransaction: o.startTransaction,
7886
}).Run
7987
}
8088

81-
return Run(parentCtx, routine)
89+
return Run(parentCtx, routine, opts...)
8290
}
8391

8492
// Run runs the given function in a new background context. Panics
8593
// thrown in the function are logged and sent to sentry. The routines context is
8694
// canceled if the program receives a shutdown signal (SIGINT, SIGTERM), if the
8795
// 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+
89102
ctx = context.WithoutCancel(ctx)
90103

91104
// add routine number to context and logger
@@ -114,7 +127,14 @@ func Run(ctx context.Context, routine func(context.Context)) (cancel context.Can
114127
defer errors.HandleWithCtx(ctx, fmt.Sprintf("routine %d", num)) // handle panics
115128
defer cancel()
116129

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+
118138
defer span.Finish()
119139

120140
routine(span.Context())

0 commit comments

Comments
 (0)