Skip to content

Commit

Permalink
pkg/routine: Fix spans
Browse files Browse the repository at this point in the history
  • Loading branch information
monstermunchkin committed Nov 27, 2024
1 parent 4597413 commit ec953c7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
11 changes: 5 additions & 6 deletions pkg/routine/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ func (r *routineThatKeepsRunningOneInstance) Run(ctx context.Context) {
// until it returns. Return the backoff duration after which another single run
// should be performed.
func (r *routineThatKeepsRunningOneInstance) singleRun(ctx context.Context) time.Duration {
span := sentry.StartSpan(ctx, "function", sentry.WithDescription(fmt.Sprintf("Routine %s", r.Name)))
defer span.Finish()

ctx = span.Context()

l := redis.NewLock("routine:lock:"+r.Name, redis.SetTTL(r.lockTTL))
lockCtx, cancel, err := l.AcquireAndKeepUp(ctx)
if err != nil {
Expand All @@ -74,7 +69,11 @@ func (r *routineThatKeepsRunningOneInstance) singleRun(ctx context.Context) time
routinePanicked := true
func() {
defer errors.HandleWithCtx(ctx, fmt.Sprintf("routine %d", r.num)) // handle panics
r.Routine(lockCtx)

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

r.Routine(span.Context())
routinePanicked = false
}()
if routinePanicked {
Expand Down
13 changes: 6 additions & 7 deletions pkg/routine/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"syscall"

"github.com/getsentry/sentry-go"

"github.com/pace/bricks/maintenance/errors"
"github.com/pace/bricks/maintenance/log"
pkgcontext "github.com/pace/bricks/pkg/context"
Expand Down Expand Up @@ -94,16 +93,12 @@ func Run(parentCtx context.Context, routine func(context.Context)) (cancel conte
// add routine number to context and logger
num := atomic.AddInt64(&ctr, 1)

span := sentry.StartTransaction(ctx, fmt.Sprintf("Routine %d", num), sentry.WithOpName("function"))
defer span.Finish()

ctx = span.Context()

ctx = context.WithValue(ctx, ctxNumKey{}, num)
logger := log.Ctx(ctx).With().Int64("routine", num).Logger()
ctx = logger.WithContext(ctx)
// get cancel function
ctx, cancel = context.WithCancel(ctx)

// register context to be cancelled when the program is shut down
contextsMx.Lock()
contexts[num] = cancel
Expand All @@ -120,7 +115,11 @@ func Run(parentCtx context.Context, routine func(context.Context)) (cancel conte
go func() {
defer errors.HandleWithCtx(ctx, fmt.Sprintf("routine %d", num)) // handle panics
defer cancel()
routine(ctx)

span := sentry.StartSpan(ctx, "function", sentry.WithDescription(fmt.Sprintf("routine %d", num)))
defer span.Finish()

routine(span.Context())
}()
return
}
Expand Down

0 comments on commit ec953c7

Please sign in to comment.