-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstore.js
More file actions
744 lines (659 loc) · 24.5 KB
/
Copy pathstore.js
File metadata and controls
744 lines (659 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
import { randomUUID } from 'crypto';
export class MemoryRealtimeCacheStore {
constructor(options = {}) {
this.backend = 'memory';
this.defaultTtlSeconds = Number(options.defaultTtlSeconds || 7200);
this.webhookStatsRetentionSeconds = Number(options.webhookStatsRetentionSeconds || 25 * 60 * 60);
this.streams = new Map();
this.nodes = new Map();
this.webhookObservations = [];
}
async stats() {
this.sweepExpired();
return {
streams: this.streams.size,
nodes: this.nodes.size,
webhookObservations: this.webhookObservations.length
};
}
async listStreams() {
this.sweepExpired();
return Array.from(this.streams.values())
.map((entry) => withDerivedLatency(entry.value))
.sort((a, b) => String(b.updatedAt || '').localeCompare(String(a.updatedAt || '')));
}
async getStream(streamId) {
this.sweepExpired();
const stream = this.streams.get(streamId)?.value || null;
return withDerivedLatency(stream);
}
async upsertState(streamId, state, ttlSeconds) {
const stream = this.getOrCreateStream(streamId, ttlSeconds);
stream.state = {
...(stream.state || {}),
...state
};
stream.updatedAt = new Date().toISOString();
this.touchStream(streamId, ttlSeconds);
return stream;
}
async putSummary(streamId, summary, ttlSeconds) {
const stream = this.getOrCreateStream(streamId, ttlSeconds);
stream.summary = {
...(stream.summary || {}),
...summary,
updatedAt: summary.updatedAt || new Date().toISOString()
};
stream.updatedAt = new Date().toISOString();
this.touchStream(streamId, ttlSeconds);
return stream;
}
async putMetrics(streamId, metrics, ttlSeconds) {
const stream = this.getOrCreateStream(streamId, ttlSeconds);
stream.metrics = mergeMetrics(stream.metrics || {}, metrics || {});
stream.updatedAt = new Date().toISOString();
this.touchStream(streamId, ttlSeconds);
return stream;
}
async putLatencySample(streamId, sample, ttlSeconds) {
const stream = this.getOrCreateStream(streamId, ttlSeconds);
stream.latency = mergeLatencySample(stream.latency || {}, sample || {});
stream.updatedAt = new Date().toISOString();
this.touchStream(streamId, ttlSeconds);
return stream;
}
async putParticipants(streamId, participants, ttlSeconds) {
const stream = this.getOrCreateStream(streamId, ttlSeconds);
stream.participants = participants;
stream.updatedAt = new Date().toISOString();
this.touchStream(streamId, ttlSeconds);
return stream;
}
async appendEvent(streamId, event, options = {}) {
const stream = this.getOrCreateStream(streamId, options.ttlSeconds);
stream.events = stream.events || [];
const eventWithTimestamp = {
...event,
at: event.at || new Date().toISOString()
};
stream.events.push(eventWithTimestamp);
stream.latency = mergeLatencyFromEvent(stream.latency || {}, eventWithTimestamp);
const maxEvents = Number(options.maxEvents || 100);
if (stream.events.length > maxEvents) {
stream.events.splice(0, stream.events.length - maxEvents);
}
stream.updatedAt = new Date().toISOString();
this.touchStream(streamId, options.ttlSeconds);
return stream;
}
async putNodeHealth(nodeId, health, ttlSeconds) {
const node = {
nodeId,
...health,
updatedAt: new Date().toISOString()
};
this.nodes.set(nodeId, {
expiresAt: Date.now() + secondsToMs(ttlSeconds || this.defaultTtlSeconds),
value: node
});
return node;
}
async recordWebhookObservation(observation) {
const normalized = normalizeWebhookObservation(observation);
this.webhookObservations.push(normalized);
this.sweepExpiredWebhookObservations();
return this.webhookStats();
}
async webhookStats() {
this.sweepExpiredWebhookObservations();
return buildWebhookStats((category, cutoffMs, nowMs) => (
this.webhookObservations.filter((observation) => (
observation.category === category &&
observation.timestampMs >= cutoffMs &&
observation.timestampMs <= nowMs
)).length
));
}
async prometheusMetrics() {
const streams = await this.listStreams();
const webhookStats = await this.webhookStats();
return renderPrometheusMetrics(streams, this.nodes.size, webhookStats);
}
async close() {}
getOrCreateStream(streamId, ttlSeconds) {
const existing = this.streams.get(streamId)?.value;
if (existing) return existing;
const stream = {
streamId,
state: {},
summary: null,
metrics: {},
latency: {},
participants: null,
events: [],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
this.streams.set(streamId, {
expiresAt: Date.now() + secondsToMs(ttlSeconds || this.defaultTtlSeconds),
value: stream
});
return stream;
}
touchStream(streamId, ttlSeconds) {
const entry = this.streams.get(streamId);
if (!entry) return;
entry.expiresAt = Date.now() + secondsToMs(ttlSeconds || this.defaultTtlSeconds);
}
sweepExpired() {
const now = Date.now();
for (const [streamId, entry] of this.streams.entries()) {
if (entry.expiresAt <= now) this.streams.delete(streamId);
}
for (const [nodeId, entry] of this.nodes.entries()) {
if (entry.expiresAt <= now) this.nodes.delete(nodeId);
}
this.sweepExpiredWebhookObservations();
}
sweepExpiredWebhookObservations() {
const cutoffMs = Date.now() - secondsToMs(this.webhookStatsRetentionSeconds);
this.webhookObservations = this.webhookObservations.filter((observation) => observation.timestampMs >= cutoffMs);
}
}
export class RedisRealtimeCacheStore {
static async create(options = {}) {
const { createClient } = await import('redis');
const client = createClient({
url: options.url,
password: options.password
});
client.on('error', (error) => {
console.warn(`[06-realtime-cache] redis error: ${error.message}`);
});
await client.connect();
return new RedisRealtimeCacheStore({ ...options, client });
}
constructor(options = {}) {
this.backend = 'redis';
this.client = options.client;
this.defaultTtlSeconds = Number(options.defaultTtlSeconds || 7200);
this.webhookStatsRetentionSeconds = Number(options.webhookStatsRetentionSeconds || 25 * 60 * 60);
}
async stats() {
return {
streams: await this.client.sCard(key('index', 'streams')),
nodes: await this.client.sCard(key('index', 'nodes')),
webhookObservations: Number(await this.client.sendCommand(['ZCARD', webhookObservationKey('all')]))
};
}
async listStreams() {
const streamIds = await this.client.sMembers(key('index', 'streams'));
const streams = [];
for (const streamId of streamIds) {
const stream = await this.getStream(streamId);
if (stream) streams.push(stream);
else await this.client.sRem(key('index', 'streams'), streamId);
}
return streams.sort((a, b) => String(b.updatedAt || '').localeCompare(String(a.updatedAt || '')));
}
async getStream(streamId) {
const raw = await this.client.get(streamKey(streamId));
return raw ? withDerivedLatency(JSON.parse(raw)) : null;
}
async upsertState(streamId, state, ttlSeconds) {
const stream = await this.getOrCreateStream(streamId);
stream.state = {
...(stream.state || {}),
...state
};
stream.updatedAt = new Date().toISOString();
await this.saveStream(stream, ttlSeconds);
return stream;
}
async putSummary(streamId, summary, ttlSeconds) {
const stream = await this.getOrCreateStream(streamId);
stream.summary = {
...(stream.summary || {}),
...summary,
updatedAt: summary.updatedAt || new Date().toISOString()
};
stream.updatedAt = new Date().toISOString();
await this.saveStream(stream, ttlSeconds);
return stream;
}
async putMetrics(streamId, metrics, ttlSeconds) {
const stream = await this.getOrCreateStream(streamId);
stream.metrics = mergeMetrics(stream.metrics || {}, metrics || {});
stream.updatedAt = new Date().toISOString();
await this.saveStream(stream, ttlSeconds);
return stream;
}
async putLatencySample(streamId, sample, ttlSeconds) {
const stream = await this.getOrCreateStream(streamId);
stream.latency = mergeLatencySample(stream.latency || {}, sample || {});
stream.updatedAt = new Date().toISOString();
await this.saveStream(stream, ttlSeconds);
return stream;
}
async putParticipants(streamId, participants, ttlSeconds) {
const stream = await this.getOrCreateStream(streamId);
stream.participants = participants;
stream.updatedAt = new Date().toISOString();
await this.saveStream(stream, ttlSeconds);
return stream;
}
async appendEvent(streamId, event, options = {}) {
const stream = await this.getOrCreateStream(streamId);
stream.events = stream.events || [];
const eventWithTimestamp = {
...event,
at: event.at || new Date().toISOString()
};
stream.events.push(eventWithTimestamp);
stream.latency = mergeLatencyFromEvent(stream.latency || {}, eventWithTimestamp);
const maxEvents = Number(options.maxEvents || 100);
if (stream.events.length > maxEvents) {
stream.events.splice(0, stream.events.length - maxEvents);
}
stream.updatedAt = new Date().toISOString();
await this.saveStream(stream, options.ttlSeconds);
return stream;
}
async putNodeHealth(nodeId, health, ttlSeconds) {
const node = {
nodeId,
...health,
updatedAt: new Date().toISOString()
};
await this.client.sAdd(key('index', 'nodes'), nodeId);
await this.client.set(nodeKey(nodeId), JSON.stringify(node), {
EX: Number(ttlSeconds || this.defaultTtlSeconds)
});
return node;
}
async recordWebhookObservation(observation) {
const normalized = normalizeWebhookObservation(observation);
const member = `${normalized.timestampMs}:${randomUUID()}`;
await this.client.sendCommand(['ZADD', webhookObservationKey(normalized.category), String(normalized.timestampMs), member]);
if (normalized.category !== 'total') {
await this.client.sendCommand(['ZADD', webhookObservationKey('all'), String(normalized.timestampMs), `${member}:all`]);
}
await this.cleanupWebhookObservations();
return this.webhookStats();
}
async webhookStats() {
await this.cleanupWebhookObservations();
return buildWebhookStats(async (category, cutoffMs, nowMs) => (
Number(await this.client.sendCommand(['ZCOUNT', webhookObservationKey(category), String(cutoffMs), String(nowMs)]))
));
}
async prometheusMetrics() {
const streams = await this.listStreams();
const nodeCount = await this.client.sCard(key('index', 'nodes'));
const webhookStats = await this.webhookStats();
return renderPrometheusMetrics(streams, nodeCount, webhookStats);
}
async close() {
await this.client.quit();
}
async getOrCreateStream(streamId) {
const existing = await this.getStream(streamId);
if (existing) return existing;
return {
streamId,
state: {},
summary: null,
metrics: {},
latency: {},
participants: null,
events: [],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
}
async saveStream(stream, ttlSeconds) {
const ttl = Number(ttlSeconds || this.defaultTtlSeconds);
await this.client.sAdd(key('index', 'streams'), stream.streamId);
if (stream.state?.regionCode) {
await this.client.sAdd(key('index', 'regions'), stream.state.regionCode);
await this.client.sAdd(key('region', stream.state.regionCode, 'streams'), stream.streamId);
}
await this.client.set(streamKey(stream.streamId), JSON.stringify(stream), { EX: ttl });
}
async cleanupWebhookObservations() {
const cutoffMs = Date.now() - secondsToMs(this.webhookStatsRetentionSeconds);
await Promise.all(WEBHOOK_OBSERVATION_CATEGORIES.map((category) => (
this.client.sendCommand(['ZREMRANGEBYSCORE', webhookObservationKey(category), '-inf', String(cutoffMs)])
)));
}
}
const WEBHOOK_STATS_WINDOWS = [
{ key: '1m', label: 'Past minute', seconds: 60 },
{ key: '60m', label: 'Past 60 minutes', seconds: 60 * 60 },
{ key: '24h', label: 'Past 24 hours', seconds: 24 * 60 * 60 }
];
const WEBHOOK_STATS_CATEGORIES = ['total', 'accepted', 'unverified', 'duplicate', 'concurrency_limited'];
const WEBHOOK_OBSERVATION_CATEGORIES = [...WEBHOOK_STATS_CATEGORIES, 'all'];
function renderPrometheusMetrics(streams, nodeCount, webhookStats = emptyWebhookStats()) {
const activeStreams = streams.filter(isActiveStream);
const lines = [
'# HELP rtms_realtime_active_streams Active streams in realtime cache.',
'# TYPE rtms_realtime_active_streams gauge',
`rtms_realtime_active_streams ${activeStreams.length}`,
'# HELP rtms_realtime_nodes Nodes with recent health snapshots.',
'# TYPE rtms_realtime_nodes gauge',
`rtms_realtime_nodes ${nodeCount}`,
'# HELP rtms_realtime_active_streams_by_region Active streams by region.',
'# TYPE rtms_realtime_active_streams_by_region gauge'
];
const byRegion = new Map();
const metricSums = new Map();
const latencyByRegion = new Map();
for (const stream of streams) {
const region = sanitizeLabelValue(stream.state?.regionCode || firstLatencyRegion(stream.latency) || 'unknown');
if (isActiveStream(stream)) {
byRegion.set(region, Number(byRegion.get(region) || 0) + 1);
}
for (const [name, value] of Object.entries(stream.metrics || {})) {
if (!Number.isFinite(Number(value))) continue;
const keyName = `${region}\n${sanitizeLabelValue(name)}`;
metricSums.set(keyName, Number(metricSums.get(keyName) || 0) + Number(value));
}
for (const [name, value] of Object.entries(stream.latency || {})) {
const stat = normalizeLatencyStat(value);
if (!stat) continue;
const keyName = `${region}\n${sanitizeLabelValue(name)}`;
latencyByRegion.set(keyName, mergeLatencyStat(latencyByRegion.get(keyName), stat));
}
}
for (const [region, count] of byRegion.entries()) {
lines.push(`rtms_realtime_active_streams_by_region{region="${region}"} ${count}`);
}
lines.push('# HELP rtms_realtime_metric_sum Numeric realtime metric sum by region and name.');
lines.push('# TYPE rtms_realtime_metric_sum gauge');
for (const [compoundKey, value] of metricSums.entries()) {
const [region, name] = compoundKey.split('\n');
lines.push(`rtms_realtime_metric_sum{region="${region}",metric="${name}"} ${value}`);
}
lines.push('# HELP rtms_realtime_latency_min_ms Lowest latency sample in milliseconds.');
lines.push('# TYPE rtms_realtime_latency_min_ms gauge');
for (const [compoundKey, stat] of latencyByRegion.entries()) {
const [region, name] = compoundKey.split('\n');
lines.push(`rtms_realtime_latency_min_ms{region="${region}",metric="${name}"} ${stat.minMs}`);
}
lines.push('# HELP rtms_realtime_latency_max_ms Highest latency sample in milliseconds.');
lines.push('# TYPE rtms_realtime_latency_max_ms gauge');
for (const [compoundKey, stat] of latencyByRegion.entries()) {
const [region, name] = compoundKey.split('\n');
lines.push(`rtms_realtime_latency_max_ms{region="${region}",metric="${name}"} ${stat.maxMs}`);
}
lines.push('# HELP rtms_realtime_latency_avg_ms Average latency sample in milliseconds.');
lines.push('# TYPE rtms_realtime_latency_avg_ms gauge');
for (const [compoundKey, stat] of latencyByRegion.entries()) {
const [region, name] = compoundKey.split('\n');
lines.push(`rtms_realtime_latency_avg_ms{region="${region}",metric="${name}"} ${stat.avgMs}`);
}
lines.push('# HELP rtms_realtime_latency_samples_total Latency sample count.');
lines.push('# TYPE rtms_realtime_latency_samples_total counter');
for (const [compoundKey, stat] of latencyByRegion.entries()) {
const [region, name] = compoundKey.split('\n');
lines.push(`rtms_realtime_latency_samples_total{region="${region}",metric="${name}"} ${stat.count}`);
}
lines.push('# HELP rtms_webhook_observations_window Rolling webhook observation count by category and window.');
lines.push('# TYPE rtms_webhook_observations_window gauge');
for (const window of webhookStats.windows || []) {
for (const [category, value] of Object.entries(window.counts || {})) {
lines.push(`rtms_webhook_observations_window{window="${sanitizeLabelValue(window.key)}",category="${sanitizeLabelValue(category)}"} ${Number(value || 0)}`);
}
}
return `${lines.join('\n')}\n`;
}
async function buildWebhookStats(countFn) {
const nowMs = Date.now();
const windows = [];
for (const window of WEBHOOK_STATS_WINDOWS) {
const cutoffMs = nowMs - secondsToMs(window.seconds);
const counts = {
accepted: Number(await countFn('accepted', cutoffMs, nowMs)) || 0,
unverified: Number(await countFn('unverified', cutoffMs, nowMs)) || 0,
duplicate: Number(await countFn('duplicate', cutoffMs, nowMs)) || 0,
concurrency_limited: Number(await countFn('concurrency_limited', cutoffMs, nowMs)) || 0
};
const explicitTotal = Number(await countFn('total', cutoffMs, nowMs)) || 0;
const allTotal = Number(await countFn('all', cutoffMs, nowMs)) || 0;
counts.total = explicitTotal || allTotal || counts.accepted + counts.unverified + counts.duplicate;
windows.push({
...window,
cutoffAt: new Date(cutoffMs).toISOString(),
counts
});
}
return {
generatedAt: new Date(nowMs).toISOString(),
windows
};
}
function emptyWebhookStats() {
return {
generatedAt: new Date().toISOString(),
windows: WEBHOOK_STATS_WINDOWS.map((window) => ({
...window,
cutoffAt: new Date(Date.now() - secondsToMs(window.seconds)).toISOString(),
counts: Object.fromEntries(WEBHOOK_STATS_CATEGORIES.map((category) => [category, 0]))
}))
};
}
function normalizeWebhookObservation(observation = {}) {
const rawCategory = String(observation.category || observation.status || observation.type || 'total').toLowerCase();
const category = WEBHOOK_STATS_CATEGORIES.includes(rawCategory) ? rawCategory : 'total';
const timestampMs = parseTimestampMs(observation.at || observation.receivedAt || observation.timestampMs) || Date.now();
return {
category,
timestampMs,
event: sanitizeSmallValue(observation.event || 'unknown'),
eventType: sanitizeSmallValue(observation.eventType || ''),
reason: sanitizeSmallValue(observation.reason || ''),
source: sanitizeSmallValue(observation.source || ''),
regionCode: sanitizeSmallValue(observation.regionCode || ''),
streamId: sanitizeSmallValue(observation.streamId || '')
};
}
function parseTimestampMs(value) {
if (Number.isFinite(Number(value))) {
const number = Number(value);
return number > 0 ? number : null;
}
const parsed = Date.parse(String(value || ''));
return Number.isFinite(parsed) ? parsed : null;
}
function sanitizeSmallValue(value) {
return String(value || '').replace(/[\n\r]/g, ' ').slice(0, 200);
}
function mergeMetrics(current, incoming) {
const merged = { ...current };
for (const [name, value] of Object.entries(incoming || {})) {
if (Number.isFinite(Number(value)) && Number.isFinite(Number(merged[name]))) {
merged[name] = Number(merged[name]) + Number(value);
} else {
merged[name] = value;
}
}
return merged;
}
function mergeLatencySample(current, incoming) {
const name = normalizeMetricName(incoming.name || incoming.metric || incoming.key || 'latency_ms');
const valueMs = Number(incoming.valueMs ?? incoming.latencyMs ?? incoming.rttMs ?? incoming.value ?? incoming.ms);
if (!Number.isFinite(valueMs) || valueMs < 0) return { ...current };
const existing = normalizeLatencyStat(current[name]) || {
name,
unit: 'ms',
count: 0,
sumMs: 0,
minMs: valueMs,
maxMs: valueMs,
avgMs: valueMs,
lastMs: valueMs
};
const count = Number(existing.count || 0) + 1;
const sumMs = Number(existing.sumMs || 0) + valueMs;
const minMs = Math.min(Number(existing.minMs), valueMs);
const maxMs = Math.max(Number(existing.maxMs), valueMs);
return {
...current,
[name]: {
...existing,
name,
unit: 'ms',
count,
sumMs,
minMs,
maxMs,
avgMs: sumMs / count,
lastMs: valueMs,
source: incoming.source || existing.source || '',
regionCode: incoming.regionCode || existing.regionCode || '',
nodeId: incoming.nodeId || existing.nodeId || '',
at: incoming.at || new Date().toISOString(),
labels: {
...(existing.labels || {}),
...(incoming.labels || {})
}
}
};
}
function isActiveStream(stream) {
const state = String(stream?.state?.state || '').toLowerCase();
if (!state) return true;
return !new Set([
'stopping',
'stopped',
'stop_requested',
'ended',
'failed',
'completed',
'dry_run_completed'
]).has(state);
}
function mergeLatencyFromEvent(current, event) {
const samples = latencySamplesFromEvent(event);
if (!samples.length) return current;
let merged = { ...current };
for (const sample of samples) {
merged = mergeLatencySample(merged, sample);
}
return merged;
}
function withDerivedLatency(stream) {
if (!stream) return null;
const derived = deriveLatencyFromEvents(stream.events || []);
if (!Object.keys(derived).length) return stream;
const latency = { ...(stream.latency || {}) };
for (const [name, value] of Object.entries(derived)) {
if (!normalizeLatencyStat(latency[name])) {
latency[name] = value;
}
}
return {
...stream,
latency
};
}
function deriveLatencyFromEvents(events) {
let latency = {};
for (const event of events || []) {
latency = mergeLatencyFromEvent(latency, event);
}
return latency;
}
function latencySamplesFromEvent(event = {}) {
const samples = [];
const webhookLatencyMs = Number(event.webhookIngressLatencyMs);
if (Number.isFinite(webhookLatencyMs) && webhookLatencyMs >= 0) {
samples.push({
name: 'webhook_ingress_latency_ms',
valueMs: webhookLatencyMs,
source: 'centralized-webhook-hub',
regionCode: event.regionCode,
nodeId: event.nodeId,
at: event.at,
labels: {
event: event.event,
eventType: event.eventType,
productType: event.productType
}
});
}
const rttMs = Number(event.rttMs);
if (event.type === 'signaling_ping_rtt' && Number.isFinite(rttMs) && rttMs >= 0) {
samples.push({
name: 'signaling_ping_rtt_ms',
valueMs: rttMs,
source: 'rtmsmanager.signaling',
regionCode: event.regionCode,
nodeId: event.nodeId,
at: event.at,
labels: {
signalingHost: event.signalingHost
}
});
}
return samples;
}
function normalizeLatencyStat(value) {
if (!value || typeof value !== 'object') return null;
const count = Number(value.count || 0);
const sumMs = Number(value.sumMs || 0);
const minMs = Number(value.minMs);
const maxMs = Number(value.maxMs);
if (!Number.isFinite(count) || count <= 0 || !Number.isFinite(sumMs) || !Number.isFinite(minMs) || !Number.isFinite(maxMs)) {
return null;
}
return {
...value,
count,
sumMs,
minMs,
maxMs,
avgMs: Number.isFinite(Number(value.avgMs)) ? Number(value.avgMs) : sumMs / count,
lastMs: Number.isFinite(Number(value.lastMs)) ? Number(value.lastMs) : null
};
}
function mergeLatencyStat(current, incoming) {
if (!current) return { ...incoming };
const count = Number(current.count || 0) + Number(incoming.count || 0);
const sumMs = Number(current.sumMs || 0) + Number(incoming.sumMs || 0);
return {
count,
sumMs,
minMs: Math.min(Number(current.minMs), Number(incoming.minMs)),
maxMs: Math.max(Number(current.maxMs), Number(incoming.maxMs)),
avgMs: count > 0 ? sumMs / count : 0
};
}
function firstLatencyRegion(latency) {
for (const value of Object.values(latency || {})) {
if (value?.regionCode) return value.regionCode;
}
return null;
}
function normalizeMetricName(value) {
return String(value || 'latency_ms').replace(/[^a-zA-Z0-9_:.-]/g, '_').slice(0, 120);
}
function key(...parts) {
return ['rtms', ...parts.map((part) => String(part).replace(/[^a-zA-Z0-9._:-]/g, '-'))].join(':');
}
function streamKey(streamId) {
return key('stream', streamId);
}
function nodeKey(nodeId) {
return key('node', nodeId, 'health');
}
function webhookObservationKey(category) {
return key('webhook', 'observations', category);
}
function secondsToMs(seconds) {
return Number(seconds || 0) * 1000;
}
function sanitizeLabelValue(value) {
return String(value || 'unknown').replace(/["\\\n\r]/g, '_').slice(0, 120);
}