-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
489 lines (454 loc) · 15 KB
/
index.js
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
import {ok} from 'node:assert'
import {
Gauge,
Counter,
Summary,
} from 'prom-client'
import {
createClient,
Vdv453ApiError,
} from 'vdv-453-client'
import {
createMetricsServer,
register as metricsRegister,
} from './lib/metrics.js'
import {kBestaetigungZst} from 'vdv-453-client/lib/symbols.js'
import {connectToNats, JSONCodec} from './lib/nats.js'
const sendVdv453DataToNats = async (cfg, opt = {}) => {
const {
leitstelle,
theirLeitstelle,
endpoint,
port,
subscriptions,
} = cfg
ok(leitstelle, 'missing/empty cfg.leitstelle')
ok(theirLeitstelle, 'missing/empty cfg.theirLeitstelle')
ok(endpoint, 'missing/empty cfg.endpoint')
ok(Number.isInteger(port), 'cfg.port must be an integer')
ok(Array.isArray(subscriptions), 'cfg.subscriptions must be an array')
ok(subscriptions.length > 0, 'cfg.subscriptions must not be empty')
for (let i = 0; i < subscriptions.length; i++) {
const {
service,
expires,
} = subscriptions[i]
ok(service, `invalid/empty cfg.subscriptions[${i}].service`)
// todo: handle BigInt?
ok(Number.isInteger(expires), `cfg.subscriptions[${i}].expires must be a UNIX epoch/timestamp`)
}
const {
vdv453ClientOpts,
natsOpts,
checkServerStatusInterval,
ausManualFetchInterval,
} = {
vdv453ClientOpts: {},
natsOpts: {},
checkServerStatusInterval: 5 * 1000, // milliseconds
ausManualFetchInterval: 30 * 1000, // 30 seconds, vdv-453-client's default
...opt,
}
const vdvDatenBereitAnfragesTotal = new Counter({
name: 'vdv_datenbereitanfrages_total',
help: 'number of incoming VDV-453 DatenBereitAnfrage requests received from the server',
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
],
})
const vdvClientStatusAnfragesTotal = new Counter({
name: 'vdv_clientstatusanfrages_total',
help: 'number of incoming VDV-453 ClientStatusAnfrage requests received from the server',
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
],
})
const vdvStatusAntwortsTotal = new Counter({
name: 'vdv_statusantworts_total',
help: 'number of VDV-453 StatusAntwort responses from the server',
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
],
})
// todo: switch back to a regular Gauge but set the measurement timestamp manually, once that's possible again?
// see also https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#NewMetricWithTimestamp
// see also https://github.com/siimon/prom-client/issues/177
// see also https://github.com/siimon/prom-client/issues/590
const vdvStatusAntwortOkTimestampSeconds = new Gauge({
name: 'vdv_statusantwort_ok_timestamp_seconds',
help: 'when the VDV-453 server has last reported as (not) ok via StatusAntwort',
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
'status', // 1 = ok, 0 = not ok
]
})
const trackVdvStatusAntwortOk = (svc, isOk, ts = Date.now()) => {
vdvStatusAntwortOkTimestampSeconds.set({
service: svc,
status: isOk ? '1' : '0',
}, ts / 1000)
}
const vdvDatenAbrufenAntwortsTotal = new Counter({
name: 'vdv_datenabrufenantworts_total',
help: 'number of VDV-453 DatenAbrufenAntwort responses from the server',
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
],
})
const activeSubscriptionsTotal = new Gauge({
name: 'vdv_active_subs_total',
help: 'number of subscriptions ("Abos")',
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
],
})
const trackNrOfSubscriptions = (svc, nrOfSubscriptions) => {
activeSubscriptionsTotal.set({
service: svc,
}, nrOfSubscriptions)
}
// todo: track subscription events: subscribe, update, expire, cancel
const dataFetchesTotal = new Counter({
name: 'vdv_data_fetches_total',
help: 'number of data fetches (one fetch is a series of DatenAbrufenAnfrage requests)',
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
'kind', // started/succeeded/failed
],
})
// todo: track latest successful data fetch?
const ausIstFahrtsTotal = new Summary({
name: 'vdv_aus_istfahrts_total',
help: 'number of VDV-454 AUS IstFahrts obtained in each fetch',
registers: [metricsRegister],
labelNames: [
'datensatz_alle', // if the fetch was with DatensatzAlle=true
],
})
const _updateGaugeWithIso8601Timestamp = (gaugeMetric, iso8601Ts, labels = {}) => {
const seconds = Date.parse(iso8601Ts) / 1000
if (Number.isNaN(seconds)) {
return;
}
// todo: find a way that does not not rely on an internal API
if (seconds >= gaugeMetric._getValue()) {
gaugeMetric.set(labels, seconds)
}
}
const latestAusIstFahrtZstSeconds = new Gauge({
name: 'vdv_latest_aus_istfahrt_zst_seconds',
help: 'latest Zst (timestamp) seen in any VDV-454 AUS IstFahrt',
registers: [metricsRegister],
})
const trackLatestAusIstFahrtZst = (zst) => {
_updateGaugeWithIso8601Timestamp(latestAusIstFahrtZstSeconds, zst)
}
// todo: track our own time, too, in case the two system's date+time diverge?
const latestServerZstSeconds = new Gauge({
name: 'vdv_latest_server_zst_seconds',
help: 'latest Zst (timestamp) seen in any response by the server – effectively a proxy for the server\'s current time',
registers: [metricsRegister],
})
const trackLatestServerZst = (zst) => {
_updateGaugeWithIso8601Timestamp(latestServerZstSeconds, zst)
}
const serverStartDienstZstSeconds = new Gauge({
name: 'vdv_server_startdienstzst_seconds',
help: `The server's StatusAntwort.StartDienstZst (timestamp), as obtained from StatusAnfrage requests`,
registers: [metricsRegister],
labelNames: [
'service', // VDV-453/-454 service, e.g. AUS
],
})
const trackServerStartDienstZst = (service, zst) => {
_updateGaugeWithIso8601Timestamp(serverStartDienstZstSeconds, zst, {service})
}
// NATS-related metrics
// Note: We mirror OpenDataVBB/gtfs-rt-feed's metrics here.
const natsNrOfMessagesSentTotal = new Counter({
name: 'nats_nr_of_msgs_sent_total',
help: 'number of messages sent to NATS',
registers: [metricsRegister],
labelNames: [
'topic_root', // first "segment" of the topic, e.g. `AUS` with `aus.istfahrt.foo.bar`
],
})
const natsLatestMessageSentTimestampSeconds = new Gauge({
name: 'nats_latest_msg_sent_timestamp_seconds',
help: 'when the latest message has been sent to NATS',
registers: [metricsRegister],
labelNames: [
'topic_root', // first "segment" of the topic, e.g. `AUS` with `aus.istfahrt.foo.bar`
],
})
const client = createClient({
...vdv453ClientOpts,
leitstelle,
theirLeitstelle,
endpoint,
}, {
// use hooks to expose metrics
onDatenBereitAnfrage: (svc, datenBereitAnfrage) => {
vdvDatenBereitAnfragesTotal.inc({
service: svc,
})
if (datenBereitAnfrage.$?.Zst) {
trackLatestServerZst(datenBereitAnfrage.$?.Zst)
}
},
onClientStatusAnfrage: (svc, clientStatusAnfrage) => {
vdvClientStatusAnfragesTotal.inc({
service: svc,
})
if (clientStatusAnfrage.$?.Zst) {
trackLatestServerZst(clientStatusAnfrage.$?.Zst)
}
},
onStatusAntwort: (svc, statusAntwort) => {
let ts = statusAntwort.Status?.$?.Zst && Date.parse(statusAntwort.Status?.$?.Zst)
if (!Number.isFinite(ts)) {
ts = Date.now()
}
trackVdvStatusAntwortOk(svc, true, ts)
vdvStatusAntwortsTotal.inc({
service: svc,
})
if (statusAntwort.Status?.$?.Zst) {
trackLatestServerZst(statusAntwort.Status?.$?.Zst)
}
// todo: track `statusAntwort.DatenVersionID.$text`?
},
onSubscribed: (svc, {aboId, aboSubTag, aboSubChildren}, bestaetigung, subStats) => {
// todo: track even itself?
// todo: track other parameters?
trackNrOfSubscriptions(svc, subStats.nrOfSubscriptions)
if (bestaetigung.$?.Zst) {
trackLatestServerZst(bestaetigung.$?.Zst)
}
},
onSubscriptionUpdated: (svc, {aboId, aboSubTag, aboSubChildren}, bestaetigung, subStats) => {
// todo: track even itself?
// todo: track other parameters?
trackNrOfSubscriptions(svc, subStats.nrOfSubscriptions)
if (bestaetigung.$?.Zst) {
trackLatestServerZst(bestaetigung.$?.Zst)
}
},
onSubscriptionExpired: (svc, {aboId, aboSubTag, aboSubChildren}, subStats) => {
// todo: track even itself?
// todo: track other parameters?
trackNrOfSubscriptions(svc, subStats.nrOfSubscriptions)
},
onSubscriptionCanceled: (svc, {aboId, aboSubTag, aboSubChildren}, reason, subStats) => {
// todo: track even itself?
// todo: track other parameters?
trackNrOfSubscriptions(svc, subStats.nrOfSubscriptions)
},
onDatenAbrufenAntwort: (svc, {datensatzAlle, weitereDaten, itLevel, bestaetigung}) => {
vdvDatenAbrufenAntwortsTotal.inc({
service: svc,
})
if (bestaetigung?.$?.Zst) {
trackLatestServerZst(bestaetigung?.$?.Zst)
}
// todo: track datensatzAlle?
// todo: track weitereDaten?
},
onDataFetchStarted: (svc, {datensatzAlle}) => {
dataFetchesTotal.inc({
service: svc,
kind: 'started',
})
},
onDataFetchSucceeded: (svc, {datensatzAlle}, {nrOfFetches, timePassed}) => {
dataFetchesTotal.inc({
service: svc,
kind: 'succeeded',
})
},
onDataFetchFailed: (svc, {datensatzAlle}, err, {nrOfFetches, timePassed}) => {
dataFetchesTotal.inc({
service: svc,
kind: 'failed',
})
},
onAusFetchSucceeded: ({datensatzAlle}, {nrOfIstFahrts}) => {
ausIstFahrtsTotal.observe({
datensatz_alle: datensatzAlle,
}, nrOfIstFahrts)
},
})
const {
logger,
} = client
await new Promise((resolve, reject) => {
client.httpServer.listen(port, (err) => {
if (err) reject(err)
else resolve()
})
})
logger.info(`listening on port ${port}`)
const {
natsClient,
} = await connectToNats({
logger,
}, natsOpts)
const natsJson = JSONCodec()
// todo: warn-log publish failures?
const subscribeToAUS = (expires) => {
let aboId = null
// todo: support `expires` value of `'never'`/`Infinity`, re-subscribing continuously?
const startTask = async () => {
const {aboId: _aboId} = await client.ausSubscribe({
expiresAt: expires * 1000, // vdv-453-client uses milliseconds
fetchInterval: ausManualFetchInterval,
})
aboId = _aboId
// todo: process other AUSNachricht children
client.data.on('aus:IstFahrt', (istFahrt) => {
if (istFahrt.Zst) { // it seems that not all servers implement this
trackLatestAusIstFahrtZst(istFahrt.Zst)
}
const emptySegment = '_'
// > Recommended characters: `a` to `z`, `A` to `Z` and `0` to `9` (names […] cannot contain whitespace).
// > Special characters: The period `.` and `*` and also `>`.
// Note: By mapping IDs with non-recommended characters to `_`, we accept a low chance of ID collisions here, e.g. between `foo.bar >baz` and `foo_bar__baz`.
// todo: consider replacing only special/unsafe characters (`.`/`*`/`>`/` `)
const escapeTopicSegment = id => id.replace(/[^a-zA-Z0-9]/g, '_')
const {
LinienID: linienId,
LinienText: linienText,
RichtungsID: richtungsId,
RichtungsText: richtungsText,
} = istFahrt
const {
FahrtBezeichner: fahrtBezeichner,
Betriebstag: betriebstag,
} = istFahrt.FahrtID || {}
// We make up a hierarchical topic `aus.istfahrt.$linie.$richtung.$fahrt` that allows consumers to pre-filter.
// With some IstFahrts some IDs are missing, so we use the test equivalents as fallbacks.
const linieSegment = linienId
? `id:${escapeTopicSegment(linienId)}`
: (linienText
// todo: add configurable text normalization? e.g. Unicode -> ASCII, lower case
? `text:${escapeTopicSegment(linienText)}`
: emptySegment
)
const richtungSegment = richtungsId
? `id:${escapeTopicSegment(richtungsId)}`
: (richtungsText
// todo: add configurable text normalization? e.g. Unicode -> ASCII, lower case
? `text:${escapeTopicSegment(richtungsText)}`
: emptySegment
)
const fahrtSegment = fahrtBezeichner && betriebstag
? `id:${escapeTopicSegment(fahrtBezeichner)}:tag:${escapeTopicSegment(betriebstag)}`
: emptySegment
const topic = `aus.istfahrt.${linieSegment}.${richtungSegment}.${fahrtSegment}`
// make unenumerable properties regular ones, so that they end up in the JSON
istFahrt['$BestaetigungZst'] = istFahrt[kBestaetigungZst]
logger.trace({
topic,
istFahrt,
}, 'publishing AUS IstFahrt to NATS')
const tSent = Date.now()
natsClient.publish(topic, natsJson.encode(istFahrt))
// We slice() to keep the cardinality low in case of a bug.
const topic_root = (topic.split('.')[0] || '').slice(0, 7)
natsNrOfMessagesSentTotal.inc({topic_root})
natsLatestMessageSentTimestampSeconds.set({topic_root}, tSent)
})
}
const unsubscribeFromAUS = async () => {
if (aboId === null) return;
await client.ausUnsubscribe(aboId)
}
const startPromise = startTask()
return {
startPromise,
stopTask: unsubscribeFromAUS,
}
}
const metricsServer = createMetricsServer()
await metricsServer.start()
logger.info(`serving Prometheus metrics on port ${metricsServer.address().port}`)
const startPromises = []
const stopTasks = []
const stop = async () => {
await Promise.all(stopTasks.map(task => task()))
client.httpServer.close()
metricsServer.close()
}
for (const subscription of subscriptions) {
const {
service,
expires,
} = subscription
if (subscription.service === 'AUS') {
const {
startPromise,
stopTask,
} = subscribeToAUS(expires)
startPromises.push(startPromise)
stopTasks.push(stopTask)
} else {
throw new Error(`invalid/unsupported service "${service}"`)
}
}
// fetch server status periodically
{
const checkAndResetTimer = async () => {
const svc = 'aus'
// setTimeout() handles neither async functions nor rejections, so we must catch errors here by ourselves.
try {
const {
startDienstZst,
} = await client.ausCheckServerStatus()
// todo: warn if status is not ok?
if (startDienstZst === null) {
logger.warn({
service: 'aus',
}, 'server did not provide a StatusAntwort.StartDienstZst')
} else {
trackServerStartDienstZst(svc, startDienstZst)
}
} catch (err) {
logger.warn({
err,
}, 'failed to check server status')
// todo: check for the right error message
if ((err instanceof Vdv453ApiError) && err.statusAntwort) { // server seems not ok
let ts = err.statusAntwort.Status?.$?.Zst && Date.parse(err.statusAntwort.Status?.$?.Zst)
if (!Number.isFinite(ts)) {
ts = Date.now()
}
trackVdvStatusAntwortOk(svc, false, ts)
}
} finally {
checkTimer = setTimeout(checkAndResetTimer, checkServerStatusInterval)
}
}
const checkServerStatusInitialWait = Math.max(checkServerStatusInterval / 30, 2_000) // 2 seconds minimum
let checkTimer = setTimeout(checkAndResetTimer, checkServerStatusInitialWait)
stopTasks.push(() => {
clearTimeout(checkTimer)
})
}
// todo: stop successfully started subscriptions if one of these fails
await Promise.all(startPromises)
return {
logger,
client,
stop,
}
}
export {
sendVdv453DataToNats,
}