Merge pull request #1676 from jlongtine/europa-telemetry

Europa telemetry
This commit is contained in:
Andrea Luzzardi 2022-03-08 23:03:55 -08:00 committed by GitHub
commit c2bf9a528c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 72 deletions

View File

@ -2,9 +2,13 @@ package common
import ( import (
"context" "context"
"crypto/sha256"
"fmt"
"strings" "strings"
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.dagger.io/dagger/pkg"
"go.dagger.io/dagger/telemetry" "go.dagger.io/dagger/telemetry"
) )
@ -15,8 +19,21 @@ func TrackCommand(ctx context.Context, cmd *cobra.Command, props ...*telemetry.P
Name: "command", Name: "command",
Value: commandName(cmd), Value: commandName(cmd),
}, },
{
// Hash the repository URL for privacy
Name: "git_repository_hash",
Value: hash(gitRepoURL(".")),
},
}, props...) }, 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...) return telemetry.TrackAsync(ctx, "Command Executed", props...)
} }
@ -28,56 +45,28 @@ func commandName(cmd *cobra.Command) string {
return strings.Join(parts, " ") return strings.Join(parts, " ")
} }
// 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 // hash returns the sha256 digest of the string
// func hash(s string) string { func hash(s string) string {
// return fmt.Sprintf("%x", sha256.Sum256([]byte(s))) return fmt.Sprintf("%x", sha256.Sum256([]byte(s)))
// } }
// // gitRepoURL returns the git repository remote, if any. // // gitRepoURL returns the git repository remote, if any.
// func gitRepoURL(path string) string { func gitRepoURL(path string) string {
// repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{ repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{
// DetectDotGit: true, DetectDotGit: true,
// }) })
// if err != nil { if err != nil {
// return "" return ""
// } }
// origin, err := repo.Remote("origin") origin, err := repo.Remote("origin")
// if err != nil { if err != nil {
// return "" return ""
// } }
// if urls := origin.Config().URLs; len(urls) > 0 { if urls := origin.Config().URLs; len(urls) > 0 {
// return urls[0] return urls[0]
// } }
// return "" return ""
// } }

View File

@ -15,6 +15,7 @@ import (
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/plan" "go.dagger.io/dagger/plan"
"go.dagger.io/dagger/solver" "go.dagger.io/dagger/solver"
"go.dagger.io/dagger/telemetry"
"golang.org/x/term" "golang.org/x/term"
) )
@ -60,15 +61,19 @@ var doCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load plan") 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 { err = cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error {
return p.Do(ctx, getTargetPath(args), s) return p.Do(ctx, getTargetPath(args), s)
}) })
// FIXME: rework telemetry
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to execute plan") lg.Fatal().Err(err).Msg("failed to execute plan")
} }
<-doneCh
}, },
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/pkg" "go.dagger.io/dagger/pkg"
) )
@ -34,12 +35,14 @@ var initCmd = &cobra.Command{
name := viper.GetString("name") name := viper.GetString("name")
doneCh := common.TrackCommand(ctx, cmd)
err := pkg.CueModInit(ctx, dir, name) err := pkg.CueModInit(ctx, dir, name)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to initialize project") lg.Fatal().Err(err).Msg("failed to initialize project")
} }
// FIXME: Add telemtry for init <-doneCh
}, },
} }

View File

@ -3,9 +3,11 @@ package project
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/mod" "go.dagger.io/dagger/mod"
"go.dagger.io/dagger/pkg" "go.dagger.io/dagger/pkg"
"go.dagger.io/dagger/telemetry"
) )
var updateCmd = &cobra.Command{ var updateCmd = &cobra.Command{
@ -60,10 +62,16 @@ var updateCmd = &cobra.Command{
} }
} }
doneCh := common.TrackCommand(ctx, cmd, &telemetry.Property{
Name: "num_packages",
Value: len(processedRequires),
})
if err != nil { if err != nil {
lg.Error().Err(err).Msg("error installing/updating packages") lg.Error().Err(err).Msg("error installing/updating packages")
} }
<-doneCh
}, },
} }

View File

@ -5,14 +5,12 @@ import (
"os" "os"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/plan" "go.dagger.io/dagger/plan"
"go.dagger.io/dagger/solver" "go.dagger.io/dagger/solver"
"golang.org/x/term" "golang.org/x/term"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -53,14 +51,25 @@ var upCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
cl := common.NewClient(ctx) cl := common.NewClient(ctx)
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")
}
// TODO: rework telemetry doneCh := common.TrackCommand(ctx, cmd)
// <-doneCh
err = cl.Do(ctx, p.Context(), func(ctx context.Context, s solver.Solver) error {
return p.Do(ctx, cue.ParsePath(viper.GetString("target")), s)
})
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to up environment") lg.Fatal().Err(err).Msg("failed to up environment")
} }
<-doneCh
}, },
} }
@ -79,23 +88,6 @@ var upCmd = &cobra.Command{
// return false // 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() { func init() {
upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check") upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check")
upCmd.Flags().StringArrayP("with", "w", []string{}, "") upCmd.Flags().StringArrayP("with", "w", []string{}, "")