From d09b62b1e2761d45363b0721ee78bdf902fc1975 Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Wed, 2 Mar 2022 13:53:46 -0700 Subject: [PATCH 1/6] Telemetry for commands Signed-off-by: Joel Longtine --- cmd/dagger/cmd/common/track.go | 67 +++++++++++++++++++++++--------- cmd/dagger/cmd/do.go | 2 +- cmd/dagger/cmd/project/init.go | 4 +- cmd/dagger/cmd/project/update.go | 7 ++++ cmd/dagger/cmd/up.go | 39 ++++++++----------- 5 files changed, 76 insertions(+), 43 deletions(-) diff --git a/cmd/dagger/cmd/common/track.go b/cmd/dagger/cmd/common/track.go index b492ad70..af1227ad 100644 --- a/cmd/dagger/cmd/common/track.go +++ b/cmd/dagger/cmd/common/track.go @@ -2,9 +2,14 @@ package common import ( "context" + "crypto/sha256" + "fmt" "strings" + "github.com/go-git/go-git/v5" "github.com/spf13/cobra" + "go.dagger.io/dagger/pkg" + "go.dagger.io/dagger/plan" "go.dagger.io/dagger/telemetry" ) @@ -28,6 +33,30 @@ func commandName(cmd *cobra.Command) string { return strings.Join(parts, " ") } +// TrackPlanCommand sends telemetry about a plan execution +func TrackPlanCommand(ctx context.Context, cmd *cobra.Command, daggerplan plan.Plan, props ...*telemetry.Property) chan struct{} { + props = append([]*telemetry.Property{ + { + // Hash the repository URL for privacy + Name: "git_repository_hash", + Value: hash(gitRepoURL(pkg.GetCueModParent())), + }, + { + // The project path might contain the username (e.g. /home/user/project), so we hash it for privacy. + Name: "project_path_hash", + Value: hash(pkg.GetCueModParent()), + }, + }, props...) + + if action := daggerplan.Action(); action != nil { + props = append(props, &telemetry.Property{ + Name: "action", + Value: true, + }) + } + return TrackCommand(ctx, cmd, props...) +} + // TrackProjectCommand is like TrackCommand but includes project and // optionally environment metadata. // func TrackProjectCommand(ctx context.Context, cmd *cobra.Command, w *state.Project, env *state.State, props ...*telemetry.Property) chan struct{} { @@ -57,27 +86,27 @@ func commandName(cmd *cobra.Command) string { // } // hash returns the sha256 digest of the string -// func hash(s string) string { -// return fmt.Sprintf("%x", sha256.Sum256([]byte(s))) -// } +func hash(s string) string { + return fmt.Sprintf("%x", sha256.Sum256([]byte(s))) +} // // gitRepoURL returns the git repository remote, if any. -// func gitRepoURL(path string) string { -// repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{ -// DetectDotGit: true, -// }) -// if err != nil { -// return "" -// } +func gitRepoURL(path string) string { + repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{ + DetectDotGit: true, + }) + if err != nil { + return "" + } -// origin, err := repo.Remote("origin") -// if err != nil { -// return "" -// } + origin, err := repo.Remote("origin") + if err != nil { + return "" + } -// if urls := origin.Config().URLs; len(urls) > 0 { -// return urls[0] -// } + if urls := origin.Config().URLs; len(urls) > 0 { + return urls[0] + } -// return "" -// } + return "" +} diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index 4b24d596..8c2a52d5 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -64,7 +64,7 @@ var doCmd = &cobra.Command{ return p.Do(ctx, getTargetPath(args), s) }) - // FIXME: rework telemetry + <-common.TrackPlanCommand(ctx, cmd, *p) if err != nil { lg.Fatal().Err(err).Msg("failed to execute plan") diff --git a/cmd/dagger/cmd/project/init.go b/cmd/dagger/cmd/project/init.go index 862cf606..40f7fa14 100644 --- a/cmd/dagger/cmd/project/init.go +++ b/cmd/dagger/cmd/project/init.go @@ -6,8 +6,10 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/pkg" + "go.dagger.io/dagger/telemetry" ) var sep = string(os.PathSeparator) @@ -39,7 +41,7 @@ var initCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to initialize project") } - // FIXME: Add telemtry for init + <-common.TrackCommand(ctx, cmd, &telemetry.Property{}) }, } diff --git a/cmd/dagger/cmd/project/update.go b/cmd/dagger/cmd/project/update.go index e1f97e9b..89e37f83 100644 --- a/cmd/dagger/cmd/project/update.go +++ b/cmd/dagger/cmd/project/update.go @@ -3,9 +3,11 @@ package project import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/mod" "go.dagger.io/dagger/pkg" + "go.dagger.io/dagger/telemetry" ) var updateCmd = &cobra.Command{ @@ -64,6 +66,11 @@ var updateCmd = &cobra.Command{ lg.Error().Err(err).Msg("error installing/updating packages") } + <-common.TrackCommand(ctx, cmd, &telemetry.Property{ + Name: "num_packages", + Value: len(processedRequires), + }) + }, } diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index f0a32b77..9b42232d 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -4,15 +4,12 @@ import ( "context" "os" - "cuelang.org/go/cue" - "go.dagger.io/dagger/client" "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/plan" "go.dagger.io/dagger/solver" "golang.org/x/term" - "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -53,10 +50,25 @@ var upCmd = &cobra.Command{ ctx := lg.WithContext(cmd.Context()) cl := common.NewClient(ctx) - err = europaUp(ctx, cl, args...) + // err = europaUp(ctx, cl, args...) + + p, err := plan.Load(ctx, plan.Config{ + Args: args, + With: viper.GetStringSlice("with"), + Target: viper.GetString("target"), + }) + if err != nil { + lg.Fatal().Err(err).Msg("failed to load plan") + } + + err = cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error { + err := p.Do(ctx, p.Action().Path, s) + + return err + }) // TODO: rework telemetry - // <-doneCh + <-common.TrackPlanCommand(ctx, cmd, *p) if err != nil { lg.Fatal().Err(err).Msg("failed to up environment") @@ -79,23 +91,6 @@ var upCmd = &cobra.Command{ // return false // } -func europaUp(ctx context.Context, cl *client.Client, args ...string) error { - lg := log.Ctx(ctx) - - p, err := plan.Load(ctx, plan.Config{ - Args: args, - With: viper.GetStringSlice("with"), - Target: viper.GetString("target"), - }) - if err != nil { - lg.Fatal().Err(err).Msg("failed to load plan") - } - - return cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error { - return p.Do(ctx, cue.ParsePath(viper.GetString("target")), s) - }) -} - func init() { upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check") upCmd.Flags().StringArrayP("with", "w", []string{}, "") From cc84325ba55a92b025b827477a35c9df1cbc2bf7 Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Wed, 2 Mar 2022 13:59:01 -0700 Subject: [PATCH 2/6] Remove unneeded property Signed-off-by: Joel Longtine --- cmd/dagger/cmd/project/init.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/dagger/cmd/project/init.go b/cmd/dagger/cmd/project/init.go index 40f7fa14..752be5eb 100644 --- a/cmd/dagger/cmd/project/init.go +++ b/cmd/dagger/cmd/project/init.go @@ -9,7 +9,6 @@ import ( "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/pkg" - "go.dagger.io/dagger/telemetry" ) var sep = string(os.PathSeparator) @@ -41,7 +40,9 @@ var initCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to initialize project") } - <-common.TrackCommand(ctx, cmd, &telemetry.Property{}) + // TODO: Add telemtry for init + <-common.TrackCommand(ctx, cmd) + }, } From d45c259501641516400e8238af5c6d552726f8fa Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Mon, 7 Mar 2022 10:33:39 -0700 Subject: [PATCH 3/6] Remove comment Signed-off-by: Joel Longtine --- cmd/dagger/cmd/project/init.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/dagger/cmd/project/init.go b/cmd/dagger/cmd/project/init.go index 752be5eb..8f6bc233 100644 --- a/cmd/dagger/cmd/project/init.go +++ b/cmd/dagger/cmd/project/init.go @@ -40,9 +40,7 @@ var initCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to initialize project") } - // TODO: Add telemtry for init <-common.TrackCommand(ctx, cmd) - }, } From 8c4157c3d81dbd636de0b76a300ac3e0e465416f Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Tue, 8 Mar 2022 12:51:51 -0700 Subject: [PATCH 4/6] Plan.Up -> Plan.Do Signed-off-by: Joel Longtine --- cmd/dagger/cmd/up.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 9b42232d..bff8452a 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -4,6 +4,7 @@ import ( "context" "os" + "cuelang.org/go/cue" "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/plan" @@ -62,7 +63,7 @@ var upCmd = &cobra.Command{ } err = cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error { - err := p.Do(ctx, p.Action().Path, s) + err := p.Do(ctx, cue.MakePath(), s) return err }) From 8a63f60cd02d539027976d8e1977831e23e20afc Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Tue, 8 Mar 2022 21:04:26 -0700 Subject: [PATCH 5/6] Remove TrackPlanCommand + cleanup concurrent async tracking code Signed-off-by: Joel Longtine --- cmd/dagger/cmd/common/track.go | 66 +++++++------------------------- cmd/dagger/cmd/do.go | 9 ++++- cmd/dagger/cmd/project/init.go | 4 +- cmd/dagger/cmd/project/update.go | 11 +++--- cmd/dagger/cmd/up.go | 8 ++-- 5 files changed, 32 insertions(+), 66 deletions(-) diff --git a/cmd/dagger/cmd/common/track.go b/cmd/dagger/cmd/common/track.go index af1227ad..ed0395eb 100644 --- a/cmd/dagger/cmd/common/track.go +++ b/cmd/dagger/cmd/common/track.go @@ -9,7 +9,6 @@ import ( "github.com/go-git/go-git/v5" "github.com/spf13/cobra" "go.dagger.io/dagger/pkg" - "go.dagger.io/dagger/plan" "go.dagger.io/dagger/telemetry" ) @@ -20,8 +19,21 @@ func TrackCommand(ctx context.Context, cmd *cobra.Command, props ...*telemetry.P Name: "command", Value: commandName(cmd), }, + { + // Hash the repository URL for privacy + Name: "git_repository_hash", + Value: hash(gitRepoURL(".")), + }, }, props...) + projectDir, found := pkg.GetCueModParent() + if found { + props = append(props, &telemetry.Property{ + Name: "project_path_hash", + Value: hash(projectDir), + }) + } + return telemetry.TrackAsync(ctx, "Command Executed", props...) } @@ -33,58 +45,6 @@ func commandName(cmd *cobra.Command) string { return strings.Join(parts, " ") } -// TrackPlanCommand sends telemetry about a plan execution -func TrackPlanCommand(ctx context.Context, cmd *cobra.Command, daggerplan plan.Plan, props ...*telemetry.Property) chan struct{} { - props = append([]*telemetry.Property{ - { - // Hash the repository URL for privacy - Name: "git_repository_hash", - Value: hash(gitRepoURL(pkg.GetCueModParent())), - }, - { - // The project path might contain the username (e.g. /home/user/project), so we hash it for privacy. - Name: "project_path_hash", - Value: hash(pkg.GetCueModParent()), - }, - }, props...) - - if action := daggerplan.Action(); action != nil { - props = append(props, &telemetry.Property{ - Name: "action", - Value: true, - }) - } - return TrackCommand(ctx, cmd, props...) -} - -// TrackProjectCommand is like TrackCommand but includes project and -// optionally environment metadata. -// func TrackProjectCommand(ctx context.Context, cmd *cobra.Command, w *state.Project, env *state.State, props ...*telemetry.Property) chan struct{} { -// props = append([]*telemetry.Property{ -// { -// // Hash the repository URL for privacy -// Name: "git_repository_hash", -// Value: hash(gitRepoURL(w.Path)), -// }, -// { -// // The project path might contain the username (e.g. /home/user/project), so we hash it for privacy. -// Name: "project_path_hash", -// Value: hash(w.Path), -// }, -// }, props...) - -// if env != nil { -// props = append([]*telemetry.Property{ -// { -// Name: "environment_name", -// Value: env.Name, -// }, -// }, props...) -// } - -// return TrackCommand(ctx, cmd, props...) -// } - // hash returns the sha256 digest of the string func hash(s string) string { return fmt.Sprintf("%x", sha256.Sum256([]byte(s))) diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index 8c2a52d5..d3922b66 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -15,6 +15,7 @@ import ( "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/plan" "go.dagger.io/dagger/solver" + "go.dagger.io/dagger/telemetry" "golang.org/x/term" ) @@ -60,15 +61,19 @@ var doCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to load plan") } + doneCh := common.TrackCommand(ctx, cmd, &telemetry.Property{ + Name: "action", + Value: p.Action().Path.String(), + }) + err = cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error { return p.Do(ctx, getTargetPath(args), s) }) - <-common.TrackPlanCommand(ctx, cmd, *p) - if err != nil { lg.Fatal().Err(err).Msg("failed to execute plan") } + <-doneCh }, } diff --git a/cmd/dagger/cmd/project/init.go b/cmd/dagger/cmd/project/init.go index 8f6bc233..dabae3d4 100644 --- a/cmd/dagger/cmd/project/init.go +++ b/cmd/dagger/cmd/project/init.go @@ -35,12 +35,14 @@ var initCmd = &cobra.Command{ name := viper.GetString("name") + doneCh := common.TrackCommand(ctx, cmd) + err := pkg.CueModInit(ctx, dir, name) if err != nil { lg.Fatal().Err(err).Msg("failed to initialize project") } - <-common.TrackCommand(ctx, cmd) + <-doneCh }, } diff --git a/cmd/dagger/cmd/project/update.go b/cmd/dagger/cmd/project/update.go index 89e37f83..45d40141 100644 --- a/cmd/dagger/cmd/project/update.go +++ b/cmd/dagger/cmd/project/update.go @@ -62,15 +62,16 @@ var updateCmd = &cobra.Command{ } } - if err != nil { - lg.Error().Err(err).Msg("error installing/updating packages") - } - - <-common.TrackCommand(ctx, cmd, &telemetry.Property{ + doneCh := common.TrackCommand(ctx, cmd, &telemetry.Property{ Name: "num_packages", Value: len(processedRequires), }) + if err != nil { + lg.Error().Err(err).Msg("error installing/updating packages") + } + + <-doneCh }, } diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index bff8452a..036182cb 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -51,8 +51,6 @@ var upCmd = &cobra.Command{ ctx := lg.WithContext(cmd.Context()) cl := common.NewClient(ctx) - // err = europaUp(ctx, cl, args...) - p, err := plan.Load(ctx, plan.Config{ Args: args, With: viper.GetStringSlice("with"), @@ -62,18 +60,18 @@ var upCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to load plan") } + doneCh := common.TrackCommand(ctx, cmd) + err = cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error { err := p.Do(ctx, cue.MakePath(), s) return err }) - // TODO: rework telemetry - <-common.TrackPlanCommand(ctx, cmd, *p) - if err != nil { lg.Fatal().Err(err).Msg("failed to up environment") } + <-doneCh }, } From 6e95fdefc6e450374b6a181f1aed71fac59c6635 Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Tue, 8 Mar 2022 21:10:40 -0700 Subject: [PATCH 6/6] Fix bad merge Signed-off-by: Joel Longtine --- cmd/dagger/cmd/up.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 036182cb..b2288c58 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -63,9 +63,7 @@ var upCmd = &cobra.Command{ doneCh := common.TrackCommand(ctx, cmd) err = cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error { - err := p.Do(ctx, cue.MakePath(), s) - - return err + return p.Do(ctx, cue.ParsePath(viper.GetString("target")), s) }) if err != nil {