Telemetry for commands

Signed-off-by: Joel Longtine <joel@dagger.io>
This commit is contained in:
Joel Longtine 2022-03-02 13:53:46 -07:00
parent 8571dfbb43
commit d09b62b1e2
5 changed files with 76 additions and 43 deletions

View File

@ -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 ""
}

View File

@ -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")

View File

@ -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{})
},
}

View File

@ -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),
})
},
}

View File

@ -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{}, "")