返回 CodeWhale
datapoint.ts
根目录 / telemetry-ingest / src / datapoint.ts
1 /**
2 * Batch -> Workers Analytics Engine data points.
3 *
4 * One data point per event. A batch is capped at 200 events
5 * (`BATCH_MAX_EVENTS`), and Analytics Engine allows 250 data points per Worker
6 * invocation, so a conforming batch never needs a second pass and never has to
7 * drop an event to fit.
8 *
9 * The column layout is fixed and positional because Analytics Engine columns
10 * are `blob1..blob20` / `double1..double20` — the names live only in the SQL
11 * you write. Renumbering a column silently rewrites every historical query, so
12 * the order below is append-only: to add a field, take the next free slot.
13 *
14 * The layout is chosen so the two questions the owner actually has are one
15 * query each. Both queries are written out in `README.md`:
16 *
17 * (a) installs and sessions -> index1 (install_id) + blob1 (event)
18 * (b) error classes and panic sites -> double11..16 + blob16, one GROUP BY
19 *
20 * Every value below comes out of the validated batch body and nothing else.
21 * This module cannot see anything about the connection — that is enforced by
22 * `test/no-ip.test.ts`, which fails if the type it takes is ever widened. See
23 * the red-line comment at the top of `index.ts`.
24 */
25
26 import type { Batch, Event } from "./schema";
27 import {
28 CONTEXT_FIELDS,
29 COUNTER_FIELDS,
30 ERROR_FIELDS,
31 MODEL_COUNTER_FIELDS,
32 MODEL_COUNTER_FIELDS_V6_EXTRA,
33 TURN_WALL_FIELDS,
34 } from "./schema";
35
36 /** The subset of `AnalyticsEngineDataset` this Worker uses. */
37 export interface DataPointSink {
38 writeDataPoint(point: {
39 indexes?: string[];
40 blobs?: string[];
41 doubles?: number[];
42 }): void;
43 }
44
45 /** One row, in Analytics Engine's positional form. */
46 export interface DataPoint {
47 indexes: string[];
48 blobs: string[];
49 doubles: number[];
50 }
51
52 /**
53 * Blob column names, in `blob1..blobN` order. Exported so the README's SQL and
54 * the tests read from one list rather than two.
55 */
56 export const BLOB_COLUMNS = [
57 "event", // blob1
58 "surface", // blob2
59 "os", // blob3
60 "arch", // blob4
61 "libc", // blob5
62 "app_version", // blob6
63 "git_sha", // blob7 '' when null (a local build)
64 "tty", // blob8 'true' | 'false'
65 "install_kind", // blob9
66 "previous_version", // blob10
67 "session_source", // blob11
68 "duration_bucket", // blob12 session_end; install_age on session_start (schema 6)
69 "exit_class", // blob13
70 "cold_start_bucket", // blob14
71 "providers", // blob15 comma-joined, already sorted and deduplicated
72 "panic_site", // blob16
73 "sent_at", // blob17 the batch timestamp; events carry none
74 "aggregate_counters", // blob18 closed product/operations JSON count object
75 "schema_version", // blob19
76 "privacy_version", // blob20: v3 to v6 notice, v2 consent, empty on v1; blob19 disambiguates
77 ] as const;
78
79 /**
80 * Double column names, in `double1..double20` order: the ten counters, the six
81 * error classes, then the four turn-wall buckets. Exactly 20 — Analytics
82 * Engine's ceiling — which is why `tty` is a blob.
83 */
84 export const DOUBLE_COLUMNS = [
85 ...COUNTER_FIELDS,
86 ...ERROR_FIELDS,
87 ...TURN_WALL_FIELDS,
88 ] as const;
89
90 const EMPTY_DOUBLES: number[] = DOUBLE_COLUMNS.map(() => 0);
91
92 /** Build the rows for one validated batch. */
93 export function toDataPoints(batch: Batch): DataPoint[] {
94 return batch.events.map((event) => toDataPoint(batch, event));
95 }
96
97 function toDataPoint(batch: Batch, event: Event): DataPoint {
98 const blobs = new Array<string>(BLOB_COLUMNS.length).fill("");
99 blobs[0] = event.event;
100 blobs[1] = batch.surface;
101 blobs[2] = batch.os;
102 blobs[3] = batch.arch;
103 blobs[4] = batch.libc;
104 blobs[5] = batch.app_version;
105 blobs[6] = batch.git_sha ?? "";
106 blobs[7] = batch.tty ? "true" : "false";
107 blobs[16] = batch.sent_at;
108 blobs[18] = String(batch.schema_version);
109 const privacyVersion = batch.schema_version === 2 ? batch.consent_version : batch.notice_version;
110 blobs[19] = privacyVersion === undefined ? "" : String(privacyVersion);
111
112 let doubles = EMPTY_DOUBLES;
113
114 switch (event.event) {
115 case "install_or_upgrade":
116 blobs[8] = event.kind;
117 blobs[9] = event.previous_version ?? "";
118 break;
119 case "session_start":
120 blobs[10] = event.source;
121 blobs[11] = event.install_age ?? "";
122 break;
123 case "session_end":
124 blobs[11] = event.duration_bucket;
125 blobs[12] = event.exit_class;
126 blobs[13] = event.cold_start_bucket ?? "";
127 blobs[14] = event.providers.join(",");
128 doubles = [
129 ...COUNTER_FIELDS.map((field) => event.counters[field]),
130 ...ERROR_FIELDS.map((field) => event.errors[field]),
131 ...TURN_WALL_FIELDS.map((field) => event.turn_wall[field]),
132 ];
133 break;
134 case "operations_summary": {
135 const { event: _event, ...counts } = event;
136 blobs[17] = JSON.stringify(counts);
137 break;
138 }
139 case "funnel_usage":
140 case "product_usage":
141 blobs[17] = JSON.stringify(event.counters);
142 break;
143 case "panic":
144 blobs[15] = event.site;
145 break;
146 case "model_usage":
147 blobs[14] = event.provider;
148 blobs[15] = event.model;
149 doubles = [
150 ...MODEL_COUNTER_FIELDS.map((field) => event.counters[field]),
151 ...TURN_WALL_FIELDS.map((field) => event.turn_wall[field]),
152 ...MODEL_COUNTER_FIELDS_V6_EXTRA.map((field) => event.counters[field] ?? 0),
153 ...CONTEXT_FIELDS.map((field) => event.context?.[field] ?? 0),
154 ];
155 doubles = [...doubles, ...EMPTY_DOUBLES.slice(doubles.length)];
156 break;
157 case "tool_usage":
158 blobs[15] = event.tool;
159 doubles = [event.calls, event.errors, ...EMPTY_DOUBLES.slice(2)];
160 break;
161 }
162
163 return {
164 // The one index. `install_id` is a random v4 UUID that the client rotates
165 // every 90 days, and it is the only identifier in the schema — which is
166 // also why `docs/TELEMETRY.md` says no count derived from it is a user
167 // count. It is the index because both questions group by it or count it.
168 indexes: [batch.install_id],
169 blobs,
170 doubles,
171 };
172 }
173
174 /** Write one batch. `writeDataPoint` is non-blocking and is never awaited. */
175 export function writeBatch(sink: DataPointSink, batch: Batch): number {
176 const points = toDataPoints(batch);
177 for (const point of points) {
178 sink.writeDataPoint(point);
179 }
180 return points.length;
181 }
182
182 lines TYPESCRIPT