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

feat(sg): sqlite-backed local store for sg analytics#63578

Merged
burmudar merged 12 commits into
mainfrom
nsc/sg-analytics-localstore
Jul 9, 2024
Merged

feat(sg): sqlite-backed local store for sg analytics#63578
burmudar merged 12 commits into
mainfrom
nsc/sg-analytics-localstore

Conversation

@Strum355

@Strum355 Strum355 commented Jul 1, 2024

Copy link
Copy Markdown
Contributor

Removes existing sg analytics command and replaces it with a one-per-invocation sqlite backed approach. This is a local storage for invocation events before theyre pushed to bigquery

Test plan

sqlite> select * from analytics;
0190792e-af38-751a-b93e-8481290a18b6|1|{"args":[],"command":"sg help","flags":{"help":null,"sg":null},"nargs":0,"end_time":"2024-07-03T15:20:21.069837706Z","success":true}
0190792f-4e2b-7c35-98d6-ad73cab82391|1|{"args":["dotcom"],"command":"sg live","flags":{"live":null,"sg":null},"nargs":1,"end_time":"2024-07-03T15:21:04.563232429Z","success":true}

Changelog

@cla-bot cla-bot Bot added the cla-signed label Jul 1, 2024
@Strum355 Strum355 marked this pull request as ready for review July 3, 2024 15:04
@Strum355 Strum355 force-pushed the nsc/sg-analytics-localstore branch from dcff2bd to f5bda09 Compare July 3, 2024 15:04
@Strum355 Strum355 requested a review from a team July 3, 2024 15:22
@Strum355 Strum355 changed the title wip: sqlite-backed local store for sg analytics feat(sg): sqlite-backed local store for sg analytics Jul 3, 2024
@Strum355 Strum355 force-pushed the nsc/sg-analytics-localstore branch 2 times, most recently from bfbb9f9 to 94bf52b Compare July 3, 2024 16:46

@jhchabran jhchabran left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! And thank you for cleaning stuff along the way 🙏 Have you ran (sanity check) two sg simultaneously, just to be sure?

Minor comments, nothing blocking the merge (though actually, I'd much prefer we had a test for the whoami.json)

Comment thread dev/sg/internal/analytics/analytics.go
Comment thread dev/sg/internal/analytics/analytics.go Outdated
Comment thread dev/sg/internal/analytics/analytics.go

func AddMeta(ctx context.Context, meta map[string]any) {
invc, ok := ctx.Value(invocationKey).(invocation)
if !ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for later: I totally agree that we don't want to bork the behaviour if somehow the context is missing that. But on the other hand, this shouldn't happen. In an ideal world, if we had tests for each individual command and pretend to run them, it would fail in tests, but not when running in production mode.

Or it could fail in dev mode only for example.

Let's not derail from the original intent though, just thinking out loud here 🙏

Comment thread dev/sg/internal/analytics/analytics.go Outdated
Comment thread dev/sg/internal/analytics/analytics.go
Comment thread dev/sg/internal/analytics/analytics.go
Comment thread dev/sg/internal/analytics/analytics.go Outdated
@Strum355

Strum355 commented Jul 4, 2024

Copy link
Copy Markdown
Contributor Author

Nice! And thank you for cleaning stuff along the way 🙏 Have you ran (sanity check) two sg simultaneously, just to be sure?

Minor comments, nothing blocking the merge (though actually, I'd much prefer we had a test for the whoami.json)

added a 1-retry mechanism to all queries, this should be exceedingly rare though, so once retry should cover it

@burmudar burmudar force-pushed the nsc/sg-analytics-localstore branch from ba5cfe4 to ef35f8d Compare July 9, 2024 08:35
Strum355 and others added 12 commits July 9, 2024 12:18
Reads events from the local analytics db and writes them to events table
in the bigquery `sg_analytics` dataset
```
{
  "uuid": "01908369-9f55-7ed0-b8ff-79a804e4ead5",
  "user_id": "anonymous",
  "inserted_at": "2024-07-05 15:03:25.108429 UTC",
  "recorded_at": "2024-07-05 15:01:00.578734 UTC",
  "command": "sg live",
  "version": "unknown",
  "flags_and_args": "{\"args\":[\"dotcom\"],\"flags\":{\"live\":null,\"sg\":[\"disable-analytics\"]}}",
  "duration": "0-0 0 0:0:4",
  "error": "",
  "data": null,
  "metadata": "{\"cancelled\":false,\"failed\":false,\"panicked\":false,\"success\":true}"
}, {
  "uuid": "0190836b-e1e3-7b2b-8002-e5eba10f2a27",
  "user_id": "anonymous",
  "inserted_at": "2024-07-05 15:04:22.344947 UTC",
  "recorded_at": "2024-07-05 15:03:33.360211 UTC",
  "command": "sg bazel configure",
  "version": "unknown",
  "flags_and_args": "{\"args\":[],\"flags\":{\"bazel\":null,\"configure\":null,\"sg\":[\"disable-analytics\"]}}",
  "duration": "0-0 0 0:0:9",
  "error": "signal: killed",
  "data": null,
  "metadata": "{\"cancelled\":true,\"failed\":true,\"panicked\":false,\"success\":false}"
}]
```
The following event attributes are puslished:
- command
- version
- flags_ang_args
- error
- recorded_at
- metadata: which contains cancelled, failed, panicked and sucess
(should this possibly be stored under status instead?)

The metadata field can and _should_ be expanded.

* Need to expand what is added to metadata
* Need to use and expand what is put into data

Tested locally and
https://console.cloud.google.com/bigquery?referrer=search&project=sourcegraph-local-dev&ws=!1m10!1m4!4m3!1ssourcegraph-local-dev!2ssg_analytics!3sevents!1m4!1m3!1ssourcegraph-local-dev!2sbquxjob_761f4a78_1908370012d!3sus-central1

<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
@burmudar burmudar force-pushed the nsc/sg-analytics-localstore branch from ef35f8d to 93a55c9 Compare July 9, 2024 10:39
@burmudar burmudar merged commit e669330 into main Jul 9, 2024
@burmudar burmudar deleted the nsc/sg-analytics-localstore branch July 9, 2024 10:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants