@rdlabo/workers-hono-kit 0.9.4 → 0.9.5

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.
@@ -31,6 +31,8 @@ export interface PerfLogOptions {
31
31
  * When provided, write one data point per request to a **Workers Analytics Engine** dataset. Query
32
32
  * percentiles by route/colo with the SQL API (≈90-day retention). Non-blocking. Layout:
33
33
  * `doubles = [t_app_ms, cold(0|1), status]`, `blobs = [path, colo, method]`, `indexes = [path]`.
34
+ * Route patterns longer than Analytics Engine's 96-byte index limit use a stable hash as the index;
35
+ * the complete route remains available in `blob1`.
34
36
  */
35
37
  dataset?: AnalyticsEngineDatasetLike;
36
38
  /**
@@ -18,6 +18,20 @@
18
18
  // a cold start are labelled warm (only the very first flips the flag) even though they pay cold-init
19
19
  // waits — a minor warm-side contamination, negligible at the low request rates this targets.
20
20
  let isolateWarm = false;
21
+ const ANALYTICS_INDEX_MAX_BYTES = 96;
22
+ function analyticsIndex(path) {
23
+ const bytes = new TextEncoder().encode(path);
24
+ if (bytes.byteLength <= ANALYTICS_INDEX_MAX_BYTES) {
25
+ return path;
26
+ }
27
+ // FNV-1a 64-bit keeps sampling deterministic without adding async crypto work to every response.
28
+ let hash = 0xcbf29ce484222325n;
29
+ for (const byte of bytes) {
30
+ hash ^= BigInt(byte);
31
+ hash = BigInt.asUintN(64, hash * 0x100000001b3n);
32
+ }
33
+ return `route:${hash.toString(16).padStart(16, '0')}`;
34
+ }
21
35
  /**
22
36
  * Create a Hono middleware that records a per-request latency data point and emits it to Workers
23
37
  * Logs (`console`) and/or Workers Analytics Engine (`dataset`).
@@ -84,11 +98,17 @@ export function perfLog(options = {}) {
84
98
  // In-code sampling thins Analytics Engine writes only; Workers Logs volume is controlled separately
85
99
  // by the observability `head_sampling_rate`. Low-traffic Workers should leave `sampleRate` at 1.
86
100
  if (sink && (rate >= 1 || Math.random() < rate)) {
87
- sink.writeDataPoint({
88
- doubles: [tApp, cold ? 1 : 0, status],
89
- blobs: [path, colo, method],
90
- indexes: [path],
91
- });
101
+ try {
102
+ sink.writeDataPoint({
103
+ doubles: [tApp, cold ? 1 : 0, status],
104
+ blobs: [path, colo, method],
105
+ indexes: [analyticsIndex(path)],
106
+ });
107
+ }
108
+ catch (error) {
109
+ // Telemetry must never replace an otherwise successful application response with a 500.
110
+ console.warn('[perfLog] Analytics Engine write failed', error);
111
+ }
92
112
  }
93
113
  if (emitConsole) {
94
114
  console.log(JSON.stringify({ perf: { cold, colo, method, path, status, t_app: tApp } }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdlabo/workers-hono-kit",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"