telemetry: add best-effort usage telemetry recorder - #1378
BolajiOlajide wants to merge 4 commits into
Conversation
Add the internal/telemetry package: a Recorder that records src-cli usage events to the authenticated Sourcegraph instance via the Telemetry V2 recordEvents GraphQL mutation. Recording is strictly best-effort — network, GraphQL, timeout, and old-instance (<5.2) failures are all swallowed and never affect the command the user ran. Command identity is encoded in feature/action with numeric-only metadata (no user content, no privateMetadata), and names are validated against Sourcegraph's server-side naming rules.
Telemetry must never contaminate command output, but GraphQL request execution can print OAuth reauthorization guidance on HTTP 401 responses. Send telemetry through the authenticated HTTP client directly so failures remain best-effort without interactive output, while preserving authentication and cancellation. Add regression coverage for OAuth failures, request shape, GraphQL errors, and timeout cancellation. ## Test Plan - go test ./... - go test ./internal/telemetry -run 'TestRecord_(AppliesTimeout|TimeoutCancelsHTTPRequest)' -count=20
The telemetry API already validates feature and action names authoritatively, so maintaining a second validator in src-cli adds drift risk without improving correctness. Remove the duplicate validation and allow rejected events to follow the recorder's existing best-effort failure path. ## Test Plan - go test ./...
The telemetry package has no production callers yet, so exporting event models, source configuration, options, and test-only controls commits src-cli to an API before its integration requirements are known. Keep only the recorder construction and recording operations public, fix the client identity internally, and pass event values directly. ## Test Plan - go test ./...
| // record does the work behind Record and returns any error, so it can be tested | ||
| // directly. Callers outside tests should use Record. | ||
| func (r *Recorder) record(ctx context.Context, feature, action string, metadata map[string]float64) error { | ||
| if r.client == nil { |
There was a problem hiding this comment.
is this a real case? why would client being nil be valid?
| // GraphQL, timeout, and old-instance failures are silently dropped. It applies | ||
| // its own timeout, so the caller's context need not carry a deadline. | ||
| func (r *Recorder) Record(ctx context.Context, feature, action string, metadata map[string]float64) { | ||
| _ = r.record(ctx, feature, action, metadata) |
There was a problem hiding this comment.
we should still write errors somewhere, maybe a debug log or osmething
| // eventParametersVersion is the schema version of the metadata we attach to | ||
| // each event. Bump it when the shape of the metadata changes. | ||
| eventParametersVersion = 1 |
There was a problem hiding this comment.
this assumes individual src CLI telemetry callsites will never provide their own parameters, is that the case?
I think the telemetry SDK concern should be separated from "telemetry middleware" etc
|
|
||
| const ( | ||
| // clientName identifies src-cli as the source of telemetry events. | ||
| clientName = "SRC_CLI" |
There was a problem hiding this comment.
does this match existing conventions for format of client name?
| // defaultTimeout bounds how long a single Record call may spend recording. | ||
| // It is deliberately short: telemetry is sent synchronously right before the | ||
| // process exits, so it must not add meaningful latency. | ||
| defaultTimeout = 2 * time.Second |
There was a problem hiding this comment.
can we have telemetry be recorded in the background and have process wait for completion - up to deadline - when it receives a shutdown signal? I think we do something like that in sg
Closes CPL-1005
Adds the
internal/telemetrypackage: aRecorderthat records src-cli usage events to the authenticated Sourcegraph instance via the Telemetry V2recordEventsGraphQL mutation. Recording is strictly best-effort — validation, network, GraphQL, timeout, and old-instance (<5.2) failures are all swallowed (optionally surfaced viaWithDebugfor-v) and never affect the command the user ran. Command identity is carried infeature/actionwith numeric-only, PII-freemetadata(noprivateMetadata), feature/action names are validated against Sourcegraph's server-side naming rules, and eachRecordapplies a short 2s timeout since events are sent synchronously before process exit. This is the foundational package only; wiring it intocmd/src/main.goand the opt-out/suppression logic land in follow-up changes.