@decocms/blocks 8.18.0 → 8.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/v8/client.js +1 -11
- package/dist/v8/client.js.map +1 -1
- package/dist/v8/experiments.d.ts +20 -50
- package/dist/v8/experiments.d.ts.map +1 -1
- package/dist/v8/experiments.js +35 -189
- package/dist/v8/experiments.js.map +1 -1
- package/dist/v8/fetch.d.ts.map +1 -1
- package/dist/v8/fetch.js +27 -2
- package/dist/v8/fetch.js.map +1 -1
- package/dist/v8/telemetry.d.ts +2 -0
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +21 -6
- package/dist/v8/telemetry.js.map +1 -1
- package/package.json +1 -1
- package/skills/deco-go-live/SKILL.md +1 -1
- package/skills/deco-go-live/references/qa-checklist-template.md +1 -1
- package/skills/deco-go-live/scripts/preflight.sh +2 -2
- package/skills/deco-nextjs-site/SKILL.md +1 -1
- package/skills/deco-nextjs-site/references/proxy-and-drafts.md +1 -1
- package/skills/deco-storefront-caching/references/perf-guardrails-e2e.md +1 -1
- package/skills/deco-storefront-caching/references/verification.md +1 -1
- package/skills/deco-storefront-caching/references/worker-page-cache.md +1 -1
- package/skills/deco-tanstack-site/references/worker-entry.md +1 -1
- package/skills/deco-v7-to-v8-migration/references/nextjs.md +1 -1
- package/skills/deco-v7-to-v8-migration/scripts/experiments.ts +1 -1
- package/skills/deco-v7-to-v8-migration/scripts/matchers.ts +5 -6
- package/skills/deco-variants/SKILL.md +3 -3
- package/skills/deco-variants/references/caching-variants.md +2 -5
- package/skills/deco-variants/references/experiments-and-campaigns.md +3 -3
- package/skills/deco-variants/references/multivariate.md +1 -1
- package/skills/deco-variants/references/v7-matchers.md +5 -17
- package/src/v8/client.ts +1 -12
- package/src/v8/experiments.ts +39 -209
- package/src/v8/fetch.ts +27 -2
- package/src/v8/telemetry.ts +25 -5
package/src/v8/experiments.ts
CHANGED
|
@@ -14,11 +14,10 @@
|
|
|
14
14
|
* storage in an app) and hands it to `cms.client({ visitor })`; the built-in
|
|
15
15
|
* `split-traffic` calls `assign` with it.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* it has no id; v7's stored assignments are read and honoured, never rewritten.
|
|
17
|
+
* A visitor is only its id, stored as is in the `deco_vid` cookie and written
|
|
18
|
+
* once, on a first visit. v7's `deco_segment` is neither read nor written:
|
|
19
|
+
* its stored decisions aren't honoured, and a visitor who has only it gets a
|
|
20
|
+
* new id.
|
|
22
21
|
*/
|
|
23
22
|
|
|
24
23
|
import { newId, roll } from "./bucketing.ts";
|
|
@@ -29,26 +28,10 @@ export type Experiments = Record<string, number>;
|
|
|
29
28
|
/** What a visitor got: each test's name and whether they are in it. */
|
|
30
29
|
export type Assignments = Record<string, boolean>;
|
|
31
30
|
|
|
32
|
-
/**
|
|
33
|
-
* Who is visiting: an id every assignment is computed from, plus the
|
|
34
|
-
* assignments v7 stored (`active`, `inactiveDrawn`, `pct`, `exp`), which are honoured and
|
|
35
|
-
* carried, never rewritten.
|
|
36
|
-
*/
|
|
31
|
+
/** Who is visiting: an id every assignment is computed from, and nothing else. */
|
|
37
32
|
export interface Visitor {
|
|
38
|
-
/**
|
|
39
|
-
* 16 random bytes as 22 base64url characters, drawn once per visitor. `""`
|
|
40
|
-
* for a visitor with no id of its own (one built from a cache key's
|
|
41
|
-
* assignments), which is in only the tests `active` names.
|
|
42
|
-
*/
|
|
33
|
+
/** 16 random bytes as 22 base64url characters, drawn once per visitor. */
|
|
43
34
|
id: string;
|
|
44
|
-
/** Tests v7 put this visitor in. */
|
|
45
|
-
active: string[];
|
|
46
|
-
/** Tests v7 left this visitor out of. */
|
|
47
|
-
inactiveDrawn: string[];
|
|
48
|
-
/** The percentage each of v7's decisions was made at. */
|
|
49
|
-
pct: Record<string, number>;
|
|
50
|
-
/** v7's N-way decisions. Never read; carried as they are. */
|
|
51
|
-
exp?: Record<string, string>;
|
|
52
35
|
}
|
|
53
36
|
|
|
54
37
|
/** What `readVisitor` answers. */
|
|
@@ -56,9 +39,8 @@ export interface VisitorFromRequest {
|
|
|
56
39
|
/** The request's visitor, or a new one when it had none. */
|
|
57
40
|
visitor: Visitor;
|
|
58
41
|
/**
|
|
59
|
-
* The `Set-Cookie` line to send when the visitor had no id (a first
|
|
60
|
-
*
|
|
61
|
-
* preview.
|
|
42
|
+
* The `Set-Cookie` line to send when the visitor had no id (a first
|
|
43
|
+
* visit); `null` otherwise. Never send it from a preview.
|
|
62
44
|
*/
|
|
63
45
|
cookie: string | null;
|
|
64
46
|
}
|
|
@@ -69,161 +51,47 @@ interface RequestWithHeaders {
|
|
|
69
51
|
headers: { get(name: string): string | null };
|
|
70
52
|
}
|
|
71
53
|
|
|
72
|
-
/** The visitor's
|
|
73
|
-
const VISITOR_COOKIE = "
|
|
54
|
+
/** The visitor's cookie: the id, as is. */
|
|
55
|
+
const VISITOR_COOKIE = "deco_vid";
|
|
74
56
|
|
|
75
57
|
/** One year: the id is the visitor, and it should outlive any single test. */
|
|
76
58
|
const VISITOR_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
|
|
77
59
|
|
|
78
|
-
/**
|
|
79
|
-
* A cookie over this stays out of every request. An id plus 50 of v7's
|
|
80
|
-
* decisions fits well under it; past it, `serializeVisitor` drops entries,
|
|
81
|
-
* the ones the id reproduces first.
|
|
82
|
-
*/
|
|
83
|
-
const COOKIE_BUDGET = 2048;
|
|
84
|
-
|
|
85
60
|
const ID_RE = /^[A-Za-z0-9_-]{22}$/;
|
|
86
61
|
|
|
87
|
-
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
* A record with no prototype, so a name such as `__proto__` is an ordinary
|
|
92
|
-
* key: decided, written and read back like any other.
|
|
93
|
-
*/
|
|
94
|
-
function record<T>(): Record<string, T> {
|
|
95
|
-
return Object.create(null) as Record<string, T>;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function emptyStored(): Stored {
|
|
99
|
-
return { active: [], inactiveDrawn: [], pct: record() };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function stringList(value: unknown): string[] {
|
|
103
|
-
if (!Array.isArray(value)) return [];
|
|
104
|
-
return value.filter((entry): entry is string => typeof entry === "string");
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function pctRecord(value: unknown): Record<string, number> {
|
|
108
|
-
const out = record<number>();
|
|
109
|
-
if (typeof value !== "object" || value === null) return out;
|
|
110
|
-
for (const [name, pct] of Object.entries(value as Record<string, unknown>)) {
|
|
111
|
-
if (typeof pct === "number" && Number.isFinite(pct)) out[name] = pct;
|
|
112
|
-
}
|
|
113
|
-
return out;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function expRecord(value: unknown): Record<string, string> | undefined {
|
|
117
|
-
if (typeof value !== "object" || value === null) return undefined;
|
|
118
|
-
const out = record<string>();
|
|
119
|
-
for (const [name, variant] of Object.entries(value as Record<string, unknown>)) {
|
|
120
|
-
if (typeof variant === "string") out[name] = variant;
|
|
121
|
-
}
|
|
122
|
-
return Object.keys(out).length > 0 ? out : undefined;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* A stored value, with or without an id; `null` when it doesn't read. Never
|
|
127
|
-
* throws. The value may arrive percent-encoded (some cookie APIs encode what
|
|
128
|
-
* they write; base64 never holds a `%`, so decoding first is always safe).
|
|
129
|
-
* Entries of the wrong type are dropped, and an id that is not 22 base64url
|
|
130
|
-
* characters is treated as absent.
|
|
131
|
-
*/
|
|
132
|
-
function parseStored(text: string | null | undefined): Stored | null {
|
|
133
|
-
if (typeof text !== "string" || text === "") return null;
|
|
62
|
+
/** The id in a stored value, or `undefined`. Tolerates surrounding spaces and percent-encoding. */
|
|
63
|
+
function idOf(text: string | null | undefined): string | undefined {
|
|
64
|
+
if (typeof text !== "string") return undefined;
|
|
65
|
+
let value = text.trim();
|
|
134
66
|
try {
|
|
135
|
-
|
|
136
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
137
|
-
const body = parsed as Record<string, unknown>;
|
|
138
|
-
const stored: Stored = {
|
|
139
|
-
active: stringList(body.active),
|
|
140
|
-
inactiveDrawn: stringList(body.inactiveDrawn),
|
|
141
|
-
pct: pctRecord(body.pct),
|
|
142
|
-
};
|
|
143
|
-
if (typeof body.id === "string" && ID_RE.test(body.id)) stored.id = body.id;
|
|
144
|
-
const exp = expRecord(body.exp);
|
|
145
|
-
if (exp) stored.exp = exp;
|
|
146
|
-
return stored;
|
|
67
|
+
value = decodeURIComponent(value);
|
|
147
68
|
} catch {
|
|
148
|
-
return
|
|
69
|
+
return undefined;
|
|
149
70
|
}
|
|
71
|
+
return ID_RE.test(value) ? value : undefined;
|
|
150
72
|
}
|
|
151
73
|
|
|
152
|
-
/** A new visitor: a fresh id
|
|
74
|
+
/** A new visitor: a fresh id. */
|
|
153
75
|
export function newVisitor(): Visitor {
|
|
154
|
-
return { id: newId()
|
|
76
|
+
return { id: newId() };
|
|
155
77
|
}
|
|
156
78
|
|
|
157
79
|
/**
|
|
158
|
-
* A visitor out of its stored form (what `serializeVisitor` wrote
|
|
159
|
-
* of the `
|
|
160
|
-
*
|
|
161
|
-
* decisions). Never throws.
|
|
80
|
+
* A visitor out of its stored form (what `serializeVisitor` wrote: the value
|
|
81
|
+
* of the `deco_vid` cookie or of `x-deco-visitor`). `null` when the text is
|
|
82
|
+
* missing or isn't an id. Never throws.
|
|
162
83
|
*/
|
|
163
84
|
export function parseVisitor(text: string | null | undefined): Visitor | null {
|
|
164
|
-
const
|
|
165
|
-
return
|
|
85
|
+
const id = idOf(text);
|
|
86
|
+
return id === undefined ? null : { id };
|
|
166
87
|
}
|
|
167
88
|
|
|
168
|
-
/**
|
|
169
|
-
* The visitor's stored form: v7's `btoa(encodeURIComponent(JSON.stringify(...)))`,
|
|
170
|
-
* with a fixed key order and names sorted, so two equal visitors give
|
|
171
|
-
* byte-equal values. Under 2KB: past it, v7's entries are dropped, first the
|
|
172
|
-
* ones the id reproduces anyway.
|
|
173
|
-
*/
|
|
89
|
+
/** The visitor's stored form: its id. */
|
|
174
90
|
export function serializeVisitor(visitor: Visitor): string {
|
|
175
|
-
|
|
176
|
-
const inactiveDrawn = [...visitor.inactiveDrawn].sort();
|
|
177
|
-
const dropped = new Set<string>();
|
|
178
|
-
|
|
179
|
-
const encode = (): string => {
|
|
180
|
-
const stillActive = active.filter((name) => !dropped.has(name));
|
|
181
|
-
const stillInactive = inactiveDrawn.filter((name) => !dropped.has(name));
|
|
182
|
-
const pct = record<number>();
|
|
183
|
-
for (const name of Object.keys(visitor.pct).sort()) {
|
|
184
|
-
// v7 kept a `pct` beside its N-way names too, and `exp` is carried
|
|
185
|
-
// untouched, so their fingerprint is carried with it.
|
|
186
|
-
const listed =
|
|
187
|
-
stillActive.includes(name) ||
|
|
188
|
-
stillInactive.includes(name) ||
|
|
189
|
-
(visitor.exp !== undefined && Object.hasOwn(visitor.exp, name));
|
|
190
|
-
if (listed) pct[name] = visitor.pct[name] as number;
|
|
191
|
-
}
|
|
192
|
-
const body: Record<string, unknown> = {
|
|
193
|
-
active: stillActive,
|
|
194
|
-
inactiveDrawn: stillInactive,
|
|
195
|
-
pct,
|
|
196
|
-
};
|
|
197
|
-
if (visitor.id !== undefined) body.id = visitor.id;
|
|
198
|
-
if (visitor.exp !== undefined) body.exp = visitor.exp;
|
|
199
|
-
return btoa(encodeURIComponent(JSON.stringify(body)));
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
let value = encode();
|
|
203
|
-
if (value.length <= COOKIE_BUDGET) return value;
|
|
204
|
-
|
|
205
|
-
// Over budget. A dropped entry is decided by the roll from then on, which
|
|
206
|
-
// keeps the decision only when the stored one already agrees with the roll.
|
|
207
|
-
// So drop those first, and the others only if the value is still too big.
|
|
208
|
-
const reproducible = (name: string): boolean => {
|
|
209
|
-
const pct = visitor.pct[name];
|
|
210
|
-
if (typeof pct !== "number") return false;
|
|
211
|
-
return active.includes(name) === roll(name, visitor.id) < pct;
|
|
212
|
-
};
|
|
213
|
-
for (const name of [
|
|
214
|
-
...inactiveDrawn.filter(reproducible),
|
|
215
|
-
...active.filter(reproducible),
|
|
216
|
-
...inactiveDrawn.filter((name) => !reproducible(name)),
|
|
217
|
-
...active.filter((name) => !reproducible(name)),
|
|
218
|
-
]) {
|
|
219
|
-
if (value.length <= COOKIE_BUDGET) break;
|
|
220
|
-
dropped.add(name);
|
|
221
|
-
value = encode();
|
|
222
|
-
}
|
|
223
|
-
return value;
|
|
91
|
+
return visitor.id;
|
|
224
92
|
}
|
|
225
93
|
|
|
226
|
-
/** The raw `
|
|
94
|
+
/** The raw `deco_vid` value out of a `Cookie` header. */
|
|
227
95
|
function readCookie(cookies: string | null): string | undefined {
|
|
228
96
|
if (!cookies) return undefined;
|
|
229
97
|
for (const part of cookies.split(";")) {
|
|
@@ -246,11 +114,10 @@ function isPlainHttp(url: string | undefined): boolean {
|
|
|
246
114
|
}
|
|
247
115
|
|
|
248
116
|
/**
|
|
249
|
-
* The web helper: the visitor out of a request's `
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* came over plain http. Never throws.
|
|
117
|
+
* The web helper: the visitor out of a request's `deco_vid` cookie, and the
|
|
118
|
+
* `Set-Cookie` line to send back when it had none (a first visit). The cookie
|
|
119
|
+
* isn't `HttpOnly`, so a script on the page can read the id, and is `Secure`
|
|
120
|
+
* unless the request came over plain http. Never throws.
|
|
254
121
|
*/
|
|
255
122
|
export function readVisitor(request: RequestWithHeaders): VisitorFromRequest {
|
|
256
123
|
let header: string | null = null;
|
|
@@ -259,9 +126,9 @@ export function readVisitor(request: RequestWithHeaders): VisitorFromRequest {
|
|
|
259
126
|
} catch {
|
|
260
127
|
header = null;
|
|
261
128
|
}
|
|
262
|
-
const stored =
|
|
263
|
-
if (stored
|
|
264
|
-
const visitor
|
|
129
|
+
const stored = parseVisitor(readCookie(header));
|
|
130
|
+
if (stored !== null) return { visitor: stored, cookie: null };
|
|
131
|
+
const visitor = newVisitor();
|
|
265
132
|
const parts = [
|
|
266
133
|
`${VISITOR_COOKIE}=${serializeVisitor(visitor)}`,
|
|
267
134
|
"Path=/",
|
|
@@ -276,65 +143,28 @@ function roundPct(allocation: number): number {
|
|
|
276
143
|
return Math.round(Math.min(100, Math.max(0, allocation)));
|
|
277
144
|
}
|
|
278
145
|
|
|
279
|
-
/**
|
|
280
|
-
* A v7 decision after the percentage moved: raising a percentage never
|
|
281
|
-
* removes anyone v7 put in, and lowering it never adds anyone v7 left out.
|
|
282
|
-
* Otherwise the roll decides, as for any visitor.
|
|
283
|
-
*/
|
|
284
|
-
function keptFromV7(stored: boolean, from: number, to: number): boolean | undefined {
|
|
285
|
-
if (to === from) return stored;
|
|
286
|
-
if (stored && to > from) return true;
|
|
287
|
-
if (!stored && to < from) return false;
|
|
288
|
-
return undefined;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
146
|
/**
|
|
292
147
|
* This visitor's answer for every test: `true` when they are in it.
|
|
293
148
|
*
|
|
294
149
|
* A visitor is in a test when their roll for it (a fixed number in [0, 100)
|
|
295
150
|
* from the test's name and their id) is under its percentage, so 0 puts
|
|
296
151
|
* nobody in, 100 everybody, and raising a percentage only adds people. A
|
|
297
|
-
* fractional percentage is rounded.
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
* in and one out of a test that shrinks stays out. Without a visitor every
|
|
301
|
-
* test is `false`. A visitor without an id (`""`, as a page built from a
|
|
302
|
-
* cache key's assignments gives one) has no roll: it is in exactly the tests
|
|
303
|
-
* its stored `active` and `pct` keep it in. Entries that aren't finite numbers are
|
|
304
|
-
* left out. Pure; never throws.
|
|
152
|
+
* fractional percentage is rounded. Without a visitor, or with an id that
|
|
153
|
+
* isn't 22 base64url characters, every test is `false`. Entries that aren't
|
|
154
|
+
* finite numbers are left out. Pure; never throws.
|
|
305
155
|
*/
|
|
306
156
|
export function assign(
|
|
307
157
|
experiments: Experiments | null | undefined,
|
|
308
158
|
visitor: Visitor | null | undefined,
|
|
309
159
|
): Assignments {
|
|
310
|
-
const assignments =
|
|
160
|
+
const assignments = Object.create(null) as Assignments;
|
|
311
161
|
if (typeof experiments !== "object" || experiments === null) return assignments;
|
|
312
162
|
const id = visitor !== null && typeof visitor === "object" ? visitor.id : undefined;
|
|
313
163
|
const known = typeof id === "string" && ID_RE.test(id);
|
|
314
|
-
const v: Partial<Visitor> = visitor !== null && typeof visitor === "object" ? visitor : {};
|
|
315
164
|
for (const name of Object.keys(experiments).sort()) {
|
|
316
165
|
const allocation = experiments[name];
|
|
317
166
|
if (typeof allocation !== "number" || !Number.isFinite(allocation)) continue;
|
|
318
|
-
|
|
319
|
-
const stored = list(v.active).includes(name)
|
|
320
|
-
? true
|
|
321
|
-
: list(v.inactiveDrawn).includes(name)
|
|
322
|
-
? false
|
|
323
|
-
: undefined;
|
|
324
|
-
// v7 wrote `round(traffic * 100)` here, and its own reader treated a
|
|
325
|
-
// missing or negative entry as "matches anything".
|
|
326
|
-
const raw = isRecord(v.pct) && Object.hasOwn(v.pct, name) ? v.pct[name] : undefined;
|
|
327
|
-
const from = typeof raw === "number" && Number.isFinite(raw) && raw >= 0 ? roundPct(raw) : pct;
|
|
328
|
-
const kept = stored === undefined ? undefined : keptFromV7(stored, from, pct);
|
|
329
|
-
assignments[name] = kept ?? (known && roll(name, id as string) < pct);
|
|
167
|
+
assignments[name] = known && roll(name, id as string) < roundPct(allocation);
|
|
330
168
|
}
|
|
331
169
|
return assignments;
|
|
332
170
|
}
|
|
333
|
-
|
|
334
|
-
function list(value: unknown): string[] {
|
|
335
|
-
return Array.isArray(value) ? value : [];
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
339
|
-
return typeof value === "object" && value !== null;
|
|
340
|
-
}
|
package/src/v8/fetch.ts
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* (`http.client.request.duration`), with `provider`, `operation`,
|
|
7
7
|
* `status_class`, `cached` and `retries` labels, however many retries it
|
|
8
8
|
* took. Measurements go to the CMS in the same process, and nowhere when it
|
|
9
|
-
* has no telemetry destination.
|
|
10
|
-
*
|
|
9
|
+
* has no telemetry destination. Final 5xx responses emit sampled error logs;
|
|
10
|
+
* actionable 4xx responses emit sampled warnings. Recovered attempts emit no
|
|
11
|
+
* response log. Nothing about a request's body, headers or query is recorded.
|
|
11
12
|
*/
|
|
12
13
|
import { currentTelemetry, describe, newTraceId } from "./telemetry.ts";
|
|
13
14
|
|
|
@@ -33,6 +34,11 @@ type InstrumentedFetch = (
|
|
|
33
34
|
|
|
34
35
|
/** Statuses worth retrying: rate limits and gateway errors. */
|
|
35
36
|
const RETRY_STATUSES = new Set([429, 502, 503, 504]);
|
|
37
|
+
/**
|
|
38
|
+
* Final client failures worth investigating. Misses, conflicts and auth
|
|
39
|
+
* responses can be normal application results, so they stay quiet.
|
|
40
|
+
*/
|
|
41
|
+
const WARNING_STATUSES = new Set([400, 405, 408, 422, 429]);
|
|
36
42
|
/** Only requests that are safe to repeat are retried. */
|
|
37
43
|
const IDEMPOTENT = new Set(["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]);
|
|
38
44
|
|
|
@@ -108,6 +114,25 @@ export function createInstrumentedFetch(options: InstrumentedFetchOptions): Inst
|
|
|
108
114
|
provider,
|
|
109
115
|
operation,
|
|
110
116
|
});
|
|
117
|
+
} else {
|
|
118
|
+
const status = response.status;
|
|
119
|
+
const level =
|
|
120
|
+
status >= 500 && status < 600
|
|
121
|
+
? "error"
|
|
122
|
+
: WARNING_STATUSES.has(status)
|
|
123
|
+
? "warn"
|
|
124
|
+
: undefined;
|
|
125
|
+
if (level !== undefined) {
|
|
126
|
+
telemetry[level](`${provider} ${operation} failed with HTTP ${status}`, {
|
|
127
|
+
"error.code": level === "error" ? "FETCH_HTTP_ERROR" : "FETCH_HTTP_WARNING",
|
|
128
|
+
provider,
|
|
129
|
+
operation,
|
|
130
|
+
"http.request.method": method,
|
|
131
|
+
"http.response.status_code": status,
|
|
132
|
+
retries,
|
|
133
|
+
cached: labels.cached,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
111
136
|
}
|
|
112
137
|
if (telemetry.sampleTrace()) {
|
|
113
138
|
telemetry.span({
|
package/src/v8/telemetry.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* settings (`cms.settings()`, the release's `CMS` block); a rate it doesn't
|
|
11
11
|
* save is code's `telemetry.defaults`, else the built-in 0.05 and 0. The CMS reads it outside any request, when the
|
|
12
12
|
* release changes and then at most once a minute.
|
|
13
|
-
* - Metrics are aggregated in memory (delta histograms);
|
|
13
|
+
* - Metrics are aggregated in memory (delta histograms); failure logs and traces
|
|
14
14
|
* are sampled. Batches leave in the background (after the response on
|
|
15
15
|
* Workers, on an unref'd timer elsewhere); a send that fails is retried
|
|
16
16
|
* once on 429/502/503/504 and otherwise dropped.
|
|
@@ -99,6 +99,7 @@ interface Histogram {
|
|
|
99
99
|
|
|
100
100
|
interface LogRecord {
|
|
101
101
|
time: number;
|
|
102
|
+
severity: "ERROR" | "WARN";
|
|
102
103
|
message: string;
|
|
103
104
|
attributes: Attributes;
|
|
104
105
|
}
|
|
@@ -197,9 +198,28 @@ export class TelemetryPipeline {
|
|
|
197
198
|
|
|
198
199
|
/** An error log, kept at the error sample rate. */
|
|
199
200
|
error(message: string, attributes: Attributes): void {
|
|
201
|
+
this.#recordLog("ERROR", message, attributes);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** A warning log, kept at the same sample rate as errors. */
|
|
205
|
+
warn(message: string, attributes: Attributes): void {
|
|
206
|
+
this.#recordLog("WARN", message, attributes);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
#recordLog(severity: LogRecord["severity"], message: string, attributes: Attributes): void {
|
|
200
210
|
if (!this.#settings.enabled || Math.random() >= this.#settings.errorSampleRate) return;
|
|
201
|
-
if (this.#logs.length >= MAX_LOGS)
|
|
202
|
-
|
|
211
|
+
if (this.#logs.length >= MAX_LOGS) {
|
|
212
|
+
const warningIndex =
|
|
213
|
+
severity === "ERROR" ? this.#logs.findIndex((log) => log.severity === "WARN") : -1;
|
|
214
|
+
if (warningIndex === -1) {
|
|
215
|
+
// An early host hook may have left this full batch waiting for the
|
|
216
|
+
// next request. Dropping a new log must not strand the retained ones.
|
|
217
|
+
this.#schedule();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
this.#logs.splice(warningIndex, 1);
|
|
221
|
+
}
|
|
222
|
+
this.#logs.push({ time: Date.now(), severity, message, attributes });
|
|
203
223
|
this.#schedule();
|
|
204
224
|
}
|
|
205
225
|
|
|
@@ -319,8 +339,8 @@ export class TelemetryPipeline {
|
|
|
319
339
|
scope,
|
|
320
340
|
logRecords: logs.map((log) => ({
|
|
321
341
|
timeUnixNano: nanos(log.time),
|
|
322
|
-
severityNumber: 17,
|
|
323
|
-
severityText:
|
|
342
|
+
severityNumber: log.severity === "ERROR" ? 17 : 13,
|
|
343
|
+
severityText: log.severity,
|
|
324
344
|
body: { stringValue: scrubText(log.message) },
|
|
325
345
|
attributes: encode(log.attributes),
|
|
326
346
|
})),
|