Skip to content

Commit

Permalink
chore(telemetry): add platform_user
Browse files Browse the repository at this point in the history
  • Loading branch information
snakster committed Dec 13, 2024
1 parent 5abab13 commit 0b6172f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 27 deletions.
7 changes: 6 additions & 1 deletion cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,11 +873,16 @@ func (c *cli) initAnalytics(cmd string, opts ...tel.MessageOpt) {
anasigfile := filepath.Join(c.clicfg.UserTerramateDir, "analytics_signature")
credfile := filepath.Join(c.clicfg.UserTerramateDir, credfile)

var repo *git.Repository
if c.prj.isRepo {
repo, _ = c.prj.repo()
}

r := tel.DefaultRecord
r.Set(
tel.Command(cmd),
tel.OrgName(c.cloudOrgName()),
tel.DetectFromEnv(credfile, cpsigfile, anasigfile),
tel.DetectFromEnv(credfile, cpsigfile, anasigfile, repo),
tel.StringFlag("chdir", c.parsedArgs.Chdir),
)
r.Set(opts...)
Expand Down
7 changes: 4 additions & 3 deletions cmd/terramate/cli/telemetry/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"

"github.com/terramate-io/terramate/cloud"
"github.com/terramate-io/terramate/git"
)

// Record is used to aggregate telemetry data over the runtime of the application.
Expand Down Expand Up @@ -73,10 +74,10 @@ func AuthUser(authUser cloud.UUID) MessageOpt {
}
}

// DetectFromEnv detects platform, auth type, signature, architecture and OS from the environment.
func DetectFromEnv(credfile, cpsigfile, anasigfile string) MessageOpt {
// DetectFromEnv detects platform, platform_user, auth type, signature, architecture and OS from the environment.
func DetectFromEnv(credfile, cpsigfile, anasigfile string, repo *git.Repository) MessageOpt {
return func(msg *Message) {
msg.Platform = DetectPlatformFromEnv()
msg.Platform, msg.PlatformUser = DetectPlatformFromEnv(repo)

msg.Auth = DetectAuthTypeFromEnv(credfile)
msg.Signature, _ = GenerateOrReadSignature(cpsigfile, anasigfile)
Expand Down
14 changes: 13 additions & 1 deletion cmd/terramate/cli/telemetry/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/madlambda/spells/assert"
"github.com/terramate-io/terramate"
"github.com/terramate-io/terramate/git"
"github.com/terramate-io/terramate/test/sandbox"

. "github.com/terramate-io/terramate/cmd/terramate/cli/telemetry"
Expand All @@ -33,12 +34,18 @@ func TestRecordLifecycle(t *testing.T) {
cpsigfile := filepath.Join(s.RootDir(), "userdir/checkpoint_signature")
anasigfile := filepath.Join(s.RootDir(), "userdir/analytics_signature")

repo := &git.Repository{
Owner: "owner",
}

// Create a record and set data.
rec := NewRecord()
rec.Set(
Command("my-command"),
OrgName("hello-org"),
DetectFromEnv(credfile, cpsigfile, anasigfile),
OrgUUID("b1a15394-e622-4a88-9e01-25b3cdc1d28e"),
DetectFromEnv(credfile, cpsigfile, anasigfile, repo),
AuthUser("1234567"),
BoolFlag("flag1", true),
BoolFlag("flag2", false),
StringFlag("flag3", "something"),
Expand Down Expand Up @@ -72,7 +79,11 @@ func TestRecordLifecycle(t *testing.T) {
err := json.NewDecoder(req.Body).Decode(&gotMsg)
assert.NoError(t, err)

assert.EqualInts(t, int(AuthIDPGoogle), int(gotMsg.Auth))
assert.EqualStrings(t, "1234567", gotMsg.AuthUser)

assert.EqualInts(t, int(PlatformGithub), int(gotMsg.Platform))
assert.EqualStrings(t, "owner", gotMsg.PlatformUser)

assert.EqualStrings(t, "a1a15394-e622-4a88-9e01-25b3cdc1d28f", gotMsg.Signature)

Expand All @@ -81,6 +92,7 @@ func TestRecordLifecycle(t *testing.T) {

assert.EqualStrings(t, "my-command", gotMsg.Command)
assert.EqualStrings(t, "hello-org", gotMsg.OrgName)
assert.EqualStrings(t, "b1a15394-e622-4a88-9e01-25b3cdc1d28e", gotMsg.OrgUUID)

if diff := cmp.Diff([]string{"flag1", "flag3", "flag5"}, gotMsg.Details); diff != "" {
t.Errorf("unexpected flag details: %s", diff)
Expand Down
56 changes: 35 additions & 21 deletions cmd/terramate/cli/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/rs/zerolog/log"
"github.com/terramate-io/terramate"
"github.com/terramate-io/terramate/git"
)

// PlatformType is the CI/CD platform.
Expand Down Expand Up @@ -61,7 +62,10 @@ const (
// Message is the analytics data that will be collected.
type Message struct {
Platform PlatformType `json:"platform,omitempty"`
Auth AuthType `json:"auth,omitempty"`
// PlatformUser is a platform-specific identifier.
PlatformUser string `json:"platform_user,omitempty"`

Auth AuthType `json:"auth,omitempty"`
// AuthUser is the TMC user UUID.
AuthUser string `json:"auth_user,omitempty"`

Expand All @@ -80,46 +84,56 @@ type Message struct {
}

// DetectPlatformFromEnv detects PlatformType based on environment variables.
func DetectPlatformFromEnv() PlatformType {
func DetectPlatformFromEnv(repo *git.Repository) (typ PlatformType, user string) {
if isEnvVarSet("GITHUB_ACTIONS") {
return PlatformGithub
typ = PlatformGithub
} else if isEnvVarSet("GITLAB_CI") {
return PlatformGitlab
typ = PlatformGitlab
} else if isEnvVarSet("BITBUCKET_BUILD_NUMBER") {
return PlatformBitBucket
typ = PlatformBitBucket
} else if isEnvVarSet("TF_BUILD") {
return PlatformAzureDevops
typ = PlatformAzureDevops
} else if isEnvVarSet("APPVEYOR") {
return PlatformAppVeyor
typ = PlatformAppVeyor
} else if isEnvVarSet("bamboo.buildKey") {
return PlatformBamboo
typ = PlatformBamboo
} else if isEnvVarSet("BUDDY") {
return PlatformBuddyWorks
typ = PlatformBuddyWorks
} else if isEnvVarSet("BUILDKITE") {
return PlatformBuildKite
typ = PlatformBuildKite
} else if isEnvVarSet("CIRCLECI") {
return PlatformCircleCI
typ = PlatformCircleCI
} else if isEnvVarSet("CIRRUS_CI") {
return PlatformCirrus
typ = PlatformCirrus
} else if isEnvVarSet("CODEBUILD_CI") {
return PlatformCodeBuild
typ = PlatformCodeBuild
} else if isEnvVarSet("HEROKU_TEST_RUN_ID") {
return PlatformHeroku
typ = PlatformHeroku
} else if strings.HasPrefix(os.Getenv("BUILD_TAG"), "hudson-") {
return PlatformHudson
typ = PlatformHudson
} else if isEnvVarSet("JENKINS_URL") {
return PlatformJenkins
typ = PlatformJenkins
} else if os.Getenv("BuildRunner") == "MyGet" {
return PlatformMyGet
typ = PlatformMyGet
} else if isEnvVarSet("TEAMCITY_VERSION") {
return PlatformTeamCity
typ = PlatformTeamCity
} else if isEnvVarSet("TRAVIS") {
return PlatformTravis
typ = PlatformTravis
} else if isEnvVarSet("CI") {
return PlatformGenericCI
typ = PlatformGenericCI
} else {
return PlatformLocal
typ = PlatformLocal
}

if repo != nil {
switch typ {
case PlatformBitBucket:
user = os.Getenv("BITBUCKET_WORKSPACE")
default:
user = repo.Owner
}
}
return
}

// DetectPlatformFromEnv detects AuthType based on environment variables and credentials.
Expand Down
49 changes: 48 additions & 1 deletion cmd/terramate/cli/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/madlambda/spells/assert"

"github.com/terramate-io/terramate/git"
"github.com/terramate-io/terramate/test/sandbox"

. "github.com/terramate-io/terramate/cmd/terramate/cli/telemetry"
Expand All @@ -34,9 +35,55 @@ func TestDetectPlatformFromEnv(t *testing.T) {
t.Setenv(k2, "")
}
}
assert.EqualInts(t, int(want), int(DetectPlatformFromEnv()))
platform, user := DetectPlatformFromEnv(nil)
assert.EqualInts(t, int(want), int(platform))
assert.EqualStrings(t, "", user)
})
}

t.Run("Github user", func(t *testing.T) {
for k := range tests {
if k != "GITHUB_ACTIONS" {
t.Setenv(k, "")
}
}
t.Setenv("GITHUB_ACTIONS", "1")
repo := &git.Repository{
Owner: "github-owner",
}
_, user := DetectPlatformFromEnv(repo)
assert.EqualStrings(t, "github-owner", user)
})

t.Run("Gitlab user", func(t *testing.T) {
for k := range tests {
if k != "GITLAB_CI" {
t.Setenv(k, "")
}
}
t.Setenv("GITLAB_CI", "1")
repo := &git.Repository{
Owner: "gitlab-owner",
}
_, user := DetectPlatformFromEnv(repo)
assert.EqualStrings(t, "gitlab-owner", user)
})

t.Run("Bitbucket user", func(t *testing.T) {
for k := range tests {
if k != "BITBUCKET_BUILD_NUMBER" {
t.Setenv(k, "")
}
}

t.Setenv("BITBUCKET_BUILD_NUMBER", "123")
t.Setenv("BITBUCKET_WORKSPACE", "bitbucket-owner")
repo := &git.Repository{
Owner: "not-used",
}
_, user := DetectPlatformFromEnv(repo)
assert.EqualStrings(t, "bitbucket-owner", user)
})
}

func TestDetectAuthTypeFromEnv(t *testing.T) {
Expand Down

0 comments on commit 0b6172f

Please sign in to comment.