@decocms/blocks 7.71.6 → 8.0.0-next.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/bin/deco.js +13 -0
- package/package.json +32 -3
- package/src/cms/schema.ts +1 -1
- package/src/hooks/LiveControls.tsx +2 -0
- package/src/hooks/useLoadMore.ts +2 -0
- package/src/index.ts +37 -0
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +152 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/deferredTrigger.test.ts +0 -32
- package/src/cms/draftSource.test.ts +0 -519
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loader.test.ts +0 -557
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/renderJson.test.ts +0 -167
- package/src/cms/resolve.test.ts +0 -1492
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaders.test.ts +0 -625
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/flags.test.ts +0 -148
- package/src/hooks/Image.test.ts +0 -239
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/hooks/Stats.test.ts +0 -112
- package/src/hooks/useLoadMore.test.ts +0 -205
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/override.test.ts +0 -205
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/observability.test.ts +0 -335
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/preview/DraftPreviewBadge.render.test.tsx +0 -67
- package/src/preview/DraftPreviewBadge.test.ts +0 -55
- package/src/sdk/abTesting.test.ts +0 -381
- package/src/sdk/cacheHeaders.test.ts +0 -258
- package/src/sdk/cacheStorage.test.ts +0 -242
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoaderMaxBytes.test.ts +0 -23
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/cachedLoaderSizeMetric.test.ts +0 -93
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/crypto.test.ts +0 -65
- package/src/sdk/deepOmit.test.ts +0 -45
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/experiments.test.ts +0 -328
- package/src/sdk/fetchCache.test.ts +0 -164
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/otel.test.ts +0 -691
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpMeter.test.ts +0 -395
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/replaceStream.test.ts +0 -117
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/splitExactRedirects.test.ts +0 -154
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/urlUtils.test.ts +0 -34
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/tsconfig.json +0 -7
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { createOtlpHttpMeterAdapter } from "./otelHttpMeter";
|
|
3
|
-
|
|
4
|
-
interface OtlpPayload {
|
|
5
|
-
resourceMetrics: Array<{
|
|
6
|
-
resource: {
|
|
7
|
-
attributes: Array<{ key: string; value: { stringValue: string } }>;
|
|
8
|
-
};
|
|
9
|
-
scopeMetrics: Array<{
|
|
10
|
-
scope: { name: string; version: string };
|
|
11
|
-
metrics: Array<{
|
|
12
|
-
name: string;
|
|
13
|
-
sum?: {
|
|
14
|
-
aggregationTemporality: number;
|
|
15
|
-
isMonotonic: boolean;
|
|
16
|
-
dataPoints: Array<{
|
|
17
|
-
attributes: Array<{ key: string; value: Record<string, unknown> }>;
|
|
18
|
-
startTimeUnixNano: string;
|
|
19
|
-
timeUnixNano: string;
|
|
20
|
-
asDouble: number;
|
|
21
|
-
}>;
|
|
22
|
-
};
|
|
23
|
-
gauge?: {
|
|
24
|
-
dataPoints: Array<{
|
|
25
|
-
attributes: Array<{ key: string; value: Record<string, unknown> }>;
|
|
26
|
-
timeUnixNano: string;
|
|
27
|
-
asDouble: number;
|
|
28
|
-
}>;
|
|
29
|
-
};
|
|
30
|
-
histogram?: {
|
|
31
|
-
aggregationTemporality: number;
|
|
32
|
-
dataPoints: Array<{
|
|
33
|
-
attributes: Array<{ key: string; value: Record<string, unknown> }>;
|
|
34
|
-
startTimeUnixNano: string;
|
|
35
|
-
timeUnixNano: string;
|
|
36
|
-
count: string;
|
|
37
|
-
sum: number;
|
|
38
|
-
min: number;
|
|
39
|
-
max: number;
|
|
40
|
-
bucketCounts: string[];
|
|
41
|
-
explicitBounds: number[];
|
|
42
|
-
}>;
|
|
43
|
-
};
|
|
44
|
-
}>;
|
|
45
|
-
}>;
|
|
46
|
-
}>;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function captureFetch() {
|
|
50
|
-
const calls: Array<{ url: string; init?: RequestInit }> = [];
|
|
51
|
-
const impl = vi.fn(async (url: string | URL | Request, init?: RequestInit) => {
|
|
52
|
-
calls.push({ url: String(url), init });
|
|
53
|
-
return new Response("{}", { status: 200 });
|
|
54
|
-
});
|
|
55
|
-
return { impl: impl as unknown as typeof fetch, calls };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function buildAdapter(
|
|
59
|
-
overrides: {
|
|
60
|
-
fetchImpl?: typeof fetch;
|
|
61
|
-
minFlushIntervalMs?: number;
|
|
62
|
-
maxBufferDatapoints?: number;
|
|
63
|
-
nowMs?: () => number;
|
|
64
|
-
onError?: (kind: "flush" | "overflow" | "kind-mismatch", err: unknown) => void;
|
|
65
|
-
histogramBounds?: number[];
|
|
66
|
-
metricMetadata?: Record<
|
|
67
|
-
string,
|
|
68
|
-
{ description?: string; unit?: string; boundaries?: readonly number[] }
|
|
69
|
-
>;
|
|
70
|
-
} = {},
|
|
71
|
-
) {
|
|
72
|
-
return createOtlpHttpMeterAdapter({
|
|
73
|
-
endpoint: "https://ingest.test/v1/metrics",
|
|
74
|
-
resourceAttributes: {
|
|
75
|
-
"service.name": "smoke-site",
|
|
76
|
-
"service.version": "abc123",
|
|
77
|
-
},
|
|
78
|
-
scopeVersion: "5.0.0-test",
|
|
79
|
-
fetchImpl: overrides.fetchImpl,
|
|
80
|
-
minFlushIntervalMs: overrides.minFlushIntervalMs ?? 0,
|
|
81
|
-
maxBufferDatapoints: overrides.maxBufferDatapoints,
|
|
82
|
-
nowMs: overrides.nowMs,
|
|
83
|
-
onError: overrides.onError,
|
|
84
|
-
histogramBounds: overrides.histogramBounds,
|
|
85
|
-
metricMetadata: overrides.metricMetadata,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
describe("createOtlpHttpMeterAdapter — buffer + flush", () => {
|
|
90
|
-
beforeEach(() => {
|
|
91
|
-
vi.useFakeTimers();
|
|
92
|
-
vi.setSystemTime(new Date("2026-05-18T16:00:00.000Z"));
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
afterEach(() => {
|
|
96
|
-
vi.useRealTimers();
|
|
97
|
-
vi.restoreAllMocks();
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("counterInc accumulates per attr-key and exports cumulative sum", async () => {
|
|
101
|
-
const { impl, calls } = captureFetch();
|
|
102
|
-
const meter = buildAdapter({ fetchImpl: impl });
|
|
103
|
-
|
|
104
|
-
meter.counterInc("deco.http.requests", 1, { method: "GET", status: "2xx" });
|
|
105
|
-
meter.counterInc("deco.http.requests", 1, { method: "GET", status: "2xx" });
|
|
106
|
-
meter.counterInc("deco.http.requests", 1, { method: "POST", status: "5xx" });
|
|
107
|
-
|
|
108
|
-
await meter.flush();
|
|
109
|
-
|
|
110
|
-
expect(calls).toHaveLength(1);
|
|
111
|
-
expect(calls[0].url).toBe("https://ingest.test/v1/metrics");
|
|
112
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
113
|
-
const m = payload.resourceMetrics[0].scopeMetrics[0].metrics[0];
|
|
114
|
-
expect(m.name).toBe("deco.http.requests");
|
|
115
|
-
expect(m.sum?.isMonotonic).toBe(true);
|
|
116
|
-
expect(m.sum?.aggregationTemporality).toBe(2);
|
|
117
|
-
expect(m.sum?.dataPoints).toHaveLength(2);
|
|
118
|
-
|
|
119
|
-
// Sort for stable assertion order.
|
|
120
|
-
const points = [...(m.sum?.dataPoints ?? [])].sort((a, b) =>
|
|
121
|
-
JSON.stringify(a.attributes).localeCompare(JSON.stringify(b.attributes)),
|
|
122
|
-
);
|
|
123
|
-
expect(points[0].asDouble).toBe(2); // GET+2xx
|
|
124
|
-
expect(points[1].asDouble).toBe(1); // POST+5xx
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("histogramRecord assigns buckets correctly and reports count/sum/min/max", async () => {
|
|
128
|
-
const { impl, calls } = captureFetch();
|
|
129
|
-
const meter = buildAdapter({
|
|
130
|
-
fetchImpl: impl,
|
|
131
|
-
histogramBounds: [5, 10, 25, 50, 75, 100, 250, 500, 1000],
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// 12 samples across the [5,10,25,50,75,100,250,500,1000] bounds, with
|
|
135
|
-
// the `value <= bound[i]` lower-bucket convention used by the exporter.
|
|
136
|
-
// Distribution:
|
|
137
|
-
// bucket 0 (≤5): []
|
|
138
|
-
// bucket 1 (5..10]: [8.4]
|
|
139
|
-
// bucket 2 (10..25]: [12,14,20]
|
|
140
|
-
// bucket 3 (25..50]: [30,35,38,48]
|
|
141
|
-
// bucket 4 (50..75]: [60,70]
|
|
142
|
-
// bucket 5 (75..100]:[80,87.2]
|
|
143
|
-
// bucket 6+: []
|
|
144
|
-
const samples = [8.4, 12, 14, 20, 30, 35, 38, 48, 60, 70, 80, 87.2];
|
|
145
|
-
expect(samples).toHaveLength(12);
|
|
146
|
-
const sum = samples.reduce((a, b) => a + b, 0);
|
|
147
|
-
for (const v of samples) {
|
|
148
|
-
meter.histogramRecord("outbound_request_duration_ms", v, { provider: "vtex" });
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
await meter.flush();
|
|
152
|
-
|
|
153
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
154
|
-
const m = payload.resourceMetrics[0].scopeMetrics[0].metrics[0];
|
|
155
|
-
expect(m.histogram?.aggregationTemporality).toBe(2);
|
|
156
|
-
expect(m.histogram?.dataPoints).toHaveLength(1);
|
|
157
|
-
const dp = m.histogram!.dataPoints[0];
|
|
158
|
-
expect(dp.count).toBe("12");
|
|
159
|
-
expect(dp.sum).toBeCloseTo(sum, 5);
|
|
160
|
-
expect(dp.min).toBe(8.4);
|
|
161
|
-
expect(dp.max).toBe(87.2);
|
|
162
|
-
expect(dp.bucketCounts).toEqual(["0", "1", "3", "4", "2", "2", "0", "0", "0", "0"]);
|
|
163
|
-
expect(dp.explicitBounds).toEqual([5, 10, 25, 50, 75, 100, 250, 500, 1000]);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it("gaugeSet keeps last write per attr-key", async () => {
|
|
167
|
-
const { impl, calls } = captureFetch();
|
|
168
|
-
const meter = buildAdapter({ fetchImpl: impl });
|
|
169
|
-
|
|
170
|
-
meter.gaugeSet("deco.metrics.buffer_size", 3);
|
|
171
|
-
meter.gaugeSet("deco.metrics.buffer_size", 7);
|
|
172
|
-
|
|
173
|
-
await meter.flush();
|
|
174
|
-
|
|
175
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
176
|
-
const m = payload.resourceMetrics[0].scopeMetrics[0].metrics[0];
|
|
177
|
-
expect(m.gauge?.dataPoints).toHaveLength(1);
|
|
178
|
-
expect(m.gauge?.dataPoints[0].asDouble).toBe(7);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it("resource attributes are stamped on every payload", async () => {
|
|
182
|
-
const { impl, calls } = captureFetch();
|
|
183
|
-
const meter = buildAdapter({ fetchImpl: impl });
|
|
184
|
-
meter.counterInc("x", 1);
|
|
185
|
-
await meter.flush();
|
|
186
|
-
|
|
187
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
188
|
-
const attrs = payload.resourceMetrics[0].resource.attributes;
|
|
189
|
-
expect(attrs).toContainEqual({
|
|
190
|
-
key: "service.name",
|
|
191
|
-
value: { stringValue: "smoke-site" },
|
|
192
|
-
});
|
|
193
|
-
expect(attrs).toContainEqual({
|
|
194
|
-
key: "service.version",
|
|
195
|
-
value: { stringValue: "abc123" },
|
|
196
|
-
});
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it("never throws when fetch rejects — surfaces via onError", async () => {
|
|
200
|
-
const onError = vi.fn();
|
|
201
|
-
const failing: typeof fetch = vi.fn(() =>
|
|
202
|
-
Promise.reject(new Error("network unreachable")),
|
|
203
|
-
) as unknown as typeof fetch;
|
|
204
|
-
const meter = buildAdapter({ fetchImpl: failing, onError });
|
|
205
|
-
|
|
206
|
-
meter.counterInc("x", 1);
|
|
207
|
-
await expect(meter.flush()).resolves.toBeUndefined();
|
|
208
|
-
expect(onError).toHaveBeenCalledWith("flush", expect.any(Error));
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it("non-200 HTTP response surfaces via onError but does not throw", async () => {
|
|
212
|
-
const onError = vi.fn();
|
|
213
|
-
const non200: typeof fetch = vi.fn(async () =>
|
|
214
|
-
new Response("oops", { status: 500 }),
|
|
215
|
-
) as unknown as typeof fetch;
|
|
216
|
-
const meter = buildAdapter({ fetchImpl: non200, onError });
|
|
217
|
-
meter.counterInc("x", 1);
|
|
218
|
-
await meter.flush();
|
|
219
|
-
expect(onError).toHaveBeenCalledWith("flush", expect.any(Error));
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
it("registering a metric name as two different kinds drops the second + onError", () => {
|
|
223
|
-
const onError = vi.fn();
|
|
224
|
-
const meter = buildAdapter({ onError });
|
|
225
|
-
|
|
226
|
-
meter.counterInc("conflict", 1);
|
|
227
|
-
meter.gaugeSet?.("conflict", 7);
|
|
228
|
-
|
|
229
|
-
expect(onError).toHaveBeenCalledWith("kind-mismatch", expect.any(Error));
|
|
230
|
-
expect(meter.pendingDatapointCount()).toBe(1);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it("cooldown gates flushes; cooldown is bypassed once buffer reaches the cap", async () => {
|
|
234
|
-
let mockNow = 1_000_000;
|
|
235
|
-
const { impl, calls } = captureFetch();
|
|
236
|
-
const meter = buildAdapter({
|
|
237
|
-
fetchImpl: impl,
|
|
238
|
-
minFlushIntervalMs: 5000,
|
|
239
|
-
maxBufferDatapoints: 3,
|
|
240
|
-
nowMs: () => mockNow,
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// 1st flush at t=0 — buffer has 1 entry, cooldown bypass via lastFlush=0
|
|
244
|
-
meter.counterInc("a", 1, { k: "1" });
|
|
245
|
-
await meter.flush();
|
|
246
|
-
expect(calls).toHaveLength(1);
|
|
247
|
-
|
|
248
|
-
// 2nd flush at t=2s — cooldown not elapsed AND buffer below cap → no-op
|
|
249
|
-
mockNow += 2000;
|
|
250
|
-
meter.counterInc("a", 1, { k: "2" });
|
|
251
|
-
await meter.flush();
|
|
252
|
-
expect(calls).toHaveLength(1);
|
|
253
|
-
|
|
254
|
-
// 3rd flush at t=2.5s — still under cooldown but buffer at cap (3) → flush
|
|
255
|
-
mockNow += 500;
|
|
256
|
-
meter.counterInc("a", 1, { k: "3" });
|
|
257
|
-
expect(meter.pendingDatapointCount()).toBe(3);
|
|
258
|
-
await meter.flush();
|
|
259
|
-
expect(calls).toHaveLength(2);
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
it("overflow drops new attribute-keys when buffer is at cap (existing keys still update)", () => {
|
|
263
|
-
const onError = vi.fn();
|
|
264
|
-
const meter = buildAdapter({ maxBufferDatapoints: 2, onError });
|
|
265
|
-
|
|
266
|
-
meter.counterInc("a", 1, { k: "1" });
|
|
267
|
-
meter.counterInc("a", 1, { k: "2" });
|
|
268
|
-
expect(meter.pendingDatapointCount()).toBe(2);
|
|
269
|
-
|
|
270
|
-
meter.counterInc("a", 1, { k: "3" });
|
|
271
|
-
expect(onError).toHaveBeenCalledWith("overflow", expect.any(Error));
|
|
272
|
-
expect(meter.pendingDatapointCount()).toBe(2);
|
|
273
|
-
|
|
274
|
-
// Existing key still updates (no new datapoint, just bumps the existing value).
|
|
275
|
-
meter.counterInc("a", 5, { k: "1" });
|
|
276
|
-
expect(meter.pendingDatapointCount()).toBe(2);
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it("concurrent flushes share a single in-flight POST", async () => {
|
|
280
|
-
let releaseFetch: ((res: Response) => void) | undefined;
|
|
281
|
-
const slow: typeof fetch = vi.fn(
|
|
282
|
-
() =>
|
|
283
|
-
new Promise<Response>((resolve) => {
|
|
284
|
-
releaseFetch = resolve;
|
|
285
|
-
}),
|
|
286
|
-
) as unknown as typeof fetch;
|
|
287
|
-
const meter = buildAdapter({ fetchImpl: slow });
|
|
288
|
-
|
|
289
|
-
meter.counterInc("x", 1);
|
|
290
|
-
const a = meter.flush();
|
|
291
|
-
const b = meter.flush();
|
|
292
|
-
expect(slow).toHaveBeenCalledTimes(1);
|
|
293
|
-
releaseFetch?.(new Response("{}", { status: 200 }));
|
|
294
|
-
await Promise.all([a, b]);
|
|
295
|
-
expect(slow).toHaveBeenCalledTimes(1);
|
|
296
|
-
});
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
// ---------------------------------------------------------------------------
|
|
300
|
-
// Per-metric bucket boundaries
|
|
301
|
-
// ---------------------------------------------------------------------------
|
|
302
|
-
|
|
303
|
-
interface HistoMetric {
|
|
304
|
-
name: string;
|
|
305
|
-
unit?: string;
|
|
306
|
-
histogram?: {
|
|
307
|
-
dataPoints: Array<{
|
|
308
|
-
attributes: Array<{ key: string; value: Record<string, unknown> }>;
|
|
309
|
-
count: string;
|
|
310
|
-
sum: number;
|
|
311
|
-
bucketCounts: string[];
|
|
312
|
-
explicitBounds: number[];
|
|
313
|
-
}>;
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
function histoMetrics(calls: Array<{ init?: RequestInit }>): HistoMetric[] {
|
|
318
|
-
const payload = JSON.parse(calls[0].init!.body as string) as {
|
|
319
|
-
resourceMetrics: Array<{ scopeMetrics: Array<{ metrics: HistoMetric[] }> }>;
|
|
320
|
-
};
|
|
321
|
-
return payload.resourceMetrics[0].scopeMetrics[0].metrics;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// Decades of bytes, mirroring CACHE_SIZE_BUCKET_BOUNDARIES_BYTES.
|
|
325
|
-
const BYTE_BOUNDS = [1_000, 10_000, 100_000, 1_000_000, 10_000_000, 33_554_432];
|
|
326
|
-
|
|
327
|
-
describe("createOtlpHttpMeterAdapter — per-metric bucket boundaries", () => {
|
|
328
|
-
beforeEach(() => {
|
|
329
|
-
vi.useFakeTimers();
|
|
330
|
-
vi.setSystemTime(new Date("2026-05-18T16:00:00.000Z"));
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
afterEach(() => {
|
|
334
|
-
vi.useRealTimers();
|
|
335
|
-
vi.restoreAllMocks();
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
it("buckets a metric against its own boundaries, not the adapter default", async () => {
|
|
339
|
-
const { impl, calls } = captureFetch();
|
|
340
|
-
const meter = buildAdapter({
|
|
341
|
-
fetchImpl: impl,
|
|
342
|
-
// The ms-tuned adapter default: every byte value would saturate its
|
|
343
|
-
// overflow bucket, which is the regression this test pins.
|
|
344
|
-
histogramBounds: [5, 10, 25, 50, 75, 100, 250, 500, 1000],
|
|
345
|
-
metricMetadata: { "deco.cache.size": { unit: "By", boundaries: BYTE_BOUNDS } },
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
const labels = { op: "set", profile: "productList" };
|
|
349
|
-
meter.histogramRecord("deco.cache.size", 512, labels); // bucket 0 (<= 1k)
|
|
350
|
-
meter.histogramRecord("deco.cache.size", 50_000, labels); // bucket 2 (<= 100k)
|
|
351
|
-
meter.histogramRecord("deco.cache.size", 50_000_000, labels); // overflow (> 32M)
|
|
352
|
-
|
|
353
|
-
await meter.flush();
|
|
354
|
-
|
|
355
|
-
const dp = histoMetrics(calls)[0].histogram!.dataPoints[0];
|
|
356
|
-
expect(dp.explicitBounds).toEqual(BYTE_BOUNDS);
|
|
357
|
-
expect(dp.bucketCounts).toEqual(["1", "0", "1", "0", "0", "0", "1"]);
|
|
358
|
-
expect(dp.count).toBe("3");
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
it("emits the bounds the counts were filed against, per metric, in one flush", async () => {
|
|
362
|
-
const { impl, calls } = captureFetch();
|
|
363
|
-
const meter = buildAdapter({
|
|
364
|
-
fetchImpl: impl,
|
|
365
|
-
histogramBounds: [5, 10, 25],
|
|
366
|
-
metricMetadata: { "deco.cache.size": { unit: "By", boundaries: BYTE_BOUNDS } },
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
// Two histograms with different units in the SAME adapter — the bug was
|
|
370
|
-
// that both serialized with one shared array.
|
|
371
|
-
meter.histogramRecord("deco.cache.size", 512, { op: "set" });
|
|
372
|
-
meter.histogramRecord("deco.loader.duration", 7, { name: "x" });
|
|
373
|
-
|
|
374
|
-
await meter.flush();
|
|
375
|
-
|
|
376
|
-
const byName = Object.fromEntries(histoMetrics(calls).map((m) => [m.name, m]));
|
|
377
|
-
expect(byName["deco.cache.size"].histogram!.dataPoints[0].explicitBounds).toEqual(BYTE_BOUNDS);
|
|
378
|
-
expect(byName["deco.loader.duration"].histogram!.dataPoints[0].explicitBounds).toEqual([
|
|
379
|
-
5, 10, 25,
|
|
380
|
-
]);
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
it("falls back to the adapter default when a metric declares no boundaries", async () => {
|
|
384
|
-
const { impl, calls } = captureFetch();
|
|
385
|
-
const meter = buildAdapter({ fetchImpl: impl, histogramBounds: [10, 100] });
|
|
386
|
-
|
|
387
|
-
meter.histogramRecord("deco.loader.duration", 50, { name: "x" });
|
|
388
|
-
|
|
389
|
-
await meter.flush();
|
|
390
|
-
|
|
391
|
-
const dp = histoMetrics(calls)[0].histogram!.dataPoints[0];
|
|
392
|
-
expect(dp.explicitBounds).toEqual([10, 100]);
|
|
393
|
-
expect(dp.bucketCounts).toEqual(["0", "1", "0"]);
|
|
394
|
-
});
|
|
395
|
-
});
|