Skip to content

Commit 2870425

Browse files
authored
[trace] Add trace for Devbox methods (#1098)
## Summary 1. Add `ctx` parameter for most Devbox methods 2. Add trace tasks for Devbox methods 3. Add regions for sub-methods ## How was it tested?
1 parent 9a400eb commit 2870425

File tree

23 files changed

+121
-69
lines changed

23 files changed

+121
-69
lines changed

devbox.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ type Devbox interface {
2525
ProjectDir() string
2626
// Generate creates the directory of Nix files and the Dockerfile that define
2727
// the devbox environment.
28-
Generate() error
29-
GenerateDevcontainer(force bool) error
30-
GenerateDockerfile(force bool) error
31-
GenerateEnvrcFile(force bool) error
32-
Info(pkg string, markdown bool) error
28+
Generate(ctx context.Context) error
29+
GenerateDevcontainer(ctx context.Context, force bool) error
30+
GenerateDockerfile(ctx context.Context, force bool) error
31+
GenerateEnvrcFile(ctx context.Context, force bool) error
32+
Info(ctx context.Context, pkg string, markdown bool) error
3333
Install(ctx context.Context) error
3434
IsEnvEnabled() bool
3535
ListScripts() []string
3636
PrintEnv(ctx context.Context, includeHooks bool) (string, error)
3737
PrintEnvVars(ctx context.Context) ([]string, error)
3838
PrintGlobalList() error
3939
Pull(ctx context.Context, overwrite bool, path string) error
40-
Push(url string) error
40+
Push(ctx context.Context, url string) error
4141
// Remove removes Nix packages from the config so that it no longer exists in
4242
// the devbox environment.
4343
Remove(ctx context.Context, pkgs ...string) error
@@ -48,7 +48,7 @@ type Devbox interface {
4848
Shell(ctx context.Context) error
4949
// ShellPlan creates a plan of the actions that devbox will take to generate its
5050
// shell environment.
51-
ShellPlan() (*plansdk.FlakePlan, error)
51+
ShellPlan(ctx context.Context) (*plansdk.FlakePlan, error)
5252
StartProcessManager(ctx context.Context, requestedServices []string, background bool, processComposeFileOrDir string) error
5353
StartServices(ctx context.Context, services ...string) error
5454
StopServices(ctx context.Context, allProjects bool, services ...string) error

internal/boxcli/add.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/pkg/errors"
1010
"github.com/spf13/cobra"
11+
1112
"go.jetpack.io/devbox"
1213
"go.jetpack.io/devbox/internal/boxcli/usererr"
1314
"go.jetpack.io/devbox/internal/impl/devopt"

internal/boxcli/create.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path/filepath"
1010

1111
"github.com/spf13/cobra"
12+
1213
"go.jetpack.io/devbox/internal/templates"
1314
"go.jetpack.io/devbox/internal/ux"
1415
)
@@ -55,11 +56,7 @@ func createCmd() *cobra.Command {
5556
return command
5657
}
5758

58-
func runCreateCmd(
59-
cmd *cobra.Command,
60-
args []string,
61-
flags *createCmdFlags,
62-
) error {
59+
func runCreateCmd(cmd *cobra.Command, args []string, flags *createCmdFlags) error {
6360
path := pathArg(args)
6461
if path == "" {
6562
wd, _ := os.Getwd()

internal/boxcli/generate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func sshConfigCmd() *cobra.Command {
122122
},
123123
}
124124
command.Flags().StringVarP(
125-
&flags.githubUsername, "username", "u", "", "Github username to use for ssh",
125+
&flags.githubUsername, "username", "u", "", "GitHub username to use for ssh",
126126
)
127127
flags.config.register(command)
128128
return command
@@ -139,11 +139,11 @@ func runGenerateCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
139139
}
140140
switch cmd.Use {
141141
case "debug":
142-
return box.Generate()
142+
return box.Generate(cmd.Context())
143143
case "devcontainer":
144-
return box.GenerateDevcontainer(flags.force)
144+
return box.GenerateDevcontainer(cmd.Context(), flags.force)
145145
case "dockerfile":
146-
return box.GenerateDockerfile(flags.force)
146+
return box.GenerateDockerfile(cmd.Context(), flags.force)
147147
}
148148
return nil
149149
}
@@ -161,5 +161,5 @@ func runGenerateDirenvCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
161161
return errors.WithStack(err)
162162
}
163163

164-
return box.GenerateEnvrcFile(flags.force)
164+
return box.GenerateEnvrcFile(cmd.Context(), flags.force)
165165
}

internal/boxcli/generate/devcontainer_util.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
package generate
55

66
import (
7+
"context"
78
"embed"
89
"encoding/json"
910
"html/template"
1011
"os"
1112
"path/filepath"
1213
"regexp"
14+
"runtime/trace"
1315
"strings"
1416

1517
"go.jetpack.io/devbox/internal/debug"
@@ -42,7 +44,9 @@ type dockerfileData struct {
4244
}
4345

4446
// CreateDockerfile creates a Dockerfile in path and writes devcontainerDockerfile.tmpl's content into it
45-
func CreateDockerfile(tmplFS embed.FS, path string, localFlakeDirs []string, isDevcontainer bool) error {
47+
func CreateDockerfile(ctx context.Context, tmplFS embed.FS, path string, localFlakeDirs []string, isDevcontainer bool) error {
48+
defer trace.StartRegion(ctx, "createDockerfile").End()
49+
4650
// create dockerfile
4751
file, err := os.Create(filepath.Join(path, "Dockerfile"))
4852
if err != nil {
@@ -60,7 +64,9 @@ func CreateDockerfile(tmplFS embed.FS, path string, localFlakeDirs []string, isD
6064
}
6165

6266
// CreateDevcontainer creates a devcontainer.json in path and writes getDevcontainerContent's output into it
63-
func CreateDevcontainer(path string, pkgs []string) error {
67+
func CreateDevcontainer(ctx context.Context, path string, pkgs []string) error {
68+
defer trace.StartRegion(ctx, "createDevcontainer").End()
69+
6470
// create devcontainer.json file
6571
file, err := os.Create(filepath.Join(path, "devcontainer.json"))
6672
if err != nil {
@@ -78,7 +84,9 @@ func CreateDevcontainer(path string, pkgs []string) error {
7884
return err
7985
}
8086

81-
func CreateEnvrc(tmplFS embed.FS, path string) error {
87+
func CreateEnvrc(ctx context.Context, tmplFS embed.FS, path string) error {
88+
defer trace.StartRegion(ctx, "createEnvrc").End()
89+
8290
// create .envrc file
8391
file, err := os.Create(filepath.Join(path, ".envrc"))
8492
if err != nil {

internal/boxcli/info.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package boxcli
66
import (
77
"github.com/pkg/errors"
88
"github.com/spf13/cobra"
9+
910
"go.jetpack.io/devbox"
1011
"go.jetpack.io/devbox/internal/impl/devopt"
1112
)
@@ -41,5 +42,5 @@ func infoCmdFunc(cmd *cobra.Command, pkg string, flags infoCmdFlags) error {
4142
return errors.WithStack(err)
4243
}
4344

44-
return box.Info(pkg, flags.markdown)
45+
return box.Info(cmd.Context(), pkg, flags.markdown)
4546
}

internal/boxcli/init.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package boxcli
66
import (
77
"github.com/pkg/errors"
88
"github.com/spf13/cobra"
9+
910
"go.jetpack.io/devbox"
1011
)
1112

@@ -29,9 +30,5 @@ func runInitCmd(cmd *cobra.Command, args []string) error {
2930
path := pathArg(args)
3031

3132
_, err := devbox.InitConfig(path, cmd.ErrOrStderr())
32-
if err != nil {
33-
return errors.WithStack(err)
34-
}
35-
36-
return nil
33+
return errors.WithStack(err)
3734
}

internal/boxcli/install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/pkg/errors"
1010
"github.com/spf13/cobra"
11+
1112
"go.jetpack.io/devbox"
1213
"go.jetpack.io/devbox/internal/impl/devopt"
1314
)

internal/boxcli/plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func runPlanCmd(cmd *cobra.Command, flags planCmdFlags) error {
4747
enc.SetIndent("", " ")
4848
enc.SetEscapeHTML(false)
4949

50-
shellPlan, err := box.ShellPlan()
50+
shellPlan, err := box.ShellPlan(cmd.Context())
5151
if err != nil {
5252
return err
5353
}

internal/boxcli/pull.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/AlecAivazis/survey/v2"
1212
"github.com/pkg/errors"
1313
"github.com/spf13/cobra"
14+
1415
"go.jetpack.io/devbox"
1516
"go.jetpack.io/devbox/internal/impl/devopt"
1617
)
@@ -43,11 +44,7 @@ func pullCmd() *cobra.Command {
4344
return cmd
4445
}
4546

46-
func pullCmdFunc(
47-
cmd *cobra.Command,
48-
url string,
49-
flags *pullCmdFlags,
50-
) error {
47+
func pullCmdFunc(cmd *cobra.Command, url string, flags *pullCmdFlags) error {
5148
box, err := devbox.Open(&devopt.Opts{
5249
Dir: flags.config.path,
5350
Writer: cmd.ErrOrStderr(),

0 commit comments

Comments
 (0)