Skip to content

Commit

Permalink
pkg/routine: Allow starting transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
monstermunchkin committed Jan 27, 2025
1 parent 237565b commit 3bc0fc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
18 changes: 13 additions & 5 deletions pkg/routine/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ type routineThatKeepsRunningOneInstance struct {
Name string
Routine func(context.Context)

lockTTL time.Duration
retryInterval time.Duration
backoff combinedExponentialBackoff
num int64
lockTTL time.Duration
retryInterval time.Duration
backoff combinedExponentialBackoff
num int64
startTransaction bool
}

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

span := sentry.StartSpan(lockCtx, "function", sentry.WithDescription(fmt.Sprintf("routine %d", r.num)))
var span *sentry.Span

if r.startTransaction {
span = sentry.StartTransaction(lockCtx, fmt.Sprintf("routine %d", r.num), sentry.WithOpName("function"))
} else {
span = sentry.StartSpan(lockCtx, "function", sentry.WithDescription(fmt.Sprintf("routine %d", r.num)))
}

defer span.Finish()

r.Routine(span.Context())
Expand Down
30 changes: 25 additions & 5 deletions pkg/routine/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

type options struct {
keepRunningOneInstance bool
startTransaction bool
}

// Option specifies how a routine is run.
Expand Down Expand Up @@ -57,6 +58,12 @@ func KeepRunningOneInstance() Option {
}
}

func StartAsTransaction() Option {
return func(o *options) {
o.startTransaction = true
}
}

// RunNamed runs a routine like Run does. Additionally it assigns the routine a
// name and allows using options to control how the routine is run. Routines
// with the same name show consistent behaviour for the options, like mutual
Expand All @@ -73,19 +80,25 @@ func RunNamed(parentCtx context.Context, name string, routine func(context.Conte

if o.keepRunningOneInstance {
routine = (&routineThatKeepsRunningOneInstance{
Name: name,
Routine: routine,
Name: name,
Routine: routine,
startTransaction: o.startTransaction,
}).Run
}

return Run(parentCtx, routine)
return Run(parentCtx, routine, opts...)
}

// Run runs the given function in a new background context. Panics
// thrown in the function are logged and sent to sentry. The routines context is
// canceled if the program receives a shutdown signal (SIGINT, SIGTERM), if the
// returned CancelFunc is called, or if the routine returned.
func Run(ctx context.Context, routine func(context.Context)) (cancel context.CancelFunc) {
func Run(ctx context.Context, routine func(context.Context), opts ...Option) (cancel context.CancelFunc) {
o := options{}
for _, opt := range opts {
opt(&o)
}

ctx = context.WithoutCancel(ctx)

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

span := sentry.StartSpan(ctx, "function", sentry.WithDescription(fmt.Sprintf("routine %d", num)))
var span *sentry.Span

if o.startTransaction {
span = sentry.StartTransaction(ctx, fmt.Sprintf("routine %d", num), sentry.WithOpName("function"))
} else {
span = sentry.StartSpan(ctx, "function", sentry.WithDescription(fmt.Sprintf("routine %d", num)))
}

defer span.Finish()

routine(span.Context())
Expand Down

0 comments on commit 3bc0fc1

Please sign in to comment.