Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 4a5e1e4

Browse files
feat(sg): report user os information via analytics (#64280)
Closes DINF-193 ![CleanShot 2024-08-05 at 21 28 37@2x](https://github.com/user-attachments/assets/34f121c5-5a85-456c-b12b-2f959573fcae) OS information is now part of the `sg` analytics metadata. ## Test plan <!-- REQUIRED; info at https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles --> Any `sg` operation now reports the os information. ## Changelog <!-- OPTIONAL; info at https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c -->
1 parent 02fb199 commit 4a5e1e4

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

dev/sg/internal/analytics/analytics.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path"
10+
"runtime"
1011
"sync"
1112
"time"
1213

@@ -151,6 +152,14 @@ func (i invocation) GetArgs() []any {
151152
return v.([]any)
152153
}
153154

155+
func (i invocation) GetOS() string {
156+
v, ok := i.metadata["os"]
157+
if !ok {
158+
return ""
159+
}
160+
return v.(string)
161+
}
162+
154163
var store = sync.OnceValue(func() analyticsStore {
155164
db, err := newDiskStore()
156165
if err != nil {
@@ -191,14 +200,15 @@ type analyticsStore struct {
191200
db Execer
192201
}
193202

194-
func (s analyticsStore) NewInvocation(ctx context.Context, uuid uuid.UUID, version string, meta map[string]any) error {
203+
func (s analyticsStore) NewInvocation(_ context.Context, uuid uuid.UUID, version string, meta map[string]any) error {
195204
if s.db == nil {
196205
return ErrDBNotInitialized
197206
}
198207

199208
meta["user_id"] = getEmail()
200209
meta["version"] = version
201210
meta["start_time"] = time.Now()
211+
meta["os"] = getOS()
202212

203213
b, err := json.Marshal(meta)
204214
if err != nil {
@@ -213,7 +223,7 @@ func (s analyticsStore) NewInvocation(ctx context.Context, uuid uuid.UUID, versi
213223
return nil
214224
}
215225

216-
func (s analyticsStore) AddMetadata(ctx context.Context, uuid uuid.UUID, meta map[string]any) error {
226+
func (s analyticsStore) AddMetadata(_ context.Context, uuid uuid.UUID, meta map[string]any) error {
217227
if s.db == nil {
218228
return ErrDBNotInitialized
219229
}
@@ -231,7 +241,7 @@ func (s analyticsStore) AddMetadata(ctx context.Context, uuid uuid.UUID, meta ma
231241
return nil
232242
}
233243

234-
func (s analyticsStore) DeleteInvocation(ctx context.Context, uuid string) error {
244+
func (s analyticsStore) DeleteInvocation(_ context.Context, uuid string) error {
235245
if s.db == nil {
236246
return ErrDBNotInitialized
237247
}
@@ -244,7 +254,7 @@ func (s analyticsStore) DeleteInvocation(ctx context.Context, uuid string) error
244254
return nil
245255
}
246256

247-
func (s analyticsStore) ListCompleted(ctx context.Context) ([]invocation, error) {
257+
func (s analyticsStore) ListCompleted(_ context.Context) ([]invocation, error) {
248258
if s.db == nil {
249259
return nil, nil
250260
}
@@ -304,6 +314,24 @@ func emailfunc() string {
304314

305315
var getEmail = sync.OnceValue[string](emailfunc)
306316

317+
func osFunc() string {
318+
userOS := runtime.GOOS
319+
switch userOS {
320+
case "windows":
321+
return "Windows"
322+
case "darwin":
323+
return "macOS"
324+
case "linux":
325+
return "Linux"
326+
default:
327+
// weird case, but catching it instead of throwing an error
328+
return userOS
329+
}
330+
}
331+
332+
// Dont invoke this function directly. Use the `getOS` function instead.
333+
var getOS = sync.OnceValue[string](osFunc)
334+
307335
func NewInvocation(ctx context.Context, version string, meta map[string]any) (context.Context, error) {
308336
// v7 for sortable property (not vital as we also store timestamps, but no harm to have)
309337
u, _ := uuid.NewV7()

dev/sg/internal/analytics/bigquery.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func NewEvent(i invocation) *event {
7777
"failed": i.IsFailed(),
7878
"cancelled": i.IsCancelled(),
7979
"panicked": i.IsPanicked(),
80+
"os": i.GetOS(),
8081
}
8182

8283
e.Metadata, _ = json.Marshal(metadata)

0 commit comments

Comments
 (0)