Skip to content

Commit 0ffaa66

Browse files
fix: send workspace ids as envelope properties, not measurements
The envelope builder routed every number to measurements, so the new numeric workspace ids landed in customMeasurements; *_id fields now go to properties as strings, where the dashboards join on them. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent d8bc5c7 commit 0ffaa66

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎packages/opencode/src/altimate/telemetry/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,9 @@ export namespace Telemetry {
20112011

20122012
for (const [k, v] of Object.entries(fields)) {
20132013
if (k === "session_id" || k === "project_id" || k === "_retried" || k === "_operation_id") continue
2014-
if (typeof v === "number") {
2014+
// altimate_change — an id is a dimension, not a quantity: numeric *_id fields (workspace ids)
2015+
// go to properties as strings so the dashboards join on customDimensions like every other key.
2016+
if (typeof v === "number" && !k.endsWith("_id")) {
20152017
measurements[k] = v
20162018
} else if (v !== undefined && v !== null) {
20172019
properties[k] = typeof v === "object" ? JSON.stringify(v) : String(v)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
2+
import { mkdtempSync, rmSync } from "fs"
3+
import os from "os"
4+
import path from "path"
5+
import { Telemetry } from "../../../src/altimate/telemetry"
6+
7+
describe("numeric ids are envelope properties, not measurements", () => {
8+
let home: string
9+
let posts: string[]
10+
let fetchSpy: ReturnType<typeof spyOn>
11+
let homedirSpy: ReturnType<typeof spyOn>
12+
let savedCs: string | undefined
13+
14+
beforeEach(() => {
15+
home = mkdtempSync(path.join(os.tmpdir(), "telemetry-ids-"))
16+
savedCs = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
17+
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING =
18+
"InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/"
19+
homedirSpy = spyOn(os, "homedir").mockImplementation(() => home)
20+
posts = []
21+
fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (input: any, init?: RequestInit) => {
22+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url
23+
if (url.includes("/v2/track")) posts.push(String(init?.body ?? ""))
24+
return new Response("", { status: 200 })
25+
}) as any)
26+
})
27+
28+
afterEach(async () => {
29+
await Telemetry.shutdown()
30+
fetchSpy.mockRestore()
31+
homedirSpy.mockRestore()
32+
if (savedCs === undefined) delete process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
33+
else process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = savedCs
34+
rmSync(home, { recursive: true, force: true })
35+
})
36+
37+
test("workspace ids ride in properties as strings; quantities stay measurements", async () => {
38+
await Telemetry.shutdown()
39+
await Telemetry.init()
40+
Telemetry.track({
41+
type: "workspace_switched",
42+
timestamp: Date.now(),
43+
session_id: "s",
44+
to_workspace_id: 9,
45+
from_workspace_id: 4,
46+
how: "rebind_remote",
47+
})
48+
Telemetry.track({
49+
type: "workspace_api_error",
50+
timestamp: Date.now(),
51+
session_id: "s",
52+
endpoint: "POST /datamate-project-bindings/bind",
53+
status_class: "4xx",
54+
status: 404,
55+
workspace_id: 42,
56+
})
57+
await Telemetry.flush()
58+
const envelopes = posts.flatMap((b) => JSON.parse(b) as any[])
59+
const switched = envelopes.find((e) => e.data.baseData.name === "workspace_switched").data.baseData
60+
expect(switched.properties.to_workspace_id).toBe("9")
61+
expect(switched.properties.from_workspace_id).toBe("4")
62+
expect(switched.properties.client).toBe("altimate-code")
63+
expect("to_workspace_id" in switched.measurements).toBe(false)
64+
const apiError = envelopes.find((e) => e.data.baseData.name === "workspace_api_error").data.baseData
65+
expect(apiError.properties.workspace_id).toBe("42")
66+
expect(apiError.measurements.status).toBe(404) // a quantity, still a measurement
67+
expect("workspace_id" in apiError.measurements).toBe(false)
68+
})
69+
})

0 commit comments

Comments
 (0)