-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a hidden `--trace` flag that turns on tracing to measure execution time. When set, a trace file is written (by default to trace.out) which can be inspected with `go tool trace trace.out`.
- Loading branch information
Showing
8 changed files
with
146 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package midcobra | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"runtime/trace" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type TraceMiddleware struct { | ||
executionID string // uuid | ||
tracef *os.File | ||
flag *pflag.Flag | ||
task *trace.Task | ||
} | ||
|
||
var _ Middleware = (*DebugMiddleware)(nil) | ||
|
||
func (t *TraceMiddleware) AttachToFlag(flags *pflag.FlagSet, flagName string) { | ||
flags.String(flagName, "", "write a trace to a file") | ||
t.flag = flags.Lookup(flagName) | ||
t.flag.Hidden = true | ||
t.flag.NoOptDefVal = "trace.out" | ||
} | ||
|
||
func (t *TraceMiddleware) preRun(cmd *cobra.Command, args []string) { | ||
if t == nil { | ||
return | ||
} | ||
path := t.flag.Value.String() | ||
if path == "" { | ||
return | ||
} | ||
var err error | ||
t.tracef, err = os.Create(path) | ||
if err != nil { | ||
panic("error enabling tracing: " + err.Error()) | ||
} | ||
if err := trace.Start(t.tracef); err != nil { | ||
panic("error enabling tracing: " + err.Error()) | ||
} | ||
|
||
var ctx context.Context | ||
ctx, t.task = trace.NewTask(cmd.Context(), "cliCommand") | ||
cmd.SetContext(ctx) | ||
} | ||
|
||
func (t *TraceMiddleware) postRun(cmd *cobra.Command, args []string, runErr error) { | ||
if t.tracef == nil { | ||
return | ||
} | ||
t.task.End() | ||
trace.Stop() | ||
if err := t.tracef.Close(); err != nil { | ||
panic("error closing trace file: " + err.Error()) | ||
} | ||
} | ||
|
||
func (t *TraceMiddleware) withExecutionID(execID string) Middleware { | ||
t.executionID = execID | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.