Skip to content

Commit

Permalink
feat: Use database id as unique id (#705)
Browse files Browse the repository at this point in the history
* feat: Use database id as unique id
  • Loading branch information
roneli committed May 16, 2022
1 parent cece150 commit dc00381
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cmd/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func initSentry() {
if core.Version == core.DevelopmentVersion && !viper.GetBool("debug-sentry") {
dsn = "" // Disable Sentry in development mode, unless debug-sentry was enabled
}
userId := analytics.GetUserId()
userId := analytics.GetCookieId()
if analytics.CQTeamID == userId.String() && !viper.GetBool("debug-sentry") {
dsn = ""
}
Expand Down Expand Up @@ -104,6 +104,7 @@ func initSentry() {
"ci": strconv.FormatBool(analytics.IsCI()),
"faas": strconv.FormatBool(analytics.IsFaaS()),
})
scope.SetExtra("cookie_id", userId.String())
scope.SetExtra("instance_id", instanceId)
})
}
17 changes: 12 additions & 5 deletions internal/analytics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type Client struct {
version VersionInfo
env *Environment
terminal bool
userId uuid.UUID
userId string
cookieId uuid.UUID
instanceId string

disabled bool
Expand Down Expand Up @@ -112,7 +113,8 @@ func Init(opts ...Option) error {
func New(opts ...Option) *Client {
c := &Client{
version: VersionInfo{},
userId: GetUserId(),
userId: GetCookieId().String(),
cookieId: GetCookieId(),
instanceId: uuid.New().String(),
properties: make(map[string]interface{}),
debug: false,
Expand Down Expand Up @@ -140,11 +142,11 @@ func New(opts ...Option) *Client {
return c
}

// GetUserId will read or generate a persistent `telemetry-random-id` file and return its value.
// GetCookieId will read or generate a persistent `telemetry-random-id` file and return its value.
// First it will try reading ~/.cq/telemetry-random-id and use that value if found. If not, it will move on to ./cq/telemetry-random-id, first attempting a read and if not found, will create that file filling it with a newly generated ID.
// If a directory with the same name is encountered, process is aborted and an empty string is returned.
// If a new file is generated, c.newRandomId is set.
func GetUserId() uuid.UUID {
func GetCookieId() uuid.UUID {
fs := afero.Afero{Fs: afero.NewOsFs()}
v, err := persistentdata.New(fs, "telemetry-random-id", uuid.NewString).Get()
if err != nil {
Expand Down Expand Up @@ -177,6 +179,7 @@ func Capture(eventType string, providers registry.Providers, data Message, diags
"build_date": c.version.BuildDate,
"env": c.env,
"instance_id": c.instanceId,
"cookie_id": c.cookieId,
"success": !diags.HasErrors(),
"installed_providers": pp,
"diagnostics": core.SummarizeDiagnostics(diags),
Expand All @@ -202,13 +205,17 @@ func Capture(eventType string, providers registry.Providers, data Message, diags
return
}

event := analytics.Track{UserId: c.userId.String(), Event: eventType, Timestamp: time.Now().UTC(), Properties: eventProps}
event := analytics.Track{UserId: c.userId, Event: eventType, Timestamp: time.Now().UTC(), Properties: eventProps}
if err := c.client.Enqueue(event); err != nil {
if c.debug {
log.Error().Err(err).Msg("failed to send analytics")
}
}
}
func SetUserId(userId string) {
c := currentHub
c.userId = userId
}

func SetGlobalProperty(k string, v interface{}) {
c := currentHub
Expand Down
10 changes: 10 additions & 0 deletions pkg/ui/console/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func CreateClientFromConfig(ctx context.Context, cfg *config.Config, instanceId

if dbId, ok := dialect.Identifier(ctx); ok {
setAnalyticsProperties(map[string]interface{}{"database_id": dbId})
setUserId(dbId)
}

storage = database.NewStorage(cfg.CloudQuery.Connection.DSN, dialect)
Expand Down Expand Up @@ -782,6 +783,15 @@ func setAnalyticsProperties(props map[string]interface{}) {
})
}

func setUserId(newId string) {
analytics.SetUserId(newId)
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetUser(sentry.User{
ID: newId,
})
})
}

func setConfigAnalytics(cfg *config.Config, history bool) {
cfgJSON, _ := json.Marshal(cfg)
s := sha256.New()
Expand Down

0 comments on commit dc00381

Please sign in to comment.