@bleedingdev/modern-js-server-runtime-extensions 3.8.3-ultramodern.1 → 3.8.3-ultramodern.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/backend-federation-security/index.js +415 -0
- package/dist/cjs/backend-federation-security/node.js +74 -0
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +1 -1
- package/dist/cjs/contract-gate-snapshot-store/resolve.js +4 -4
- package/dist/cjs/{contractGateAutopilot.js → contractGateSnapshotObserver.js} +14 -17
- package/dist/cjs/index.js +45 -13
- package/dist/cjs/runtime-fallback-signal/resolver.js +3 -3
- package/dist/cjs/runtimeLifecycle.js +129 -0
- package/dist/cjs/telemetry/{canaryEvaluation.js → healthEvaluation.js} +11 -13
- package/dist/cjs/telemetry/{canary.js → healthMonitor.js} +27 -44
- package/dist/cjs/telemetry/{canarySetup.js → healthSetup.js} +35 -39
- package/dist/cjs/telemetry/lifecycle.js +14 -14
- package/dist/cjs/telemetry/plugin.js +9 -9
- package/dist/cjs/telemetry/runtimeEndpoints.js +3 -3
- package/dist/cjs/telemetryCore.js +4 -4
- package/dist/esm/backend-federation-security/index.mjs +361 -0
- package/dist/esm/backend-federation-security/node.mjs +30 -0
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +1 -1
- package/dist/esm/contract-gate-snapshot-store/resolve.mjs +4 -4
- package/dist/esm/{contractGateAutopilot.mjs → contractGateSnapshotObserver.mjs} +12 -15
- package/dist/esm/index.mjs +4 -2
- package/dist/esm/runtime-fallback-signal/resolver.mjs +3 -3
- package/dist/esm/runtimeLifecycle.mjs +82 -0
- package/dist/esm/telemetry/{canaryEvaluation.mjs → healthEvaluation.mjs} +9 -11
- package/dist/esm/telemetry/{canary.mjs → healthMonitor.mjs} +25 -42
- package/dist/esm/telemetry/healthSetup.mjs +65 -0
- package/dist/esm/telemetry/lifecycle.mjs +14 -14
- package/dist/esm/telemetry/plugin.mjs +8 -8
- package/dist/esm/telemetry/runtimeEndpoints.mjs +3 -3
- package/dist/esm/telemetryCore.mjs +1 -1
- package/dist/esm-node/backend-federation-security/index.mjs +362 -0
- package/dist/esm-node/backend-federation-security/node.mjs +31 -0
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +1 -1
- package/dist/esm-node/contract-gate-snapshot-store/resolve.mjs +4 -4
- package/dist/esm-node/{contractGateAutopilot.mjs → contractGateSnapshotObserver.mjs} +12 -15
- package/dist/esm-node/index.mjs +4 -2
- package/dist/esm-node/runtime-fallback-signal/resolver.mjs +3 -3
- package/dist/esm-node/runtimeLifecycle.mjs +83 -0
- package/dist/esm-node/telemetry/{canaryEvaluation.mjs → healthEvaluation.mjs} +9 -11
- package/dist/esm-node/telemetry/{canary.mjs → healthMonitor.mjs} +25 -42
- package/dist/esm-node/telemetry/healthSetup.mjs +66 -0
- package/dist/esm-node/telemetry/lifecycle.mjs +14 -14
- package/dist/esm-node/telemetry/plugin.mjs +8 -8
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +3 -3
- package/dist/esm-node/telemetryCore.mjs +1 -1
- package/dist/types/backend-federation-security/index.d.ts +68 -0
- package/dist/types/backend-federation-security/node.d.ts +3 -0
- package/dist/types/{contractGateAutopilot.d.ts → contractGateSnapshotObserver.d.ts} +7 -6
- package/dist/types/index.d.ts +4 -2
- package/dist/types/runtime-fallback-signal/resolver.d.ts +2 -2
- package/dist/types/runtimeLifecycle.d.ts +10 -0
- package/dist/types/telemetry/healthEvaluation.d.ts +20 -0
- package/dist/types/telemetry/{canary.d.ts → healthMonitor.d.ts} +31 -28
- package/dist/types/telemetry/{canarySetup.d.ts → healthSetup.d.ts} +6 -6
- package/dist/types/telemetry/lifecycle.d.ts +4 -4
- package/dist/types/telemetry/plugin.d.ts +1 -1
- package/dist/types/telemetry/runtimeEndpoints.d.ts +3 -3
- package/dist/types/telemetryCore.d.ts +1 -1
- package/package.json +46 -8
- package/src/backend-federation-security/index.ts +831 -0
- package/src/backend-federation-security/node.ts +51 -0
- package/src/contract-gate-snapshot-store/http-store.ts +1 -1
- package/src/contract-gate-snapshot-store/resolve.ts +4 -4
- package/src/{contractGateAutopilot.ts → contractGateSnapshotObserver.ts} +18 -25
- package/src/index.ts +42 -6
- package/src/runtime-fallback-signal/resolver.ts +5 -5
- package/src/runtimeLifecycle.ts +125 -0
- package/src/telemetry/{canaryEvaluation.ts → healthEvaluation.ts} +14 -17
- package/src/telemetry/{canary.ts → healthMonitor.ts} +72 -84
- package/src/telemetry/{canarySetup.ts → healthSetup.ts} +50 -53
- package/src/telemetry/lifecycle.ts +20 -20
- package/src/telemetry/plugin.ts +16 -16
- package/src/telemetry/runtimeEndpoints.ts +6 -6
- package/src/telemetryCore.ts +11 -11
- package/tests/backendFederationSecurity.test.ts +416 -0
- package/tests/{contractGateAutopilot.test.ts → contractGateSnapshotObserver.test.ts} +59 -62
- package/tests/runtimeLifecycle.test.ts +107 -0
- package/tests/telemetryHealthMonitor.test.ts +187 -0
- package/dist/esm/telemetry/canarySetup.mjs +0 -69
- package/dist/esm-node/telemetry/canarySetup.mjs +0 -70
- package/dist/types/telemetry/canaryEvaluation.d.ts +0 -20
- package/tests/telemetryCanaryOrchestrator.test.ts +0 -365
|
@@ -2,7 +2,7 @@ import { getSnapshotGateNames, normalizeSnapshot } from "./contract-gate-snapsho
|
|
|
2
2
|
import { createFileContractGateSnapshotStore } from "./contract-gate-snapshot-store/file-store.mjs";
|
|
3
3
|
const DEFAULT_POLL_INTERVAL_MS = 15000;
|
|
4
4
|
const DEFAULT_GATE_STALE_AFTER_MS = 600000;
|
|
5
|
-
class
|
|
5
|
+
class ContractGateSnapshotObserver {
|
|
6
6
|
async start() {
|
|
7
7
|
await this.syncOnce();
|
|
8
8
|
if (this.poller) return;
|
|
@@ -20,8 +20,7 @@ class ContractGateAutopilot {
|
|
|
20
20
|
async syncOnce() {
|
|
21
21
|
const syncGeneration = ++this.syncGeneration;
|
|
22
22
|
const snapshot = await this.loadSnapshot();
|
|
23
|
-
if (syncGeneration !== this.syncGeneration) return 0;
|
|
24
|
-
if (!snapshot) return 0;
|
|
23
|
+
if (syncGeneration !== this.syncGeneration || !snapshot) return 0;
|
|
25
24
|
const snapshotGateNames = getSnapshotGateNames(snapshot);
|
|
26
25
|
const gates = normalizeSnapshot(snapshot, {
|
|
27
26
|
now: Date.now(),
|
|
@@ -29,13 +28,13 @@ class ContractGateAutopilot {
|
|
|
29
28
|
});
|
|
30
29
|
let updatedCount = snapshotGateNames ? this.clearOmittedGates(snapshotGateNames) : 0;
|
|
31
30
|
for (const gate of gates){
|
|
32
|
-
this.
|
|
31
|
+
this.monitor.addRequiredContractGate(gate.name);
|
|
33
32
|
const fingerprint = `${gate.passed ? '1' : '0'}:${gate.reason || ''}`;
|
|
34
33
|
if (this.appliedGateFingerprints.get(gate.name) !== fingerprint) {
|
|
35
|
-
this.
|
|
34
|
+
this.monitor.setContractGate(gate.name, gate.passed, gate.reason);
|
|
36
35
|
this.appliedGateFingerprints.set(gate.name, fingerprint);
|
|
37
36
|
updatedCount += 1;
|
|
38
|
-
this.logger?.info?.(`[telemetry.
|
|
37
|
+
this.logger?.info?.(`[telemetry.health.snapshot] gate=${gate.name} passed=${String(gate.passed)} reason=${gate.reason || 'none'}`);
|
|
39
38
|
}
|
|
40
39
|
}
|
|
41
40
|
return updatedCount;
|
|
@@ -43,29 +42,27 @@ class ContractGateAutopilot {
|
|
|
43
42
|
clearOmittedGates(snapshotGateNames) {
|
|
44
43
|
let updatedCount = 0;
|
|
45
44
|
for (const gateName of Array.from(this.appliedGateFingerprints.keys()))if (!snapshotGateNames.has(gateName)) {
|
|
46
|
-
this.
|
|
45
|
+
this.monitor.setContractGate(gateName, true);
|
|
47
46
|
this.appliedGateFingerprints.delete(gateName);
|
|
48
47
|
updatedCount += 1;
|
|
49
|
-
this.logger?.info?.(`[telemetry.
|
|
48
|
+
this.logger?.info?.(`[telemetry.health.snapshot] gate=${gateName} passed=true reason=omitted`);
|
|
50
49
|
}
|
|
51
50
|
return updatedCount;
|
|
52
51
|
}
|
|
53
52
|
async loadSnapshot() {
|
|
54
53
|
try {
|
|
55
|
-
|
|
56
|
-
if (!snapshot) return;
|
|
57
|
-
return snapshot;
|
|
54
|
+
return await this.gateSnapshotStore.readSnapshot() || void 0;
|
|
58
55
|
} catch (error) {
|
|
59
56
|
const source = this.gateSnapshotPath || this.gateSnapshotStore.name || 'stateStore';
|
|
60
|
-
this.logger?.warn?.(`[telemetry.
|
|
57
|
+
this.logger?.warn?.(`[telemetry.health.snapshot] failed to load gate snapshot ${source}: ${error instanceof Error ? error.message : String(error)}`);
|
|
61
58
|
return;
|
|
62
59
|
}
|
|
63
60
|
}
|
|
64
61
|
constructor(options){
|
|
65
62
|
this.syncGeneration = 0;
|
|
66
63
|
this.appliedGateFingerprints = new Map();
|
|
67
|
-
this.
|
|
68
|
-
if (!options.gateSnapshotStore && !options.gateSnapshotPath) throw new Error('
|
|
64
|
+
this.monitor = options.monitor;
|
|
65
|
+
if (!options.gateSnapshotStore && !options.gateSnapshotPath) throw new Error('ContractGateSnapshotObserver requires gateSnapshotPath or gateSnapshotStore');
|
|
69
66
|
this.gateSnapshotPath = options.gateSnapshotPath;
|
|
70
67
|
this.gateSnapshotStore = options.gateSnapshotStore || createFileContractGateSnapshotStore(options.gateSnapshotPath);
|
|
71
68
|
this.pollIntervalMs = Math.max(250, options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS);
|
|
@@ -73,4 +70,4 @@ class ContractGateAutopilot {
|
|
|
73
70
|
this.logger = options.logger;
|
|
74
71
|
}
|
|
75
72
|
}
|
|
76
|
-
export {
|
|
73
|
+
export { ContractGateSnapshotObserver };
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { BackendFederationRemoteEntryError, evaluateBackendFederationCommonJsEntry, loadBoundedBackendFederationResource, loadVerifiedBackendFederationEntry, redactBackendFederationUrl, resolveBackendFederationRemoteEntryVerification } from "./backend-federation-security/index.mjs";
|
|
2
|
+
export { ContractGateSnapshotObserver } from "./contractGateSnapshotObserver.mjs";
|
|
2
3
|
export { DEFAULT_ENVIRONMENT_NAME, parseServerRuntimeExtensionsEnv } from "./env.mjs";
|
|
3
4
|
export { getRequestPathname, injectMfAssetCacheHeadersPlugin, isMfManifestAsset, isMfRemoteEntryAsset, resolveMfAssetCacheHeaders } from "./mfCache.mjs";
|
|
4
5
|
export { collectDirectRemoteModuleFederationCss, collectDirectRemoteModuleFederationCssWithMeta, collectModuleFederationManifestCss, createModuleFederationCssCollector, injectModuleFederationCssPlugin } from "./module-federation-css/index.mjs";
|
|
5
|
-
export {
|
|
6
|
+
export { createDisposableServerRuntimeHandle, disposeServerRuntime, initializeDisposableServerRuntime, registerServerRuntimeDisposer } from "./runtimeLifecycle.mjs";
|
|
7
|
+
export { DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_STATUS_ENDPOINT, TelemetryHealthMonitor, TelemetryRegistry, TelemetryStartupHealthError, createOtlpTelemetryExporter, createRuntimeFallbackSignalRuntimeState, createRuntimeSignalError, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, enforceRuntimeFallbackSignalAuthToken, enforceRuntimeFallbackSignalTrustPolicy, getRuntimeSignalErrorStatusCode, hasEnabledTelemetryExporters, injectTelemetryPlugin, normalizeRequiredRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackTrustPolicy, parseRuntimeFallbackSignalPayloadFromRawBody, resolveRuntimeFallbackSignalEndpoint, resolveTelemetrySloOptions } from "./telemetry.mjs";
|
|
@@ -11,7 +11,7 @@ function normalizeRuntimeFallbackSignalAuthConfig(configured) {
|
|
|
11
11
|
const expectedFromConfig = 'string' == typeof configured?.expectedValue && configured.expectedValue.trim().length > 0 ? configured.expectedValue.trim() : void 0;
|
|
12
12
|
const expectedValue = expectedFromConfig || expectedFromEnv;
|
|
13
13
|
const enabled = configured?.enabled === true;
|
|
14
|
-
if (enabled && !expectedValue) throw new Error('[telemetry.
|
|
14
|
+
if (enabled && !expectedValue) throw new Error('[telemetry.health.runtimeFallbackSignal] auth.enabled true but no expected token configured');
|
|
15
15
|
return {
|
|
16
16
|
enabled,
|
|
17
17
|
headerName,
|
|
@@ -19,9 +19,9 @@ function normalizeRuntimeFallbackSignalAuthConfig(configured) {
|
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
function normalizeRequiredRuntimeFallbackSignalAuthConfig(configured) {
|
|
22
|
-
if (configured?.enabled === false) throw new Error('[telemetry.
|
|
22
|
+
if (configured?.enabled === false) throw new Error('[telemetry.health.runtimeFallbackSignal] auth.enabled cannot be false when runtimeFallbackSignal.enabled is true');
|
|
23
23
|
const authConfig = normalizeRuntimeFallbackSignalAuthConfig(configured);
|
|
24
|
-
if (!authConfig.expectedValue) throw new Error('[telemetry.
|
|
24
|
+
if (!authConfig.expectedValue) throw new Error('[telemetry.health.runtimeFallbackSignal] auth.expectedValue or auth.expectedValueEnv is required when runtimeFallbackSignal.enabled is true');
|
|
25
25
|
return {
|
|
26
26
|
...authConfig,
|
|
27
27
|
enabled: true
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const runtimeLifecycleStates = new WeakMap();
|
|
2
|
+
const getRuntimeLifecycleState = (owner)=>{
|
|
3
|
+
let state = runtimeLifecycleStates.get(owner);
|
|
4
|
+
if (!state) {
|
|
5
|
+
state = {
|
|
6
|
+
disposers: new Set(),
|
|
7
|
+
retired: false
|
|
8
|
+
};
|
|
9
|
+
runtimeLifecycleStates.set(owner, state);
|
|
10
|
+
}
|
|
11
|
+
return state;
|
|
12
|
+
};
|
|
13
|
+
const registerServerRuntimeDisposer = (owner, disposer)=>{
|
|
14
|
+
const state = getRuntimeLifecycleState(owner);
|
|
15
|
+
if (state.retired) throw new Error('Cannot register a disposer on a retired server runtime.');
|
|
16
|
+
state.disposers.add(disposer);
|
|
17
|
+
return ()=>{
|
|
18
|
+
state.disposers.delete(disposer);
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
const disposeServerRuntime = (owner)=>{
|
|
22
|
+
const state = getRuntimeLifecycleState(owner);
|
|
23
|
+
if (state.disposePromise) return state.disposePromise;
|
|
24
|
+
state.retired = true;
|
|
25
|
+
state.disposePromise = Promise.resolve().then(async ()=>{
|
|
26
|
+
const disposers = [
|
|
27
|
+
...state.disposers
|
|
28
|
+
].reverse();
|
|
29
|
+
state.disposers.clear();
|
|
30
|
+
const errors = [];
|
|
31
|
+
for (const disposer of disposers)try {
|
|
32
|
+
await disposer();
|
|
33
|
+
} catch (error) {
|
|
34
|
+
errors.push(error);
|
|
35
|
+
}
|
|
36
|
+
if (errors.length > 0) throw new AggregateError(errors, 'Failed to dispose server runtime.');
|
|
37
|
+
});
|
|
38
|
+
return state.disposePromise;
|
|
39
|
+
};
|
|
40
|
+
const createDisposableServerRuntimeHandle = (owner, handle)=>{
|
|
41
|
+
let activeRequests = 0;
|
|
42
|
+
let retired = false;
|
|
43
|
+
let resolveDrained;
|
|
44
|
+
let drained;
|
|
45
|
+
let disposePromise;
|
|
46
|
+
const disposableHandle = async (request, ...args)=>{
|
|
47
|
+
if (retired) throw new Error('Cannot dispatch through a retired server runtime.');
|
|
48
|
+
activeRequests += 1;
|
|
49
|
+
try {
|
|
50
|
+
return await handle(request, ...args);
|
|
51
|
+
} finally{
|
|
52
|
+
activeRequests -= 1;
|
|
53
|
+
if (0 === activeRequests) resolveDrained?.();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
disposableHandle.dispose = ()=>{
|
|
57
|
+
disposePromise ??= (async ()=>{
|
|
58
|
+
retired = true;
|
|
59
|
+
if (activeRequests > 0) {
|
|
60
|
+
drained ??= new Promise((resolve)=>{
|
|
61
|
+
resolveDrained = resolve;
|
|
62
|
+
});
|
|
63
|
+
await drained;
|
|
64
|
+
}
|
|
65
|
+
await disposeServerRuntime(owner);
|
|
66
|
+
})();
|
|
67
|
+
return disposePromise;
|
|
68
|
+
};
|
|
69
|
+
return disposableHandle;
|
|
70
|
+
};
|
|
71
|
+
const initializeDisposableServerRuntime = async (owner, handle, initialize)=>{
|
|
72
|
+
try {
|
|
73
|
+
await initialize();
|
|
74
|
+
} catch (error) {
|
|
75
|
+
try {
|
|
76
|
+
await disposeServerRuntime(owner);
|
|
77
|
+
} catch {}
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
return createDisposableServerRuntimeHandle(owner, handle);
|
|
81
|
+
};
|
|
82
|
+
export { createDisposableServerRuntimeHandle, disposeServerRuntime, initializeDisposableServerRuntime, registerServerRuntimeDisposer };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const evaluateTelemetryHealth = (metrics, thresholds)=>{
|
|
2
2
|
const failures = [];
|
|
3
3
|
const { queueStats, unhealthyExporterCount } = metrics;
|
|
4
4
|
if (queueStats.utilization > thresholds.maxQueueUtilization) failures.push({
|
|
@@ -18,18 +18,16 @@ const evaluateCanaryHealth = (metrics, thresholds)=>{
|
|
|
18
18
|
});
|
|
19
19
|
for (const gateName of metrics.requiredContractGates){
|
|
20
20
|
const gate = metrics.contractGates.get(gateName);
|
|
21
|
-
if (
|
|
22
|
-
failures.push({
|
|
23
|
-
reason: '
|
|
21
|
+
if (gate) {
|
|
22
|
+
if (!gate.passed) failures.push({
|
|
23
|
+
reason: 'contract_gate_failed',
|
|
24
24
|
gate: gateName,
|
|
25
|
-
message: `Contract gate "${gateName}" is
|
|
25
|
+
message: gate.reason || `Contract gate "${gateName}" is not passing`
|
|
26
26
|
});
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (!gate.passed) failures.push({
|
|
30
|
-
reason: 'contract_gate_failed',
|
|
27
|
+
} else failures.push({
|
|
28
|
+
reason: 'contract_gate_missing',
|
|
31
29
|
gate: gateName,
|
|
32
|
-
message:
|
|
30
|
+
message: `Contract gate "${gateName}" is missing`
|
|
33
31
|
});
|
|
34
32
|
}
|
|
35
33
|
return {
|
|
@@ -38,4 +36,4 @@ const evaluateCanaryHealth = (metrics, thresholds)=>{
|
|
|
38
36
|
unhealthyExporterCount
|
|
39
37
|
};
|
|
40
38
|
};
|
|
41
|
-
export {
|
|
39
|
+
export { evaluateTelemetryHealth };
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { evaluateCanaryHealth } from "./canaryEvaluation.mjs";
|
|
2
1
|
import { clamp } from "./envelope.mjs";
|
|
3
|
-
|
|
2
|
+
import { evaluateTelemetryHealth } from "./healthEvaluation.mjs";
|
|
3
|
+
class TelemetryHealthMonitor {
|
|
4
4
|
setRequiredContractGates(gates) {
|
|
5
5
|
this.requiredContractGates = Array.from(new Set(gates.map((item)=>item.trim()).filter(Boolean)));
|
|
6
6
|
}
|
|
7
7
|
addRequiredContractGate(name) {
|
|
8
8
|
const normalizedName = name.trim();
|
|
9
|
-
if (!normalizedName)
|
|
10
|
-
if (!this.requiredContractGates.includes(normalizedName)) this.requiredContractGates.push(normalizedName);
|
|
9
|
+
if (normalizedName && !this.requiredContractGates.includes(normalizedName)) this.requiredContractGates.push(normalizedName);
|
|
11
10
|
}
|
|
12
11
|
setContractGate(name, passed, reason) {
|
|
13
12
|
this.contractGates.set(name, {
|
|
@@ -18,23 +17,13 @@ class TelemetryCanaryOrchestrator {
|
|
|
18
17
|
});
|
|
19
18
|
}
|
|
20
19
|
setContractGates(gates) {
|
|
21
|
-
for (const [name, value] of Object.entries(gates))
|
|
22
|
-
|
|
23
|
-
this.setContractGate(name, value);
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
this.setContractGate(name, value.passed, value.reason);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
resetToCanary() {
|
|
30
|
-
this.state = 'canary';
|
|
31
|
-
this.consecutiveHealthy = 0;
|
|
32
|
-
this.consecutiveFailures = 0;
|
|
20
|
+
for (const [name, value] of Object.entries(gates))if ('boolean' == typeof value) this.setContractGate(name, value);
|
|
21
|
+
else this.setContractGate(name, value.passed, value.reason);
|
|
33
22
|
}
|
|
34
23
|
collectFailures() {
|
|
35
24
|
const queueStats = this.registry.getQueueStats();
|
|
36
25
|
const unhealthyExporterCount = this.registry.getExporterHealth().filter((item)=>!item.healthy).length;
|
|
37
|
-
return
|
|
26
|
+
return evaluateTelemetryHealth({
|
|
38
27
|
queueStats,
|
|
39
28
|
unhealthyExporterCount,
|
|
40
29
|
requiredContractGates: this.requiredContractGates,
|
|
@@ -46,27 +35,26 @@ class TelemetryCanaryOrchestrator {
|
|
|
46
35
|
});
|
|
47
36
|
}
|
|
48
37
|
evaluate() {
|
|
49
|
-
const now = Date.now();
|
|
50
38
|
const { failures, queueStats, unhealthyExporterCount } = this.collectFailures();
|
|
51
|
-
let
|
|
39
|
+
let transition = 'none';
|
|
52
40
|
if (failures.length > 0) {
|
|
53
41
|
this.consecutiveHealthy = 0;
|
|
54
42
|
this.consecutiveFailures += 1;
|
|
55
|
-
if ('
|
|
56
|
-
this.state = '
|
|
57
|
-
|
|
43
|
+
if ('unhealthy' !== this.state && this.consecutiveFailures >= this.minConsecutiveFailedEvaluations) {
|
|
44
|
+
this.state = 'unhealthy';
|
|
45
|
+
transition = 'became_unhealthy';
|
|
58
46
|
}
|
|
59
47
|
} else {
|
|
60
48
|
this.consecutiveFailures = 0;
|
|
61
49
|
this.consecutiveHealthy += 1;
|
|
62
|
-
if ('
|
|
63
|
-
this.state = '
|
|
64
|
-
|
|
50
|
+
if ('healthy' !== this.state && this.consecutiveHealthy >= this.minConsecutiveHealthyEvaluations) {
|
|
51
|
+
this.state = 'healthy';
|
|
52
|
+
transition = 'became_healthy';
|
|
65
53
|
}
|
|
66
54
|
}
|
|
67
|
-
const
|
|
68
|
-
timestamp: now,
|
|
69
|
-
|
|
55
|
+
const evaluation = {
|
|
56
|
+
timestamp: Date.now(),
|
|
57
|
+
transition,
|
|
70
58
|
state: this.state,
|
|
71
59
|
consecutiveHealthy: this.consecutiveHealthy,
|
|
72
60
|
consecutiveFailures: this.consecutiveFailures,
|
|
@@ -78,21 +66,17 @@ class TelemetryCanaryOrchestrator {
|
|
|
78
66
|
}))
|
|
79
67
|
};
|
|
80
68
|
try {
|
|
81
|
-
this.onEvaluate?.(
|
|
82
|
-
} catch (_error) {}
|
|
83
|
-
if ('promote' === action) try {
|
|
84
|
-
this.onPromote?.(decision);
|
|
69
|
+
this.onEvaluate?.(evaluation);
|
|
85
70
|
} catch (_error) {}
|
|
86
|
-
if ('
|
|
87
|
-
this.
|
|
71
|
+
if ('none' !== transition) try {
|
|
72
|
+
this.onTransition?.(evaluation);
|
|
88
73
|
} catch (_error) {}
|
|
89
|
-
return
|
|
74
|
+
return evaluation;
|
|
90
75
|
}
|
|
91
76
|
getStatusSnapshot() {
|
|
92
|
-
const now = Date.now();
|
|
93
77
|
const { failures, queueStats, unhealthyExporterCount } = this.collectFailures();
|
|
94
78
|
return {
|
|
95
|
-
timestamp: now,
|
|
79
|
+
timestamp: Date.now(),
|
|
96
80
|
state: this.state,
|
|
97
81
|
consecutiveHealthy: this.consecutiveHealthy,
|
|
98
82
|
consecutiveFailures: this.consecutiveFailures,
|
|
@@ -123,20 +107,19 @@ class TelemetryCanaryOrchestrator {
|
|
|
123
107
|
constructor(options){
|
|
124
108
|
this.requiredContractGates = [];
|
|
125
109
|
this.contractGates = new Map();
|
|
126
|
-
this.state = '
|
|
110
|
+
this.state = 'pending';
|
|
127
111
|
this.consecutiveHealthy = 0;
|
|
128
112
|
this.consecutiveFailures = 0;
|
|
129
113
|
this.registry = options.registry;
|
|
130
114
|
this.evaluationIntervalMs = Math.max(250, options.evaluationIntervalMs ?? 15000);
|
|
131
115
|
this.minConsecutiveHealthyEvaluations = Math.max(1, options.minConsecutiveHealthyEvaluations ?? 3);
|
|
132
|
-
this.
|
|
116
|
+
this.minConsecutiveFailedEvaluations = Math.max(1, options.minConsecutiveFailedEvaluations ?? 2);
|
|
133
117
|
this.maxQueueUtilization = clamp(options.maxQueueUtilization ?? 0.8, 0, 1);
|
|
134
118
|
this.maxTotalDropped = Math.max(0, options.maxTotalDropped ?? 0);
|
|
135
119
|
this.maxUnhealthyExporters = Math.max(0, options.maxUnhealthyExporters ?? 0);
|
|
136
120
|
this.setRequiredContractGates(options.requiredContractGates || []);
|
|
137
121
|
this.onEvaluate = options.onEvaluate;
|
|
138
|
-
this.
|
|
139
|
-
this.onRollback = options.onRollback;
|
|
122
|
+
this.onTransition = options.onTransition;
|
|
140
123
|
}
|
|
141
124
|
}
|
|
142
|
-
export {
|
|
125
|
+
export { TelemetryHealthMonitor };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH, resolveContractGateSnapshotPath, resolveContractGateSnapshotStore } from "../contract-gate-snapshot-store/index.mjs";
|
|
2
|
+
import { DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS, DEFAULT_RUNTIME_FALLBACK_GATE_NAME, DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES, createRuntimeFallbackSignalRuntimeState, normalizeRequiredRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackTrustPolicy, resolveRuntimeFallbackSignalEndpoint } from "../runtimeFallbackSignal.mjs";
|
|
3
|
+
import { TelemetryHealthMonitor } from "../telemetryCore.mjs";
|
|
4
|
+
function emitHealthTransitionMetric(registry, evaluation) {
|
|
5
|
+
try {
|
|
6
|
+
registry.enqueueMetric({
|
|
7
|
+
name: 'telemetry.health.transition',
|
|
8
|
+
value: 1,
|
|
9
|
+
unit: 'count',
|
|
10
|
+
tags: {
|
|
11
|
+
transition: evaluation.transition,
|
|
12
|
+
state: evaluation.state,
|
|
13
|
+
failures: String(evaluation.failures.length)
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
} catch (_error) {}
|
|
17
|
+
}
|
|
18
|
+
const setupTelemetryHealthMonitoring = ({ registry, appDirectory, legacyHealthConfig })=>{
|
|
19
|
+
if (!legacyHealthConfig?.enabled) return {};
|
|
20
|
+
const contractGates = legacyHealthConfig.contractGates;
|
|
21
|
+
const healthMonitor = new TelemetryHealthMonitor({
|
|
22
|
+
registry,
|
|
23
|
+
evaluationIntervalMs: legacyHealthConfig.evaluationIntervalMs,
|
|
24
|
+
minConsecutiveHealthyEvaluations: legacyHealthConfig.minConsecutiveHealthyEvaluations,
|
|
25
|
+
minConsecutiveFailedEvaluations: legacyHealthConfig.rollbackConsecutiveFailures,
|
|
26
|
+
maxQueueUtilization: legacyHealthConfig.maxQueueUtilization,
|
|
27
|
+
maxTotalDropped: legacyHealthConfig.maxTotalDropped,
|
|
28
|
+
maxUnhealthyExporters: legacyHealthConfig.maxUnhealthyExporters,
|
|
29
|
+
requiredContractGates: Object.keys(contractGates || {}),
|
|
30
|
+
onTransition: (evaluation)=>{
|
|
31
|
+
emitHealthTransitionMetric(registry, evaluation);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
if (contractGates) healthMonitor.setContractGates(contractGates);
|
|
35
|
+
let gateSnapshotStorePromise;
|
|
36
|
+
let runtimeFallbackSignalConfig;
|
|
37
|
+
const snapshotObservationEnabled = legacyHealthConfig.autopilot?.enabled ?? true;
|
|
38
|
+
if (snapshotObservationEnabled) {
|
|
39
|
+
const gateSnapshotPath = resolveContractGateSnapshotPath(appDirectory, legacyHealthConfig.autopilot?.gateSnapshotPath);
|
|
40
|
+
gateSnapshotStorePromise = resolveContractGateSnapshotStore({
|
|
41
|
+
appDirectory,
|
|
42
|
+
gateSnapshotPath: gateSnapshotPath || DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH,
|
|
43
|
+
stateStore: legacyHealthConfig.autopilot?.stateStore
|
|
44
|
+
});
|
|
45
|
+
const runtimeSignalConfig = legacyHealthConfig.autopilot?.runtimeFallbackSignal;
|
|
46
|
+
if (runtimeSignalConfig?.enabled === true) runtimeFallbackSignalConfig = {
|
|
47
|
+
endpoint: resolveRuntimeFallbackSignalEndpoint(runtimeSignalConfig.endpoint),
|
|
48
|
+
gateName: runtimeSignalConfig.gateName?.trim() || DEFAULT_RUNTIME_FALLBACK_GATE_NAME,
|
|
49
|
+
gateSnapshotStore: gateSnapshotStorePromise,
|
|
50
|
+
failureHoldMs: Math.max(1000, runtimeSignalConfig.failureHoldMs ?? DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS),
|
|
51
|
+
maxBodyBytes: Math.max(512, runtimeSignalConfig.maxBodyBytes ?? DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES),
|
|
52
|
+
auth: normalizeRequiredRuntimeFallbackSignalAuthConfig(runtimeSignalConfig.auth),
|
|
53
|
+
trustPolicy: normalizeRuntimeFallbackTrustPolicy(runtimeSignalConfig.trustPolicy),
|
|
54
|
+
runtimeState: createRuntimeFallbackSignalRuntimeState()
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const runtimeStatusAuthConfig = runtimeFallbackSignalConfig?.auth ?? normalizeRuntimeFallbackSignalAuthConfig(legacyHealthConfig.autopilot?.runtimeFallbackSignal?.auth);
|
|
58
|
+
return {
|
|
59
|
+
healthMonitor,
|
|
60
|
+
gateSnapshotStorePromise,
|
|
61
|
+
runtimeFallbackSignalConfig,
|
|
62
|
+
runtimeStatusAuthConfig
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export { setupTelemetryHealthMonitoring };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { resolveContractGateSnapshotPath } from "../contract-gate-snapshot-store/index.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { ContractGateSnapshotObserver } from "../contractGateSnapshotObserver.mjs";
|
|
3
3
|
import { createOtlpTelemetryExporter, createVictoriaMetricsTelemetryExporter, maybeWarnLegacyOtlpEndpoint } from "../telemetryCore.mjs";
|
|
4
4
|
const activeTelemetryLaneClosers = new Set();
|
|
5
5
|
let telemetryBeforeExitHookInstalled = false;
|
|
@@ -12,15 +12,15 @@ const ensureTelemetryBeforeExitHook = ()=>{
|
|
|
12
12
|
])close();
|
|
13
13
|
});
|
|
14
14
|
};
|
|
15
|
-
const registerTelemetryLifecycle = ({ api, registry, telemetryConfig,
|
|
16
|
-
let
|
|
15
|
+
const registerTelemetryLifecycle = ({ api, registry, telemetryConfig, legacyHealthConfig, healthMonitor, gateSnapshotStorePromise, appDirectory })=>{
|
|
16
|
+
let contractGateSnapshotObserver;
|
|
17
17
|
let telemetryLaneClosed = false;
|
|
18
18
|
const closeTelemetryLane = async ()=>{
|
|
19
19
|
if (telemetryLaneClosed) return;
|
|
20
20
|
telemetryLaneClosed = true;
|
|
21
21
|
activeTelemetryLaneClosers.delete(closeTelemetryLane);
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
contractGateSnapshotObserver?.stop();
|
|
23
|
+
healthMonitor?.stop();
|
|
24
24
|
await registry.shutdown();
|
|
25
25
|
};
|
|
26
26
|
let prepared = false;
|
|
@@ -41,20 +41,20 @@ const registerTelemetryLifecycle = ({ api, registry, telemetryConfig, canaryConf
|
|
|
41
41
|
await registry.startupHealthCheck({
|
|
42
42
|
failLoud: telemetryConfig.failLoudStartup ?? true
|
|
43
43
|
});
|
|
44
|
-
if (!
|
|
45
|
-
|
|
44
|
+
if (!healthMonitor) return;
|
|
45
|
+
healthMonitor.start();
|
|
46
46
|
if (gateSnapshotStorePromise) {
|
|
47
47
|
const gateSnapshotStore = await gateSnapshotStorePromise;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
gateSnapshotPath: resolveContractGateSnapshotPath(appDirectory,
|
|
48
|
+
contractGateSnapshotObserver = new ContractGateSnapshotObserver({
|
|
49
|
+
monitor: healthMonitor,
|
|
50
|
+
gateSnapshotPath: resolveContractGateSnapshotPath(appDirectory, legacyHealthConfig?.autopilot?.gateSnapshotPath),
|
|
51
51
|
gateSnapshotStore,
|
|
52
|
-
pollIntervalMs:
|
|
53
|
-
gateStaleAfterMs:
|
|
52
|
+
pollIntervalMs: legacyHealthConfig?.autopilot?.pollIntervalMs,
|
|
53
|
+
gateStaleAfterMs: legacyHealthConfig?.autopilot?.gateStaleAfterMs
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
|
-
if (
|
|
57
|
-
|
|
56
|
+
if (contractGateSnapshotObserver) await contractGateSnapshotObserver.start();
|
|
57
|
+
healthMonitor.evaluate();
|
|
58
58
|
});
|
|
59
59
|
};
|
|
60
60
|
export { registerTelemetryLifecycle };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { parseTraceparent } from "@modern-js/create-request/server";
|
|
2
2
|
import { logger } from "@modern-js/utils";
|
|
3
3
|
import { parseServerRuntimeExtensionsEnv } from "../env.mjs";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { TelemetryHealthMonitor, TelemetryRegistry, TelemetryStartupHealthError, createOtlpTelemetryExporter, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, toTelemetryEnvelope } from "../telemetryCore.mjs";
|
|
5
|
+
import { setupTelemetryHealthMonitoring } from "./healthSetup.mjs";
|
|
6
6
|
import { registerTelemetryLifecycle } from "./lifecycle.mjs";
|
|
7
7
|
import { createRuntimeFallbackSignalMiddleware, createRuntimeStatusMiddleware } from "./runtimeEndpoints.mjs";
|
|
8
8
|
const hasEnabledTelemetryExporters = (config)=>Boolean(config?.exporters?.otlp?.enabled || config?.exporters?.victoriaMetrics?.enabled);
|
|
@@ -36,15 +36,15 @@ const injectTelemetryPlugin = ()=>({
|
|
|
36
36
|
redactionKeys: telemetryConfig.redactionKeys,
|
|
37
37
|
slo: resolveTelemetrySloOptions(telemetryConfig.slo)
|
|
38
38
|
});
|
|
39
|
-
const {
|
|
39
|
+
const { healthMonitor, gateSnapshotStorePromise, runtimeFallbackSignalConfig, runtimeStatusAuthConfig } = setupTelemetryHealthMonitoring({
|
|
40
40
|
registry,
|
|
41
41
|
appDirectory,
|
|
42
|
-
|
|
42
|
+
legacyHealthConfig: telemetryConfig.canary
|
|
43
43
|
});
|
|
44
44
|
if (runtimeFallbackSignalConfig) middlewares.push(createRuntimeFallbackSignalMiddleware(runtimeFallbackSignalConfig));
|
|
45
45
|
middlewares.push(createRuntimeStatusMiddleware({
|
|
46
46
|
registry,
|
|
47
|
-
|
|
47
|
+
healthMonitor,
|
|
48
48
|
runtimeFallbackSignalConfig,
|
|
49
49
|
runtimeStatusAuthConfig
|
|
50
50
|
}));
|
|
@@ -76,12 +76,12 @@ const injectTelemetryPlugin = ()=>({
|
|
|
76
76
|
api,
|
|
77
77
|
registry,
|
|
78
78
|
telemetryConfig,
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
legacyHealthConfig: telemetryConfig.canary,
|
|
80
|
+
healthMonitor,
|
|
81
81
|
gateSnapshotStorePromise,
|
|
82
82
|
appDirectory
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
85
|
});
|
|
86
86
|
export { DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_STATUS_ENDPOINT, createRuntimeFallbackSignalRuntimeState, createRuntimeSignalError, enforceRuntimeFallbackSignalAuthToken, enforceRuntimeFallbackSignalTrustPolicy, getRuntimeSignalErrorStatusCode, normalizeRequiredRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackTrustPolicy, parseRuntimeFallbackSignalPayloadFromRawBody, resolveRuntimeFallbackSignalEndpoint } from "../runtimeFallbackSignal.mjs";
|
|
87
|
-
export {
|
|
87
|
+
export { TelemetryHealthMonitor, TelemetryRegistry, TelemetryStartupHealthError, createOtlpTelemetryExporter, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, hasEnabledTelemetryExporters, injectTelemetryPlugin, resolveTelemetrySloOptions };
|
|
@@ -34,7 +34,7 @@ const createRuntimeFallbackSignalMiddleware = (signalConfig)=>({
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
|
-
const createRuntimeStatusMiddleware = ({ registry,
|
|
37
|
+
const createRuntimeStatusMiddleware = ({ registry, healthMonitor, runtimeFallbackSignalConfig, runtimeStatusAuthConfig })=>({
|
|
38
38
|
name: 'telemetry-runtime-status',
|
|
39
39
|
path: DEFAULT_RUNTIME_STATUS_ENDPOINT,
|
|
40
40
|
method: 'get',
|
|
@@ -53,9 +53,9 @@ const createRuntimeStatusMiddleware = ({ registry, canaryOrchestrator, runtimeFa
|
|
|
53
53
|
queueStats: registry.getQueueStats(),
|
|
54
54
|
exporterHealth: registry.getExporterHealth()
|
|
55
55
|
},
|
|
56
|
-
|
|
56
|
+
health: healthMonitor ? {
|
|
57
57
|
enabled: true,
|
|
58
|
-
...
|
|
58
|
+
...healthMonitor.getStatusSnapshot()
|
|
59
59
|
} : {
|
|
60
60
|
enabled: false
|
|
61
61
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { TelemetryCanaryOrchestrator } from "./telemetry/canary.mjs";
|
|
2
1
|
export { toTelemetryEnvelope } from "./telemetry/envelope.mjs";
|
|
3
2
|
export { createOtlpTelemetryExporter, createVictoriaMetricsTelemetryExporter, maybeWarnLegacyOtlpEndpoint } from "./telemetry/exporters.mjs";
|
|
3
|
+
export { TelemetryHealthMonitor } from "./telemetry/healthMonitor.mjs";
|
|
4
4
|
export { createTelemetryAwareMetrics } from "./telemetry/metrics.mjs";
|
|
5
5
|
export { TelemetryRegistry } from "./telemetry/registry.mjs";
|
|
6
6
|
export { TelemetryStartupHealthError } from "./telemetry/registryTypes.mjs";
|