@bleedingdev/modern-js-server-runtime-extensions 3.5.0-ultramodern.4 → 3.5.0-ultramodern.40

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.
Files changed (175) hide show
  1. package/dist/cjs/contract-gate-snapshot-normalization.js +122 -0
  2. package/dist/cjs/contract-gate-snapshot-store/file-store.js +102 -0
  3. package/dist/cjs/contract-gate-snapshot-store/http-store.js +133 -0
  4. package/dist/cjs/contract-gate-snapshot-store/index.js +58 -0
  5. package/dist/cjs/contract-gate-snapshot-store/resolve.js +124 -0
  6. package/dist/cjs/contract-gate-snapshot-store/types.js +44 -0
  7. package/dist/cjs/contractGateAutopilot.js +22 -70
  8. package/dist/cjs/index.js +6 -25
  9. package/dist/cjs/module-federation-css/collector.js +79 -0
  10. package/dist/cjs/module-federation-css/index.js +55 -0
  11. package/dist/cjs/module-federation-css/manifest.js +129 -0
  12. package/dist/cjs/module-federation-css/plugin.js +71 -0
  13. package/dist/cjs/module-federation-css/remote.js +133 -0
  14. package/dist/cjs/runtime-fallback-signal/index.js +93 -0
  15. package/dist/cjs/runtime-fallback-signal/model.js +157 -0
  16. package/dist/cjs/runtime-fallback-signal/resolver.js +163 -0
  17. package/dist/cjs/runtime-fallback-signal/store.js +83 -0
  18. package/dist/cjs/runtimeFallbackSignal.js +32 -281
  19. package/dist/cjs/telemetry/canary.js +180 -0
  20. package/dist/cjs/telemetry/canaryEvaluation.js +79 -0
  21. package/dist/cjs/telemetry/canarySetup.js +107 -0
  22. package/dist/cjs/telemetry/envelope.js +223 -0
  23. package/dist/cjs/telemetry/exporters.js +148 -0
  24. package/dist/cjs/telemetry/lifecycle.js +98 -0
  25. package/dist/cjs/telemetry/metrics.js +115 -0
  26. package/dist/cjs/telemetry/plugin.js +185 -0
  27. package/dist/cjs/telemetry/registry.js +296 -0
  28. package/dist/cjs/telemetry/registryTypes.js +46 -0
  29. package/dist/cjs/telemetry/runtimeEndpoints.js +135 -0
  30. package/dist/cjs/telemetry.js +32 -343
  31. package/dist/cjs/telemetryCore.js +14 -767
  32. package/dist/esm/contract-gate-snapshot-normalization.mjs +81 -0
  33. package/dist/esm/contract-gate-snapshot-store/file-store.mjs +51 -0
  34. package/dist/esm/contract-gate-snapshot-store/http-store.mjs +92 -0
  35. package/dist/esm/contract-gate-snapshot-store/index.mjs +4 -0
  36. package/dist/esm/contract-gate-snapshot-store/resolve.mjs +70 -0
  37. package/dist/esm/contract-gate-snapshot-store/types.mjs +3 -0
  38. package/dist/esm/contractGateAutopilot.mjs +21 -69
  39. package/dist/esm/index.mjs +1 -2
  40. package/dist/esm/module-federation-css/collector.mjs +38 -0
  41. package/dist/esm/module-federation-css/index.mjs +4 -0
  42. package/dist/esm/module-federation-css/manifest.mjs +69 -0
  43. package/dist/esm/module-federation-css/plugin.mjs +33 -0
  44. package/dist/esm/module-federation-css/remote.mjs +92 -0
  45. package/dist/esm/runtime-fallback-signal/index.mjs +3 -0
  46. package/dist/esm/runtime-fallback-signal/model.mjs +73 -0
  47. package/dist/esm/runtime-fallback-signal/resolver.mjs +107 -0
  48. package/dist/esm/runtime-fallback-signal/store.mjs +39 -0
  49. package/dist/esm/runtimeFallbackSignal.mjs +1 -222
  50. package/dist/esm/telemetry/canary.mjs +142 -0
  51. package/dist/esm/telemetry/canaryEvaluation.mjs +41 -0
  52. package/dist/esm/telemetry/canarySetup.mjs +69 -0
  53. package/dist/esm/telemetry/envelope.mjs +164 -0
  54. package/dist/esm/telemetry/exporters.mjs +104 -0
  55. package/dist/esm/telemetry/lifecycle.mjs +60 -0
  56. package/dist/esm/telemetry/metrics.mjs +77 -0
  57. package/dist/esm/telemetry/plugin.mjs +87 -0
  58. package/dist/esm/telemetry/registry.mjs +258 -0
  59. package/dist/esm/telemetry/registryTypes.mjs +8 -0
  60. package/dist/esm/telemetry/runtimeEndpoints.mjs +94 -0
  61. package/dist/esm/telemetry.mjs +1 -275
  62. package/dist/esm/telemetryCore.mjs +6 -759
  63. package/dist/esm-node/contract-gate-snapshot-normalization.mjs +82 -0
  64. package/dist/esm-node/contract-gate-snapshot-store/file-store.mjs +52 -0
  65. package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +93 -0
  66. package/dist/esm-node/contract-gate-snapshot-store/index.mjs +5 -0
  67. package/dist/esm-node/contract-gate-snapshot-store/resolve.mjs +71 -0
  68. package/dist/esm-node/contract-gate-snapshot-store/types.mjs +4 -0
  69. package/dist/esm-node/contractGateAutopilot.mjs +21 -69
  70. package/dist/esm-node/index.mjs +1 -2
  71. package/dist/esm-node/module-federation-css/collector.mjs +39 -0
  72. package/dist/esm-node/module-federation-css/index.mjs +5 -0
  73. package/dist/esm-node/module-federation-css/manifest.mjs +70 -0
  74. package/dist/esm-node/module-federation-css/plugin.mjs +34 -0
  75. package/dist/esm-node/module-federation-css/remote.mjs +93 -0
  76. package/dist/esm-node/runtime-fallback-signal/index.mjs +4 -0
  77. package/dist/esm-node/runtime-fallback-signal/model.mjs +74 -0
  78. package/dist/esm-node/runtime-fallback-signal/resolver.mjs +108 -0
  79. package/dist/esm-node/runtime-fallback-signal/store.mjs +40 -0
  80. package/dist/esm-node/runtimeFallbackSignal.mjs +1 -222
  81. package/dist/esm-node/telemetry/canary.mjs +143 -0
  82. package/dist/esm-node/telemetry/canaryEvaluation.mjs +42 -0
  83. package/dist/esm-node/telemetry/canarySetup.mjs +70 -0
  84. package/dist/esm-node/telemetry/envelope.mjs +165 -0
  85. package/dist/esm-node/telemetry/exporters.mjs +105 -0
  86. package/dist/esm-node/telemetry/lifecycle.mjs +61 -0
  87. package/dist/esm-node/telemetry/metrics.mjs +78 -0
  88. package/dist/esm-node/telemetry/plugin.mjs +88 -0
  89. package/dist/esm-node/telemetry/registry.mjs +259 -0
  90. package/dist/esm-node/telemetry/registryTypes.mjs +9 -0
  91. package/dist/esm-node/telemetry/runtimeEndpoints.mjs +95 -0
  92. package/dist/esm-node/telemetry.mjs +1 -275
  93. package/dist/esm-node/telemetryCore.mjs +6 -759
  94. package/dist/types/contract-gate-snapshot-normalization.d.ts +15 -0
  95. package/dist/types/contract-gate-snapshot-store/file-store.d.ts +3 -0
  96. package/dist/types/contract-gate-snapshot-store/http-store.d.ts +5 -0
  97. package/dist/types/contract-gate-snapshot-store/index.d.ts +5 -0
  98. package/dist/types/contract-gate-snapshot-store/resolve.d.ts +9 -0
  99. package/dist/types/{contractGateSnapshotStore.d.ts → contract-gate-snapshot-store/types.d.ts} +1 -11
  100. package/dist/types/contractGateAutopilot.d.ts +3 -6
  101. package/dist/types/index.d.ts +1 -2
  102. package/dist/types/module-federation-css/collector.d.ts +25 -0
  103. package/dist/types/module-federation-css/index.d.ts +8 -0
  104. package/dist/types/module-federation-css/manifest.d.ts +28 -0
  105. package/dist/types/module-federation-css/plugin.d.ts +23 -0
  106. package/dist/types/module-federation-css/remote.d.ts +20 -0
  107. package/dist/types/runtime-fallback-signal/index.d.ts +4 -0
  108. package/dist/types/runtime-fallback-signal/model.d.ts +70 -0
  109. package/dist/types/runtime-fallback-signal/resolver.d.ts +29 -0
  110. package/dist/types/runtime-fallback-signal/store.d.ts +4 -0
  111. package/dist/types/runtimeFallbackSignal.d.ts +1 -94
  112. package/dist/types/telemetry/canary.d.ts +85 -0
  113. package/dist/types/telemetry/canaryEvaluation.d.ts +20 -0
  114. package/dist/types/telemetry/canarySetup.d.ts +17 -0
  115. package/dist/types/telemetry/envelope.d.ts +55 -0
  116. package/dist/types/telemetry/exporters.d.ts +12 -0
  117. package/dist/types/telemetry/lifecycle.d.ts +18 -0
  118. package/dist/types/telemetry/metrics.d.ts +3 -0
  119. package/dist/types/telemetry/plugin.d.ts +12 -0
  120. package/dist/types/telemetry/registry.d.ts +69 -0
  121. package/dist/types/telemetry/registryTypes.d.ts +50 -0
  122. package/dist/types/telemetry/runtimeEndpoints.d.ts +35 -0
  123. package/dist/types/telemetry.d.ts +1 -12
  124. package/dist/types/telemetryCore.d.ts +6 -257
  125. package/package.json +10 -10
  126. package/src/contract-gate-snapshot-normalization.ts +155 -0
  127. package/src/contract-gate-snapshot-store/file-store.ts +83 -0
  128. package/src/contract-gate-snapshot-store/http-store.ts +159 -0
  129. package/src/contract-gate-snapshot-store/index.ts +20 -0
  130. package/src/contract-gate-snapshot-store/resolve.ts +179 -0
  131. package/src/contract-gate-snapshot-store/types.ts +64 -0
  132. package/src/contractGateAutopilot.ts +39 -131
  133. package/src/index.ts +1 -17
  134. package/src/module-federation-css/collector.ts +82 -0
  135. package/src/module-federation-css/index.ts +14 -0
  136. package/src/module-federation-css/manifest.ts +163 -0
  137. package/src/module-federation-css/plugin.ts +86 -0
  138. package/src/module-federation-css/remote.ts +165 -0
  139. package/src/runtime-fallback-signal/index.ts +34 -0
  140. package/src/runtime-fallback-signal/model.ts +230 -0
  141. package/src/runtime-fallback-signal/resolver.ts +304 -0
  142. package/src/runtime-fallback-signal/store.ts +78 -0
  143. package/src/runtimeFallbackSignal.ts +1 -584
  144. package/src/telemetry/canary.ts +291 -0
  145. package/src/telemetry/canaryEvaluation.ts +83 -0
  146. package/src/telemetry/canarySetup.ts +160 -0
  147. package/src/telemetry/envelope.ts +277 -0
  148. package/src/telemetry/exporters.ts +165 -0
  149. package/src/telemetry/lifecycle.ts +138 -0
  150. package/src/telemetry/metrics.ts +141 -0
  151. package/src/telemetry/plugin.ts +197 -0
  152. package/src/telemetry/registry.ts +424 -0
  153. package/src/telemetry/registryTypes.ts +63 -0
  154. package/src/telemetry/runtimeEndpoints.ts +172 -0
  155. package/src/telemetry.ts +1 -554
  156. package/src/telemetryCore.ts +34 -1332
  157. package/tests/contractGateAutopilot.test.ts +205 -0
  158. package/tests/contractGateSnapshotStore.test.ts +54 -1
  159. package/tests/helpers.ts +1 -0
  160. package/tests/moduleFederationCss.test.ts +1 -1
  161. package/tests/registration.test.ts +1 -1
  162. package/tests/telemetry.test.ts +51 -0
  163. package/tests/telemetryAutopilot.test.ts +141 -0
  164. package/tests/telemetryCanaryOrchestrator.test.ts +225 -0
  165. package/tests/telemetryLifecycle.test.ts +253 -2
  166. package/tests/telemetryRegistryQueueMatrix.test.ts +227 -0
  167. package/dist/cjs/contractGateSnapshotStore.js +0 -253
  168. package/dist/cjs/moduleFederationCss.js +0 -285
  169. package/dist/esm/contractGateSnapshotStore.mjs +0 -190
  170. package/dist/esm/moduleFederationCss.mjs +0 -225
  171. package/dist/esm-node/contractGateSnapshotStore.mjs +0 -192
  172. package/dist/esm-node/moduleFederationCss.mjs +0 -226
  173. package/dist/types/moduleFederationCss.d.ts +0 -87
  174. package/src/contractGateSnapshotStore.ts +0 -420
  175. package/src/moduleFederationCss.ts +0 -473
@@ -0,0 +1,291 @@
1
+ import {
2
+ evaluateCanaryHealth,
3
+ type TelemetryCanaryHealthEvaluation,
4
+ } from './canaryEvaluation';
5
+ import { clamp } from './envelope';
6
+
7
+ import type { TelemetryRegistry } from './registry';
8
+
9
+ import type { TelemetryQueueStats } from './registryTypes';
10
+
11
+ export type TelemetryCanaryState = 'canary' | 'promoted' | 'rolled_back';
12
+ export type TelemetryCanaryAction = 'hold' | 'promote' | 'rollback';
13
+ export type TelemetryCanaryFailureReason =
14
+ | 'queue_utilization'
15
+ | 'queue_dropped'
16
+ | 'unhealthy_exporter'
17
+ | 'contract_gate_missing'
18
+ | 'contract_gate_failed';
19
+
20
+ export interface TelemetryCanaryFailure {
21
+ reason: TelemetryCanaryFailureReason;
22
+ gate?: string;
23
+ message?: string;
24
+ threshold?: number;
25
+ value?: number;
26
+ }
27
+
28
+ export interface TelemetryCanaryContractGateStatus {
29
+ name: string;
30
+ passed: boolean;
31
+ reason?: string;
32
+ updatedAt: number;
33
+ }
34
+
35
+ export interface TelemetryCanaryDecision {
36
+ timestamp: number;
37
+ action: TelemetryCanaryAction;
38
+ state: TelemetryCanaryState;
39
+ consecutiveHealthy: number;
40
+ consecutiveFailures: number;
41
+ failures: TelemetryCanaryFailure[];
42
+ queueStats: TelemetryQueueStats;
43
+ unhealthyExporterCount: number;
44
+ contractGates: TelemetryCanaryContractGateStatus[];
45
+ }
46
+
47
+ export interface TelemetryCanaryStatusSnapshot {
48
+ timestamp: number;
49
+ state: TelemetryCanaryState;
50
+ consecutiveHealthy: number;
51
+ consecutiveFailures: number;
52
+ queueStats: TelemetryQueueStats;
53
+ unhealthyExporterCount: number;
54
+ requiredContractGates: string[];
55
+ contractGates: TelemetryCanaryContractGateStatus[];
56
+ failurePreview: TelemetryCanaryFailure[];
57
+ }
58
+
59
+ export interface TelemetryCanaryOrchestratorOptions {
60
+ registry: TelemetryRegistry;
61
+ evaluationIntervalMs?: number;
62
+ minConsecutiveHealthyEvaluations?: number;
63
+ rollbackConsecutiveFailures?: number;
64
+ maxQueueUtilization?: number;
65
+ maxTotalDropped?: number;
66
+ maxUnhealthyExporters?: number;
67
+ requiredContractGates?: string[];
68
+ onEvaluate?: (decision: TelemetryCanaryDecision) => void;
69
+ onPromote?: (decision: TelemetryCanaryDecision) => void;
70
+ onRollback?: (decision: TelemetryCanaryDecision) => void;
71
+ }
72
+
73
+ export class TelemetryCanaryOrchestrator {
74
+ private readonly registry: TelemetryRegistry;
75
+ private readonly evaluationIntervalMs: number;
76
+ private readonly minConsecutiveHealthyEvaluations: number;
77
+ private readonly rollbackConsecutiveFailures: number;
78
+ private readonly maxQueueUtilization: number;
79
+ private readonly maxTotalDropped: number;
80
+ private readonly maxUnhealthyExporters: number;
81
+ private requiredContractGates: string[] = [];
82
+ private readonly onEvaluate?: (decision: TelemetryCanaryDecision) => void;
83
+ private readonly onPromote?: (decision: TelemetryCanaryDecision) => void;
84
+ private readonly onRollback?: (decision: TelemetryCanaryDecision) => void;
85
+ private readonly contractGates = new Map<
86
+ string,
87
+ TelemetryCanaryContractGateStatus
88
+ >();
89
+ private state: TelemetryCanaryState = 'canary';
90
+ private consecutiveHealthy = 0;
91
+ private consecutiveFailures = 0;
92
+ private evaluationTimer?: ReturnType<typeof setInterval>;
93
+
94
+ constructor(options: TelemetryCanaryOrchestratorOptions) {
95
+ this.registry = options.registry;
96
+ this.evaluationIntervalMs = Math.max(
97
+ 250,
98
+ options.evaluationIntervalMs ?? 15_000,
99
+ );
100
+ this.minConsecutiveHealthyEvaluations = Math.max(
101
+ 1,
102
+ options.minConsecutiveHealthyEvaluations ?? 3,
103
+ );
104
+ this.rollbackConsecutiveFailures = Math.max(
105
+ 1,
106
+ options.rollbackConsecutiveFailures ?? 2,
107
+ );
108
+ this.maxQueueUtilization = clamp(options.maxQueueUtilization ?? 0.8, 0, 1);
109
+ this.maxTotalDropped = Math.max(0, options.maxTotalDropped ?? 0);
110
+ this.maxUnhealthyExporters = Math.max(
111
+ 0,
112
+ options.maxUnhealthyExporters ?? 0,
113
+ );
114
+ this.setRequiredContractGates(options.requiredContractGates || []);
115
+ this.onEvaluate = options.onEvaluate;
116
+ this.onPromote = options.onPromote;
117
+ this.onRollback = options.onRollback;
118
+ }
119
+
120
+ setRequiredContractGates(gates: string[]) {
121
+ this.requiredContractGates = Array.from(
122
+ new Set(gates.map(item => item.trim()).filter(Boolean)),
123
+ );
124
+ }
125
+
126
+ addRequiredContractGate(name: string) {
127
+ const normalizedName = name.trim();
128
+ if (!normalizedName) {
129
+ return;
130
+ }
131
+
132
+ if (!this.requiredContractGates.includes(normalizedName)) {
133
+ this.requiredContractGates.push(normalizedName);
134
+ }
135
+ }
136
+
137
+ setContractGate(name: string, passed: boolean, reason?: string) {
138
+ this.contractGates.set(name, {
139
+ name,
140
+ passed,
141
+ reason,
142
+ updatedAt: Date.now(),
143
+ });
144
+ }
145
+
146
+ setContractGates(
147
+ gates: Record<string, boolean | { passed: boolean; reason?: string }>,
148
+ ) {
149
+ for (const [name, value] of Object.entries(gates)) {
150
+ if (typeof value === 'boolean') {
151
+ this.setContractGate(name, value);
152
+ continue;
153
+ }
154
+
155
+ this.setContractGate(name, value.passed, value.reason);
156
+ }
157
+ }
158
+
159
+ resetToCanary() {
160
+ this.state = 'canary';
161
+ this.consecutiveHealthy = 0;
162
+ this.consecutiveFailures = 0;
163
+ }
164
+
165
+ private collectFailures(): TelemetryCanaryHealthEvaluation {
166
+ const queueStats = this.registry.getQueueStats();
167
+ const unhealthyExporterCount = this.registry
168
+ .getExporterHealth()
169
+ .filter(item => !item.healthy).length;
170
+
171
+ return evaluateCanaryHealth(
172
+ {
173
+ queueStats,
174
+ unhealthyExporterCount,
175
+ requiredContractGates: this.requiredContractGates,
176
+ contractGates: this.contractGates,
177
+ },
178
+ {
179
+ maxQueueUtilization: this.maxQueueUtilization,
180
+ maxTotalDropped: this.maxTotalDropped,
181
+ maxUnhealthyExporters: this.maxUnhealthyExporters,
182
+ },
183
+ );
184
+ }
185
+
186
+ evaluate(): TelemetryCanaryDecision {
187
+ const now = Date.now();
188
+ const { failures, queueStats, unhealthyExporterCount } =
189
+ this.collectFailures();
190
+ let action: TelemetryCanaryAction = 'hold';
191
+
192
+ if (failures.length > 0) {
193
+ this.consecutiveHealthy = 0;
194
+ this.consecutiveFailures += 1;
195
+
196
+ if (
197
+ this.state !== 'rolled_back' &&
198
+ this.consecutiveFailures >= this.rollbackConsecutiveFailures
199
+ ) {
200
+ this.state = 'rolled_back';
201
+ action = 'rollback';
202
+ }
203
+ } else {
204
+ this.consecutiveFailures = 0;
205
+ this.consecutiveHealthy += 1;
206
+ if (
207
+ this.state === 'canary' &&
208
+ this.consecutiveHealthy >= this.minConsecutiveHealthyEvaluations
209
+ ) {
210
+ this.state = 'promoted';
211
+ action = 'promote';
212
+ }
213
+ }
214
+
215
+ const decision: TelemetryCanaryDecision = {
216
+ timestamp: now,
217
+ action,
218
+ state: this.state,
219
+ consecutiveHealthy: this.consecutiveHealthy,
220
+ consecutiveFailures: this.consecutiveFailures,
221
+ failures,
222
+ queueStats,
223
+ unhealthyExporterCount,
224
+ contractGates: Array.from(this.contractGates.values()).map(item => ({
225
+ ...item,
226
+ })),
227
+ };
228
+
229
+ try {
230
+ this.onEvaluate?.(decision);
231
+ } catch (_error) {
232
+ // canary observer hooks must never crash server.
233
+ }
234
+
235
+ if (action === 'promote') {
236
+ try {
237
+ this.onPromote?.(decision);
238
+ } catch (_error) {
239
+ // canary observer hooks must never crash server.
240
+ }
241
+ }
242
+
243
+ if (action === 'rollback') {
244
+ try {
245
+ this.onRollback?.(decision);
246
+ } catch (_error) {
247
+ // canary observer hooks must never crash server.
248
+ }
249
+ }
250
+
251
+ return decision;
252
+ }
253
+
254
+ getStatusSnapshot(): TelemetryCanaryStatusSnapshot {
255
+ const now = Date.now();
256
+ const { failures, queueStats, unhealthyExporterCount } =
257
+ this.collectFailures();
258
+ return {
259
+ timestamp: now,
260
+ state: this.state,
261
+ consecutiveHealthy: this.consecutiveHealthy,
262
+ consecutiveFailures: this.consecutiveFailures,
263
+ queueStats,
264
+ unhealthyExporterCount,
265
+ requiredContractGates: [...this.requiredContractGates],
266
+ contractGates: Array.from(this.contractGates.values()).map(item => ({
267
+ ...item,
268
+ })),
269
+ failurePreview: failures,
270
+ };
271
+ }
272
+
273
+ start() {
274
+ if (this.evaluationTimer) {
275
+ return;
276
+ }
277
+ this.evaluationTimer = setInterval(() => {
278
+ this.evaluate();
279
+ }, this.evaluationIntervalMs);
280
+ if (typeof (this.evaluationTimer as NodeJS.Timeout).unref === 'function') {
281
+ (this.evaluationTimer as NodeJS.Timeout).unref();
282
+ }
283
+ }
284
+
285
+ stop() {
286
+ if (this.evaluationTimer) {
287
+ clearInterval(this.evaluationTimer);
288
+ this.evaluationTimer = undefined;
289
+ }
290
+ }
291
+ }
@@ -0,0 +1,83 @@
1
+ import type {
2
+ TelemetryCanaryContractGateStatus,
3
+ TelemetryCanaryFailure,
4
+ } from './canary';
5
+
6
+ import type { TelemetryQueueStats } from './registryTypes';
7
+
8
+ interface TelemetryCanaryHealthMetrics {
9
+ queueStats: TelemetryQueueStats;
10
+ unhealthyExporterCount: number;
11
+ requiredContractGates: readonly string[];
12
+ contractGates: ReadonlyMap<string, TelemetryCanaryContractGateStatus>;
13
+ }
14
+
15
+ interface TelemetryCanaryHealthThresholds {
16
+ maxQueueUtilization: number;
17
+ maxTotalDropped: number;
18
+ maxUnhealthyExporters: number;
19
+ }
20
+
21
+ export interface TelemetryCanaryHealthEvaluation {
22
+ failures: TelemetryCanaryFailure[];
23
+ queueStats: TelemetryQueueStats;
24
+ unhealthyExporterCount: number;
25
+ }
26
+
27
+ export const evaluateCanaryHealth = (
28
+ metrics: TelemetryCanaryHealthMetrics,
29
+ thresholds: TelemetryCanaryHealthThresholds,
30
+ ): TelemetryCanaryHealthEvaluation => {
31
+ const failures: TelemetryCanaryFailure[] = [];
32
+ const { queueStats, unhealthyExporterCount } = metrics;
33
+
34
+ if (queueStats.utilization > thresholds.maxQueueUtilization) {
35
+ failures.push({
36
+ reason: 'queue_utilization',
37
+ threshold: thresholds.maxQueueUtilization,
38
+ value: queueStats.utilization,
39
+ });
40
+ }
41
+
42
+ if (queueStats.totalDropped > thresholds.maxTotalDropped) {
43
+ failures.push({
44
+ reason: 'queue_dropped',
45
+ threshold: thresholds.maxTotalDropped,
46
+ value: queueStats.totalDropped,
47
+ });
48
+ }
49
+
50
+ if (unhealthyExporterCount > thresholds.maxUnhealthyExporters) {
51
+ failures.push({
52
+ reason: 'unhealthy_exporter',
53
+ threshold: thresholds.maxUnhealthyExporters,
54
+ value: unhealthyExporterCount,
55
+ });
56
+ }
57
+
58
+ for (const gateName of metrics.requiredContractGates) {
59
+ const gate = metrics.contractGates.get(gateName);
60
+ if (!gate) {
61
+ failures.push({
62
+ reason: 'contract_gate_missing',
63
+ gate: gateName,
64
+ message: `Contract gate "${gateName}" is missing`,
65
+ });
66
+ continue;
67
+ }
68
+
69
+ if (!gate.passed) {
70
+ failures.push({
71
+ reason: 'contract_gate_failed',
72
+ gate: gateName,
73
+ message: gate.reason || `Contract gate "${gateName}" is not passing`,
74
+ });
75
+ }
76
+ }
77
+
78
+ return {
79
+ failures,
80
+ queueStats,
81
+ unhealthyExporterCount,
82
+ };
83
+ };
@@ -0,0 +1,160 @@
1
+ import type { ServerTelemetryUserConfig } from '@modern-js/server-core';
2
+ import {
3
+ type ContractGateSnapshotStore,
4
+ DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH,
5
+ resolveContractGateSnapshotPath,
6
+ resolveContractGateSnapshotStore,
7
+ } from '../contract-gate-snapshot-store';
8
+ import {
9
+ createRuntimeFallbackSignalRuntimeState,
10
+ DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS,
11
+ DEFAULT_RUNTIME_FALLBACK_GATE_NAME,
12
+ DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES,
13
+ normalizeRequiredRuntimeFallbackSignalAuthConfig,
14
+ normalizeRuntimeFallbackSignalAuthConfig,
15
+ normalizeRuntimeFallbackTrustPolicy,
16
+ type RuntimeFallbackSignalAuthConfig,
17
+ type RuntimeFallbackSignalConfig,
18
+ resolveRuntimeFallbackSignalEndpoint,
19
+ } from '../runtimeFallbackSignal';
20
+ import {
21
+ type TelemetryCanaryDecision,
22
+ TelemetryCanaryOrchestrator,
23
+ TelemetryRegistry,
24
+ } from '../telemetryCore';
25
+
26
+ type SetupTelemetryCanaryOptions = {
27
+ registry: TelemetryRegistry;
28
+ appDirectory: string;
29
+ canaryConfig: ServerTelemetryUserConfig['canary'] | undefined;
30
+ };
31
+
32
+ type SetupTelemetryCanaryResult = {
33
+ canaryOrchestrator?: TelemetryCanaryOrchestrator;
34
+ gateSnapshotStorePromise?: Promise<ContractGateSnapshotStore>;
35
+ runtimeFallbackSignalConfig?: RuntimeFallbackSignalConfig;
36
+ runtimeStatusAuthConfig?: RuntimeFallbackSignalAuthConfig;
37
+ };
38
+
39
+ function emitCanaryDecisionMetric(
40
+ registry: TelemetryRegistry,
41
+ decision: TelemetryCanaryDecision,
42
+ action: 'promote' | 'rollback',
43
+ ) {
44
+ try {
45
+ registry.enqueueMetric({
46
+ name: `telemetry.canary.${action}`,
47
+ value: 1,
48
+ unit: 'count',
49
+ tags: {
50
+ action,
51
+ state: decision.state,
52
+ failures: String(decision.failures.length),
53
+ },
54
+ });
55
+ } catch (_error) {
56
+ // Canary decision metrics are best-effort and must never break request flow.
57
+ }
58
+ }
59
+
60
+ export const setupTelemetryCanary = ({
61
+ registry,
62
+ appDirectory,
63
+ canaryConfig,
64
+ }: SetupTelemetryCanaryOptions): SetupTelemetryCanaryResult => {
65
+ if (!canaryConfig?.enabled) {
66
+ return {};
67
+ }
68
+
69
+ const contractGates = canaryConfig.contractGates as
70
+ | Record<string, boolean | { passed: boolean; reason?: string }>
71
+ | undefined;
72
+
73
+ const canaryOrchestrator = new TelemetryCanaryOrchestrator({
74
+ registry,
75
+ evaluationIntervalMs: canaryConfig.evaluationIntervalMs,
76
+ minConsecutiveHealthyEvaluations:
77
+ canaryConfig.minConsecutiveHealthyEvaluations,
78
+ rollbackConsecutiveFailures: canaryConfig.rollbackConsecutiveFailures,
79
+ maxQueueUtilization: canaryConfig.maxQueueUtilization,
80
+ maxTotalDropped: canaryConfig.maxTotalDropped,
81
+ maxUnhealthyExporters: canaryConfig.maxUnhealthyExporters,
82
+ requiredContractGates: Object.keys(contractGates || {}),
83
+ onPromote: decision => {
84
+ emitCanaryDecisionMetric(registry, decision, 'promote');
85
+ },
86
+ onRollback: decision => {
87
+ emitCanaryDecisionMetric(registry, decision, 'rollback');
88
+ },
89
+ });
90
+
91
+ if (contractGates) {
92
+ canaryOrchestrator.setContractGates(contractGates);
93
+ }
94
+
95
+ let gateSnapshotStorePromise: Promise<ContractGateSnapshotStore> | undefined;
96
+ let runtimeFallbackSignalConfig: RuntimeFallbackSignalConfig | undefined;
97
+
98
+ const autopilotEnabled = canaryConfig.autopilot?.enabled ?? true;
99
+ if (autopilotEnabled) {
100
+ const gateSnapshotPath = resolveContractGateSnapshotPath(
101
+ appDirectory,
102
+ canaryConfig.autopilot?.gateSnapshotPath,
103
+ );
104
+ gateSnapshotStorePromise = resolveContractGateSnapshotStore({
105
+ appDirectory,
106
+ gateSnapshotPath: gateSnapshotPath || DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH,
107
+ stateStore: canaryConfig.autopilot?.stateStore,
108
+ });
109
+
110
+ const runtimeSignalConfig = canaryConfig.autopilot?.runtimeFallbackSignal;
111
+ // The signal endpoint can persist failing contract gates and force
112
+ // canary orchestrator into rollback, so it is opt-in (default
113
+ // OFF) and requires a configured auth token when enabled.
114
+ const runtimeSignalEnabled = runtimeSignalConfig?.enabled === true;
115
+ if (runtimeSignalEnabled && gateSnapshotStorePromise) {
116
+ runtimeFallbackSignalConfig = {
117
+ endpoint: resolveRuntimeFallbackSignalEndpoint(
118
+ runtimeSignalConfig?.endpoint,
119
+ ),
120
+ gateName:
121
+ runtimeSignalConfig?.gateName?.trim() ||
122
+ DEFAULT_RUNTIME_FALLBACK_GATE_NAME,
123
+ gateSnapshotStore: gateSnapshotStorePromise,
124
+ failureHoldMs: Math.max(
125
+ 1_000,
126
+ runtimeSignalConfig?.failureHoldMs ??
127
+ DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS,
128
+ ),
129
+ maxBodyBytes: Math.max(
130
+ 512,
131
+ runtimeSignalConfig?.maxBodyBytes ??
132
+ DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES,
133
+ ),
134
+ auth: normalizeRequiredRuntimeFallbackSignalAuthConfig(
135
+ runtimeSignalConfig?.auth,
136
+ ),
137
+ trustPolicy: normalizeRuntimeFallbackTrustPolicy(
138
+ runtimeSignalConfig?.trustPolicy,
139
+ ),
140
+ runtimeState: createRuntimeFallbackSignalRuntimeState(),
141
+ };
142
+ }
143
+ }
144
+
145
+ // The runtime status endpoint exposes detail only to authenticated
146
+ // callers. Auth can be configured through runtimeFallbackSignal.auth
147
+ // even when the signal endpoint itself stays disabled.
148
+ const runtimeStatusAuthConfig =
149
+ runtimeFallbackSignalConfig?.auth ??
150
+ normalizeRuntimeFallbackSignalAuthConfig(
151
+ canaryConfig.autopilot?.runtimeFallbackSignal?.auth,
152
+ );
153
+
154
+ return {
155
+ canaryOrchestrator,
156
+ gateSnapshotStorePromise,
157
+ runtimeFallbackSignalConfig,
158
+ runtimeStatusAuthConfig,
159
+ };
160
+ };