Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go/cmd/vtbackup/cli/vtbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ func run(_ *cobra.Command, args []string) error {
}()

go servenv.RunDefault()
// Some stats plugins use OnRun to initialize. Wait for them to finish
// initializing before continuing, so we don't lose any stats.
if err := stats.AwaitBackend(ctx); err != nil {
return fmt.Errorf("failed to await stats backend: %w", err)
}

if detachedMode {
// this method will call os.Exit and kill this process
Expand Down
17 changes: 17 additions & 0 deletions go/stats/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package stats

import (
"bytes"
"context"
"expvar"
"fmt"
"strconv"
Expand All @@ -45,6 +46,7 @@ var (
emitStats bool
statsEmitPeriod = 60 * time.Second
statsBackend string
statsBackendInit = make(chan struct{})
combineDimensions string
dropVariables string
)
Expand Down Expand Up @@ -209,6 +211,18 @@ var pushBackends = make(map[string]PushBackend)
var pushBackendsLock sync.Mutex
var once sync.Once

func AwaitBackend(ctx context.Context) error {
if statsBackend == "" {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-statsBackendInit:
return nil
}
}

// RegisterPushBackend allows modules to register PushBackend implementations.
// Should be called on init().
func RegisterPushBackend(name string, backend PushBackend) {
Expand All @@ -218,6 +232,9 @@ func RegisterPushBackend(name string, backend PushBackend) {
log.Fatalf("PushBackend %s already exists; can't register the same name multiple times", name)
}
pushBackends[name] = backend
if name == statsBackend {
close(statsBackendInit)
}
if emitStats {
// Start a single goroutine to emit stats periodically
once.Do(func() {
Expand Down