Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add telemetry #328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cmd/extension/extension_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"os/exec"
"path"
"path/filepath"
"time"

cp "github.com/otiai10/copy"
"github.com/spf13/cobra"

"github.com/FriendsOfShopware/shopware-cli/extension"
"github.com/FriendsOfShopware/shopware-cli/internal/telemetry"
"github.com/FriendsOfShopware/shopware-cli/logging"
)

Expand All @@ -24,6 +26,8 @@ var extensionZipCmd = &cobra.Command{
Short: "Zip a Extension",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
duration := time.Now()

extPath, err := filepath.Abs(args[0])
if err != nil {
return err
Expand Down Expand Up @@ -187,7 +191,15 @@ var extensionZipCmd = &cobra.Command{
return fmt.Errorf("create zip file: %w", err)
}

logging.FromContext(cmd.Context()).Infof("Created file %s", fileName)
seconds := time.Since(duration).Seconds()

telemetry.Track("extension_zip", map[string]interface{}{
"duration": seconds,
"changelog-generated": extCfg.Changelog.Enabled,
"ai-changelog": extCfg.Changelog.AiEnabled,
})

logging.FromContext(cmd.Context()).Infof("Created file %s in %f seconds", fileName, seconds)

return nil
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posthog/posthog-go v0.0.0-20240315130956-036dfa9f3555 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
github.com/otiai10/mint v1.5.1/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posthog/posthog-go v0.0.0-20240315130956-036dfa9f3555 h1:RqJZxk2VAaZYCCk4ZVo7iLqp4a03LWitjE0tNIMyvMU=
github.com/posthog/posthog-go v0.0.0-20240315130956-036dfa9f3555/go.mod h1:QjlpryJtfYLrZF2GUkAhejH4E7WlDbdKkvOi5hLmkdg=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
6 changes: 6 additions & 0 deletions internal/system/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ func GetShopwareCliCacheDir() string {

return path.Join(cacheDir, "shopware-cli")
}

func GetShopwareCliConfigDir() string {
configDir, _ := os.UserConfigDir()

return path.Join(configDir, "shopware-cli")
}
62 changes: 62 additions & 0 deletions internal/telemetry/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package telemetry

import (
"crypto/sha256"
"encoding/hex"
"os"
"path"

"github.com/FriendsOfShopware/shopware-cli/internal/system"
"github.com/google/uuid"
)

// gatherDistinctId returns a distinct ID for the current user, so we anoynmously track the usage of the CLI

Check failure on line 13 in internal/telemetry/id.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func gatherDistinctId() string {
if os.Getenv("CI") == "true" {
return gatherByCI()
}

return gatherByMachine()
}

func gatherByCI() string {
if os.Getenv("GITHUB_REPOSITORY") != "" {
return hash(os.Getenv("GITHUB_REPOSITORY"))
}

// GitLab
if os.Getenv("CI_PROJECT_NAME") != "" {
return hash(os.Getenv("CI_PROJECT_NAME"))
}

// Bitbucket
if os.Getenv("BITBUCKET_REPO_FULL_NAME") != "" {
return hash(os.Getenv("BITBUCKET_REPO_FULL_NAME"))
}

// We cannot determine the CI system, so we generate a random UUID
return hash(uuid.New().String())
}

func gatherByMachine() string {
configDir := system.GetShopwareCliConfigDir()

telemetryFile := path.Join(configDir, "telemetry-id")

if contents, err := os.ReadFile(telemetryFile); err == nil {
return string(contents)
}

if _, err := os.Stat(configDir); os.IsNotExist(err) {
_ = os.MkdirAll(configDir, 0o700)
}

id := hash(uuid.New().String())
_ = os.WriteFile(telemetryFile, []byte(id), 0o600)

return id
}

func hash(s string) string {
return hex.EncodeToString(sha256.New().Sum([]byte(s)))
}
53 changes: 53 additions & 0 deletions internal/telemetry/posthog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package telemetry

import (
"os"

"github.com/posthog/posthog-go"
)

var (
client posthog.Client
distinctId = ""
)

func Init() {
doNotTrack := os.Getenv("DO_NOT_TRACK")

if doNotTrack == "1" {
return
}

var err error
client, err = posthog.NewWithConfig(
"phc_bkTsf3bZw70kXFoqMj3Qkeo3dtzC3x1JvuhamdJI8mJ",
posthog.Config{
Endpoint: "https://eu.posthog.com",
},
)
if err != nil {
return
}

distinctId = gatherDistinctId()
}

func Track(event string, properties map[string]interface{}) {
if client == nil {
return
}

client.Enqueue(posthog.Capture{

Check failure on line 40 in internal/telemetry/posthog.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `client.Enqueue` is not checked (errcheck)
Event: event,
DistinctId: distinctId,
Properties: properties,
})
}

func Close() {
if client == nil {
return
}

client.Close()
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"context"

"github.com/FriendsOfShopware/shopware-cli/cmd"
"github.com/FriendsOfShopware/shopware-cli/internal/telemetry"
"github.com/FriendsOfShopware/shopware-cli/logging"
)

func main() {
telemetry.Init()
defer telemetry.Close()

logger := logging.NewLogger()
cmd.Execute(logging.WithLogger(context.Background(), logger))
}
Loading