@bleedingdev/modern-js-server-runtime-extensions 3.5.0-ultramodern.30 → 3.5.0-ultramodern.32
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/contract-gate-snapshot-store/file-store.js +102 -0
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +142 -0
- package/dist/cjs/contract-gate-snapshot-store/index.js +58 -0
- package/dist/cjs/contract-gate-snapshot-store/resolve.js +122 -0
- package/dist/cjs/contract-gate-snapshot-store/types.js +44 -0
- package/dist/cjs/contractGateAutopilot.js +27 -6
- package/dist/cjs/index.js +13 -13
- package/dist/cjs/module-federation-css/collector.js +79 -0
- package/dist/cjs/module-federation-css/index.js +55 -0
- package/dist/cjs/module-federation-css/manifest.js +144 -0
- package/dist/cjs/module-federation-css/plugin.js +71 -0
- package/dist/cjs/module-federation-css/remote.js +133 -0
- package/dist/cjs/runtime-fallback-signal/auth.js +87 -0
- package/dist/cjs/runtime-fallback-signal/constants.js +72 -0
- package/dist/cjs/runtime-fallback-signal/contract-gate.js +64 -0
- package/dist/cjs/runtime-fallback-signal/endpoint.js +50 -0
- package/dist/cjs/runtime-fallback-signal/errors.js +44 -0
- package/dist/cjs/runtime-fallback-signal/index.js +99 -0
- package/dist/cjs/runtime-fallback-signal/payload.js +72 -0
- package/dist/cjs/runtime-fallback-signal/source.js +66 -0
- package/dist/cjs/runtime-fallback-signal/state.js +57 -0
- package/dist/cjs/runtime-fallback-signal/status.js +47 -0
- package/dist/cjs/runtime-fallback-signal/trust-policy.js +108 -0
- package/dist/cjs/runtime-fallback-signal/types.js +18 -0
- package/dist/cjs/runtimeFallbackSignal.js +32 -281
- package/dist/cjs/telemetry/canary.js +180 -0
- package/dist/cjs/telemetry/canaryEvaluation.js +79 -0
- package/dist/cjs/telemetry/canarySetup.js +107 -0
- package/dist/cjs/telemetry/envelope.js +148 -0
- package/dist/cjs/telemetry/exporters.js +148 -0
- package/dist/cjs/telemetry/lifecycle.js +98 -0
- package/dist/cjs/telemetry/metrics.js +115 -0
- package/dist/cjs/telemetry/plugin.js +185 -0
- package/dist/cjs/telemetry/registry.js +348 -0
- package/dist/cjs/telemetry/registryTypes.js +46 -0
- package/dist/cjs/telemetry/runtimeEndpoints.js +135 -0
- package/dist/cjs/telemetry.js +32 -343
- package/dist/cjs/telemetryCore.js +14 -767
- package/dist/esm/contract-gate-snapshot-store/file-store.mjs +51 -0
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +92 -0
- package/dist/esm/contract-gate-snapshot-store/index.mjs +4 -0
- package/dist/esm/contract-gate-snapshot-store/resolve.mjs +68 -0
- package/dist/esm/contract-gate-snapshot-store/types.mjs +3 -0
- package/dist/esm/contractGateAutopilot.mjs +26 -5
- package/dist/esm/index.mjs +2 -2
- package/dist/esm/module-federation-css/collector.mjs +38 -0
- package/dist/esm/module-federation-css/index.mjs +4 -0
- package/dist/esm/module-federation-css/manifest.mjs +69 -0
- package/dist/esm/module-federation-css/plugin.mjs +33 -0
- package/dist/esm/module-federation-css/remote.mjs +92 -0
- package/dist/esm/runtime-fallback-signal/auth.mjs +40 -0
- package/dist/esm/runtime-fallback-signal/constants.mjs +10 -0
- package/dist/esm/runtime-fallback-signal/contract-gate.mjs +26 -0
- package/dist/esm/runtime-fallback-signal/endpoint.mjs +12 -0
- package/dist/esm/runtime-fallback-signal/errors.mjs +6 -0
- package/dist/esm/runtime-fallback-signal/index.mjs +9 -0
- package/dist/esm/runtime-fallback-signal/payload.mjs +31 -0
- package/dist/esm/runtime-fallback-signal/source.mjs +22 -0
- package/dist/esm/runtime-fallback-signal/state.mjs +16 -0
- package/dist/esm/runtime-fallback-signal/status.mjs +9 -0
- package/dist/esm/runtime-fallback-signal/trust-policy.mjs +67 -0
- package/dist/esm/runtime-fallback-signal/types.mjs +0 -0
- package/dist/esm/runtimeFallbackSignal.mjs +1 -222
- package/dist/esm/telemetry/canary.mjs +142 -0
- package/dist/esm/telemetry/canaryEvaluation.mjs +41 -0
- package/dist/esm/telemetry/canarySetup.mjs +69 -0
- package/dist/esm/telemetry/envelope.mjs +101 -0
- package/dist/esm/telemetry/exporters.mjs +104 -0
- package/dist/esm/telemetry/lifecycle.mjs +60 -0
- package/dist/esm/telemetry/metrics.mjs +77 -0
- package/dist/esm/telemetry/plugin.mjs +87 -0
- package/dist/esm/telemetry/registry.mjs +310 -0
- package/dist/esm/telemetry/registryTypes.mjs +8 -0
- package/dist/esm/telemetry/runtimeEndpoints.mjs +94 -0
- package/dist/esm/telemetry.mjs +1 -275
- package/dist/esm/telemetryCore.mjs +6 -759
- package/dist/esm-node/contract-gate-snapshot-store/file-store.mjs +52 -0
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +93 -0
- package/dist/esm-node/contract-gate-snapshot-store/index.mjs +5 -0
- package/dist/esm-node/contract-gate-snapshot-store/resolve.mjs +70 -0
- package/dist/esm-node/contract-gate-snapshot-store/types.mjs +4 -0
- package/dist/esm-node/contractGateAutopilot.mjs +26 -5
- package/dist/esm-node/index.mjs +2 -2
- package/dist/esm-node/module-federation-css/collector.mjs +39 -0
- package/dist/esm-node/module-federation-css/index.mjs +5 -0
- package/dist/esm-node/module-federation-css/manifest.mjs +70 -0
- package/dist/esm-node/module-federation-css/plugin.mjs +34 -0
- package/dist/esm-node/module-federation-css/remote.mjs +93 -0
- package/dist/esm-node/runtime-fallback-signal/auth.mjs +41 -0
- package/dist/esm-node/runtime-fallback-signal/constants.mjs +11 -0
- package/dist/esm-node/runtime-fallback-signal/contract-gate.mjs +27 -0
- package/dist/esm-node/runtime-fallback-signal/endpoint.mjs +13 -0
- package/dist/esm-node/runtime-fallback-signal/errors.mjs +7 -0
- package/dist/esm-node/runtime-fallback-signal/index.mjs +10 -0
- package/dist/esm-node/runtime-fallback-signal/payload.mjs +32 -0
- package/dist/esm-node/runtime-fallback-signal/source.mjs +23 -0
- package/dist/esm-node/runtime-fallback-signal/state.mjs +17 -0
- package/dist/esm-node/runtime-fallback-signal/status.mjs +10 -0
- package/dist/esm-node/runtime-fallback-signal/trust-policy.mjs +68 -0
- package/dist/esm-node/runtime-fallback-signal/types.mjs +1 -0
- package/dist/esm-node/runtimeFallbackSignal.mjs +1 -222
- package/dist/esm-node/telemetry/canary.mjs +143 -0
- package/dist/esm-node/telemetry/canaryEvaluation.mjs +42 -0
- package/dist/esm-node/telemetry/canarySetup.mjs +70 -0
- package/dist/esm-node/telemetry/envelope.mjs +102 -0
- package/dist/esm-node/telemetry/exporters.mjs +105 -0
- package/dist/esm-node/telemetry/lifecycle.mjs +61 -0
- package/dist/esm-node/telemetry/metrics.mjs +78 -0
- package/dist/esm-node/telemetry/plugin.mjs +88 -0
- package/dist/esm-node/telemetry/registry.mjs +311 -0
- package/dist/esm-node/telemetry/registryTypes.mjs +9 -0
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +95 -0
- package/dist/esm-node/telemetry.mjs +1 -275
- package/dist/esm-node/telemetryCore.mjs +6 -759
- package/dist/types/contract-gate-snapshot-store/file-store.d.ts +3 -0
- package/dist/types/contract-gate-snapshot-store/http-store.d.ts +11 -0
- package/dist/types/contract-gate-snapshot-store/index.d.ts +5 -0
- package/dist/types/contract-gate-snapshot-store/resolve.d.ts +9 -0
- package/dist/types/{contractGateSnapshotStore.d.ts → contract-gate-snapshot-store/types.d.ts} +1 -11
- package/dist/types/contractGateAutopilot.d.ts +4 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/types/module-federation-css/collector.d.ts +25 -0
- package/dist/types/module-federation-css/index.d.ts +8 -0
- package/dist/types/module-federation-css/manifest.d.ts +33 -0
- package/dist/types/module-federation-css/plugin.d.ts +23 -0
- package/dist/types/module-federation-css/remote.d.ts +20 -0
- package/dist/types/runtime-fallback-signal/auth.d.ts +18 -0
- package/dist/types/runtime-fallback-signal/constants.d.ts +9 -0
- package/dist/types/runtime-fallback-signal/contract-gate.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/endpoint.d.ts +1 -0
- package/dist/types/runtime-fallback-signal/errors.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/index.d.ts +10 -0
- package/dist/types/runtime-fallback-signal/payload.d.ts +6 -0
- package/dist/types/runtime-fallback-signal/source.d.ts +3 -0
- package/dist/types/runtime-fallback-signal/state.d.ts +3 -0
- package/dist/types/runtime-fallback-signal/status.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/trust-policy.d.ts +13 -0
- package/dist/types/runtime-fallback-signal/types.d.ts +50 -0
- package/dist/types/runtimeFallbackSignal.d.ts +1 -94
- package/dist/types/telemetry/canary.d.ts +85 -0
- package/dist/types/telemetry/canaryEvaluation.d.ts +19 -0
- package/dist/types/telemetry/canarySetup.d.ts +17 -0
- package/dist/types/telemetry/envelope.d.ts +45 -0
- package/dist/types/telemetry/exporters.d.ts +12 -0
- package/dist/types/telemetry/lifecycle.d.ts +18 -0
- package/dist/types/telemetry/metrics.d.ts +3 -0
- package/dist/types/telemetry/plugin.d.ts +12 -0
- package/dist/types/telemetry/registry.d.ts +72 -0
- package/dist/types/telemetry/registryTypes.d.ts +50 -0
- package/dist/types/telemetry/runtimeEndpoints.d.ts +35 -0
- package/dist/types/telemetry.d.ts +1 -12
- package/dist/types/telemetryCore.d.ts +6 -257
- package/package.json +7 -7
- package/src/contract-gate-snapshot-store/file-store.ts +85 -0
- package/src/contract-gate-snapshot-store/http-store.ts +159 -0
- package/src/contract-gate-snapshot-store/index.ts +20 -0
- package/src/contract-gate-snapshot-store/resolve.ts +175 -0
- package/src/contract-gate-snapshot-store/types.ts +64 -0
- package/src/contractGateAutopilot.ts +45 -9
- package/src/index.ts +6 -6
- package/src/module-federation-css/collector.ts +82 -0
- package/src/module-federation-css/index.ts +14 -0
- package/src/module-federation-css/manifest.ts +166 -0
- package/src/module-federation-css/plugin.ts +86 -0
- package/src/module-federation-css/remote.ts +165 -0
- package/src/runtime-fallback-signal/auth.ts +117 -0
- package/src/runtime-fallback-signal/constants.ts +19 -0
- package/src/runtime-fallback-signal/contract-gate.ts +43 -0
- package/src/runtime-fallback-signal/endpoint.ts +22 -0
- package/src/runtime-fallback-signal/errors.ts +10 -0
- package/src/runtime-fallback-signal/index.ts +36 -0
- package/src/runtime-fallback-signal/payload.ts +79 -0
- package/src/runtime-fallback-signal/source.ts +49 -0
- package/src/runtime-fallback-signal/state.ts +34 -0
- package/src/runtime-fallback-signal/status.ts +22 -0
- package/src/runtime-fallback-signal/trust-policy.ts +179 -0
- package/src/runtime-fallback-signal/types.ts +66 -0
- package/src/runtimeFallbackSignal.ts +1 -584
- package/src/telemetry/canary.ts +291 -0
- package/src/telemetry/canaryEvaluation.ts +83 -0
- package/src/telemetry/canarySetup.ts +160 -0
- package/src/telemetry/envelope.ts +189 -0
- package/src/telemetry/exporters.ts +165 -0
- package/src/telemetry/lifecycle.ts +138 -0
- package/src/telemetry/metrics.ts +141 -0
- package/src/telemetry/plugin.ts +197 -0
- package/src/telemetry/registry.ts +467 -0
- package/src/telemetry/registryTypes.ts +63 -0
- package/src/telemetry/runtimeEndpoints.ts +172 -0
- package/src/telemetry.ts +1 -554
- package/src/telemetryCore.ts +34 -1332
- package/tests/contractGateAutopilot.test.ts +205 -0
- package/tests/contractGateSnapshotStore.test.ts +54 -1
- package/tests/helpers.ts +1 -0
- package/tests/moduleFederationCss.test.ts +1 -1
- package/tests/registration.test.ts +1 -1
- package/tests/telemetry.test.ts +40 -0
- package/tests/telemetryLifecycle.test.ts +253 -2
- package/dist/cjs/contractGateSnapshotStore.js +0 -253
- package/dist/cjs/moduleFederationCss.js +0 -285
- package/dist/esm/contractGateSnapshotStore.mjs +0 -190
- package/dist/esm/moduleFederationCss.mjs +0 -225
- package/dist/esm-node/contractGateSnapshotStore.mjs +0 -192
- package/dist/esm-node/moduleFederationCss.mjs +0 -226
- package/dist/types/moduleFederationCss.d.ts +0 -87
- package/src/contractGateSnapshotStore.ts +0 -420
- package/src/moduleFederationCss.ts +0 -473
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { parseTraceparent } from '@modern-js/create-request/server';
|
|
2
|
+
import type {
|
|
3
|
+
Context,
|
|
4
|
+
Next,
|
|
5
|
+
ServerEnv,
|
|
6
|
+
ServerPlugin,
|
|
7
|
+
ServerTelemetryUserConfig,
|
|
8
|
+
} from '@modern-js/server-core';
|
|
9
|
+
import type { CoreMonitor } from '@modern-js/types';
|
|
10
|
+
import { logger } from '@modern-js/utils';
|
|
11
|
+
import { parseServerRuntimeExtensionsEnv } from '../env';
|
|
12
|
+
import {
|
|
13
|
+
TelemetryRegistry,
|
|
14
|
+
type TelemetryRegistryOptions,
|
|
15
|
+
toTelemetryEnvelope,
|
|
16
|
+
} from '../telemetryCore';
|
|
17
|
+
import { setupTelemetryCanary } from './canarySetup';
|
|
18
|
+
import { registerTelemetryLifecycle } from './lifecycle';
|
|
19
|
+
import {
|
|
20
|
+
createRuntimeFallbackSignalMiddleware,
|
|
21
|
+
createRuntimeStatusMiddleware,
|
|
22
|
+
} from './runtimeEndpoints';
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
createRuntimeFallbackSignalRuntimeState,
|
|
26
|
+
createRuntimeSignalError,
|
|
27
|
+
DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT,
|
|
28
|
+
DEFAULT_RUNTIME_STATUS_ENDPOINT,
|
|
29
|
+
enforceRuntimeFallbackSignalAuthToken,
|
|
30
|
+
enforceRuntimeFallbackSignalTrustPolicy,
|
|
31
|
+
getRuntimeSignalErrorStatusCode,
|
|
32
|
+
normalizeRequiredRuntimeFallbackSignalAuthConfig,
|
|
33
|
+
normalizeRuntimeFallbackSignalAuthConfig,
|
|
34
|
+
normalizeRuntimeFallbackTrustPolicy,
|
|
35
|
+
parseRuntimeFallbackSignalPayloadFromRawBody,
|
|
36
|
+
type RuntimeFallbackSignalAuthConfig,
|
|
37
|
+
type RuntimeFallbackSignalRuntimeState,
|
|
38
|
+
type RuntimeFallbackSignalSource,
|
|
39
|
+
type RuntimeFallbackSignalTrustContext,
|
|
40
|
+
type RuntimeFallbackSignalTrustPolicy,
|
|
41
|
+
type RuntimeSignalError,
|
|
42
|
+
type RuntimeSignalErrorCode,
|
|
43
|
+
resolveRuntimeFallbackSignalEndpoint,
|
|
44
|
+
} from '../runtimeFallbackSignal';
|
|
45
|
+
export {
|
|
46
|
+
createOtlpTelemetryExporter,
|
|
47
|
+
createTelemetryAwareMetrics,
|
|
48
|
+
createVictoriaMetricsTelemetryExporter,
|
|
49
|
+
type OtlpExporterOptions,
|
|
50
|
+
type TelemetryCanaryAction,
|
|
51
|
+
type TelemetryCanaryContractGateStatus,
|
|
52
|
+
type TelemetryCanaryDecision,
|
|
53
|
+
type TelemetryCanaryFailure,
|
|
54
|
+
type TelemetryCanaryFailureReason,
|
|
55
|
+
TelemetryCanaryOrchestrator,
|
|
56
|
+
type TelemetryCanaryOrchestratorOptions,
|
|
57
|
+
type TelemetryCanaryState,
|
|
58
|
+
type TelemetryCanaryStatusSnapshot,
|
|
59
|
+
type TelemetryEnvelope,
|
|
60
|
+
type TelemetryExporter,
|
|
61
|
+
type TelemetryExporterHealthStatus,
|
|
62
|
+
type TelemetryQueueStats,
|
|
63
|
+
TelemetryRegistry,
|
|
64
|
+
type TelemetryRegistryOptions,
|
|
65
|
+
type TelemetrySignalType,
|
|
66
|
+
type TelemetrySloAlert,
|
|
67
|
+
type TelemetrySloAlertType,
|
|
68
|
+
TelemetryStartupHealthError,
|
|
69
|
+
type VictoriaMetricsExporterOptions,
|
|
70
|
+
} from '../telemetryCore';
|
|
71
|
+
|
|
72
|
+
export const hasEnabledTelemetryExporters = (
|
|
73
|
+
config: ServerTelemetryUserConfig | undefined,
|
|
74
|
+
) =>
|
|
75
|
+
Boolean(
|
|
76
|
+
config?.exporters?.otlp?.enabled ||
|
|
77
|
+
config?.exporters?.victoriaMetrics?.enabled,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Builds the registry SLO options from `server.telemetry.slo`, attaching a
|
|
82
|
+
* logger sink so threshold breaches surface as server warnings (parity with
|
|
83
|
+
* the pre-extraction prod-server harness).
|
|
84
|
+
*/
|
|
85
|
+
export const resolveTelemetrySloOptions = (
|
|
86
|
+
sloConfig: ServerTelemetryUserConfig['slo'],
|
|
87
|
+
warnSink: (message: string) => void = message => logger.warn(message),
|
|
88
|
+
): NonNullable<TelemetryRegistryOptions['slo']> => ({
|
|
89
|
+
queueUtilizationWarnThreshold: sloConfig?.queueUtilizationWarnThreshold,
|
|
90
|
+
queueDroppedWarnThreshold: sloConfig?.queueDroppedWarnThreshold,
|
|
91
|
+
alertCooldownMs: sloConfig?.alertCooldownMs,
|
|
92
|
+
onAlert: alert => {
|
|
93
|
+
warnSink(
|
|
94
|
+
`[telemetry.slo] ${alert.type} threshold=${alert.threshold} value=${alert.value} depth=${alert.queueDepth}/${alert.queueCapacity} dropped=${alert.totalDropped}`,
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
export const injectTelemetryPlugin = (): ServerPlugin => ({
|
|
100
|
+
name: '@modern-js/inject-telemetry',
|
|
101
|
+
setup(api) {
|
|
102
|
+
const serverConfig = api.getServerConfig();
|
|
103
|
+
const telemetryConfig = serverConfig?.server?.telemetry;
|
|
104
|
+
if (!telemetryConfig) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (
|
|
109
|
+
telemetryConfig.enabled !== true &&
|
|
110
|
+
!hasEnabledTelemetryExporters(telemetryConfig)
|
|
111
|
+
) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const { middlewares, metaName, appDirectory } = api.getServerContext();
|
|
116
|
+
const serviceName = telemetryConfig.service || metaName || 'modern-js';
|
|
117
|
+
const moduleName = telemetryConfig.module || 'server';
|
|
118
|
+
const environmentName =
|
|
119
|
+
telemetryConfig.environment ||
|
|
120
|
+
parseServerRuntimeExtensionsEnv().environmentName;
|
|
121
|
+
|
|
122
|
+
const registry = new TelemetryRegistry({
|
|
123
|
+
service: serviceName,
|
|
124
|
+
module: moduleName,
|
|
125
|
+
environment: environmentName,
|
|
126
|
+
samplingRate: telemetryConfig.samplingRate,
|
|
127
|
+
flushIntervalMs: telemetryConfig.flushIntervalMs,
|
|
128
|
+
maxBatchSize: telemetryConfig.maxBatchSize,
|
|
129
|
+
maxQueueSize: telemetryConfig.maxQueueSize,
|
|
130
|
+
redactionKeys: telemetryConfig.redactionKeys,
|
|
131
|
+
slo: resolveTelemetrySloOptions(telemetryConfig.slo),
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const {
|
|
135
|
+
canaryOrchestrator,
|
|
136
|
+
gateSnapshotStorePromise,
|
|
137
|
+
runtimeFallbackSignalConfig,
|
|
138
|
+
runtimeStatusAuthConfig,
|
|
139
|
+
} = setupTelemetryCanary({
|
|
140
|
+
registry,
|
|
141
|
+
appDirectory,
|
|
142
|
+
canaryConfig: telemetryConfig.canary,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (runtimeFallbackSignalConfig) {
|
|
146
|
+
middlewares.push(
|
|
147
|
+
createRuntimeFallbackSignalMiddleware(runtimeFallbackSignalConfig),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
middlewares.push(
|
|
151
|
+
createRuntimeStatusMiddleware({
|
|
152
|
+
registry,
|
|
153
|
+
canaryOrchestrator,
|
|
154
|
+
runtimeFallbackSignalConfig,
|
|
155
|
+
runtimeStatusAuthConfig,
|
|
156
|
+
}),
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
middlewares.push({
|
|
160
|
+
name: 'inject-telemetry',
|
|
161
|
+
handler: async (c: Context<ServerEnv>, next: Next) => {
|
|
162
|
+
const monitors = c.get('monitors');
|
|
163
|
+
if (monitors) {
|
|
164
|
+
const traceContext = parseTraceparent(c.req.header('traceparent'));
|
|
165
|
+
const monitor: CoreMonitor = event => {
|
|
166
|
+
registry.enqueue(
|
|
167
|
+
toTelemetryEnvelope(event, {
|
|
168
|
+
service: serviceName,
|
|
169
|
+
module: moduleName,
|
|
170
|
+
environment: environmentName,
|
|
171
|
+
traceId: traceContext?.traceId,
|
|
172
|
+
spanId: traceContext?.spanId,
|
|
173
|
+
attributes: {
|
|
174
|
+
requestMethod: c.req.method,
|
|
175
|
+
requestPath: c.req.path,
|
|
176
|
+
},
|
|
177
|
+
}),
|
|
178
|
+
);
|
|
179
|
+
};
|
|
180
|
+
monitors.push(monitor);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
await next();
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
registerTelemetryLifecycle({
|
|
188
|
+
api,
|
|
189
|
+
registry,
|
|
190
|
+
telemetryConfig,
|
|
191
|
+
canaryConfig: telemetryConfig.canary,
|
|
192
|
+
canaryOrchestrator,
|
|
193
|
+
gateSnapshotStorePromise,
|
|
194
|
+
appDirectory,
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
});
|
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clamp,
|
|
3
|
+
redactObject,
|
|
4
|
+
type TelemetryEnvelope,
|
|
5
|
+
type TelemetryExporter,
|
|
6
|
+
} from './envelope';
|
|
7
|
+
import {
|
|
8
|
+
type TelemetryExporterHealthStatus,
|
|
9
|
+
type TelemetryQueueStats,
|
|
10
|
+
type TelemetryRegistryOptions,
|
|
11
|
+
type TelemetrySloAlert,
|
|
12
|
+
type TelemetrySloAlertType,
|
|
13
|
+
TelemetryStartupHealthError,
|
|
14
|
+
} from './registryTypes';
|
|
15
|
+
|
|
16
|
+
export class TelemetryRegistry {
|
|
17
|
+
private readonly exporters: TelemetryExporter[] = [];
|
|
18
|
+
private readonly queue: TelemetryEnvelope[] = [];
|
|
19
|
+
private readonly redactionKeys: Set<string>;
|
|
20
|
+
private readonly service: string;
|
|
21
|
+
private readonly module: string;
|
|
22
|
+
private readonly environment: string;
|
|
23
|
+
private readonly samplingRate: number;
|
|
24
|
+
private readonly maxBatchSize: number;
|
|
25
|
+
private readonly maxQueueSize: number;
|
|
26
|
+
private readonly flushIntervalMs: number;
|
|
27
|
+
private readonly flushTimer?: ReturnType<typeof setInterval>;
|
|
28
|
+
private droppedCount = 0;
|
|
29
|
+
private totalDroppedCount = 0;
|
|
30
|
+
private flushing: Promise<void> | null = null;
|
|
31
|
+
private readonly exporterHealth = new Map<
|
|
32
|
+
string,
|
|
33
|
+
TelemetryExporterHealthStatus
|
|
34
|
+
>();
|
|
35
|
+
private readonly queueUtilizationWarnThreshold: number;
|
|
36
|
+
private readonly queueDroppedWarnThreshold: number;
|
|
37
|
+
private readonly alertCooldownMs: number;
|
|
38
|
+
private readonly onSloAlert?: (alert: TelemetrySloAlert) => void;
|
|
39
|
+
private readonly lastSloAlertAt = new Map<TelemetrySloAlertType, number>();
|
|
40
|
+
|
|
41
|
+
constructor(options: TelemetryRegistryOptions) {
|
|
42
|
+
this.service = options.service;
|
|
43
|
+
this.module = options.module;
|
|
44
|
+
this.environment = options.environment;
|
|
45
|
+
this.samplingRate = clamp(options.samplingRate ?? 1, 0, 1);
|
|
46
|
+
this.maxBatchSize = Math.max(1, options.maxBatchSize ?? 50);
|
|
47
|
+
this.maxQueueSize = Math.max(1, options.maxQueueSize ?? 1000);
|
|
48
|
+
this.flushIntervalMs = Math.max(50, options.flushIntervalMs ?? 1000);
|
|
49
|
+
this.redactionKeys = new Set(options.redactionKeys || []);
|
|
50
|
+
this.queueUtilizationWarnThreshold = clamp(
|
|
51
|
+
options.slo?.queueUtilizationWarnThreshold ?? 0.8,
|
|
52
|
+
0,
|
|
53
|
+
1,
|
|
54
|
+
);
|
|
55
|
+
this.queueDroppedWarnThreshold = Math.max(
|
|
56
|
+
1,
|
|
57
|
+
options.slo?.queueDroppedWarnThreshold ?? 1,
|
|
58
|
+
);
|
|
59
|
+
this.alertCooldownMs = Math.max(0, options.slo?.alertCooldownMs ?? 60_000);
|
|
60
|
+
this.onSloAlert = options.slo?.onAlert;
|
|
61
|
+
|
|
62
|
+
this.flushTimer = setInterval(() => {
|
|
63
|
+
void this.flush();
|
|
64
|
+
}, this.flushIntervalMs);
|
|
65
|
+
if (typeof (this.flushTimer as NodeJS.Timeout).unref === 'function') {
|
|
66
|
+
(this.flushTimer as NodeJS.Timeout).unref();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async register(exporter: TelemetryExporter) {
|
|
71
|
+
this.exporters.push(exporter);
|
|
72
|
+
this.exporterHealth.set(exporter.name, {
|
|
73
|
+
name: exporter.name,
|
|
74
|
+
healthy: true,
|
|
75
|
+
failures: 0,
|
|
76
|
+
});
|
|
77
|
+
if (exporter.init) {
|
|
78
|
+
try {
|
|
79
|
+
await exporter.init({
|
|
80
|
+
service: this.service,
|
|
81
|
+
module: this.module,
|
|
82
|
+
environment: this.environment,
|
|
83
|
+
});
|
|
84
|
+
this.markExporterHealthy(exporter.name);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
this.markExporterFailure(exporter.name, error);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
this.markExporterHealthy(exporter.name);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private getOrCreateExporterHealth(name: string) {
|
|
95
|
+
const existing = this.exporterHealth.get(name);
|
|
96
|
+
if (existing) {
|
|
97
|
+
return existing;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const next: TelemetryExporterHealthStatus = {
|
|
101
|
+
name,
|
|
102
|
+
healthy: true,
|
|
103
|
+
failures: 0,
|
|
104
|
+
};
|
|
105
|
+
this.exporterHealth.set(name, next);
|
|
106
|
+
return next;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private markExporterHealthy(name: string) {
|
|
110
|
+
const status = this.getOrCreateExporterHealth(name);
|
|
111
|
+
status.healthy = true;
|
|
112
|
+
status.lastSuccessAt = Date.now();
|
|
113
|
+
status.lastError = undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private markExporterFailure(name: string, error: unknown) {
|
|
117
|
+
const status = this.getOrCreateExporterHealth(name);
|
|
118
|
+
status.healthy = false;
|
|
119
|
+
status.failures += 1;
|
|
120
|
+
status.lastFailureAt = Date.now();
|
|
121
|
+
status.lastError = error instanceof Error ? error.message : String(error);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private maybeEmitSloAlert(
|
|
125
|
+
type: TelemetrySloAlertType,
|
|
126
|
+
value: number,
|
|
127
|
+
threshold: number,
|
|
128
|
+
) {
|
|
129
|
+
if (!this.onSloAlert || value < threshold) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const now = Date.now();
|
|
134
|
+
const lastTimestamp = this.lastSloAlertAt.get(type) ?? 0;
|
|
135
|
+
if (now - lastTimestamp < this.alertCooldownMs) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
this.lastSloAlertAt.set(type, now);
|
|
140
|
+
const queueDepth = this.queue.length;
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
this.onSloAlert({
|
|
144
|
+
timestamp: now,
|
|
145
|
+
service: this.service,
|
|
146
|
+
module: this.module,
|
|
147
|
+
environment: this.environment,
|
|
148
|
+
type,
|
|
149
|
+
value,
|
|
150
|
+
threshold,
|
|
151
|
+
queueDepth,
|
|
152
|
+
queueCapacity: this.maxQueueSize,
|
|
153
|
+
queueUtilization: queueDepth / this.maxQueueSize,
|
|
154
|
+
totalDropped: this.totalDroppedCount,
|
|
155
|
+
});
|
|
156
|
+
} catch (_error) {
|
|
157
|
+
// SLO alert hooks must never crash telemetry pipeline.
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
enqueue(envelope: TelemetryEnvelope) {
|
|
162
|
+
if (this.samplingRate < 1 && Math.random() > this.samplingRate) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const redactedEnvelope =
|
|
167
|
+
this.redactionKeys.size > 0
|
|
168
|
+
? ({
|
|
169
|
+
...envelope,
|
|
170
|
+
attributes: redactObject(envelope.attributes, this.redactionKeys),
|
|
171
|
+
} as TelemetryEnvelope)
|
|
172
|
+
: envelope;
|
|
173
|
+
|
|
174
|
+
if (this.queue.length >= this.maxQueueSize) {
|
|
175
|
+
this.queue.shift();
|
|
176
|
+
this.droppedCount += 1;
|
|
177
|
+
this.totalDroppedCount += 1;
|
|
178
|
+
this.maybeEmitSloAlert(
|
|
179
|
+
'queue.drop',
|
|
180
|
+
this.droppedCount,
|
|
181
|
+
this.queueDroppedWarnThreshold,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
this.queue.push(redactedEnvelope);
|
|
186
|
+
this.maybeEmitSloAlert(
|
|
187
|
+
'queue.utilization',
|
|
188
|
+
this.queue.length / this.maxQueueSize,
|
|
189
|
+
this.queueUtilizationWarnThreshold,
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
if (this.queue.length >= this.maxBatchSize) {
|
|
193
|
+
void this.flush();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
enqueueMetric(input: {
|
|
198
|
+
name: string;
|
|
199
|
+
value: number;
|
|
200
|
+
unit?: string;
|
|
201
|
+
traceId?: string;
|
|
202
|
+
spanId?: string;
|
|
203
|
+
parentSpanId?: string;
|
|
204
|
+
tags?: Record<string, string>;
|
|
205
|
+
attributes?: Record<string, unknown>;
|
|
206
|
+
}) {
|
|
207
|
+
this.enqueue({
|
|
208
|
+
timestamp: Date.now(),
|
|
209
|
+
service: this.service,
|
|
210
|
+
module: this.module,
|
|
211
|
+
environment: this.environment,
|
|
212
|
+
signalType: 'metric',
|
|
213
|
+
name: input.name,
|
|
214
|
+
value: input.value,
|
|
215
|
+
unit: input.unit || 'count',
|
|
216
|
+
traceId: input.traceId,
|
|
217
|
+
spanId: input.spanId,
|
|
218
|
+
parentSpanId: input.parentSpanId,
|
|
219
|
+
tags: input.tags,
|
|
220
|
+
attributes: input.attributes,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
enqueueLog(input: {
|
|
225
|
+
name: string;
|
|
226
|
+
level: string;
|
|
227
|
+
traceId?: string;
|
|
228
|
+
spanId?: string;
|
|
229
|
+
parentSpanId?: string;
|
|
230
|
+
tags?: Record<string, string>;
|
|
231
|
+
attributes?: Record<string, unknown>;
|
|
232
|
+
error?: TelemetryEnvelope['error'];
|
|
233
|
+
}) {
|
|
234
|
+
this.enqueue({
|
|
235
|
+
timestamp: Date.now(),
|
|
236
|
+
service: this.service,
|
|
237
|
+
module: this.module,
|
|
238
|
+
environment: this.environment,
|
|
239
|
+
signalType: 'log',
|
|
240
|
+
name: input.name,
|
|
241
|
+
level: input.level,
|
|
242
|
+
traceId: input.traceId,
|
|
243
|
+
spanId: input.spanId,
|
|
244
|
+
parentSpanId: input.parentSpanId,
|
|
245
|
+
tags: input.tags,
|
|
246
|
+
attributes: input.attributes,
|
|
247
|
+
error: input.error,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
enqueueTrace(input: {
|
|
252
|
+
name: string;
|
|
253
|
+
traceId?: string;
|
|
254
|
+
spanId?: string;
|
|
255
|
+
parentSpanId?: string;
|
|
256
|
+
tags?: Record<string, string>;
|
|
257
|
+
attributes?: Record<string, unknown>;
|
|
258
|
+
}) {
|
|
259
|
+
this.enqueue({
|
|
260
|
+
timestamp: Date.now(),
|
|
261
|
+
service: this.service,
|
|
262
|
+
module: this.module,
|
|
263
|
+
environment: this.environment,
|
|
264
|
+
signalType: 'trace',
|
|
265
|
+
name: input.name,
|
|
266
|
+
traceId: input.traceId,
|
|
267
|
+
spanId: input.spanId,
|
|
268
|
+
parentSpanId: input.parentSpanId,
|
|
269
|
+
tags: input.tags,
|
|
270
|
+
attributes: input.attributes,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private buildDroppedEnvelope(droppedCount: number): TelemetryEnvelope {
|
|
275
|
+
return {
|
|
276
|
+
timestamp: Date.now(),
|
|
277
|
+
service: this.service,
|
|
278
|
+
module: this.module,
|
|
279
|
+
environment: this.environment,
|
|
280
|
+
signalType: 'metric',
|
|
281
|
+
name: 'telemetry.queue.dropped',
|
|
282
|
+
value: droppedCount,
|
|
283
|
+
unit: 'count',
|
|
284
|
+
tags: {
|
|
285
|
+
reason: 'queue_backpressure',
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private buildQueueDepthEnvelope(queueDepth: number): TelemetryEnvelope {
|
|
291
|
+
return {
|
|
292
|
+
timestamp: Date.now(),
|
|
293
|
+
service: this.service,
|
|
294
|
+
module: this.module,
|
|
295
|
+
environment: this.environment,
|
|
296
|
+
signalType: 'metric',
|
|
297
|
+
name: 'telemetry.queue.depth',
|
|
298
|
+
value: queueDepth,
|
|
299
|
+
unit: 'count',
|
|
300
|
+
tags: {
|
|
301
|
+
capacity: String(this.maxQueueSize),
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private buildQueueUtilizationEnvelope(queueDepth: number): TelemetryEnvelope {
|
|
307
|
+
return {
|
|
308
|
+
timestamp: Date.now(),
|
|
309
|
+
service: this.service,
|
|
310
|
+
module: this.module,
|
|
311
|
+
environment: this.environment,
|
|
312
|
+
signalType: 'metric',
|
|
313
|
+
name: 'telemetry.queue.utilization',
|
|
314
|
+
value: queueDepth / this.maxQueueSize,
|
|
315
|
+
unit: 'ratio',
|
|
316
|
+
tags: {
|
|
317
|
+
capacity: String(this.maxQueueSize),
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private async emitBatch(batch: TelemetryEnvelope[]) {
|
|
323
|
+
const results = await Promise.allSettled(
|
|
324
|
+
this.exporters.map(async exporter => {
|
|
325
|
+
await exporter.emit(batch);
|
|
326
|
+
return exporter.name;
|
|
327
|
+
}),
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
for (const [index, result] of results.entries()) {
|
|
331
|
+
const exporterName = this.exporters[index]?.name || `exporter-${index}`;
|
|
332
|
+
if (result.status === 'rejected') {
|
|
333
|
+
this.markExporterFailure(exporterName, result.reason);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
this.markExporterHealthy(exporterName);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private buildStartupProbeEnvelope(): TelemetryEnvelope {
|
|
342
|
+
return {
|
|
343
|
+
timestamp: Date.now(),
|
|
344
|
+
service: this.service,
|
|
345
|
+
module: this.module,
|
|
346
|
+
environment: this.environment,
|
|
347
|
+
signalType: 'log',
|
|
348
|
+
name: 'telemetry.exporter.startup_probe',
|
|
349
|
+
level: 'info',
|
|
350
|
+
tags: {
|
|
351
|
+
phase: 'startup',
|
|
352
|
+
},
|
|
353
|
+
attributes: {
|
|
354
|
+
source: 'TelemetryRegistry',
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async startupHealthCheck(options?: { failLoud?: boolean }) {
|
|
360
|
+
if (this.exporters.length === 0) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const probeBatch = [this.buildStartupProbeEnvelope()];
|
|
365
|
+
const failedExporters: TelemetryExporterHealthStatus[] = [];
|
|
366
|
+
|
|
367
|
+
await Promise.all(
|
|
368
|
+
this.exporters.map(async exporter => {
|
|
369
|
+
try {
|
|
370
|
+
await exporter.emit(probeBatch);
|
|
371
|
+
this.markExporterHealthy(exporter.name);
|
|
372
|
+
} catch (error) {
|
|
373
|
+
this.markExporterFailure(exporter.name, error);
|
|
374
|
+
const status = this.exporterHealth.get(exporter.name);
|
|
375
|
+
if (status) {
|
|
376
|
+
failedExporters.push({ ...status });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}),
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
if ((options?.failLoud ?? true) && failedExporters.length > 0) {
|
|
383
|
+
throw new TelemetryStartupHealthError(failedExporters);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
getExporterHealth(): TelemetryExporterHealthStatus[] {
|
|
388
|
+
return Array.from(this.exporterHealth.values()).map(item => ({
|
|
389
|
+
...item,
|
|
390
|
+
}));
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
getQueueStats(): TelemetryQueueStats {
|
|
394
|
+
return {
|
|
395
|
+
depth: this.queue.length,
|
|
396
|
+
capacity: this.maxQueueSize,
|
|
397
|
+
utilization: this.queue.length / this.maxQueueSize,
|
|
398
|
+
pendingDropped: this.droppedCount,
|
|
399
|
+
totalDropped: this.totalDroppedCount,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private async flushInternal() {
|
|
404
|
+
const queueDepthBeforeFlush = this.queue.length;
|
|
405
|
+
if (queueDepthBeforeFlush > 0) {
|
|
406
|
+
this.queue.unshift(
|
|
407
|
+
this.buildQueueUtilizationEnvelope(queueDepthBeforeFlush),
|
|
408
|
+
);
|
|
409
|
+
this.queue.unshift(this.buildQueueDepthEnvelope(queueDepthBeforeFlush));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (this.droppedCount > 0) {
|
|
413
|
+
const droppedCount = this.droppedCount;
|
|
414
|
+
this.droppedCount = 0;
|
|
415
|
+
this.queue.unshift(this.buildDroppedEnvelope(droppedCount));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (this.queue.length === 0) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (this.exporters.length === 0) {
|
|
423
|
+
this.queue.length = 0;
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
while (this.queue.length > 0) {
|
|
428
|
+
const batch = this.queue.splice(0, this.maxBatchSize);
|
|
429
|
+
await this.emitBatch(batch);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
await Promise.allSettled(
|
|
433
|
+
this.exporters.map(async exporter => {
|
|
434
|
+
if (exporter.flush) {
|
|
435
|
+
await exporter.flush();
|
|
436
|
+
}
|
|
437
|
+
}),
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
flush() {
|
|
442
|
+
if (this.flushing) {
|
|
443
|
+
return this.flushing;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
this.flushing = this.flushInternal().finally(() => {
|
|
447
|
+
this.flushing = null;
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
return this.flushing;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
async shutdown() {
|
|
454
|
+
if (this.flushTimer) {
|
|
455
|
+
clearInterval(this.flushTimer);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
await this.flush();
|
|
459
|
+
await Promise.allSettled(
|
|
460
|
+
this.exporters.map(async exporter => {
|
|
461
|
+
if (exporter.shutdown) {
|
|
462
|
+
await exporter.shutdown();
|
|
463
|
+
}
|
|
464
|
+
}),
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
}
|