Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add --cpu-profiling flag. #1663

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
21 changes: 21 additions & 0 deletions cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path"
"path/filepath"
"runtime/pprof"
"strings"
"time"

Expand Down Expand Up @@ -112,6 +113,7 @@ type cliSpec struct {
LogDestination string `optional:"true" default:"stderr" enum:"stderr,stdout" help:"Destination channel of log messages: 'stderr' or 'stdout'."`
Quiet bool `optional:"false" help:"Disable outputs."`
Verbose int `short:"v" optional:"true" default:"0" type:"counter" help:"Increase verboseness of output"`
CPUProfiling bool `optional:"true" default:"false" help:"Create a CPU profile file when running"`

deprecatedGlobalSafeguardsCliSpec

Expand Down Expand Up @@ -417,6 +419,18 @@ func newCLI(version string, args []string, stdin io.Reader, stdout, stderr io.Wr
fatalWithDetails(err, "parsing cli args %v", args)
}

if parsedArgs.CPUProfiling {
stdfmt.Println("Creating CPU profile...")
f, err := os.Create("terramate.prof")
if err != nil {
fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
fatal(err)
}
}

configureLogging(parsedArgs.LogLevel, parsedArgs.LogFmt,
parsedArgs.LogDestination, stdout, stderr)
// If we don't re-create the logger after configuring we get some
Expand Down Expand Up @@ -631,6 +645,13 @@ func (c *cli) run() {

logger.Debug().Msg("Handle command.")

// We start the CPU Profiling during the flags parsing, but can't defer
// the stop there, as the CLI parsing returns far before the program is
// done running. Therefore we schedule it here.
if c.parsedArgs.CPUProfiling {
defer pprof.StopCPUProfile()
}

switch c.ctx.Command() {
case "fmt":
c.format()
Expand Down
Loading