Merge pull request #1801 from aluzzardi/ci-telemetry

telemetry: include CI events
This commit is contained in:
Andrea Luzzardi 2022-03-14 16:11:26 -07:00 committed by GitHub
commit e9d3b2fd15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 54 deletions

View File

@ -2,13 +2,9 @@ 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"
) )
@ -21,22 +17,6 @@ func TrackCommand(ctx context.Context, cmd *cobra.Command, props ...*telemetry.P
}, },
}, props...) }, props...)
if repo := gitRepoURL("."); repo != "" {
props = append(props, &telemetry.Property{
// Hash the repository URL for privacy
Name: "git_repository_hash",
Value: hash(repo),
})
}
if projectDir, found := pkg.GetCueModParent(); found {
// Hash the project path for privacy
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...)
} }
@ -47,29 +27,3 @@ func commandName(cmd *cobra.Command) string {
} }
return strings.Join(parts, " ") return strings.Join(parts, " ")
} }
// hash returns the sha256 digest of the string
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 ""
}
origin, err := repo.Remote("origin")
if err != nil {
return ""
}
if urls := origin.Config().URLs; len(urls) > 0 {
return urls[0]
}
return ""
}

View File

@ -3,17 +3,21 @@ package telemetry
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/sha256"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"time" "time"
"github.com/go-git/go-git/v5"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.dagger.io/dagger/pkg"
"go.dagger.io/dagger/version" "go.dagger.io/dagger/version"
) )
@ -42,15 +46,11 @@ func Track(ctx context.Context, eventName string, properties ...*Property) {
Str("event", eventName). Str("event", eventName).
Logger() Logger()
if telemetryDisabled() || isCI() { if telemetryDisabled() {
return return
} }
deviceID, err := getDeviceID() repo := gitRepoURL(".")
if err != nil {
lg.Trace().Err(err).Msg("failed to get device id")
return
}
// Base properties // Base properties
props := map[string]interface{}{ props := map[string]interface{}{
@ -60,11 +60,28 @@ func Track(ctx context.Context, eventName string, properties ...*Property) {
"arch": runtime.GOARCH, "arch": runtime.GOARCH,
} }
if repo != "" {
// Hash the repository URL for privacy
props["git_repository_hash"] = hash(repo)
}
if projectDir, found := pkg.GetCueModParent(); found {
// Hash the project path for privacy
props["project_path_hash"] = hash(projectDir)
}
// Merge extra properties // Merge extra properties
for _, p := range properties { for _, p := range properties {
props[p.Name] = p.Value props[p.Name] = p.Value
} }
lg = lg.With().Fields(props).Logger()
deviceID, err := getDeviceID(repo)
if err != nil {
lg.Trace().Err(err).Msg("failed to get device id")
return
}
lg = lg.With().Str("device_id", deviceID).Fields(props).Logger()
ev := &event{ ev := &event{
DeviceID: deviceID, DeviceID: deviceID,
@ -143,7 +160,13 @@ func telemetryDisabled() bool {
os.Getenv("DO_NOT_TRACK") != "" // https://consoledonottrack.com/ os.Getenv("DO_NOT_TRACK") != "" // https://consoledonottrack.com/
} }
func getDeviceID() (string, error) { func getDeviceID(repo string) (string, error) {
if isCI() {
if repo == "" {
return "", fmt.Errorf("unable to determine device ID")
}
return "ci-" + hash(repo), nil
}
idFile, err := homedir.Expand("~/.config/dagger/cli_id") idFile, err := homedir.Expand("~/.config/dagger/cli_id")
if err != nil { if err != nil {
return "", err return "", err
@ -165,3 +188,29 @@ func getDeviceID() (string, error) {
} }
return string(id), nil return string(id), nil
} }
// hash returns the sha256 digest of the string
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 ""
}
origin, err := repo.Remote("origin")
if err != nil {
return ""
}
if urls := origin.Config().URLs; len(urls) > 0 {
return urls[0]
}
return ""
}