Skip to content

Commit

Permalink
Stop tracer provider from root command
Browse files Browse the repository at this point in the history
Waits for tracer provider to stop within 5 seconds, otherwise an error
message is logged. Because both methods, stopLoggers() and
stopTracerProvider(), are bounded by a timeout, execute them
concurrently and wait for them to finish.
  • Loading branch information
ka3de committed Nov 10, 2023
1 parent e82a28e commit 6225e08
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import (
"go.k6.io/k6/log"
)

const waitLoggerCloseTimeout = time.Second * 5
const (
waitLoggerCloseTimeout = time.Second * 5
waitForTracerProviderStopTimeout = time.Second * 5
)

// This is to keep all fields needed for the main/root k6 command
type rootCommand struct {
Expand Down Expand Up @@ -97,7 +100,21 @@ func (c *rootCommand) execute() {
exitCode := -1
defer func() {
cancel()
c.stopLoggers()

// Wait for loggers and tracer provider to stop.
// As both methods already use a timeout, just wait.
var wg sync.WaitGroup
ff := [...]func(){c.stopLoggers, c.stopTracerProvider}
wg.Add(len(ff))
for _, f := range ff {
f := f
go func() {
f()
wg.Done()
}()
}
wg.Wait()

c.globalState.OSExit(exitCode)
}()

Expand Down Expand Up @@ -158,6 +175,17 @@ func (c *rootCommand) stopLoggers() {
}
}

func (c *rootCommand) stopTracerProvider() {
ctx, cancel := context.WithTimeout(context.Background(), waitForTracerProviderStopTimeout)
defer cancel()

if err := c.globalState.TracerProvider.Shutdown(ctx); err != nil {
c.globalState.FallbackLogger.Errorf(
"The tracer provider didn't stop gracefully: %v", err,
)
}
}

func rootCmdPersistentFlagSet(gs *state.GlobalState) *pflag.FlagSet {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
// TODO: refactor this config, the default value management with pflag is
Expand Down

0 comments on commit 6225e08

Please sign in to comment.