@bleedingdev/modern-js-server-core 3.2.0-ultramodern.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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/cjs/adapters/node/helper/index.js +55 -0
- package/dist/cjs/adapters/node/helper/loadCache.js +58 -0
- package/dist/cjs/adapters/node/helper/loadConfig.js +78 -0
- package/dist/cjs/adapters/node/helper/loadEnv.js +66 -0
- package/dist/cjs/adapters/node/helper/loadPlugin.js +47 -0
- package/dist/cjs/adapters/node/helper/utils.js +39 -0
- package/dist/cjs/adapters/node/hono.js +95 -0
- package/dist/cjs/adapters/node/index.js +87 -0
- package/dist/cjs/adapters/node/node.js +180 -0
- package/dist/cjs/adapters/node/plugins/index.js +72 -0
- package/dist/cjs/adapters/node/plugins/nodeServer.js +43 -0
- package/dist/cjs/adapters/node/plugins/resource.js +206 -0
- package/dist/cjs/adapters/node/plugins/static.js +379 -0
- package/dist/cjs/constants.js +77 -0
- package/dist/cjs/context.js +41 -0
- package/dist/cjs/helper.js +45 -0
- package/dist/cjs/hono.js +46 -0
- package/dist/cjs/index.js +247 -0
- package/dist/cjs/plugins/compat/hooks.js +80 -0
- package/dist/cjs/plugins/compat/index.js +64 -0
- package/dist/cjs/plugins/contractGateAutopilot.js +158 -0
- package/dist/cjs/plugins/contractGateSnapshotStore.js +239 -0
- package/dist/cjs/plugins/default.js +61 -0
- package/dist/cjs/plugins/favicon.js +48 -0
- package/dist/cjs/plugins/index.js +157 -0
- package/dist/cjs/plugins/log.js +92 -0
- package/dist/cjs/plugins/mfCache.js +78 -0
- package/dist/cjs/plugins/middlewares.js +49 -0
- package/dist/cjs/plugins/monitors.js +192 -0
- package/dist/cjs/plugins/processedBy.js +50 -0
- package/dist/cjs/plugins/render/csrRscRender.js +74 -0
- package/dist/cjs/plugins/render/dataHandler.js +53 -0
- package/dist/cjs/plugins/render/index.js +158 -0
- package/dist/cjs/plugins/render/inject.js +92 -0
- package/dist/cjs/plugins/render/render.js +273 -0
- package/dist/cjs/plugins/render/renderRscHandler.js +72 -0
- package/dist/cjs/plugins/render/serverActionHandler.js +47 -0
- package/dist/cjs/plugins/render/ssrCache.js +198 -0
- package/dist/cjs/plugins/render/ssrRender.js +96 -0
- package/dist/cjs/plugins/render/utils.js +49 -0
- package/dist/cjs/plugins/route.js +68 -0
- package/dist/cjs/plugins/telemetry.js +1283 -0
- package/dist/cjs/serverBase.js +176 -0
- package/dist/cjs/types/config/bff.js +18 -0
- package/dist/cjs/types/config/dev.js +18 -0
- package/dist/cjs/types/config/html.js +18 -0
- package/dist/cjs/types/config/index.js +93 -0
- package/dist/cjs/types/config/output.js +18 -0
- package/dist/cjs/types/config/security.js +18 -0
- package/dist/cjs/types/config/server.js +18 -0
- package/dist/cjs/types/config/share.js +18 -0
- package/dist/cjs/types/config/source.js +18 -0
- package/dist/cjs/types/config/tools.js +18 -0
- package/dist/cjs/types/index.js +79 -0
- package/dist/cjs/types/plugins/base.js +18 -0
- package/dist/cjs/types/plugins/index.js +58 -0
- package/dist/cjs/types/plugins/plugin.js +18 -0
- package/dist/cjs/types/render.js +18 -0
- package/dist/cjs/types/requestHandler.js +18 -0
- package/dist/cjs/types/server.js +18 -0
- package/dist/cjs/utils/entry.js +40 -0
- package/dist/cjs/utils/env.js +51 -0
- package/dist/cjs/utils/error.js +93 -0
- package/dist/cjs/utils/index.js +114 -0
- package/dist/cjs/utils/middlewareCollector.js +63 -0
- package/dist/cjs/utils/publicDir.js +92 -0
- package/dist/cjs/utils/request.js +86 -0
- package/dist/cjs/utils/serverConfig.js +43 -0
- package/dist/cjs/utils/storage.js +69 -0
- package/dist/cjs/utils/transformStream.js +65 -0
- package/dist/cjs/utils/warmup.js +40 -0
- package/dist/esm/adapters/node/helper/index.mjs +5 -0
- package/dist/esm/adapters/node/helper/loadCache.mjs +14 -0
- package/dist/esm/adapters/node/helper/loadConfig.mjs +31 -0
- package/dist/esm/adapters/node/helper/loadEnv.mjs +22 -0
- package/dist/esm/adapters/node/helper/loadPlugin.mjs +13 -0
- package/dist/esm/adapters/node/helper/utils.mjs +5 -0
- package/dist/esm/adapters/node/hono.mjs +55 -0
- package/dist/esm/adapters/node/index.mjs +4 -0
- package/dist/esm/adapters/node/node.mjs +130 -0
- package/dist/esm/adapters/node/plugins/index.mjs +3 -0
- package/dist/esm/adapters/node/plugins/nodeServer.mjs +9 -0
- package/dist/esm/adapters/node/plugins/resource.mjs +138 -0
- package/dist/esm/adapters/node/plugins/static.mjs +329 -0
- package/dist/esm/constants.mjs +28 -0
- package/dist/esm/context.mjs +4 -0
- package/dist/esm/helper.mjs +11 -0
- package/dist/esm/hono.mjs +2 -0
- package/dist/esm/index.mjs +12 -0
- package/dist/esm/plugins/compat/hooks.mjs +40 -0
- package/dist/esm/plugins/compat/index.mjs +27 -0
- package/dist/esm/plugins/contractGateAutopilot.mjs +124 -0
- package/dist/esm/plugins/contractGateSnapshotStore.mjs +180 -0
- package/dist/esm/plugins/default.mjs +27 -0
- package/dist/esm/plugins/favicon.mjs +14 -0
- package/dist/esm/plugins/index.mjs +11 -0
- package/dist/esm/plugins/log.mjs +58 -0
- package/dist/esm/plugins/mfCache.mjs +35 -0
- package/dist/esm/plugins/middlewares.mjs +15 -0
- package/dist/esm/plugins/monitors.mjs +149 -0
- package/dist/esm/plugins/processedBy.mjs +16 -0
- package/dist/esm/plugins/render/csrRscRender.mjs +40 -0
- package/dist/esm/plugins/render/dataHandler.mjs +19 -0
- package/dist/esm/plugins/render/index.mjs +84 -0
- package/dist/esm/plugins/render/inject.mjs +55 -0
- package/dist/esm/plugins/render/render.mjs +230 -0
- package/dist/esm/plugins/render/renderRscHandler.mjs +38 -0
- package/dist/esm/plugins/render/serverActionHandler.mjs +13 -0
- package/dist/esm/plugins/render/ssrCache.mjs +158 -0
- package/dist/esm/plugins/render/ssrRender.mjs +62 -0
- package/dist/esm/plugins/render/utils.mjs +15 -0
- package/dist/esm/plugins/route.mjs +34 -0
- package/dist/esm/plugins/telemetry.mjs +1195 -0
- package/dist/esm/rslib-runtime.mjs +18 -0
- package/dist/esm/serverBase.mjs +139 -0
- package/dist/esm/types/config/bff.mjs +0 -0
- package/dist/esm/types/config/dev.mjs +0 -0
- package/dist/esm/types/config/html.mjs +0 -0
- package/dist/esm/types/config/index.mjs +6 -0
- package/dist/esm/types/config/output.mjs +0 -0
- package/dist/esm/types/config/security.mjs +0 -0
- package/dist/esm/types/config/server.mjs +0 -0
- package/dist/esm/types/config/share.mjs +0 -0
- package/dist/esm/types/config/source.mjs +0 -0
- package/dist/esm/types/config/tools.mjs +0 -0
- package/dist/esm/types/index.mjs +4 -0
- package/dist/esm/types/plugins/base.mjs +0 -0
- package/dist/esm/types/plugins/index.mjs +1 -0
- package/dist/esm/types/plugins/plugin.mjs +0 -0
- package/dist/esm/types/render.mjs +0 -0
- package/dist/esm/types/requestHandler.mjs +0 -0
- package/dist/esm/types/server.mjs +0 -0
- package/dist/esm/utils/entry.mjs +3 -0
- package/dist/esm/utils/env.mjs +14 -0
- package/dist/esm/utils/error.mjs +53 -0
- package/dist/esm/utils/index.mjs +9 -0
- package/dist/esm/utils/middlewareCollector.mjs +26 -0
- package/dist/esm/utils/publicDir.mjs +33 -0
- package/dist/esm/utils/request.mjs +40 -0
- package/dist/esm/utils/serverConfig.mjs +9 -0
- package/dist/esm/utils/storage.mjs +35 -0
- package/dist/esm/utils/transformStream.mjs +28 -0
- package/dist/esm/utils/warmup.mjs +6 -0
- package/dist/esm-node/adapters/node/helper/index.mjs +6 -0
- package/dist/esm-node/adapters/node/helper/loadCache.mjs +15 -0
- package/dist/esm-node/adapters/node/helper/loadConfig.mjs +32 -0
- package/dist/esm-node/adapters/node/helper/loadEnv.mjs +23 -0
- package/dist/esm-node/adapters/node/helper/loadPlugin.mjs +14 -0
- package/dist/esm-node/adapters/node/helper/utils.mjs +6 -0
- package/dist/esm-node/adapters/node/hono.mjs +56 -0
- package/dist/esm-node/adapters/node/index.mjs +5 -0
- package/dist/esm-node/adapters/node/node.mjs +131 -0
- package/dist/esm-node/adapters/node/plugins/index.mjs +4 -0
- package/dist/esm-node/adapters/node/plugins/nodeServer.mjs +10 -0
- package/dist/esm-node/adapters/node/plugins/resource.mjs +139 -0
- package/dist/esm-node/adapters/node/plugins/static.mjs +330 -0
- package/dist/esm-node/constants.mjs +29 -0
- package/dist/esm-node/context.mjs +5 -0
- package/dist/esm-node/helper.mjs +12 -0
- package/dist/esm-node/hono.mjs +3 -0
- package/dist/esm-node/index.mjs +13 -0
- package/dist/esm-node/plugins/compat/hooks.mjs +41 -0
- package/dist/esm-node/plugins/compat/index.mjs +28 -0
- package/dist/esm-node/plugins/contractGateAutopilot.mjs +125 -0
- package/dist/esm-node/plugins/contractGateSnapshotStore.mjs +182 -0
- package/dist/esm-node/plugins/default.mjs +28 -0
- package/dist/esm-node/plugins/favicon.mjs +15 -0
- package/dist/esm-node/plugins/index.mjs +12 -0
- package/dist/esm-node/plugins/log.mjs +59 -0
- package/dist/esm-node/plugins/mfCache.mjs +36 -0
- package/dist/esm-node/plugins/middlewares.mjs +16 -0
- package/dist/esm-node/plugins/monitors.mjs +150 -0
- package/dist/esm-node/plugins/processedBy.mjs +17 -0
- package/dist/esm-node/plugins/render/csrRscRender.mjs +41 -0
- package/dist/esm-node/plugins/render/dataHandler.mjs +20 -0
- package/dist/esm-node/plugins/render/index.mjs +85 -0
- package/dist/esm-node/plugins/render/inject.mjs +56 -0
- package/dist/esm-node/plugins/render/render.mjs +231 -0
- package/dist/esm-node/plugins/render/renderRscHandler.mjs +39 -0
- package/dist/esm-node/plugins/render/serverActionHandler.mjs +14 -0
- package/dist/esm-node/plugins/render/ssrCache.mjs +159 -0
- package/dist/esm-node/plugins/render/ssrRender.mjs +63 -0
- package/dist/esm-node/plugins/render/utils.mjs +16 -0
- package/dist/esm-node/plugins/route.mjs +35 -0
- package/dist/esm-node/plugins/telemetry.mjs +1196 -0
- package/dist/esm-node/rslib-runtime.mjs +19 -0
- package/dist/esm-node/serverBase.mjs +140 -0
- package/dist/esm-node/types/config/bff.mjs +1 -0
- package/dist/esm-node/types/config/dev.mjs +1 -0
- package/dist/esm-node/types/config/html.mjs +1 -0
- package/dist/esm-node/types/config/index.mjs +7 -0
- package/dist/esm-node/types/config/output.mjs +1 -0
- package/dist/esm-node/types/config/security.mjs +1 -0
- package/dist/esm-node/types/config/server.mjs +1 -0
- package/dist/esm-node/types/config/share.mjs +1 -0
- package/dist/esm-node/types/config/source.mjs +1 -0
- package/dist/esm-node/types/config/tools.mjs +1 -0
- package/dist/esm-node/types/index.mjs +5 -0
- package/dist/esm-node/types/plugins/base.mjs +1 -0
- package/dist/esm-node/types/plugins/index.mjs +2 -0
- package/dist/esm-node/types/plugins/plugin.mjs +1 -0
- package/dist/esm-node/types/render.mjs +1 -0
- package/dist/esm-node/types/requestHandler.mjs +1 -0
- package/dist/esm-node/types/server.mjs +1 -0
- package/dist/esm-node/utils/entry.mjs +4 -0
- package/dist/esm-node/utils/env.mjs +15 -0
- package/dist/esm-node/utils/error.mjs +54 -0
- package/dist/esm-node/utils/index.mjs +10 -0
- package/dist/esm-node/utils/middlewareCollector.mjs +27 -0
- package/dist/esm-node/utils/publicDir.mjs +34 -0
- package/dist/esm-node/utils/request.mjs +41 -0
- package/dist/esm-node/utils/serverConfig.mjs +10 -0
- package/dist/esm-node/utils/storage.mjs +36 -0
- package/dist/esm-node/utils/transformStream.mjs +29 -0
- package/dist/esm-node/utils/warmup.mjs +7 -0
- package/dist/types/adapters/node/helper/index.d.ts +6 -0
- package/dist/types/adapters/node/helper/loadCache.d.ts +2 -0
- package/dist/types/adapters/node/helper/loadConfig.d.ts +3 -0
- package/dist/types/adapters/node/helper/loadEnv.d.ts +3 -0
- package/dist/types/adapters/node/helper/loadPlugin.d.ts +3 -0
- package/dist/types/adapters/node/helper/utils.d.ts +21 -0
- package/dist/types/adapters/node/hono.d.ts +19 -0
- package/dist/types/adapters/node/index.d.ts +5 -0
- package/dist/types/adapters/node/node.d.ts +17 -0
- package/dist/types/adapters/node/plugins/index.d.ts +3 -0
- package/dist/types/adapters/node/plugins/nodeServer.d.ts +6 -0
- package/dist/types/adapters/node/plugins/resource.d.ts +11 -0
- package/dist/types/adapters/node/plugins/static.d.ts +25 -0
- package/dist/types/constants.d.ts +26 -0
- package/dist/types/context.d.ts +3 -0
- package/dist/types/helper.d.ts +10 -0
- package/dist/types/hono.d.ts +3 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/plugins/compat/hooks.d.ts +8 -0
- package/dist/types/plugins/compat/index.d.ts +3 -0
- package/dist/types/plugins/contractGateAutopilot.d.ts +35 -0
- package/dist/types/plugins/contractGateSnapshotStore.d.ts +57 -0
- package/dist/types/plugins/default.d.ts +7 -0
- package/dist/types/plugins/favicon.d.ts +2 -0
- package/dist/types/plugins/index.d.ts +11 -0
- package/dist/types/plugins/log.d.ts +2 -0
- package/dist/types/plugins/mfCache.d.ts +12 -0
- package/dist/types/plugins/middlewares.d.ts +2 -0
- package/dist/types/plugins/monitors.d.ts +6 -0
- package/dist/types/plugins/processedBy.d.ts +2 -0
- package/dist/types/plugins/render/csrRscRender.d.ts +2 -0
- package/dist/types/plugins/render/dataHandler.d.ts +5 -0
- package/dist/types/plugins/render/index.d.ts +3 -0
- package/dist/types/plugins/render/inject.d.ts +7 -0
- package/dist/types/plugins/render/render.d.ts +16 -0
- package/dist/types/plugins/render/renderRscHandler.d.ts +2 -0
- package/dist/types/plugins/render/serverActionHandler.d.ts +2 -0
- package/dist/types/plugins/render/ssrCache.d.ts +18 -0
- package/dist/types/plugins/render/ssrRender.d.ts +26 -0
- package/dist/types/plugins/render/utils.d.ts +3 -0
- package/dist/types/plugins/route.d.ts +2 -0
- package/dist/types/plugins/telemetry.d.ts +309 -0
- package/dist/types/serverBase.d.ts +38 -0
- package/dist/types/types/config/bff.d.ts +142 -0
- package/dist/types/types/config/dev.d.ts +4 -0
- package/dist/types/types/config/html.d.ts +15 -0
- package/dist/types/types/config/index.d.ts +35 -0
- package/dist/types/types/config/output.d.ts +20 -0
- package/dist/types/types/config/security.d.ts +4 -0
- package/dist/types/types/config/server.d.ts +402 -0
- package/dist/types/types/config/share.d.ts +3 -0
- package/dist/types/types/config/source.d.ts +7 -0
- package/dist/types/types/config/tools.d.ts +2 -0
- package/dist/types/types/index.d.ts +4 -0
- package/dist/types/types/plugins/base.d.ts +57 -0
- package/dist/types/types/plugins/index.d.ts +2 -0
- package/dist/types/types/plugins/plugin.d.ts +31 -0
- package/dist/types/types/render.d.ts +24 -0
- package/dist/types/types/requestHandler.d.ts +48 -0
- package/dist/types/types/server.d.ts +67 -0
- package/dist/types/utils/entry.d.ts +3 -0
- package/dist/types/utils/env.d.ts +2 -0
- package/dist/types/utils/error.d.ts +8 -0
- package/dist/types/utils/index.d.ts +9 -0
- package/dist/types/utils/middlewareCollector.d.ts +12 -0
- package/dist/types/utils/publicDir.d.ts +40 -0
- package/dist/types/utils/request.d.ts +17 -0
- package/dist/types/utils/serverConfig.d.ts +8 -0
- package/dist/types/utils/storage.d.ts +5 -0
- package/dist/types/utils/transformStream.d.ts +5 -0
- package/dist/types/utils/warmup.d.ts +1 -0
- package/package.json +103 -0
- package/rslib.config.mts +4 -0
- package/rstest.config.mts +7 -0
|
@@ -0,0 +1,1196 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { ContractGateAutopilot } from "./contractGateAutopilot.mjs";
|
|
3
|
+
import { CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION, DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH, resolveContractGateSnapshotPath, resolveContractGateSnapshotStore } from "./contractGateSnapshotStore.mjs";
|
|
4
|
+
class TelemetryStartupHealthError extends Error {
|
|
5
|
+
constructor(failedExporters){
|
|
6
|
+
super(`Telemetry startup health check failed for exporters: ${failedExporters.map((item)=>item.name).join(', ')}`), this.code = 'TELEMETRY_EXPORTER_STARTUP_HEALTH_FAILED';
|
|
7
|
+
this.name = 'TelemetryStartupHealthError';
|
|
8
|
+
this.failedExporters = failedExporters;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/i;
|
|
12
|
+
const DEFAULT_OTLP_ENDPOINT = 'http://127.0.0.1:4318/v1/logs';
|
|
13
|
+
const DEFAULT_VM_ENDPOINT = 'http://127.0.0.1:8428/api/v1/import/prometheus';
|
|
14
|
+
const DEFAULT_TIMEOUT_MS = 5000;
|
|
15
|
+
const DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT = '/_modern/contract-gates/runtime-fallback';
|
|
16
|
+
const DEFAULT_RUNTIME_STATUS_ENDPOINT = '/_modern/runtime/status';
|
|
17
|
+
const DEFAULT_RUNTIME_FALLBACK_GATE_NAME = 'runtime-mf-fallback-health';
|
|
18
|
+
const DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS = 300000;
|
|
19
|
+
const DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES = 16384;
|
|
20
|
+
const DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER = 'x-modernjs-runtime-signal-token';
|
|
21
|
+
const DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW = 30;
|
|
22
|
+
const DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS = 60000;
|
|
23
|
+
const DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS = 10000;
|
|
24
|
+
function clamp(value, min, max) {
|
|
25
|
+
return Math.max(min, Math.min(max, value));
|
|
26
|
+
}
|
|
27
|
+
function isRecord(value) {
|
|
28
|
+
return 'object' == typeof value && null !== value && !Array.isArray(value);
|
|
29
|
+
}
|
|
30
|
+
function redactObject(value, redactionKeys) {
|
|
31
|
+
if (!isRecord(value)) return;
|
|
32
|
+
const output = {};
|
|
33
|
+
for (const [key, nested] of Object.entries(value)){
|
|
34
|
+
if (redactionKeys.has(key)) {
|
|
35
|
+
output[key] = '[REDACTED]';
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(nested)) {
|
|
39
|
+
output[key] = nested.map((item)=>{
|
|
40
|
+
if (isRecord(item)) return redactObject(item, redactionKeys);
|
|
41
|
+
return item;
|
|
42
|
+
});
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (isRecord(nested)) {
|
|
46
|
+
output[key] = redactObject(nested, redactionKeys);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
output[key] = nested;
|
|
50
|
+
}
|
|
51
|
+
return output;
|
|
52
|
+
}
|
|
53
|
+
function normalizeLabels(labels) {
|
|
54
|
+
if (!labels) return;
|
|
55
|
+
const normalized = {};
|
|
56
|
+
for (const [key, value] of Object.entries(labels))if (null != value) normalized[key] = String(value);
|
|
57
|
+
return Object.keys(normalized).length > 0 ? normalized : void 0;
|
|
58
|
+
}
|
|
59
|
+
function parseTraceparent(header) {
|
|
60
|
+
if (!header) return;
|
|
61
|
+
const match = header.trim().match(TRACEPARENT_REGEX);
|
|
62
|
+
if (!match) return;
|
|
63
|
+
const traceId = match[1]?.toLowerCase();
|
|
64
|
+
const spanId = match[2]?.toLowerCase();
|
|
65
|
+
if (!traceId || !spanId) return;
|
|
66
|
+
return {
|
|
67
|
+
traceId,
|
|
68
|
+
spanId
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function extractError(args) {
|
|
72
|
+
for (const arg of args)if (arg instanceof Error) return {
|
|
73
|
+
name: arg.name,
|
|
74
|
+
message: arg.message,
|
|
75
|
+
stack: arg.stack
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function toTelemetryEnvelope(event, input) {
|
|
79
|
+
const base = {
|
|
80
|
+
timestamp: Date.now(),
|
|
81
|
+
service: input.service,
|
|
82
|
+
module: input.module,
|
|
83
|
+
environment: input.environment,
|
|
84
|
+
...input.traceId ? {
|
|
85
|
+
traceId: input.traceId
|
|
86
|
+
} : {},
|
|
87
|
+
...input.spanId ? {
|
|
88
|
+
spanId: input.spanId
|
|
89
|
+
} : {},
|
|
90
|
+
...input.attributes ? {
|
|
91
|
+
attributes: input.attributes
|
|
92
|
+
} : {}
|
|
93
|
+
};
|
|
94
|
+
if ('log' === event.type) {
|
|
95
|
+
const payload = event.payload;
|
|
96
|
+
const args = payload.args || [];
|
|
97
|
+
const signalType = 'trace' === payload.level ? 'trace' : 'log';
|
|
98
|
+
return {
|
|
99
|
+
...base,
|
|
100
|
+
signalType,
|
|
101
|
+
name: payload.message,
|
|
102
|
+
level: payload.level,
|
|
103
|
+
attributes: {
|
|
104
|
+
...base.attributes || {},
|
|
105
|
+
args
|
|
106
|
+
},
|
|
107
|
+
error: extractError(args)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if ('timing' === event.type) return {
|
|
111
|
+
...base,
|
|
112
|
+
signalType: 'metric',
|
|
113
|
+
name: event.payload.name,
|
|
114
|
+
value: event.payload.dur,
|
|
115
|
+
unit: 'ms',
|
|
116
|
+
tags: normalizeLabels(event.payload.tags),
|
|
117
|
+
attributes: {
|
|
118
|
+
...base.attributes || {},
|
|
119
|
+
desc: event.payload.desc,
|
|
120
|
+
args: event.payload.args
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
return {
|
|
124
|
+
...base,
|
|
125
|
+
signalType: 'metric',
|
|
126
|
+
name: event.payload.name,
|
|
127
|
+
value: 1,
|
|
128
|
+
unit: 'count',
|
|
129
|
+
tags: normalizeLabels(event.payload.tags),
|
|
130
|
+
attributes: {
|
|
131
|
+
...base.attributes || {},
|
|
132
|
+
args: event.payload.args
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
async function postWithTimeout(options) {
|
|
137
|
+
const controller = new AbortController();
|
|
138
|
+
const timer = setTimeout(()=>controller.abort(), options.timeoutMs);
|
|
139
|
+
if ('function' == typeof timer.unref) timer.unref();
|
|
140
|
+
try {
|
|
141
|
+
const response = await fetch(options.endpoint, {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
body: options.body,
|
|
144
|
+
headers: options.headers,
|
|
145
|
+
signal: controller.signal
|
|
146
|
+
});
|
|
147
|
+
if (!response.ok) throw new Error(`Telemetry exporter request failed: ${response.status} ${response.statusText}`);
|
|
148
|
+
} finally{
|
|
149
|
+
clearTimeout(timer);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function sanitizeMetricName(value) {
|
|
153
|
+
return value.replace(/[^a-zA-Z0-9_:]/g, '_').replace(/_+/g, '_');
|
|
154
|
+
}
|
|
155
|
+
function escapeLabelValue(value) {
|
|
156
|
+
return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n');
|
|
157
|
+
}
|
|
158
|
+
function toPrometheusLine(envelope, metricPrefix) {
|
|
159
|
+
const metricName = sanitizeMetricName(`${metricPrefix}_${envelope.signalType}_${envelope.name}`);
|
|
160
|
+
const labels = {
|
|
161
|
+
service: envelope.service,
|
|
162
|
+
module: envelope.module,
|
|
163
|
+
environment: envelope.environment,
|
|
164
|
+
...envelope.level ? {
|
|
165
|
+
level: envelope.level
|
|
166
|
+
} : {},
|
|
167
|
+
...envelope.traceId ? {
|
|
168
|
+
trace_id: envelope.traceId
|
|
169
|
+
} : {},
|
|
170
|
+
...envelope.spanId ? {
|
|
171
|
+
span_id: envelope.spanId
|
|
172
|
+
} : {},
|
|
173
|
+
...envelope.tags || {}
|
|
174
|
+
};
|
|
175
|
+
const labelPairs = Object.entries(labels).sort(([a], [b])=>a.localeCompare(b)).map(([key, value])=>`${sanitizeMetricName(key)}="${escapeLabelValue(value)}"`);
|
|
176
|
+
const labelText = labelPairs.length > 0 ? `{${labelPairs.join(',')}}` : '';
|
|
177
|
+
const value = 'number' == typeof envelope.value && Number.isFinite(envelope.value) ? envelope.value : 1;
|
|
178
|
+
const timestampMs = envelope.timestamp;
|
|
179
|
+
return `${metricName}${labelText} ${value} ${timestampMs}`;
|
|
180
|
+
}
|
|
181
|
+
function createOtlpTelemetryExporter(options = {}) {
|
|
182
|
+
const endpoint = options.endpoint || DEFAULT_OTLP_ENDPOINT;
|
|
183
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
184
|
+
const headers = {
|
|
185
|
+
'content-type': 'application/json',
|
|
186
|
+
...options.headers || {}
|
|
187
|
+
};
|
|
188
|
+
return {
|
|
189
|
+
name: 'otlp',
|
|
190
|
+
async emit (batch) {
|
|
191
|
+
if (0 === batch.length) return;
|
|
192
|
+
const body = JSON.stringify({
|
|
193
|
+
resource: {
|
|
194
|
+
service: batch[0]?.service,
|
|
195
|
+
module: batch[0]?.module,
|
|
196
|
+
environment: batch[0]?.environment
|
|
197
|
+
},
|
|
198
|
+
emittedAt: Date.now(),
|
|
199
|
+
events: batch
|
|
200
|
+
});
|
|
201
|
+
await postWithTimeout({
|
|
202
|
+
endpoint,
|
|
203
|
+
body,
|
|
204
|
+
headers,
|
|
205
|
+
timeoutMs
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function createVictoriaMetricsTelemetryExporter(options = {}) {
|
|
211
|
+
const endpoint = options.endpoint || DEFAULT_VM_ENDPOINT;
|
|
212
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
213
|
+
const metricPrefix = sanitizeMetricName(options.metricPrefix || 'modernjs');
|
|
214
|
+
const headers = {
|
|
215
|
+
'content-type': 'text/plain; version=0.0.4',
|
|
216
|
+
...options.headers || {}
|
|
217
|
+
};
|
|
218
|
+
return {
|
|
219
|
+
name: 'victoria-metrics',
|
|
220
|
+
async emit (batch) {
|
|
221
|
+
if (0 === batch.length) return;
|
|
222
|
+
const lines = batch.map((item)=>toPrometheusLine(item, metricPrefix));
|
|
223
|
+
await postWithTimeout({
|
|
224
|
+
endpoint,
|
|
225
|
+
body: `${lines.join('\n')}\n`,
|
|
226
|
+
headers,
|
|
227
|
+
timeoutMs
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
class TelemetryRegistry {
|
|
233
|
+
async register(exporter) {
|
|
234
|
+
this.exporters.push(exporter);
|
|
235
|
+
this.exporterHealth.set(exporter.name, {
|
|
236
|
+
name: exporter.name,
|
|
237
|
+
healthy: true,
|
|
238
|
+
failures: 0
|
|
239
|
+
});
|
|
240
|
+
if (exporter.init) try {
|
|
241
|
+
await exporter.init({
|
|
242
|
+
service: this.service,
|
|
243
|
+
module: this.module,
|
|
244
|
+
environment: this.environment
|
|
245
|
+
});
|
|
246
|
+
this.markExporterHealthy(exporter.name);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
this.markExporterFailure(exporter.name, error);
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
else this.markExporterHealthy(exporter.name);
|
|
252
|
+
}
|
|
253
|
+
getOrCreateExporterHealth(name) {
|
|
254
|
+
const existing = this.exporterHealth.get(name);
|
|
255
|
+
if (existing) return existing;
|
|
256
|
+
const next = {
|
|
257
|
+
name,
|
|
258
|
+
healthy: true,
|
|
259
|
+
failures: 0
|
|
260
|
+
};
|
|
261
|
+
this.exporterHealth.set(name, next);
|
|
262
|
+
return next;
|
|
263
|
+
}
|
|
264
|
+
markExporterHealthy(name) {
|
|
265
|
+
const status = this.getOrCreateExporterHealth(name);
|
|
266
|
+
status.healthy = true;
|
|
267
|
+
status.lastSuccessAt = Date.now();
|
|
268
|
+
status.lastError = void 0;
|
|
269
|
+
}
|
|
270
|
+
markExporterFailure(name, error) {
|
|
271
|
+
const status = this.getOrCreateExporterHealth(name);
|
|
272
|
+
status.healthy = false;
|
|
273
|
+
status.failures += 1;
|
|
274
|
+
status.lastFailureAt = Date.now();
|
|
275
|
+
status.lastError = error instanceof Error ? error.message : String(error);
|
|
276
|
+
}
|
|
277
|
+
maybeEmitSloAlert(type, value, threshold) {
|
|
278
|
+
if (!this.onSloAlert || value < threshold) return;
|
|
279
|
+
const now = Date.now();
|
|
280
|
+
const lastTimestamp = this.lastSloAlertAt.get(type) ?? 0;
|
|
281
|
+
if (now - lastTimestamp < this.alertCooldownMs) return;
|
|
282
|
+
this.lastSloAlertAt.set(type, now);
|
|
283
|
+
const queueDepth = this.queue.length;
|
|
284
|
+
try {
|
|
285
|
+
this.onSloAlert({
|
|
286
|
+
timestamp: now,
|
|
287
|
+
service: this.service,
|
|
288
|
+
module: this.module,
|
|
289
|
+
environment: this.environment,
|
|
290
|
+
type,
|
|
291
|
+
value,
|
|
292
|
+
threshold,
|
|
293
|
+
queueDepth,
|
|
294
|
+
queueCapacity: this.maxQueueSize,
|
|
295
|
+
queueUtilization: queueDepth / this.maxQueueSize,
|
|
296
|
+
totalDropped: this.totalDroppedCount
|
|
297
|
+
});
|
|
298
|
+
} catch (_error) {}
|
|
299
|
+
}
|
|
300
|
+
enqueue(envelope) {
|
|
301
|
+
if (this.samplingRate < 1 && Math.random() > this.samplingRate) return;
|
|
302
|
+
const redactedEnvelope = this.redactionKeys.size > 0 ? {
|
|
303
|
+
...envelope,
|
|
304
|
+
attributes: redactObject(envelope.attributes, this.redactionKeys)
|
|
305
|
+
} : envelope;
|
|
306
|
+
if (this.queue.length >= this.maxQueueSize) {
|
|
307
|
+
this.queue.shift();
|
|
308
|
+
this.droppedCount += 1;
|
|
309
|
+
this.totalDroppedCount += 1;
|
|
310
|
+
this.maybeEmitSloAlert('queue.drop', this.totalDroppedCount, this.queueDroppedWarnThreshold);
|
|
311
|
+
}
|
|
312
|
+
this.queue.push(redactedEnvelope);
|
|
313
|
+
this.maybeEmitSloAlert('queue.utilization', this.queue.length / this.maxQueueSize, this.queueUtilizationWarnThreshold);
|
|
314
|
+
if (this.queue.length >= this.maxBatchSize) this.flush();
|
|
315
|
+
}
|
|
316
|
+
enqueueMetric(input) {
|
|
317
|
+
this.enqueue({
|
|
318
|
+
timestamp: Date.now(),
|
|
319
|
+
service: this.service,
|
|
320
|
+
module: this.module,
|
|
321
|
+
environment: this.environment,
|
|
322
|
+
signalType: 'metric',
|
|
323
|
+
name: input.name,
|
|
324
|
+
value: input.value,
|
|
325
|
+
unit: input.unit || 'count',
|
|
326
|
+
traceId: input.traceId,
|
|
327
|
+
spanId: input.spanId,
|
|
328
|
+
parentSpanId: input.parentSpanId,
|
|
329
|
+
tags: input.tags,
|
|
330
|
+
attributes: input.attributes
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
enqueueLog(input) {
|
|
334
|
+
this.enqueue({
|
|
335
|
+
timestamp: Date.now(),
|
|
336
|
+
service: this.service,
|
|
337
|
+
module: this.module,
|
|
338
|
+
environment: this.environment,
|
|
339
|
+
signalType: 'log',
|
|
340
|
+
name: input.name,
|
|
341
|
+
level: input.level,
|
|
342
|
+
traceId: input.traceId,
|
|
343
|
+
spanId: input.spanId,
|
|
344
|
+
parentSpanId: input.parentSpanId,
|
|
345
|
+
tags: input.tags,
|
|
346
|
+
attributes: input.attributes,
|
|
347
|
+
error: input.error
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
enqueueTrace(input) {
|
|
351
|
+
this.enqueue({
|
|
352
|
+
timestamp: Date.now(),
|
|
353
|
+
service: this.service,
|
|
354
|
+
module: this.module,
|
|
355
|
+
environment: this.environment,
|
|
356
|
+
signalType: 'trace',
|
|
357
|
+
name: input.name,
|
|
358
|
+
traceId: input.traceId,
|
|
359
|
+
spanId: input.spanId,
|
|
360
|
+
parentSpanId: input.parentSpanId,
|
|
361
|
+
tags: input.tags,
|
|
362
|
+
attributes: input.attributes
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
buildDroppedEnvelope(droppedCount) {
|
|
366
|
+
return {
|
|
367
|
+
timestamp: Date.now(),
|
|
368
|
+
service: this.service,
|
|
369
|
+
module: this.module,
|
|
370
|
+
environment: this.environment,
|
|
371
|
+
signalType: 'metric',
|
|
372
|
+
name: 'telemetry.queue.dropped',
|
|
373
|
+
value: droppedCount,
|
|
374
|
+
unit: 'count',
|
|
375
|
+
tags: {
|
|
376
|
+
reason: 'queue_backpressure'
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
buildQueueDepthEnvelope(queueDepth) {
|
|
381
|
+
return {
|
|
382
|
+
timestamp: Date.now(),
|
|
383
|
+
service: this.service,
|
|
384
|
+
module: this.module,
|
|
385
|
+
environment: this.environment,
|
|
386
|
+
signalType: 'metric',
|
|
387
|
+
name: 'telemetry.queue.depth',
|
|
388
|
+
value: queueDepth,
|
|
389
|
+
unit: 'count',
|
|
390
|
+
tags: {
|
|
391
|
+
capacity: String(this.maxQueueSize)
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
buildQueueUtilizationEnvelope(queueDepth) {
|
|
396
|
+
return {
|
|
397
|
+
timestamp: Date.now(),
|
|
398
|
+
service: this.service,
|
|
399
|
+
module: this.module,
|
|
400
|
+
environment: this.environment,
|
|
401
|
+
signalType: 'metric',
|
|
402
|
+
name: 'telemetry.queue.utilization',
|
|
403
|
+
value: queueDepth / this.maxQueueSize,
|
|
404
|
+
unit: 'ratio',
|
|
405
|
+
tags: {
|
|
406
|
+
capacity: String(this.maxQueueSize)
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
async emitBatch(batch) {
|
|
411
|
+
const results = await Promise.allSettled(this.exporters.map(async (exporter)=>{
|
|
412
|
+
await exporter.emit(batch);
|
|
413
|
+
return exporter.name;
|
|
414
|
+
}));
|
|
415
|
+
for (const [index, result] of results.entries()){
|
|
416
|
+
const exporterName = this.exporters[index]?.name || `exporter-${index}`;
|
|
417
|
+
if ('rejected' === result.status) {
|
|
418
|
+
this.markExporterFailure(exporterName, result.reason);
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
this.markExporterHealthy(exporterName);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
buildStartupProbeEnvelope() {
|
|
425
|
+
return {
|
|
426
|
+
timestamp: Date.now(),
|
|
427
|
+
service: this.service,
|
|
428
|
+
module: this.module,
|
|
429
|
+
environment: this.environment,
|
|
430
|
+
signalType: 'log',
|
|
431
|
+
name: 'telemetry.exporter.startup_probe',
|
|
432
|
+
level: 'info',
|
|
433
|
+
tags: {
|
|
434
|
+
phase: 'startup'
|
|
435
|
+
},
|
|
436
|
+
attributes: {
|
|
437
|
+
source: 'TelemetryRegistry'
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
async startupHealthCheck(options) {
|
|
442
|
+
if (0 === this.exporters.length) return;
|
|
443
|
+
const probeBatch = [
|
|
444
|
+
this.buildStartupProbeEnvelope()
|
|
445
|
+
];
|
|
446
|
+
const failedExporters = [];
|
|
447
|
+
await Promise.all(this.exporters.map(async (exporter)=>{
|
|
448
|
+
try {
|
|
449
|
+
await exporter.emit(probeBatch);
|
|
450
|
+
this.markExporterHealthy(exporter.name);
|
|
451
|
+
} catch (error) {
|
|
452
|
+
this.markExporterFailure(exporter.name, error);
|
|
453
|
+
const status = this.exporterHealth.get(exporter.name);
|
|
454
|
+
if (status) failedExporters.push({
|
|
455
|
+
...status
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}));
|
|
459
|
+
if ((options?.failLoud ?? true) && failedExporters.length > 0) throw new TelemetryStartupHealthError(failedExporters);
|
|
460
|
+
}
|
|
461
|
+
getExporterHealth() {
|
|
462
|
+
return Array.from(this.exporterHealth.values()).map((item)=>({
|
|
463
|
+
...item
|
|
464
|
+
}));
|
|
465
|
+
}
|
|
466
|
+
getQueueStats() {
|
|
467
|
+
return {
|
|
468
|
+
depth: this.queue.length,
|
|
469
|
+
capacity: this.maxQueueSize,
|
|
470
|
+
utilization: this.queue.length / this.maxQueueSize,
|
|
471
|
+
pendingDropped: this.droppedCount,
|
|
472
|
+
totalDropped: this.totalDroppedCount
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
async flushInternal() {
|
|
476
|
+
const queueDepthBeforeFlush = this.queue.length;
|
|
477
|
+
if (queueDepthBeforeFlush > 0) {
|
|
478
|
+
this.queue.unshift(this.buildQueueUtilizationEnvelope(queueDepthBeforeFlush));
|
|
479
|
+
this.queue.unshift(this.buildQueueDepthEnvelope(queueDepthBeforeFlush));
|
|
480
|
+
}
|
|
481
|
+
if (this.droppedCount > 0) {
|
|
482
|
+
const droppedCount = this.droppedCount;
|
|
483
|
+
this.droppedCount = 0;
|
|
484
|
+
this.queue.unshift(this.buildDroppedEnvelope(droppedCount));
|
|
485
|
+
}
|
|
486
|
+
if (0 === this.queue.length) return;
|
|
487
|
+
if (0 === this.exporters.length) {
|
|
488
|
+
this.queue.length = 0;
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
while(this.queue.length > 0){
|
|
492
|
+
const batch = this.queue.splice(0, this.maxBatchSize);
|
|
493
|
+
await this.emitBatch(batch);
|
|
494
|
+
}
|
|
495
|
+
await Promise.allSettled(this.exporters.map(async (exporter)=>{
|
|
496
|
+
if (exporter.flush) await exporter.flush();
|
|
497
|
+
}));
|
|
498
|
+
}
|
|
499
|
+
flush() {
|
|
500
|
+
if (this.flushing) return this.flushing;
|
|
501
|
+
this.flushing = this.flushInternal().finally(()=>{
|
|
502
|
+
this.flushing = null;
|
|
503
|
+
});
|
|
504
|
+
return this.flushing;
|
|
505
|
+
}
|
|
506
|
+
async shutdown() {
|
|
507
|
+
if (this.flushTimer) clearInterval(this.flushTimer);
|
|
508
|
+
await this.flush();
|
|
509
|
+
await Promise.allSettled(this.exporters.map(async (exporter)=>{
|
|
510
|
+
if (exporter.shutdown) await exporter.shutdown();
|
|
511
|
+
}));
|
|
512
|
+
}
|
|
513
|
+
constructor(options){
|
|
514
|
+
this.exporters = [];
|
|
515
|
+
this.queue = [];
|
|
516
|
+
this.droppedCount = 0;
|
|
517
|
+
this.totalDroppedCount = 0;
|
|
518
|
+
this.flushing = null;
|
|
519
|
+
this.exporterHealth = new Map();
|
|
520
|
+
this.lastSloAlertAt = new Map();
|
|
521
|
+
this.service = options.service;
|
|
522
|
+
this.module = options.module;
|
|
523
|
+
this.environment = options.environment;
|
|
524
|
+
this.samplingRate = clamp(options.samplingRate ?? 1, 0, 1);
|
|
525
|
+
this.maxBatchSize = Math.max(1, options.maxBatchSize ?? 50);
|
|
526
|
+
this.maxQueueSize = Math.max(1, options.maxQueueSize ?? 1000);
|
|
527
|
+
this.flushIntervalMs = Math.max(50, options.flushIntervalMs ?? 1000);
|
|
528
|
+
this.redactionKeys = new Set(options.redactionKeys || []);
|
|
529
|
+
this.queueUtilizationWarnThreshold = clamp(options.slo?.queueUtilizationWarnThreshold ?? 0.8, 0, 1);
|
|
530
|
+
this.queueDroppedWarnThreshold = Math.max(1, options.slo?.queueDroppedWarnThreshold ?? 1);
|
|
531
|
+
this.alertCooldownMs = Math.max(0, options.slo?.alertCooldownMs ?? 60000);
|
|
532
|
+
this.onSloAlert = options.slo?.onAlert;
|
|
533
|
+
this.flushTimer = setInterval(()=>{
|
|
534
|
+
this.flush();
|
|
535
|
+
}, this.flushIntervalMs);
|
|
536
|
+
if ('function' == typeof this.flushTimer.unref) this.flushTimer.unref();
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
class TelemetryCanaryOrchestrator {
|
|
540
|
+
setRequiredContractGates(gates) {
|
|
541
|
+
this.requiredContractGates = Array.from(new Set(gates.map((item)=>item.trim()).filter(Boolean)));
|
|
542
|
+
}
|
|
543
|
+
addRequiredContractGate(name) {
|
|
544
|
+
const normalizedName = name.trim();
|
|
545
|
+
if (!normalizedName) return;
|
|
546
|
+
if (!this.requiredContractGates.includes(normalizedName)) this.requiredContractGates.push(normalizedName);
|
|
547
|
+
}
|
|
548
|
+
setContractGate(name, passed, reason) {
|
|
549
|
+
this.contractGates.set(name, {
|
|
550
|
+
name,
|
|
551
|
+
passed,
|
|
552
|
+
reason,
|
|
553
|
+
updatedAt: Date.now()
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
setContractGates(gates) {
|
|
557
|
+
for (const [name, value] of Object.entries(gates)){
|
|
558
|
+
if ('boolean' == typeof value) {
|
|
559
|
+
this.setContractGate(name, value);
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
this.setContractGate(name, value.passed, value.reason);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
resetToCanary() {
|
|
566
|
+
this.state = 'canary';
|
|
567
|
+
this.consecutiveHealthy = 0;
|
|
568
|
+
this.consecutiveFailures = 0;
|
|
569
|
+
}
|
|
570
|
+
collectFailures() {
|
|
571
|
+
const failures = [];
|
|
572
|
+
const queueStats = this.registry.getQueueStats();
|
|
573
|
+
const unhealthyExporterCount = this.registry.getExporterHealth().filter((item)=>!item.healthy).length;
|
|
574
|
+
if (queueStats.utilization > this.maxQueueUtilization) failures.push({
|
|
575
|
+
reason: 'queue_utilization',
|
|
576
|
+
threshold: this.maxQueueUtilization,
|
|
577
|
+
value: queueStats.utilization
|
|
578
|
+
});
|
|
579
|
+
if (queueStats.totalDropped > this.maxTotalDropped) failures.push({
|
|
580
|
+
reason: 'queue_dropped',
|
|
581
|
+
threshold: this.maxTotalDropped,
|
|
582
|
+
value: queueStats.totalDropped
|
|
583
|
+
});
|
|
584
|
+
if (unhealthyExporterCount > this.maxUnhealthyExporters) failures.push({
|
|
585
|
+
reason: 'unhealthy_exporter',
|
|
586
|
+
threshold: this.maxUnhealthyExporters,
|
|
587
|
+
value: unhealthyExporterCount
|
|
588
|
+
});
|
|
589
|
+
for (const gateName of this.requiredContractGates){
|
|
590
|
+
const gate = this.contractGates.get(gateName);
|
|
591
|
+
if (!gate) {
|
|
592
|
+
failures.push({
|
|
593
|
+
reason: 'contract_gate_missing',
|
|
594
|
+
gate: gateName,
|
|
595
|
+
message: `Contract gate "${gateName}" is missing`
|
|
596
|
+
});
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
if (!gate.passed) failures.push({
|
|
600
|
+
reason: 'contract_gate_failed',
|
|
601
|
+
gate: gateName,
|
|
602
|
+
message: gate.reason || `Contract gate "${gateName}" is not passing`
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
return {
|
|
606
|
+
failures,
|
|
607
|
+
queueStats,
|
|
608
|
+
unhealthyExporterCount
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
evaluate() {
|
|
612
|
+
const now = Date.now();
|
|
613
|
+
const { failures, queueStats, unhealthyExporterCount } = this.collectFailures();
|
|
614
|
+
let action = 'hold';
|
|
615
|
+
if (failures.length > 0) {
|
|
616
|
+
this.consecutiveHealthy = 0;
|
|
617
|
+
this.consecutiveFailures += 1;
|
|
618
|
+
if ('rolled_back' !== this.state && this.consecutiveFailures >= this.rollbackConsecutiveFailures) {
|
|
619
|
+
this.state = 'rolled_back';
|
|
620
|
+
action = 'rollback';
|
|
621
|
+
}
|
|
622
|
+
} else {
|
|
623
|
+
this.consecutiveFailures = 0;
|
|
624
|
+
this.consecutiveHealthy += 1;
|
|
625
|
+
if ('canary' === this.state && this.consecutiveHealthy >= this.minConsecutiveHealthyEvaluations) {
|
|
626
|
+
this.state = 'promoted';
|
|
627
|
+
action = 'promote';
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
const decision = {
|
|
631
|
+
timestamp: now,
|
|
632
|
+
action,
|
|
633
|
+
state: this.state,
|
|
634
|
+
consecutiveHealthy: this.consecutiveHealthy,
|
|
635
|
+
consecutiveFailures: this.consecutiveFailures,
|
|
636
|
+
failures,
|
|
637
|
+
queueStats,
|
|
638
|
+
unhealthyExporterCount,
|
|
639
|
+
contractGates: Array.from(this.contractGates.values()).map((item)=>({
|
|
640
|
+
...item
|
|
641
|
+
}))
|
|
642
|
+
};
|
|
643
|
+
try {
|
|
644
|
+
this.onEvaluate?.(decision);
|
|
645
|
+
} catch (_error) {}
|
|
646
|
+
if ('promote' === action) try {
|
|
647
|
+
this.onPromote?.(decision);
|
|
648
|
+
} catch (_error) {}
|
|
649
|
+
if ('rollback' === action) try {
|
|
650
|
+
this.onRollback?.(decision);
|
|
651
|
+
} catch (_error) {}
|
|
652
|
+
return decision;
|
|
653
|
+
}
|
|
654
|
+
getStatusSnapshot() {
|
|
655
|
+
const now = Date.now();
|
|
656
|
+
const { failures, queueStats, unhealthyExporterCount } = this.collectFailures();
|
|
657
|
+
return {
|
|
658
|
+
timestamp: now,
|
|
659
|
+
state: this.state,
|
|
660
|
+
consecutiveHealthy: this.consecutiveHealthy,
|
|
661
|
+
consecutiveFailures: this.consecutiveFailures,
|
|
662
|
+
queueStats,
|
|
663
|
+
unhealthyExporterCount,
|
|
664
|
+
requiredContractGates: [
|
|
665
|
+
...this.requiredContractGates
|
|
666
|
+
],
|
|
667
|
+
contractGates: Array.from(this.contractGates.values()).map((item)=>({
|
|
668
|
+
...item
|
|
669
|
+
})),
|
|
670
|
+
failurePreview: failures
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
start() {
|
|
674
|
+
if (this.evaluationTimer) return;
|
|
675
|
+
this.evaluationTimer = setInterval(()=>{
|
|
676
|
+
this.evaluate();
|
|
677
|
+
}, this.evaluationIntervalMs);
|
|
678
|
+
if ('function' == typeof this.evaluationTimer.unref) this.evaluationTimer.unref();
|
|
679
|
+
}
|
|
680
|
+
stop() {
|
|
681
|
+
if (this.evaluationTimer) {
|
|
682
|
+
clearInterval(this.evaluationTimer);
|
|
683
|
+
this.evaluationTimer = void 0;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
constructor(options){
|
|
687
|
+
this.requiredContractGates = [];
|
|
688
|
+
this.contractGates = new Map();
|
|
689
|
+
this.state = 'canary';
|
|
690
|
+
this.consecutiveHealthy = 0;
|
|
691
|
+
this.consecutiveFailures = 0;
|
|
692
|
+
this.registry = options.registry;
|
|
693
|
+
this.evaluationIntervalMs = Math.max(250, options.evaluationIntervalMs ?? 15000);
|
|
694
|
+
this.minConsecutiveHealthyEvaluations = Math.max(1, options.minConsecutiveHealthyEvaluations ?? 3);
|
|
695
|
+
this.rollbackConsecutiveFailures = Math.max(1, options.rollbackConsecutiveFailures ?? 2);
|
|
696
|
+
this.maxQueueUtilization = clamp(options.maxQueueUtilization ?? 0.8, 0, 1);
|
|
697
|
+
this.maxTotalDropped = Math.max(0, options.maxTotalDropped ?? 0);
|
|
698
|
+
this.maxUnhealthyExporters = Math.max(0, options.maxUnhealthyExporters ?? 0);
|
|
699
|
+
this.setRequiredContractGates(options.requiredContractGates || []);
|
|
700
|
+
this.onEvaluate = options.onEvaluate;
|
|
701
|
+
this.onPromote = options.onPromote;
|
|
702
|
+
this.onRollback = options.onRollback;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
function normalizeMetricsInput(prefixOrTags, tags) {
|
|
706
|
+
if ('string' == typeof prefixOrTags) return {
|
|
707
|
+
prefix: prefixOrTags,
|
|
708
|
+
tags: tags || {}
|
|
709
|
+
};
|
|
710
|
+
if (isRecord(prefixOrTags)) return {
|
|
711
|
+
prefix: void 0,
|
|
712
|
+
tags: prefixOrTags
|
|
713
|
+
};
|
|
714
|
+
return {
|
|
715
|
+
prefix: void 0,
|
|
716
|
+
tags: tags || {}
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
function normalizeMetricName(name, prefix) {
|
|
720
|
+
return prefix && prefix.length > 0 ? `${prefix}.${name}` : name;
|
|
721
|
+
}
|
|
722
|
+
function toTelemetryMetricTags(tags) {
|
|
723
|
+
const output = {};
|
|
724
|
+
for (const [key, value] of Object.entries(tags))if (null != value) output[key] = String(value);
|
|
725
|
+
return output;
|
|
726
|
+
}
|
|
727
|
+
function getTraceContext(tags) {
|
|
728
|
+
const traceId = 'string' == typeof tags.trace_id ? tags.trace_id : 'string' == typeof tags.traceId ? tags.traceId : void 0;
|
|
729
|
+
const spanId = 'string' == typeof tags.span_id ? tags.span_id : 'string' == typeof tags.spanId ? tags.spanId : void 0;
|
|
730
|
+
const parentSpanId = 'string' == typeof tags.parent_span_id ? tags.parent_span_id : 'string' == typeof tags.parentSpanId ? tags.parentSpanId : void 0;
|
|
731
|
+
return {
|
|
732
|
+
traceId,
|
|
733
|
+
spanId,
|
|
734
|
+
parentSpanId
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
const createTelemetryAwareMetrics = (baseMetrics, registry)=>{
|
|
738
|
+
const emitCounter = (name, value, prefixOrTags, tags)=>{
|
|
739
|
+
const normalized = normalizeMetricsInput(prefixOrTags, tags);
|
|
740
|
+
baseMetrics.emitCounter(name, value, normalized.prefix, normalized.tags);
|
|
741
|
+
try {
|
|
742
|
+
const metricName = normalizeMetricName(name, normalized.prefix);
|
|
743
|
+
const traceContext = getTraceContext(normalized.tags);
|
|
744
|
+
registry.enqueueMetric({
|
|
745
|
+
name: metricName,
|
|
746
|
+
value,
|
|
747
|
+
unit: 'count',
|
|
748
|
+
traceId: traceContext.traceId,
|
|
749
|
+
spanId: traceContext.spanId,
|
|
750
|
+
parentSpanId: traceContext.parentSpanId,
|
|
751
|
+
tags: toTelemetryMetricTags(normalized.tags),
|
|
752
|
+
attributes: normalized.tags
|
|
753
|
+
});
|
|
754
|
+
} catch (_error) {}
|
|
755
|
+
};
|
|
756
|
+
const emitTimer = (name, value, prefixOrTags, tags)=>{
|
|
757
|
+
const normalized = normalizeMetricsInput(prefixOrTags, tags);
|
|
758
|
+
baseMetrics.emitTimer(name, value, normalized.prefix, normalized.tags);
|
|
759
|
+
try {
|
|
760
|
+
const metricName = normalizeMetricName(name, normalized.prefix);
|
|
761
|
+
const traceContext = getTraceContext(normalized.tags);
|
|
762
|
+
registry.enqueueMetric({
|
|
763
|
+
name: metricName,
|
|
764
|
+
value,
|
|
765
|
+
unit: 'ms',
|
|
766
|
+
traceId: traceContext.traceId,
|
|
767
|
+
spanId: traceContext.spanId,
|
|
768
|
+
parentSpanId: traceContext.parentSpanId,
|
|
769
|
+
tags: toTelemetryMetricTags(normalized.tags),
|
|
770
|
+
attributes: normalized.tags
|
|
771
|
+
});
|
|
772
|
+
} catch (_error) {}
|
|
773
|
+
};
|
|
774
|
+
return {
|
|
775
|
+
...baseMetrics,
|
|
776
|
+
emitCounter,
|
|
777
|
+
emitTimer
|
|
778
|
+
};
|
|
779
|
+
};
|
|
780
|
+
function resolveRuntimeFallbackSignalEndpoint(configuredEndpoint) {
|
|
781
|
+
const rawEndpoint = configuredEndpoint?.trim();
|
|
782
|
+
if (!rawEndpoint) return DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT;
|
|
783
|
+
if (rawEndpoint.startsWith('/')) return rawEndpoint;
|
|
784
|
+
try {
|
|
785
|
+
return new URL(rawEndpoint).pathname || DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT;
|
|
786
|
+
} catch (_error) {
|
|
787
|
+
return `/${rawEndpoint.replace(/^\/+/, '')}`;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
function createRuntimeSignalError(message, code) {
|
|
791
|
+
const error = new Error(message);
|
|
792
|
+
error.code = code;
|
|
793
|
+
return error;
|
|
794
|
+
}
|
|
795
|
+
function getUtf8ByteLength(input) {
|
|
796
|
+
if ("u" > typeof Buffer) return Buffer.byteLength(input);
|
|
797
|
+
return new TextEncoder().encode(input).length;
|
|
798
|
+
}
|
|
799
|
+
function normalizeRuntimeSignalOrigin(value) {
|
|
800
|
+
if ('string' != typeof value || 0 === value.trim().length) return;
|
|
801
|
+
try {
|
|
802
|
+
return new URL(value).origin;
|
|
803
|
+
} catch (_error) {
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
function normalizeRuntimeSignalAppName(payload) {
|
|
808
|
+
if ('string' != typeof payload.appName) return 'unknown';
|
|
809
|
+
const normalized = payload.appName.trim();
|
|
810
|
+
return normalized.length > 0 ? normalized : 'unknown';
|
|
811
|
+
}
|
|
812
|
+
function normalizeRuntimeSignalRuntimeDigest(payload) {
|
|
813
|
+
if ('string' == typeof payload.runtimeDigest && payload.runtimeDigest.trim()) return payload.runtimeDigest.trim();
|
|
814
|
+
const metadata = payload.metadata;
|
|
815
|
+
if (metadata && 'object' == typeof metadata && !Array.isArray(metadata) && 'string' == typeof metadata.runtimeDigest) {
|
|
816
|
+
const digest = String(metadata.runtimeDigest).trim();
|
|
817
|
+
if (digest) return digest;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
function normalizeRuntimeFallbackSignalAuthConfig(configured) {
|
|
821
|
+
const headerName = 'string' == typeof configured?.headerName && configured.headerName.trim() ? configured.headerName.trim().toLowerCase() : DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER;
|
|
822
|
+
const expectedFromEnv = 'string' == typeof configured?.expectedValueEnv && configured.expectedValueEnv.trim().length > 0 ? process.env[configured.expectedValueEnv.trim()] : void 0;
|
|
823
|
+
const expectedFromConfig = 'string' == typeof configured?.expectedValue && configured.expectedValue.trim().length > 0 ? configured.expectedValue.trim() : void 0;
|
|
824
|
+
const expectedValue = expectedFromConfig || expectedFromEnv;
|
|
825
|
+
const enabled = configured?.enabled === true;
|
|
826
|
+
if (enabled && !expectedValue) throw new Error('[telemetry.canary.autopilot.runtimeFallbackSignal] auth.enabled is true but no expected token is configured');
|
|
827
|
+
return {
|
|
828
|
+
enabled,
|
|
829
|
+
headerName,
|
|
830
|
+
expectedValue
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
function enforceRuntimeFallbackSignalAuthToken(token, authConfig) {
|
|
834
|
+
if (!authConfig.enabled) return;
|
|
835
|
+
if (!token || token !== authConfig.expectedValue) throw createRuntimeSignalError('runtime fallback signal auth failed', 'UNAUTHORIZED');
|
|
836
|
+
}
|
|
837
|
+
function enforceRuntimeFallbackSignalAuth(c, runtimeSignalConfig) {
|
|
838
|
+
enforceRuntimeFallbackSignalAuthToken(c.req.header(runtimeSignalConfig.auth.headerName), runtimeSignalConfig.auth);
|
|
839
|
+
}
|
|
840
|
+
function normalizeRuntimeFallbackTrustPolicy(configured) {
|
|
841
|
+
const allowedApps = Array.isArray(configured?.allowedApps) ? configured.allowedApps.map((item)=>'string' == typeof item ? item.trim() : '').filter(Boolean) : [];
|
|
842
|
+
const allowedEntryOrigins = Array.isArray(configured?.allowedEntryOrigins) ? configured.allowedEntryOrigins.map((item)=>normalizeRuntimeSignalOrigin(item)).filter((item)=>Boolean(item)) : [];
|
|
843
|
+
const expectedRuntimeDigestsRaw = configured?.expectedRuntimeDigests || {};
|
|
844
|
+
const expectedRuntimeDigests = {};
|
|
845
|
+
Object.entries(expectedRuntimeDigestsRaw).forEach(([appName, digest])=>{
|
|
846
|
+
if ('string' == typeof appName && appName.trim().length > 0 && 'string' == typeof digest && digest.trim().length > 0) expectedRuntimeDigests[appName.trim()] = digest.trim();
|
|
847
|
+
});
|
|
848
|
+
return {
|
|
849
|
+
allowedApps,
|
|
850
|
+
allowedEntryOrigins,
|
|
851
|
+
expectedRuntimeDigests,
|
|
852
|
+
enforceRuntimeDigest: configured?.enforceRuntimeDigest === true,
|
|
853
|
+
maxSignalsPerWindow: Math.max(1, Math.floor(configured?.maxSignalsPerWindow ?? DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW)),
|
|
854
|
+
windowMs: Math.max(1000, Math.floor(configured?.windowMs ?? DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS)),
|
|
855
|
+
dedupeWindowMs: Math.max(0, Math.floor(configured?.dedupeWindowMs ?? DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS))
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
function createRuntimeFallbackSignalRuntimeState() {
|
|
859
|
+
return {
|
|
860
|
+
rateLimitBySource: new Map(),
|
|
861
|
+
dedupeByFingerprint: new Map()
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
function cleanupRuntimeFallbackSignalRuntimeState(now, runtimeState, trustPolicy) {
|
|
865
|
+
const dedupeExpiryMs = Math.max(trustPolicy.dedupeWindowMs, trustPolicy.windowMs, 1000);
|
|
866
|
+
runtimeState.dedupeByFingerprint.forEach((lastSeenAt, fingerprint)=>{
|
|
867
|
+
if (now - lastSeenAt > dedupeExpiryMs) runtimeState.dedupeByFingerprint.delete(fingerprint);
|
|
868
|
+
});
|
|
869
|
+
runtimeState.rateLimitBySource.forEach((state, source)=>{
|
|
870
|
+
if (now - state.windowStartedAt > 2 * trustPolicy.windowMs) runtimeState.rateLimitBySource.delete(source);
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
function enforceRuntimeFallbackSignalTrustPolicy(payload, runtimeSignalContext) {
|
|
874
|
+
const { trustPolicy, runtimeState } = runtimeSignalContext;
|
|
875
|
+
const now = Date.now();
|
|
876
|
+
cleanupRuntimeFallbackSignalRuntimeState(now, runtimeState, trustPolicy);
|
|
877
|
+
const appName = normalizeRuntimeSignalAppName(payload);
|
|
878
|
+
const entryOrigin = normalizeRuntimeSignalOrigin(payload.entry);
|
|
879
|
+
const runtimeDigest = normalizeRuntimeSignalRuntimeDigest(payload);
|
|
880
|
+
if (trustPolicy.allowedApps.length > 0 && !trustPolicy.allowedApps.includes(appName)) throw createRuntimeSignalError(`runtime fallback signal app "${appName}" is not trusted`, 'UNTRUSTED_SOURCE');
|
|
881
|
+
if (trustPolicy.allowedEntryOrigins.length > 0) {
|
|
882
|
+
if (!entryOrigin || !trustPolicy.allowedEntryOrigins.includes(entryOrigin)) throw createRuntimeSignalError(`runtime fallback signal entry origin "${entryOrigin || 'unknown'}" is not trusted`, 'UNTRUSTED_SOURCE');
|
|
883
|
+
}
|
|
884
|
+
const expectedDigest = trustPolicy.expectedRuntimeDigests[appName];
|
|
885
|
+
if (expectedDigest && runtimeDigest !== expectedDigest) throw createRuntimeSignalError(`runtime fallback runtimeDigest mismatch for app "${appName}"`, 'UNTRUSTED_SOURCE');
|
|
886
|
+
if (trustPolicy.enforceRuntimeDigest && !runtimeDigest) throw createRuntimeSignalError(`runtime fallback signal for app "${appName}" is missing runtimeDigest`, 'UNTRUSTED_SOURCE');
|
|
887
|
+
const dedupeFingerprint = JSON.stringify({
|
|
888
|
+
appName,
|
|
889
|
+
entryOrigin: entryOrigin || 'unknown',
|
|
890
|
+
reason: payload.reason || 'runtime_fallback',
|
|
891
|
+
phase: payload.phase || 'unknown',
|
|
892
|
+
runtimeDigest: runtimeDigest || 'unknown'
|
|
893
|
+
});
|
|
894
|
+
const dedupeWindowMs = trustPolicy.dedupeWindowMs;
|
|
895
|
+
if (dedupeWindowMs > 0) {
|
|
896
|
+
const lastSeenAt = runtimeState.dedupeByFingerprint.get(dedupeFingerprint);
|
|
897
|
+
runtimeState.dedupeByFingerprint.set(dedupeFingerprint, now);
|
|
898
|
+
if ('number' == typeof lastSeenAt && now - lastSeenAt <= dedupeWindowMs) return {
|
|
899
|
+
deduped: true
|
|
900
|
+
};
|
|
901
|
+
} else runtimeState.dedupeByFingerprint.set(dedupeFingerprint, now);
|
|
902
|
+
const sourceKey = `${appName}@${entryOrigin || 'unknown'}`;
|
|
903
|
+
const rateState = runtimeState.rateLimitBySource.get(sourceKey);
|
|
904
|
+
if (!rateState || now - rateState.windowStartedAt > trustPolicy.windowMs) runtimeState.rateLimitBySource.set(sourceKey, {
|
|
905
|
+
count: 1,
|
|
906
|
+
windowStartedAt: now
|
|
907
|
+
});
|
|
908
|
+
else {
|
|
909
|
+
if (rateState.count >= trustPolicy.maxSignalsPerWindow) throw createRuntimeSignalError(`runtime fallback signal rate-limited for source "${sourceKey}"`, 'RATE_LIMITED');
|
|
910
|
+
rateState.count += 1;
|
|
911
|
+
}
|
|
912
|
+
return {
|
|
913
|
+
deduped: false
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
function maybeWarnLegacyOtlpEndpoint(endpoint) {
|
|
917
|
+
if (!endpoint || !endpoint.includes('/v1/metrics')) return;
|
|
918
|
+
console.warn(`[telemetry] OTLP endpoint "${endpoint}" looks like a metrics path. UltraModern telemetry exporter expects log-style envelopes (default: ${DEFAULT_OTLP_ENDPOINT}).`);
|
|
919
|
+
}
|
|
920
|
+
async function parseRuntimeFallbackSignalPayload(c, maxBodyBytes) {
|
|
921
|
+
const contentLengthHeader = c.req.header('content-length');
|
|
922
|
+
if (contentLengthHeader) {
|
|
923
|
+
const contentLength = Number.parseInt(contentLengthHeader, 10);
|
|
924
|
+
if (Number.isFinite(contentLength) && contentLength > maxBodyBytes) throw createRuntimeSignalError('runtime fallback signal payload too large', 'PAYLOAD_TOO_LARGE');
|
|
925
|
+
}
|
|
926
|
+
const rawBody = await c.req.raw.text();
|
|
927
|
+
const payload = parseRuntimeFallbackSignalPayloadFromRawBody(rawBody, maxBodyBytes);
|
|
928
|
+
return {
|
|
929
|
+
rawBody,
|
|
930
|
+
payload
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
function parseRuntimeFallbackSignalPayloadFromRawBody(rawBody, maxBodyBytes) {
|
|
934
|
+
if (!rawBody || 0 === rawBody.trim().length) throw createRuntimeSignalError('runtime fallback signal body is empty', 'INVALID_PAYLOAD');
|
|
935
|
+
if (getUtf8ByteLength(rawBody) > maxBodyBytes) throw createRuntimeSignalError('runtime fallback signal payload too large', 'PAYLOAD_TOO_LARGE');
|
|
936
|
+
let payload;
|
|
937
|
+
try {
|
|
938
|
+
payload = JSON.parse(rawBody);
|
|
939
|
+
} catch (_error) {
|
|
940
|
+
throw createRuntimeSignalError('runtime fallback signal body must be valid JSON', 'INVALID_PAYLOAD');
|
|
941
|
+
}
|
|
942
|
+
if (!payload || 'object' != typeof payload || Array.isArray(payload)) throw createRuntimeSignalError('runtime fallback signal body must be a JSON object', 'INVALID_PAYLOAD');
|
|
943
|
+
return payload;
|
|
944
|
+
}
|
|
945
|
+
function getRuntimeSignalErrorStatusCode(signalError) {
|
|
946
|
+
if ('PAYLOAD_TOO_LARGE' === signalError.code) return 413;
|
|
947
|
+
if ('INVALID_PAYLOAD' === signalError.code) return 400;
|
|
948
|
+
if ('UNAUTHORIZED' === signalError.code) return 401;
|
|
949
|
+
if ('RATE_LIMITED' === signalError.code) return 429;
|
|
950
|
+
if ('UNTRUSTED_SOURCE' === signalError.code) return 403;
|
|
951
|
+
return 500;
|
|
952
|
+
}
|
|
953
|
+
async function persistRuntimeFallbackContractGate(payload, runtimeSignalConfig) {
|
|
954
|
+
const now = Date.now();
|
|
955
|
+
const gateSnapshotStore = await runtimeSignalConfig.gateSnapshotStore;
|
|
956
|
+
const snapshot = await gateSnapshotStore.readSnapshot() || {};
|
|
957
|
+
const existingGates = snapshot.gates && 'object' == typeof snapshot.gates ? snapshot.gates : {};
|
|
958
|
+
const reason = 'string' == typeof payload.reason ? payload.reason : 'runtime_fallback';
|
|
959
|
+
const phase = 'string' == typeof payload.phase ? payload.phase : 'unknown';
|
|
960
|
+
const appName = 'string' == typeof payload.appName ? payload.appName : 'unknown';
|
|
961
|
+
const entry = 'string' == typeof payload.entry ? payload.entry : void 0;
|
|
962
|
+
snapshot.schemaVersion = 'number' == typeof snapshot.schemaVersion ? snapshot.schemaVersion : CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION;
|
|
963
|
+
snapshot.updatedAt = now;
|
|
964
|
+
snapshot.gates = {
|
|
965
|
+
...existingGates,
|
|
966
|
+
[runtimeSignalConfig.gateName]: {
|
|
967
|
+
passed: false,
|
|
968
|
+
reason: `runtime_fallback:${reason} phase=${phase} app=${appName}${entry ? ` entry=${entry}` : ''}`,
|
|
969
|
+
updatedAt: now,
|
|
970
|
+
expiresAt: now + runtimeSignalConfig.failureHoldMs,
|
|
971
|
+
source: 'runtime-mf-fallback-signal',
|
|
972
|
+
metadata: payload
|
|
973
|
+
}
|
|
974
|
+
};
|
|
975
|
+
await gateSnapshotStore.writeSnapshot(snapshot);
|
|
976
|
+
}
|
|
977
|
+
function emitCanaryDecisionMetric(registry, decision, action) {
|
|
978
|
+
try {
|
|
979
|
+
registry.enqueueMetric({
|
|
980
|
+
name: `telemetry.canary.${action}`,
|
|
981
|
+
value: 1,
|
|
982
|
+
unit: 'count',
|
|
983
|
+
tags: {
|
|
984
|
+
action,
|
|
985
|
+
state: decision.state,
|
|
986
|
+
failures: String(decision.failures.length)
|
|
987
|
+
}
|
|
988
|
+
});
|
|
989
|
+
} catch (_error) {}
|
|
990
|
+
}
|
|
991
|
+
const hasEnabledTelemetryExporters = (config)=>Boolean(config?.exporters?.otlp?.enabled || config?.exporters?.victoriaMetrics?.enabled);
|
|
992
|
+
const injectTelemetryPlugin = ()=>({
|
|
993
|
+
name: '@modern-js/inject-telemetry',
|
|
994
|
+
setup (api) {
|
|
995
|
+
const serverConfig = api.getServerConfig();
|
|
996
|
+
const telemetryConfig = serverConfig?.server?.telemetry;
|
|
997
|
+
if (!telemetryConfig) return;
|
|
998
|
+
if (true !== telemetryConfig.enabled && !hasEnabledTelemetryExporters(telemetryConfig)) return;
|
|
999
|
+
const { middlewares, metaName, appDirectory } = api.getServerContext();
|
|
1000
|
+
const serviceName = telemetryConfig.service || metaName || 'modern-js';
|
|
1001
|
+
const moduleName = telemetryConfig.module || 'server';
|
|
1002
|
+
const environmentName = telemetryConfig.environment || process.env.MODERN_ENV || process.env.NODE_ENV || 'development';
|
|
1003
|
+
const registry = new TelemetryRegistry({
|
|
1004
|
+
service: serviceName,
|
|
1005
|
+
module: moduleName,
|
|
1006
|
+
environment: environmentName,
|
|
1007
|
+
samplingRate: telemetryConfig.samplingRate,
|
|
1008
|
+
flushIntervalMs: telemetryConfig.flushIntervalMs,
|
|
1009
|
+
maxBatchSize: telemetryConfig.maxBatchSize,
|
|
1010
|
+
maxQueueSize: telemetryConfig.maxQueueSize,
|
|
1011
|
+
redactionKeys: telemetryConfig.redactionKeys
|
|
1012
|
+
});
|
|
1013
|
+
let canaryOrchestrator;
|
|
1014
|
+
let contractGateAutopilot;
|
|
1015
|
+
let runtimeFallbackSignalConfig;
|
|
1016
|
+
let gateSnapshotStorePromise;
|
|
1017
|
+
const canaryConfig = telemetryConfig.canary;
|
|
1018
|
+
if (canaryConfig?.enabled) {
|
|
1019
|
+
const contractGates = canaryConfig.contractGates;
|
|
1020
|
+
canaryOrchestrator = new TelemetryCanaryOrchestrator({
|
|
1021
|
+
registry,
|
|
1022
|
+
evaluationIntervalMs: canaryConfig.evaluationIntervalMs,
|
|
1023
|
+
minConsecutiveHealthyEvaluations: canaryConfig.minConsecutiveHealthyEvaluations,
|
|
1024
|
+
rollbackConsecutiveFailures: canaryConfig.rollbackConsecutiveFailures,
|
|
1025
|
+
maxQueueUtilization: canaryConfig.maxQueueUtilization,
|
|
1026
|
+
maxTotalDropped: canaryConfig.maxTotalDropped,
|
|
1027
|
+
maxUnhealthyExporters: canaryConfig.maxUnhealthyExporters,
|
|
1028
|
+
requiredContractGates: Object.keys(contractGates || {}),
|
|
1029
|
+
onPromote: (decision)=>{
|
|
1030
|
+
emitCanaryDecisionMetric(registry, decision, 'promote');
|
|
1031
|
+
},
|
|
1032
|
+
onRollback: (decision)=>{
|
|
1033
|
+
emitCanaryDecisionMetric(registry, decision, 'rollback');
|
|
1034
|
+
}
|
|
1035
|
+
});
|
|
1036
|
+
if (contractGates) canaryOrchestrator.setContractGates(contractGates);
|
|
1037
|
+
const autopilotEnabled = canaryConfig.autopilot?.enabled ?? true;
|
|
1038
|
+
if (autopilotEnabled) {
|
|
1039
|
+
const gateSnapshotPath = resolveContractGateSnapshotPath(appDirectory, canaryConfig.autopilot?.gateSnapshotPath);
|
|
1040
|
+
gateSnapshotStorePromise = resolveContractGateSnapshotStore({
|
|
1041
|
+
appDirectory,
|
|
1042
|
+
gateSnapshotPath: gateSnapshotPath || DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH,
|
|
1043
|
+
stateStore: canaryConfig.autopilot?.stateStore
|
|
1044
|
+
});
|
|
1045
|
+
const runtimeSignalConfig = canaryConfig.autopilot?.runtimeFallbackSignal;
|
|
1046
|
+
const runtimeSignalEnabled = runtimeSignalConfig?.enabled ?? true;
|
|
1047
|
+
if (runtimeSignalEnabled && gateSnapshotStorePromise) runtimeFallbackSignalConfig = {
|
|
1048
|
+
endpoint: resolveRuntimeFallbackSignalEndpoint(runtimeSignalConfig?.endpoint),
|
|
1049
|
+
gateName: runtimeSignalConfig?.gateName?.trim() || DEFAULT_RUNTIME_FALLBACK_GATE_NAME,
|
|
1050
|
+
gateSnapshotStore: gateSnapshotStorePromise,
|
|
1051
|
+
failureHoldMs: Math.max(1000, runtimeSignalConfig?.failureHoldMs ?? DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS),
|
|
1052
|
+
maxBodyBytes: Math.max(512, runtimeSignalConfig?.maxBodyBytes ?? DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES),
|
|
1053
|
+
auth: normalizeRuntimeFallbackSignalAuthConfig(runtimeSignalConfig?.auth),
|
|
1054
|
+
trustPolicy: normalizeRuntimeFallbackTrustPolicy(runtimeSignalConfig?.trustPolicy),
|
|
1055
|
+
runtimeState: createRuntimeFallbackSignalRuntimeState()
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
if (runtimeFallbackSignalConfig) {
|
|
1060
|
+
const signalConfig = runtimeFallbackSignalConfig;
|
|
1061
|
+
middlewares.push({
|
|
1062
|
+
name: 'telemetry-runtime-fallback-signal',
|
|
1063
|
+
path: signalConfig.endpoint,
|
|
1064
|
+
method: 'post',
|
|
1065
|
+
order: 'pre',
|
|
1066
|
+
handler: async (c)=>{
|
|
1067
|
+
try {
|
|
1068
|
+
enforceRuntimeFallbackSignalAuth(c, signalConfig);
|
|
1069
|
+
const { payload } = await parseRuntimeFallbackSignalPayload(c, signalConfig.maxBodyBytes);
|
|
1070
|
+
const trustResult = enforceRuntimeFallbackSignalTrustPolicy(payload, signalConfig);
|
|
1071
|
+
if (trustResult.deduped) return c.json({
|
|
1072
|
+
ok: true,
|
|
1073
|
+
deduped: true
|
|
1074
|
+
}, 202);
|
|
1075
|
+
await persistRuntimeFallbackContractGate(payload, signalConfig);
|
|
1076
|
+
return c.json({
|
|
1077
|
+
ok: true
|
|
1078
|
+
}, 202);
|
|
1079
|
+
} catch (error) {
|
|
1080
|
+
const signalError = error;
|
|
1081
|
+
const status = getRuntimeSignalErrorStatusCode(signalError);
|
|
1082
|
+
return c.json({
|
|
1083
|
+
ok: false,
|
|
1084
|
+
error: signalError instanceof Error ? signalError.message : String(signalError)
|
|
1085
|
+
}, status);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
middlewares.push({
|
|
1091
|
+
name: 'telemetry-runtime-status',
|
|
1092
|
+
path: DEFAULT_RUNTIME_STATUS_ENDPOINT,
|
|
1093
|
+
method: 'get',
|
|
1094
|
+
order: 'pre',
|
|
1095
|
+
handler: async (c)=>{
|
|
1096
|
+
try {
|
|
1097
|
+
if (runtimeFallbackSignalConfig?.auth.enabled) enforceRuntimeFallbackSignalAuthToken(c.req.header(runtimeFallbackSignalConfig.auth.headerName), runtimeFallbackSignalConfig.auth);
|
|
1098
|
+
return c.json({
|
|
1099
|
+
ok: true,
|
|
1100
|
+
timestamp: Date.now(),
|
|
1101
|
+
telemetry: {
|
|
1102
|
+
queueStats: registry.getQueueStats(),
|
|
1103
|
+
exporterHealth: registry.getExporterHealth()
|
|
1104
|
+
},
|
|
1105
|
+
canary: canaryOrchestrator ? {
|
|
1106
|
+
enabled: true,
|
|
1107
|
+
...canaryOrchestrator.getStatusSnapshot()
|
|
1108
|
+
} : {
|
|
1109
|
+
enabled: false
|
|
1110
|
+
},
|
|
1111
|
+
runtimeFallbackSignal: runtimeFallbackSignalConfig ? {
|
|
1112
|
+
enabled: true,
|
|
1113
|
+
endpoint: runtimeFallbackSignalConfig.endpoint,
|
|
1114
|
+
gateName: runtimeFallbackSignalConfig.gateName,
|
|
1115
|
+
failureHoldMs: runtimeFallbackSignalConfig.failureHoldMs,
|
|
1116
|
+
maxBodyBytes: runtimeFallbackSignalConfig.maxBodyBytes,
|
|
1117
|
+
auth: {
|
|
1118
|
+
enabled: runtimeFallbackSignalConfig.auth.enabled,
|
|
1119
|
+
headerName: runtimeFallbackSignalConfig.auth.headerName
|
|
1120
|
+
},
|
|
1121
|
+
trustPolicy: {
|
|
1122
|
+
allowedApps: runtimeFallbackSignalConfig.trustPolicy.allowedApps,
|
|
1123
|
+
allowedEntryOrigins: runtimeFallbackSignalConfig.trustPolicy.allowedEntryOrigins,
|
|
1124
|
+
enforceRuntimeDigest: runtimeFallbackSignalConfig.trustPolicy.enforceRuntimeDigest,
|
|
1125
|
+
expectedRuntimeDigestsCount: Object.keys(runtimeFallbackSignalConfig.trustPolicy.expectedRuntimeDigests).length,
|
|
1126
|
+
maxSignalsPerWindow: runtimeFallbackSignalConfig.trustPolicy.maxSignalsPerWindow,
|
|
1127
|
+
windowMs: runtimeFallbackSignalConfig.trustPolicy.windowMs,
|
|
1128
|
+
dedupeWindowMs: runtimeFallbackSignalConfig.trustPolicy.dedupeWindowMs
|
|
1129
|
+
}
|
|
1130
|
+
} : {
|
|
1131
|
+
enabled: false
|
|
1132
|
+
}
|
|
1133
|
+
});
|
|
1134
|
+
} catch (error) {
|
|
1135
|
+
const signalError = error;
|
|
1136
|
+
return c.json({
|
|
1137
|
+
ok: false,
|
|
1138
|
+
error: signalError instanceof Error ? signalError.message : String(signalError)
|
|
1139
|
+
}, getRuntimeSignalErrorStatusCode(signalError));
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
});
|
|
1143
|
+
middlewares.push({
|
|
1144
|
+
name: 'inject-telemetry',
|
|
1145
|
+
handler: async (c, next)=>{
|
|
1146
|
+
const monitors = c.get('monitors');
|
|
1147
|
+
if (monitors) {
|
|
1148
|
+
const traceContext = parseTraceparent(c.req.header('traceparent'));
|
|
1149
|
+
const monitor = (event)=>{
|
|
1150
|
+
registry.enqueue(toTelemetryEnvelope(event, {
|
|
1151
|
+
service: serviceName,
|
|
1152
|
+
module: moduleName,
|
|
1153
|
+
environment: environmentName,
|
|
1154
|
+
traceId: traceContext?.traceId,
|
|
1155
|
+
spanId: traceContext?.spanId,
|
|
1156
|
+
attributes: {
|
|
1157
|
+
requestMethod: c.req.method,
|
|
1158
|
+
requestPath: c.req.path
|
|
1159
|
+
}
|
|
1160
|
+
}));
|
|
1161
|
+
};
|
|
1162
|
+
monitors.push(monitor);
|
|
1163
|
+
}
|
|
1164
|
+
await next();
|
|
1165
|
+
}
|
|
1166
|
+
});
|
|
1167
|
+
let prepared = false;
|
|
1168
|
+
api.onPrepare(async ()=>{
|
|
1169
|
+
if (prepared) return;
|
|
1170
|
+
prepared = true;
|
|
1171
|
+
if (telemetryConfig.exporters?.otlp?.enabled) {
|
|
1172
|
+
maybeWarnLegacyOtlpEndpoint(telemetryConfig.exporters.otlp.endpoint);
|
|
1173
|
+
await registry.register(createOtlpTelemetryExporter(telemetryConfig.exporters.otlp));
|
|
1174
|
+
}
|
|
1175
|
+
if (telemetryConfig.exporters?.victoriaMetrics?.enabled) await registry.register(createVictoriaMetricsTelemetryExporter(telemetryConfig.exporters.victoriaMetrics));
|
|
1176
|
+
await registry.startupHealthCheck({
|
|
1177
|
+
failLoud: telemetryConfig.failLoudStartup ?? true
|
|
1178
|
+
});
|
|
1179
|
+
if (!canaryOrchestrator) return;
|
|
1180
|
+
canaryOrchestrator.start();
|
|
1181
|
+
if (gateSnapshotStorePromise) {
|
|
1182
|
+
const gateSnapshotStore = await gateSnapshotStorePromise;
|
|
1183
|
+
contractGateAutopilot = new ContractGateAutopilot({
|
|
1184
|
+
orchestrator: canaryOrchestrator,
|
|
1185
|
+
gateSnapshotPath: resolveContractGateSnapshotPath(appDirectory, canaryConfig?.autopilot?.gateSnapshotPath),
|
|
1186
|
+
gateSnapshotStore,
|
|
1187
|
+
pollIntervalMs: canaryConfig?.autopilot?.pollIntervalMs,
|
|
1188
|
+
gateStaleAfterMs: canaryConfig?.autopilot?.gateStaleAfterMs
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
if (contractGateAutopilot) await contractGateAutopilot.start();
|
|
1192
|
+
canaryOrchestrator.evaluate();
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
export { DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_STATUS_ENDPOINT, TelemetryCanaryOrchestrator, TelemetryRegistry, TelemetryStartupHealthError, createOtlpTelemetryExporter, createRuntimeFallbackSignalRuntimeState, createRuntimeSignalError, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, enforceRuntimeFallbackSignalAuthToken, enforceRuntimeFallbackSignalTrustPolicy, getRuntimeSignalErrorStatusCode, hasEnabledTelemetryExporters, injectTelemetryPlugin, normalizeRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackTrustPolicy, parseRuntimeFallbackSignalPayloadFromRawBody, resolveRuntimeFallbackSignalEndpoint };
|