@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.9 → 3.9.3-ultramodern.10
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/README.md +3 -1
- package/dist/cjs/backend-federation-security/index.js +73 -152
- package/dist/cjs/bff-policy/index.js +7 -0
- package/dist/cjs/bff-policy/node.js +20 -4
- package/dist/cjs/bff-policy/operationContracts.js +150 -135
- package/dist/cjs/bff-policy/operationIdentity.js +81 -0
- package/dist/cjs/bff-policy/producer.js +79 -0
- package/dist/cjs/localisedLoader.js +65 -0
- package/dist/cjs/localisedLoaderPlugin.js +2 -2
- package/dist/cjs/module-federation-css/remote.js +2 -1
- package/dist/cjs/static-serving/staticModuleFederation.js +49 -12
- package/dist/esm/backend-federation-security/index.mjs +73 -152
- package/dist/esm/bff-policy/index.mjs +1 -0
- package/dist/esm/bff-policy/operationIdentity.mjs +30 -0
- package/dist/esm/bff-policy/producer.mjs +31 -0
- package/dist/esm/localisedLoader.mjs +24 -0
- package/dist/esm/localisedLoaderPlugin.mjs +2 -2
- package/dist/esm/module-federation-css/remote.mjs +2 -1
- package/dist/esm-node/backend-federation-security/index.mjs +73 -152
- package/dist/esm-node/bff-policy/index.mjs +1 -0
- package/dist/esm-node/bff-policy/node.mjs +1 -0
- package/dist/esm-node/bff-policy/operationContracts.mjs +5 -27
- package/dist/esm-node/bff-policy/operationIdentity.mjs +31 -0
- package/dist/esm-node/bff-policy/producer.mjs +32 -0
- package/dist/esm-node/localisedLoader.mjs +25 -0
- package/dist/esm-node/localisedLoaderPlugin.mjs +2 -2
- package/dist/esm-node/module-federation-css/remote.mjs +2 -1
- package/dist/esm-node/static-serving/staticModuleFederation.mjs +49 -12
- package/dist/types/backend-federation-security/node.d.ts +1 -1
- package/dist/types/bff-policy/index.d.ts +1 -0
- package/dist/types/bff-policy/node.d.ts +1 -0
- package/dist/types/bff-policy/operationContracts.d.ts +2 -41
- package/dist/types/bff-policy/operationIdentity.d.ts +34 -0
- package/dist/types/bff-policy/producer.d.ts +10 -0
- package/dist/types/localisedLoader.d.ts +15 -0
- package/dist/types/module-federation-css/remote.d.ts +2 -0
- package/dist/types/serverConfig.d.ts +4 -2
- package/dist/types/telemetry/runtimeEndpoints.d.ts +2 -13
- package/dist/types-esm/backend-federation-security/node.d.mts +1 -1
- package/dist/types-esm/bff-policy/index.d.mts +1 -0
- package/dist/types-esm/bff-policy/node.d.mts +1 -0
- package/dist/types-esm/bff-policy/operationContracts.d.mts +2 -41
- package/dist/types-esm/bff-policy/operationIdentity.d.mts +34 -0
- package/dist/types-esm/bff-policy/producer.d.mts +10 -0
- package/dist/types-esm/localisedLoader.d.mts +15 -0
- package/dist/types-esm/module-federation-css/remote.d.mts +2 -0
- package/dist/types-esm/serverConfig.d.mts +4 -2
- package/dist/types-esm/telemetry/runtimeEndpoints.d.mts +2 -13
- package/package.json +48 -20
- package/src/backend-federation-security/index.ts +151 -318
- package/src/backend-federation-security/node.ts +24 -22
- package/src/bff-policy/index.ts +1 -0
- package/src/bff-policy/node.ts +1 -0
- package/src/bff-policy/operationContracts.ts +13 -86
- package/src/bff-policy/operationIdentity.ts +90 -0
- package/src/bff-policy/producer.ts +40 -0
- package/src/contract-gate-snapshot-store/resolve.ts +0 -1
- package/src/localisedLoader.ts +50 -0
- package/src/localisedLoaderPlugin.ts +6 -7
- package/src/module-federation-css/remote.ts +3 -1
- package/src/serverConfig.ts +7 -0
- package/src/static-serving/staticModuleFederation.ts +127 -22
- package/src/telemetry/runtimeEndpoints.ts +2 -2
- package/rslib.config.mts +0 -38
- package/rstest.config.mts +0 -18
- package/tests/backendFederationSecurity.test.ts +0 -416
- package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +0 -450
- package/tests/bff-policy/crossProjectPolicy.test.ts +0 -197
- package/tests/bff-policy/operationContracts.test.ts +0 -221
- package/tests/bff-policy/optionalZodPeer.test.ts +0 -30
- package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +0 -76
- package/tests/contractGateSnapshotObserver.test.ts +0 -347
- package/tests/contractGateSnapshotStore.test.ts +0 -225
- package/tests/env.test.ts +0 -54
- package/tests/helpers.ts +0 -20
- package/tests/localisedLoader.test.ts +0 -120
- package/tests/mfCache.test.ts +0 -63
- package/tests/moduleFederationCss.test.ts +0 -348
- package/tests/nativeErrorResponse.test.ts +0 -118
- package/tests/nativeServerAssembly.test.ts +0 -385
- package/tests/runtimeLifecycle.test.ts +0 -159
- package/tests/static-serving/ordering.test.ts +0 -214
- package/tests/static-serving/static.test.ts +0 -411
- package/tests/telemetry.test.ts +0 -248
- package/tests/telemetryHealthMonitor.test.ts +0 -183
- package/tests/telemetryHealthObservation.test.ts +0 -302
- package/tests/telemetryLifecycle.test.ts +0 -123
- package/tests/telemetryRegistryQueueMatrix.test.ts +0 -130
- package/tests/telemetryTraceparent.test.ts +0 -167
- package/tests/tsconfig.json +0 -11
- package/tsconfig.json +0 -10
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
import { createDefaultPlugins, createServerBase } from '@modern-js/server-core';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import os from 'os';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import { injectTelemetryPlugin } from '../src/telemetry';
|
|
6
|
-
import { getDefaultAppContext, getDefaultConfig } from './helpers';
|
|
7
|
-
|
|
8
|
-
type ServerBase = ReturnType<typeof createServerBase>;
|
|
9
|
-
|
|
10
|
-
const SIGNAL_ENDPOINT = '/_modern/contract-gates/runtime-fallback';
|
|
11
|
-
const TOKEN_HEADER = 'x-modernjs-runtime-signal-token';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Boots a real server with the telemetry plugin and the given snapshot-
|
|
15
|
-
* observation
|
|
16
|
-
* config, then tears the temp workspace down. `server.init()` is left to the
|
|
17
|
-
* caller so fail-closed init can be asserted.
|
|
18
|
-
*/
|
|
19
|
-
const withHealthObservation = async (
|
|
20
|
-
snapshotObservation: Record<string, unknown>,
|
|
21
|
-
run: (server: ServerBase, snapshotPath: string) => Promise<void>,
|
|
22
|
-
) => {
|
|
23
|
-
const tempDir = fs.mkdtempSync(
|
|
24
|
-
path.join(os.tmpdir(), 'modern-telemetry-health-observation-'),
|
|
25
|
-
);
|
|
26
|
-
const snapshotPath = path.join(tempDir, '.modern/contract-gates.json');
|
|
27
|
-
try {
|
|
28
|
-
const config = getDefaultConfig();
|
|
29
|
-
config.server = {
|
|
30
|
-
telemetry: {
|
|
31
|
-
enabled: true,
|
|
32
|
-
health: {
|
|
33
|
-
enabled: true,
|
|
34
|
-
minConsecutiveFailedEvaluations: 1,
|
|
35
|
-
snapshotObservation: {
|
|
36
|
-
enabled: true,
|
|
37
|
-
gateSnapshotPath: snapshotPath,
|
|
38
|
-
...snapshotObservation,
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
} as any;
|
|
43
|
-
|
|
44
|
-
const server = createServerBase({
|
|
45
|
-
config,
|
|
46
|
-
pwd: tempDir,
|
|
47
|
-
appContext: getDefaultAppContext(),
|
|
48
|
-
});
|
|
49
|
-
server.addPlugins([
|
|
50
|
-
...createDefaultPlugins({ logger: false }),
|
|
51
|
-
injectTelemetryPlugin(),
|
|
52
|
-
]);
|
|
53
|
-
|
|
54
|
-
await run(server, snapshotPath);
|
|
55
|
-
} finally {
|
|
56
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const postSignal = (server: ServerBase, token: string | null, body: unknown) =>
|
|
61
|
-
server.request(
|
|
62
|
-
SIGNAL_ENDPOINT,
|
|
63
|
-
{
|
|
64
|
-
method: 'POST',
|
|
65
|
-
headers: new Headers({
|
|
66
|
-
'content-type': 'application/json',
|
|
67
|
-
...(token ? { [TOKEN_HEADER]: token } : {}),
|
|
68
|
-
}),
|
|
69
|
-
body: JSON.stringify(body),
|
|
70
|
-
},
|
|
71
|
-
{},
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
describe('telemetry health observation runtime signal', () => {
|
|
75
|
-
test('rejects oversized runtime fallback signal payload', async () => {
|
|
76
|
-
await withHealthObservation(
|
|
77
|
-
{
|
|
78
|
-
runtimeFallbackSignal: {
|
|
79
|
-
enabled: true,
|
|
80
|
-
endpoint: SIGNAL_ENDPOINT,
|
|
81
|
-
maxBodyBytes: 16,
|
|
82
|
-
auth: { expectedValue: 'oversize-signal-token' },
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
async server => {
|
|
86
|
-
await server.init();
|
|
87
|
-
const response = await postSignal(server, 'oversize-signal-token', {
|
|
88
|
-
reason: 'x'.repeat(2_048),
|
|
89
|
-
});
|
|
90
|
-
expect(response.status).toBe(413);
|
|
91
|
-
const payload = (await response.json()) as { error?: string };
|
|
92
|
-
expect(String(payload.error)).toContain('payload too large');
|
|
93
|
-
},
|
|
94
|
-
);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('requires runtime fallback auth token when configured', async () => {
|
|
98
|
-
await withHealthObservation(
|
|
99
|
-
{
|
|
100
|
-
runtimeFallbackSignal: {
|
|
101
|
-
enabled: true,
|
|
102
|
-
endpoint: SIGNAL_ENDPOINT,
|
|
103
|
-
auth: {
|
|
104
|
-
enabled: true,
|
|
105
|
-
headerName: TOKEN_HEADER,
|
|
106
|
-
expectedValue: 'top-secret-token',
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
async (server, snapshotPath) => {
|
|
111
|
-
await server.init();
|
|
112
|
-
const signal = {
|
|
113
|
-
reason: 'remote_load_failed',
|
|
114
|
-
phase: 'load',
|
|
115
|
-
appName: 'crm',
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const rejected = await postSignal(server, null, signal);
|
|
119
|
-
expect(rejected.status).toBe(401);
|
|
120
|
-
expect(fs.existsSync(snapshotPath)).toBe(false);
|
|
121
|
-
|
|
122
|
-
const accepted = await postSignal(server, 'top-secret-token', signal);
|
|
123
|
-
expect(accepted.status).toBe(202);
|
|
124
|
-
expect(fs.existsSync(snapshotPath)).toBe(true);
|
|
125
|
-
},
|
|
126
|
-
);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
test('enforces runtime fallback trust policy before mutating gate snapshots', async () => {
|
|
130
|
-
await withHealthObservation(
|
|
131
|
-
{
|
|
132
|
-
runtimeFallbackSignal: {
|
|
133
|
-
enabled: true,
|
|
134
|
-
endpoint: SIGNAL_ENDPOINT,
|
|
135
|
-
auth: { expectedValue: 'trust-policy-token' },
|
|
136
|
-
trustPolicy: {
|
|
137
|
-
allowedApps: ['crm-shell'],
|
|
138
|
-
allowedEntryOrigins: ['https://erp.example.com'],
|
|
139
|
-
expectedRuntimeDigests: { 'crm-shell': 'digest-crm-v1' },
|
|
140
|
-
enforceRuntimeDigest: true,
|
|
141
|
-
},
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
async (server, snapshotPath) => {
|
|
145
|
-
await server.init();
|
|
146
|
-
const base = {
|
|
147
|
-
reason: 'remote_load_failed',
|
|
148
|
-
phase: 'load',
|
|
149
|
-
appName: 'crm-shell',
|
|
150
|
-
entry: 'https://erp.example.com/remoteEntry.js',
|
|
151
|
-
runtimeDigest: 'digest-crm-v1',
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
const untrustedApp = await postSignal(server, 'trust-policy-token', {
|
|
155
|
-
...base,
|
|
156
|
-
appName: 'unknown-app',
|
|
157
|
-
});
|
|
158
|
-
expect(untrustedApp.status).toBe(403);
|
|
159
|
-
|
|
160
|
-
const digestMismatch = await postSignal(server, 'trust-policy-token', {
|
|
161
|
-
...base,
|
|
162
|
-
runtimeDigest: 'digest-wrong',
|
|
163
|
-
});
|
|
164
|
-
expect(digestMismatch.status).toBe(403);
|
|
165
|
-
|
|
166
|
-
const trusted = await postSignal(server, 'trust-policy-token', {
|
|
167
|
-
...base,
|
|
168
|
-
metadata: {
|
|
169
|
-
compatibility: { '@tanstack/react-router': '1.170.15' },
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
expect(trusted.status).toBe(202);
|
|
173
|
-
|
|
174
|
-
const snapshot = JSON.parse(fs.readFileSync(snapshotPath, 'utf8')) as {
|
|
175
|
-
gates?: Record<string, any>;
|
|
176
|
-
};
|
|
177
|
-
const gate = snapshot.gates?.['runtime-mf-fallback-health'];
|
|
178
|
-
expect(gate?.passed).toBe(false);
|
|
179
|
-
expect(
|
|
180
|
-
gate?.metadata?.metadata?.compatibility?.['@tanstack/react-router'],
|
|
181
|
-
).toBe('1.170.15');
|
|
182
|
-
},
|
|
183
|
-
);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
test('dedupes repeated fallback events and rate limits rotated identities', async () => {
|
|
187
|
-
await withHealthObservation(
|
|
188
|
-
{
|
|
189
|
-
runtimeFallbackSignal: {
|
|
190
|
-
enabled: true,
|
|
191
|
-
endpoint: SIGNAL_ENDPOINT,
|
|
192
|
-
auth: { expectedValue: 'rate-limit-token' },
|
|
193
|
-
trustPolicy: {
|
|
194
|
-
dedupeWindowMs: 60_000,
|
|
195
|
-
maxSignalsPerWindow: 1,
|
|
196
|
-
windowMs: 60_000,
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
},
|
|
200
|
-
async server => {
|
|
201
|
-
await server.init();
|
|
202
|
-
const sendSignal = (appName: string) =>
|
|
203
|
-
postSignal(server, 'rate-limit-token', {
|
|
204
|
-
reason: 'remote_mount_failed',
|
|
205
|
-
phase: 'mount',
|
|
206
|
-
appName,
|
|
207
|
-
entry: `https://${appName}.example.com/remoteEntry.js`,
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
expect((await sendSignal('app-a')).status).toBe(202);
|
|
211
|
-
|
|
212
|
-
const duplicate = await sendSignal('app-a');
|
|
213
|
-
expect(duplicate.status).toBe(202);
|
|
214
|
-
expect(((await duplicate.json()) as any).deduped).toBe(true);
|
|
215
|
-
|
|
216
|
-
// A rotated appName/entry pair used to mint a fresh rate-limit bucket;
|
|
217
|
-
// the limiter is now keyed on connection identity instead.
|
|
218
|
-
expect((await sendSignal('app-b')).status).toBe(429);
|
|
219
|
-
},
|
|
220
|
-
);
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
test('keeps the runtime fallback signal endpoint disabled unless explicitly enabled', async () => {
|
|
224
|
-
// no runtimeFallbackSignal config: the health kill switch must stay OFF.
|
|
225
|
-
await withHealthObservation({}, async (server, snapshotPath) => {
|
|
226
|
-
await server.init();
|
|
227
|
-
const response = await postSignal(server, null, {
|
|
228
|
-
reason: 'remote_load_failed',
|
|
229
|
-
phase: 'load',
|
|
230
|
-
appName: 'dashboard',
|
|
231
|
-
});
|
|
232
|
-
expect(response.status).toBe(404);
|
|
233
|
-
expect(fs.existsSync(snapshotPath)).toBe(false);
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
test('refuses to enable the runtime fallback signal endpoint without an auth token', async () => {
|
|
238
|
-
await withHealthObservation(
|
|
239
|
-
// enabled with no auth token configured: init must fail closed.
|
|
240
|
-
{ runtimeFallbackSignal: { enabled: true } },
|
|
241
|
-
async server => {
|
|
242
|
-
await expect(server.init()).rejects.toThrow(
|
|
243
|
-
/requires an auth token|auth\.expectedValue/,
|
|
244
|
-
);
|
|
245
|
-
},
|
|
246
|
-
);
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
test('runtime status endpoint stays a bare health probe without configured auth', async () => {
|
|
250
|
-
await withHealthObservation({}, async server => {
|
|
251
|
-
await server.init();
|
|
252
|
-
const response = await server.request('/_modern/runtime/status', {}, {});
|
|
253
|
-
expect(response.status).toBe(200);
|
|
254
|
-
const payload = (await response.json()) as Record<string, unknown>;
|
|
255
|
-
expect(payload.ok).toBe(true);
|
|
256
|
-
// No telemetry/health/trust internals are disclosed without auth.
|
|
257
|
-
expect(payload.telemetry).toBeUndefined();
|
|
258
|
-
expect(payload.health).toBeUndefined();
|
|
259
|
-
expect(payload.runtimeFallbackSignal).toBeUndefined();
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
test('runtime status auth works even when the signal endpoint stays disabled', async () => {
|
|
264
|
-
await withHealthObservation(
|
|
265
|
-
{
|
|
266
|
-
runtimeFallbackSignal: {
|
|
267
|
-
// endpoint stays disabled, but its auth config still guards the
|
|
268
|
-
// status endpoint detail view.
|
|
269
|
-
auth: { enabled: true, expectedValue: 'status-only-token' },
|
|
270
|
-
},
|
|
271
|
-
},
|
|
272
|
-
async server => {
|
|
273
|
-
await server.init();
|
|
274
|
-
|
|
275
|
-
const signalResponse = await postSignal(server, 'status-only-token', {
|
|
276
|
-
appName: 'dashboard',
|
|
277
|
-
});
|
|
278
|
-
expect(signalResponse.status).toBe(404);
|
|
279
|
-
|
|
280
|
-
const unauthorized = await server.request(
|
|
281
|
-
'/_modern/runtime/status',
|
|
282
|
-
{},
|
|
283
|
-
{},
|
|
284
|
-
);
|
|
285
|
-
expect(unauthorized.status).toBe(401);
|
|
286
|
-
|
|
287
|
-
const authorized = await server.request(
|
|
288
|
-
'/_modern/runtime/status',
|
|
289
|
-
{
|
|
290
|
-
method: 'GET',
|
|
291
|
-
headers: new Headers({ [TOKEN_HEADER]: 'status-only-token' }),
|
|
292
|
-
},
|
|
293
|
-
{},
|
|
294
|
-
);
|
|
295
|
-
expect(authorized.status).toBe(200);
|
|
296
|
-
const payload = (await authorized.json()) as Record<string, any>;
|
|
297
|
-
expect(payload.health?.enabled).toBe(true);
|
|
298
|
-
expect(payload.runtimeFallbackSignal?.enabled).toBe(false);
|
|
299
|
-
},
|
|
300
|
-
);
|
|
301
|
-
});
|
|
302
|
-
});
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
import {
|
|
3
|
-
createDefaultPlugins,
|
|
4
|
-
createServerBase,
|
|
5
|
-
type ServerPlugin,
|
|
6
|
-
} from '@modern-js/server-core';
|
|
7
|
-
import { injectTelemetryPlugin } from '../src/telemetry';
|
|
8
|
-
import { getDefaultAppContext, getDefaultConfig } from './helpers';
|
|
9
|
-
|
|
10
|
-
const emitMonitorEventsPlugin = (input: {
|
|
11
|
-
path: string;
|
|
12
|
-
message: string;
|
|
13
|
-
count: number;
|
|
14
|
-
}): ServerPlugin => ({
|
|
15
|
-
name: 'emit-monitor-events',
|
|
16
|
-
setup(api) {
|
|
17
|
-
api.onPrepare(() => {
|
|
18
|
-
const { middlewares } = api.getServerContext();
|
|
19
|
-
middlewares.push({
|
|
20
|
-
name: 'emit-monitor-events',
|
|
21
|
-
path: input.path,
|
|
22
|
-
handler: async (c: any) => {
|
|
23
|
-
const monitors = c.get('monitors');
|
|
24
|
-
for (let index = 0; index < input.count; index++) {
|
|
25
|
-
monitors?.info(`${input.message}-${index}`);
|
|
26
|
-
}
|
|
27
|
-
return c.json({ ok: true });
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const waitFor = async (
|
|
35
|
-
predicate: () => boolean | Promise<boolean>,
|
|
36
|
-
timeoutMs = 2_000,
|
|
37
|
-
) => {
|
|
38
|
-
const startedAt = Date.now();
|
|
39
|
-
while (!(await predicate())) {
|
|
40
|
-
if (Date.now() - startedAt > timeoutMs) {
|
|
41
|
-
throw new Error('waitFor timed out');
|
|
42
|
-
}
|
|
43
|
-
await new Promise(resolve => setTimeout(resolve, 10));
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
describe('telemetry plugin lifecycle', () => {
|
|
48
|
-
test('flushes pending envelopes when the native runtime is disposed', async () => {
|
|
49
|
-
const fetchCalls: Array<{ url: string; body: string }> = [];
|
|
50
|
-
const fetchMock = rs.fn(async (url: any, init?: any) => {
|
|
51
|
-
fetchCalls.push({ url: String(url), body: String(init?.body ?? '') });
|
|
52
|
-
return new Response('{}', { status: 200 });
|
|
53
|
-
});
|
|
54
|
-
const originalFetch = globalThis.fetch;
|
|
55
|
-
globalThis.fetch = fetchMock as any;
|
|
56
|
-
|
|
57
|
-
const nodeServerStub = new EventEmitter();
|
|
58
|
-
const stubNodeServerPlugin: ServerPlugin = {
|
|
59
|
-
name: 'stub-node-server',
|
|
60
|
-
setup(api) {
|
|
61
|
-
api.updateServerContext({ nodeServer: nodeServerStub } as any);
|
|
62
|
-
},
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
let server: ReturnType<typeof createServerBase> | undefined;
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
const config = getDefaultConfig();
|
|
69
|
-
config.server = {
|
|
70
|
-
telemetry: {
|
|
71
|
-
enabled: true,
|
|
72
|
-
// make sure nothing flushes before native disposal:
|
|
73
|
-
flushIntervalMs: 60_000,
|
|
74
|
-
maxBatchSize: 500,
|
|
75
|
-
exporters: {
|
|
76
|
-
otlp: {
|
|
77
|
-
enabled: true,
|
|
78
|
-
endpoint: 'http://127.0.0.1:9/v1/logs',
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
} as any;
|
|
83
|
-
|
|
84
|
-
server = createServerBase({
|
|
85
|
-
config,
|
|
86
|
-
pwd: process.cwd(),
|
|
87
|
-
appContext: getDefaultAppContext(),
|
|
88
|
-
});
|
|
89
|
-
server.addPlugins([
|
|
90
|
-
...createDefaultPlugins({ logger: false }),
|
|
91
|
-
stubNodeServerPlugin,
|
|
92
|
-
injectTelemetryPlugin(),
|
|
93
|
-
emitMonitorEventsPlugin({
|
|
94
|
-
path: '/emit',
|
|
95
|
-
message: 'close-flush-probe',
|
|
96
|
-
count: 1,
|
|
97
|
-
}),
|
|
98
|
-
]);
|
|
99
|
-
await server.init();
|
|
100
|
-
|
|
101
|
-
// init performed the startup health probe only.
|
|
102
|
-
const callsAfterInit = fetchCalls.length;
|
|
103
|
-
expect(callsAfterInit).toBeGreaterThan(0);
|
|
104
|
-
|
|
105
|
-
const response = await server.request('/emit', {}, {});
|
|
106
|
-
expect(response.status).toBe(200);
|
|
107
|
-
|
|
108
|
-
// The envelope is queued but not flushed (long flush interval).
|
|
109
|
-
expect(
|
|
110
|
-
fetchCalls.some(call => call.body.includes('close-flush-probe')),
|
|
111
|
-
).toBe(false);
|
|
112
|
-
|
|
113
|
-
await server?.dispose();
|
|
114
|
-
|
|
115
|
-
await waitFor(() =>
|
|
116
|
-
fetchCalls.some(call => call.body.includes('close-flush-probe')),
|
|
117
|
-
);
|
|
118
|
-
} finally {
|
|
119
|
-
await server?.dispose();
|
|
120
|
-
globalThis.fetch = originalFetch;
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
});
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type TelemetryEnvelope,
|
|
3
|
-
type TelemetryExporter,
|
|
4
|
-
TelemetryRegistry,
|
|
5
|
-
type TelemetrySloAlert,
|
|
6
|
-
} from '../src/telemetry';
|
|
7
|
-
|
|
8
|
-
const createEnvelope = (
|
|
9
|
-
partial: Partial<TelemetryEnvelope> = {},
|
|
10
|
-
): TelemetryEnvelope => ({
|
|
11
|
-
timestamp: Date.now(),
|
|
12
|
-
service: 'svc',
|
|
13
|
-
module: 'server',
|
|
14
|
-
environment: 'test',
|
|
15
|
-
signalType: 'metric',
|
|
16
|
-
name: 'server.handle.request',
|
|
17
|
-
value: 1,
|
|
18
|
-
unit: 'count',
|
|
19
|
-
...partial,
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const createRegistry = (
|
|
23
|
-
options: Partial<ConstructorParameters<typeof TelemetryRegistry>[0]> = {},
|
|
24
|
-
) =>
|
|
25
|
-
new TelemetryRegistry({
|
|
26
|
-
service: 'svc',
|
|
27
|
-
module: 'server',
|
|
28
|
-
environment: 'test',
|
|
29
|
-
flushIntervalMs: 60_000,
|
|
30
|
-
maxBatchSize: 100,
|
|
31
|
-
...options,
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const createMemoryExporter = (
|
|
35
|
-
batches: TelemetryEnvelope[][],
|
|
36
|
-
): TelemetryExporter => ({
|
|
37
|
-
name: 'memory',
|
|
38
|
-
emit(batch) {
|
|
39
|
-
batches.push([...batch]);
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
describe('telemetry registry queue matrix', () => {
|
|
44
|
-
test('aggregates exporter health across successful and failed emitters', async () => {
|
|
45
|
-
const batches: TelemetryEnvelope[][] = [];
|
|
46
|
-
const registry = createRegistry({ maxQueueSize: 4 });
|
|
47
|
-
const failingExporter = {
|
|
48
|
-
name: 'failing',
|
|
49
|
-
async emit() {
|
|
50
|
-
throw new Error('export failed');
|
|
51
|
-
},
|
|
52
|
-
} satisfies TelemetryExporter;
|
|
53
|
-
|
|
54
|
-
await registry.register(createMemoryExporter(batches));
|
|
55
|
-
await registry.register(failingExporter);
|
|
56
|
-
|
|
57
|
-
registry.enqueue(createEnvelope({ name: 'health.sample' }));
|
|
58
|
-
await registry.flush();
|
|
59
|
-
|
|
60
|
-
const healthByName = new Map(
|
|
61
|
-
registry.getExporterHealth().map(item => [item.name, item]),
|
|
62
|
-
);
|
|
63
|
-
expect(healthByName.get('memory')).toMatchObject({
|
|
64
|
-
name: 'memory',
|
|
65
|
-
healthy: true,
|
|
66
|
-
failures: 0,
|
|
67
|
-
});
|
|
68
|
-
expect(healthByName.get('memory')?.lastSuccessAt).toEqual(
|
|
69
|
-
expect.any(Number),
|
|
70
|
-
);
|
|
71
|
-
expect(healthByName.get('failing')).toMatchObject({
|
|
72
|
-
name: 'failing',
|
|
73
|
-
healthy: false,
|
|
74
|
-
failures: 1,
|
|
75
|
-
lastError: 'export failed',
|
|
76
|
-
});
|
|
77
|
-
expect(healthByName.get('failing')?.lastFailureAt).toEqual(
|
|
78
|
-
expect.any(Number),
|
|
79
|
-
);
|
|
80
|
-
expect(batches.flat().some(item => item.name === 'health.sample')).toBe(
|
|
81
|
-
true,
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
await registry.shutdown();
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('clamps NaN queue utilization SLO threshold before alerting', async () => {
|
|
88
|
-
const alerts: TelemetrySloAlert[] = [];
|
|
89
|
-
const registry = createRegistry({
|
|
90
|
-
maxQueueSize: 2,
|
|
91
|
-
slo: {
|
|
92
|
-
queueUtilizationWarnThreshold: Number.NaN,
|
|
93
|
-
alertCooldownMs: 0,
|
|
94
|
-
onAlert(alert) {
|
|
95
|
-
alerts.push(alert);
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
registry.enqueue(createEnvelope({ name: 'half-full' }));
|
|
101
|
-
expect(registry.getQueueStats()).toEqual({
|
|
102
|
-
depth: 1,
|
|
103
|
-
capacity: 2,
|
|
104
|
-
utilization: 0.5,
|
|
105
|
-
pendingDropped: 0,
|
|
106
|
-
totalDropped: 0,
|
|
107
|
-
});
|
|
108
|
-
expect(alerts.filter(alert => alert.type === 'queue.utilization')).toEqual(
|
|
109
|
-
[],
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
registry.enqueue(createEnvelope({ name: 'full' }));
|
|
113
|
-
const utilizationAlerts = alerts.filter(
|
|
114
|
-
alert => alert.type === 'queue.utilization',
|
|
115
|
-
);
|
|
116
|
-
expect(utilizationAlerts).toEqual([
|
|
117
|
-
expect.objectContaining({
|
|
118
|
-
type: 'queue.utilization',
|
|
119
|
-
value: 1,
|
|
120
|
-
threshold: 1,
|
|
121
|
-
queueDepth: 2,
|
|
122
|
-
queueCapacity: 2,
|
|
123
|
-
queueUtilization: 1,
|
|
124
|
-
totalDropped: 0,
|
|
125
|
-
}),
|
|
126
|
-
]);
|
|
127
|
-
|
|
128
|
-
await registry.shutdown();
|
|
129
|
-
});
|
|
130
|
-
});
|