Skip to content

Commit b105e67

Browse files
committed
chore(cli): scaletest: move logic for flushing traces into tracing provider
1 parent e2efeff commit b105e67

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

cli/scaletest.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ func (s *scaletestTracingFlags) provider(ctx context.Context) (trace.TracerProvi
109109
return tracerProvider, func(ctx context.Context) error {
110110
var err error
111111
closeTracingOnce.Do(func() {
112-
err = closeTracing(ctx)
112+
// Allow time to upload traces even if ctx is canceled
113+
traceCtx, traceCancel := context.WithTimeout(context.Background(), 10*time.Second)
114+
defer traceCancel()
115+
err = closeTracing(traceCtx)
113116
})
114117

115118
return err
@@ -637,10 +640,11 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd {
637640
}
638641
defer func() {
639642
// Allow time for traces to flush even if command context is
640-
// canceled.
641-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
642-
defer cancel()
643-
_ = closeTracing(ctx)
643+
// canceled. This is a no-op if tracing is not enabled.
644+
_, _ = fmt.Fprintln(inv.Stderr, "\nUploading traces...")
645+
if err := closeTracing(ctx); err != nil {
646+
_, _ = fmt.Fprintf(inv.Stderr, "\nError uploading traces: %+v\n", err)
647+
}
644648
}()
645649
tracer := tracerProvider.Tracer(scaletestTracerName)
646650

@@ -754,17 +758,6 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd {
754758
return xerrors.Errorf("cleanup tests: %w", err)
755759
}
756760

757-
// Upload traces.
758-
if tracingEnabled {
759-
_, _ = fmt.Fprintln(inv.Stderr, "\nUploading traces...")
760-
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
761-
defer cancel()
762-
err := closeTracing(ctx)
763-
if err != nil {
764-
_, _ = fmt.Fprintf(inv.Stderr, "\nError uploading traces: %+v\n", err)
765-
}
766-
}
767-
768761
if res.TotalFail > 0 {
769762
return xerrors.New("load test failed, see above for more details")
770763
}
@@ -905,7 +898,7 @@ func (r *RootCmd) scaletestTrafficGen() *clibase.Cmd {
905898
var (
906899
duration time.Duration
907900
bps int64
908-
client = new(codersdk.Client)
901+
client = &codersdk.Client{}
909902
tracingFlags = &scaletestTracingFlags{}
910903
strategy = &scaletestStrategyFlags{}
911904
cleanupStrategy = &scaletestStrategyFlags{cleanup: true}
@@ -947,10 +940,11 @@ func (r *RootCmd) scaletestTrafficGen() *clibase.Cmd {
947940
}
948941
defer func() {
949942
// Allow time for traces to flush even if command context is
950-
// canceled.
951-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
952-
defer cancel()
953-
_ = closeTracing(ctx)
943+
// canceled. This is a no-op if tracing is not enabled.
944+
_, _ = fmt.Fprintln(inv.Stderr, "\nUploading traces...")
945+
if err := closeTracing(ctx); err != nil {
946+
_, _ = fmt.Fprintf(inv.Stderr, "\nError uploading traces: %+v\n", err)
947+
}
954948
}()
955949
tracer := tracerProvider.Tracer(scaletestTracerName)
956950

@@ -1018,17 +1012,6 @@ func (r *RootCmd) scaletestTrafficGen() *clibase.Cmd {
10181012
}
10191013
}
10201014

1021-
// Upload traces.
1022-
if tracingEnabled {
1023-
_, _ = fmt.Fprintln(inv.Stderr, "\nUploading traces...")
1024-
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
1025-
defer cancel()
1026-
err := closeTracing(ctx)
1027-
if err != nil {
1028-
_, _ = fmt.Fprintf(inv.Stderr, "\nError uploading traces: %+v\n", err)
1029-
}
1030-
}
1031-
10321015
if res.TotalFail > 0 {
10331016
return xerrors.New("load test failed, see above for more details")
10341017
}

0 commit comments

Comments
 (0)