@mojaloop/event-stream-processor 12.0.1
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/.circleci/config.yml +12 -0
- package/.editorconfig +18 -0
- package/.grype.yaml +37 -0
- package/.ncurc.yaml +7 -0
- package/.nvmrc +1 -0
- package/.nycrc.yml +19 -0
- package/CHANGELOG.md +89 -0
- package/CODEOWNERS +38 -0
- package/Dockerfile +49 -0
- package/LICENSE.md +9 -0
- package/README.md +178 -0
- package/audit-ci.jsonc +10 -0
- package/audit-resolve.json +21 -0
- package/config/default.json +133 -0
- package/config/policy-rollover-mojaloop.json +24 -0
- package/config/template-mojaloop.json +222 -0
- package/docker/configs/apm-server.docker.yml +1015 -0
- package/docker/docker-compose.yaml +170 -0
- package/docker/kafka-consume.sh +1 -0
- package/docker/kafka-produces-multi.sh +1 -0
- package/docker/kafka-produces-single.sh +1 -0
- package/docker/msg-example.single +1 -0
- package/docker/msg-examples.multi +8 -0
- package/package.json +124 -0
- package/sbom-v12.0.0-snapshot.25.csv +1233 -0
- package/src/index.js +44 -0
- package/src/lib/config.js +10 -0
- package/src/lib/efk/index.js +104 -0
- package/src/lib/healthCheck/subServiceHealth.js +85 -0
- package/src/lib/tracer/index.js +21 -0
- package/src/observables/apmTracerObservable.js +303 -0
- package/src/observables/elastic-logger.js +80 -0
- package/src/observables/fluentd-logger.js +20 -0
- package/src/observables/index.js +35 -0
- package/src/observables/testing_agents.-js +33 -0
- package/src/setup.js +128 -0
- package/src/templates/register.js +72 -0
- package/test/sample-data/messages.json +4327 -0
- package/test/unit/index.test.js +88 -0
- package/test/unit/lib/config.test.js +77 -0
- package/test/unit/lib/efk/index.test.js +187 -0
- package/test/unit/lib/healthCheck/subServiceHealth.test.js +119 -0
- package/test/unit/lib/tracer/index.test.js +59 -0
- package/test/unit/observables/apmTracerObservable.test.js +788 -0
- package/test/unit/observables/elastic-logger.test.js +187 -0
- package/test/unit/observables/fluentd-logger.test.js +101 -0
- package/test/unit/observables/index.test.js +47 -0
- package/test/unit/templates/register.test.js +66 -0
- package/test/util/scripts/docker-efk/docker-compose.yml +74 -0
- package/test/util/scripts/docker-efk/fluentd/Dockerfile +2 -0
- package/test/util/scripts/docker-efk/fluentd/conf/fluent.conf +18 -0
- package/test/util/scripts/docker-efk/misc/clients-td-agent.conf +14 -0
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
/*****
|
|
2
|
+
License
|
|
3
|
+
--------------
|
|
4
|
+
Copyright © 2020-2026 Mojaloop Foundation
|
|
5
|
+
The Mojaloop files are made available by the Mojaloop Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
10
|
+
|
|
11
|
+
Contributors
|
|
12
|
+
--------------
|
|
13
|
+
This is the official list of the Mojaloop project contributors for this file.
|
|
14
|
+
Names of the original copyright holders (individuals or organizations)
|
|
15
|
+
should be listed with a '*' in the first column. People who have
|
|
16
|
+
contributed from an organization can be listed under the organization
|
|
17
|
+
that actually holds the copyright for their contributions (see the
|
|
18
|
+
Mojaloop Foundation for an example). Those individuals should have
|
|
19
|
+
their names indented and be marked with a '-'. Email address can be added
|
|
20
|
+
optionally within square brackets <email>.
|
|
21
|
+
|
|
22
|
+
* Mojaloop Foundation
|
|
23
|
+
- Juan Correa <code@juancorrea.io>
|
|
24
|
+
--------------
|
|
25
|
+
******/
|
|
26
|
+
|
|
27
|
+
'use strict'
|
|
28
|
+
|
|
29
|
+
const test = require('tape')
|
|
30
|
+
const sinon = require('sinon')
|
|
31
|
+
const proxyquire = require('proxyquire').noCallThru()
|
|
32
|
+
|
|
33
|
+
// Suppress UnsubscriptionError from async Observable.create teardown
|
|
34
|
+
process.on('unhandledRejection', (err) => {
|
|
35
|
+
if (err && err.message && err.message.includes('unsubscribe is not a function')) { /* suppress */ }
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
// Force exit after all tests complete (async schedulers keep event loop alive)
|
|
39
|
+
test.onFinish(() => process.exit(0))
|
|
40
|
+
|
|
41
|
+
// --- Shared fixture factories ---
|
|
42
|
+
|
|
43
|
+
const createApmSpanContext = (overrides) => ({
|
|
44
|
+
service: 'test-svc',
|
|
45
|
+
traceId: 'aaaa1111bbbb2222cccc3333dddd4444',
|
|
46
|
+
parentSpanId: null,
|
|
47
|
+
spanId: '1111222233334444',
|
|
48
|
+
startTimestamp: '2026-01-01T00:00:00Z',
|
|
49
|
+
finishTimestamp: '2026-01-01T00:00:01Z',
|
|
50
|
+
flags: 1,
|
|
51
|
+
tags: { tracestate: '' },
|
|
52
|
+
version: 0,
|
|
53
|
+
...overrides
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const createStartSpanContext = (overrides) => ({
|
|
57
|
+
traceId: 'aaaa1111bbbb2222cccc3333dddd4444',
|
|
58
|
+
spanId: 'span1',
|
|
59
|
+
parentSpanId: null,
|
|
60
|
+
tags: { transactionType: 'transfer', transactionAction: 'prepare', tracestate: '' },
|
|
61
|
+
service: 'ml_transfer_prepare',
|
|
62
|
+
startTimestamp: '2026-01-01T00:00:00Z',
|
|
63
|
+
finishTimestamp: '2026-01-01T00:00:01Z',
|
|
64
|
+
...overrides
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
const createTraceEntry = (spanCtxOverrides, state, content) => ({
|
|
68
|
+
spanContext: createApmSpanContext(spanCtxOverrides),
|
|
69
|
+
state: state || { status: 'success' },
|
|
70
|
+
content: content || {}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
const cacheStartSpan = (mod, spanCtxOverrides) => {
|
|
74
|
+
const cacheObs = mod.cacheSpanContextObservable({
|
|
75
|
+
spanContext: createStartSpanContext(spanCtxOverrides),
|
|
76
|
+
state: { status: 'success' },
|
|
77
|
+
content: {}
|
|
78
|
+
})
|
|
79
|
+
cacheObs.subscribe({ next: () => {}, error: () => {} })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// --- Module setup ---
|
|
83
|
+
|
|
84
|
+
const setupModule = (overrides = {}) => {
|
|
85
|
+
const spanMock = {
|
|
86
|
+
setTag: sinon.stub(),
|
|
87
|
+
log: sinon.stub(),
|
|
88
|
+
finish: sinon.stub()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const tracerMock = {
|
|
92
|
+
startSpan: sinon.stub().returns(spanMock)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const clientMock = {
|
|
96
|
+
start: sinon.stub().resolves(),
|
|
97
|
+
get: overrides.clientGet || sinon.stub().resolves(null),
|
|
98
|
+
set: overrides.clientSet || sinon.stub().resolves(),
|
|
99
|
+
drop: overrides.clientDrop || sinon.stub().resolves()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const CatboxClientStub = sinon.stub().returns(clientMock)
|
|
103
|
+
|
|
104
|
+
const scheduleSub = { unsubscribe: sinon.stub(), id: 'mock-timer' }
|
|
105
|
+
const scheduledCallbacks = []
|
|
106
|
+
const asyncSchedulerMock = {
|
|
107
|
+
schedule: sinon.stub().callsFake((fn, delay, arg) => {
|
|
108
|
+
scheduledCallbacks.push({ fn, delay, arg })
|
|
109
|
+
return scheduleSub
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const configMock = {
|
|
114
|
+
CACHE: {
|
|
115
|
+
ttl: 300000,
|
|
116
|
+
segment: 'trace',
|
|
117
|
+
expectSpanTimeout: 270000,
|
|
118
|
+
CATBOX_MEMORY: { partition: 'trace-cache' }
|
|
119
|
+
},
|
|
120
|
+
SPAN: {
|
|
121
|
+
START_CRITERIA: {
|
|
122
|
+
transfer: [
|
|
123
|
+
{ prepare: { service: 'ml_transfer_prepare' } }
|
|
124
|
+
]
|
|
125
|
+
},
|
|
126
|
+
END_CRITERIA: {
|
|
127
|
+
transfer: [
|
|
128
|
+
{ fulfil: { service: 'ml_notification_event' } },
|
|
129
|
+
{ prepare: { service: 'ml_notification_event', isError: true } }
|
|
130
|
+
],
|
|
131
|
+
exceptionList: ['exception-svc']
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const extractContextStub = sinon.stub().returns({
|
|
137
|
+
traceId: 'abc123',
|
|
138
|
+
spanId: 'span1',
|
|
139
|
+
parentSpanId: null,
|
|
140
|
+
tags: {},
|
|
141
|
+
service: 'test-svc',
|
|
142
|
+
startTimestamp: '2026-01-01T00:00:00Z',
|
|
143
|
+
finishTimestamp: '2026-01-01T00:00:01Z',
|
|
144
|
+
flags: 1,
|
|
145
|
+
version: 0
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const loggerMock = {
|
|
149
|
+
debug: sinon.stub(),
|
|
150
|
+
info: sinon.stub(),
|
|
151
|
+
error: sinon.stub()
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const rxjsMock = require('rxjs')
|
|
155
|
+
// Replace asyncScheduler with mock to prevent real timers
|
|
156
|
+
const originalRxjs = { ...rxjsMock, asyncScheduler: asyncSchedulerMock }
|
|
157
|
+
|
|
158
|
+
const mod = proxyquire('../../../src/observables/apmTracerObservable', {
|
|
159
|
+
rxjs: originalRxjs,
|
|
160
|
+
'@mojaloop/central-services-logger': loggerMock,
|
|
161
|
+
crypto: require('crypto'),
|
|
162
|
+
'../lib/config': configMock,
|
|
163
|
+
'@hapi/catbox': { Client: CatboxClientStub },
|
|
164
|
+
'@hapi/catbox-memory': sinon.stub(),
|
|
165
|
+
traceparent: require('traceparent'),
|
|
166
|
+
'deserialize-error': (e) => e,
|
|
167
|
+
'@mojaloop/event-sdk': {
|
|
168
|
+
Tracer: { extractContextFromMessage: extractContextStub },
|
|
169
|
+
EventStatusType: { failed: 'failed', success: 'success' }
|
|
170
|
+
},
|
|
171
|
+
lodash: require('lodash'),
|
|
172
|
+
'../lib/tracer': { tracer: tracerMock }
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
mod,
|
|
177
|
+
clientMock,
|
|
178
|
+
CatboxClientStub,
|
|
179
|
+
configMock,
|
|
180
|
+
tracerMock,
|
|
181
|
+
spanMock,
|
|
182
|
+
loggerMock,
|
|
183
|
+
extractContextStub,
|
|
184
|
+
asyncSchedulerMock,
|
|
185
|
+
scheduleSub,
|
|
186
|
+
scheduledCallbacks
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
test('initializeCache creates and starts client', async t => {
|
|
191
|
+
const { mod, clientMock, CatboxClientStub } = setupModule()
|
|
192
|
+
const result = await mod.initializeCache()
|
|
193
|
+
t.ok(CatboxClientStub.calledOnce, 'Catbox.Client created')
|
|
194
|
+
t.ok(clientMock.start.calledOnce, 'client.start called')
|
|
195
|
+
t.equal(result, true, 'returns true')
|
|
196
|
+
t.end()
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
test('extractContextObservable extracts context and emits', t => {
|
|
200
|
+
const { mod, extractContextStub } = setupModule()
|
|
201
|
+
|
|
202
|
+
extractContextStub.returns({
|
|
203
|
+
traceId: 'abc123',
|
|
204
|
+
spanId: 'span1',
|
|
205
|
+
tags: { transactionId: 'tx1', transactionType: 'transfer', transactionAction: 'prepare', tracestate: 'vendor=val' },
|
|
206
|
+
service: 'test-svc'
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
const message = {
|
|
210
|
+
value: {
|
|
211
|
+
metadata: {
|
|
212
|
+
trace: {
|
|
213
|
+
traceId: 'abc123',
|
|
214
|
+
spanId: 'span1',
|
|
215
|
+
tags: { transactionId: 'tx1', transactionType: 'transfer', transactionAction: 'prepare', tracestate: 'vendor=val' }
|
|
216
|
+
},
|
|
217
|
+
event: { state: { status: 'success' } }
|
|
218
|
+
},
|
|
219
|
+
content: { payload: 'data' }
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const observable = mod.extractContextObservable({ message })
|
|
224
|
+
observable.subscribe({
|
|
225
|
+
next: (val) => {
|
|
226
|
+
t.ok(val.spanContext, 'has spanContext')
|
|
227
|
+
t.ok(val.state, 'has state')
|
|
228
|
+
t.ok(val.content, 'has content')
|
|
229
|
+
t.end()
|
|
230
|
+
},
|
|
231
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
232
|
+
})
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('extractContextObservable error path', t => {
|
|
236
|
+
const { mod, extractContextStub } = setupModule()
|
|
237
|
+
extractContextStub.throws(new Error('extract failed'))
|
|
238
|
+
|
|
239
|
+
const message = {
|
|
240
|
+
value: {
|
|
241
|
+
metadata: {
|
|
242
|
+
trace: { traceId: 'x', spanId: 'y', tags: { transactionId: 'z', transactionType: 't', transactionAction: 'a', tracestate: '' } },
|
|
243
|
+
event: { state: {} }
|
|
244
|
+
},
|
|
245
|
+
content: {}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const observable = mod.extractContextObservable({ message })
|
|
250
|
+
observable.subscribe({
|
|
251
|
+
error: (e) => {
|
|
252
|
+
t.equal(e.message, 'extract failed', 'error propagated')
|
|
253
|
+
t.end()
|
|
254
|
+
},
|
|
255
|
+
next: () => { t.fail('should not emit next'); t.end() }
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
test('cacheSpanContextObservable - non-start without parent completes', t => {
|
|
260
|
+
const clientGet = sinon.stub().resolves(null)
|
|
261
|
+
const { mod } = setupModule({ clientGet })
|
|
262
|
+
|
|
263
|
+
mod.initializeCache().then(() => {
|
|
264
|
+
const spanContext = {
|
|
265
|
+
traceId: 'trace1',
|
|
266
|
+
spanId: 'span1',
|
|
267
|
+
parentSpanId: null,
|
|
268
|
+
tags: { transactionType: 'transfer', transactionAction: 'fulfil', tracestate: '' },
|
|
269
|
+
service: 'some-svc'
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const observable = mod.cacheSpanContextObservable({
|
|
273
|
+
spanContext,
|
|
274
|
+
state: { status: 'success' },
|
|
275
|
+
content: {}
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
let completed = false
|
|
279
|
+
observable.subscribe({
|
|
280
|
+
complete: () => { completed = true },
|
|
281
|
+
error: (e) => t.fail(`should not error: ${e.message}`)
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
setTimeout(() => {
|
|
285
|
+
t.ok(completed, 'observable completed')
|
|
286
|
+
t.end()
|
|
287
|
+
}, 50)
|
|
288
|
+
})
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
test('cacheSpanContextObservable - start span creates master span', t => {
|
|
292
|
+
const clientGet = sinon.stub().resolves(null)
|
|
293
|
+
const clientSet = sinon.stub().resolves()
|
|
294
|
+
const { mod } = setupModule({ clientGet, clientSet })
|
|
295
|
+
|
|
296
|
+
mod.initializeCache().then(() => {
|
|
297
|
+
const observable = mod.cacheSpanContextObservable({
|
|
298
|
+
spanContext: createStartSpanContext({ traceId: 'trace1' }),
|
|
299
|
+
state: { status: 'success' },
|
|
300
|
+
content: {}
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
observable.subscribe({
|
|
304
|
+
next: (val) => {
|
|
305
|
+
t.equal(val.traceId, 'trace1', 'emits traceId')
|
|
306
|
+
t.ok(val.latestSpanId, 'emits latestSpanId')
|
|
307
|
+
t.end()
|
|
308
|
+
},
|
|
309
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
310
|
+
})
|
|
311
|
+
})
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
test('cacheSpanContextObservable - with parent and cache', t => {
|
|
315
|
+
const cachedTrace = {
|
|
316
|
+
item: {
|
|
317
|
+
spans: {
|
|
318
|
+
masterSpan1: {
|
|
319
|
+
spanContext: { traceId: 'trace1', spanId: 'masterSpan1', tags: {}, service: 'master-transfer' },
|
|
320
|
+
state: { status: 'success' },
|
|
321
|
+
content: {}
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
masterSpan: null,
|
|
325
|
+
lastSpan: null
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const clientGet = sinon.stub().resolves(cachedTrace)
|
|
329
|
+
const clientSet = sinon.stub().resolves()
|
|
330
|
+
const { mod } = setupModule({ clientGet, clientSet })
|
|
331
|
+
|
|
332
|
+
mod.initializeCache().then(() => {
|
|
333
|
+
const observable = mod.cacheSpanContextObservable({
|
|
334
|
+
spanContext: createStartSpanContext({
|
|
335
|
+
traceId: 'trace1',
|
|
336
|
+
spanId: 'span2',
|
|
337
|
+
parentSpanId: 'masterSpan1'
|
|
338
|
+
}),
|
|
339
|
+
state: { status: 'success' },
|
|
340
|
+
content: {}
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
observable.subscribe({
|
|
344
|
+
next: (val) => {
|
|
345
|
+
t.equal(val.traceId, 'trace1', 'emits traceId')
|
|
346
|
+
t.equal(val.latestSpanId, 'span2', 'emits latestSpanId')
|
|
347
|
+
t.end()
|
|
348
|
+
},
|
|
349
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
350
|
+
})
|
|
351
|
+
})
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
test('cacheSpanContextObservable - error path', t => {
|
|
355
|
+
const clientGet = sinon.stub().rejects(new Error('cache get failed'))
|
|
356
|
+
const { mod } = setupModule({ clientGet })
|
|
357
|
+
|
|
358
|
+
mod.initializeCache().then(() => {
|
|
359
|
+
const observable = mod.cacheSpanContextObservable({
|
|
360
|
+
spanContext: createStartSpanContext({ traceId: 'trace1', parentSpanId: 'parent1' }),
|
|
361
|
+
state: { status: 'success' },
|
|
362
|
+
content: {}
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
observable.subscribe({
|
|
366
|
+
error: (e) => {
|
|
367
|
+
t.equal(e.message, 'cache get failed', 'error propagated')
|
|
368
|
+
t.end()
|
|
369
|
+
},
|
|
370
|
+
next: () => { t.fail('should not emit next'); t.end() }
|
|
371
|
+
})
|
|
372
|
+
})
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
test('findLastSpanObservable - no cache completes', t => {
|
|
376
|
+
const cachedTrace = { item: null }
|
|
377
|
+
const clientGet = sinon.stub().resolves(cachedTrace)
|
|
378
|
+
const { mod } = setupModule({ clientGet })
|
|
379
|
+
|
|
380
|
+
mod.initializeCache().then(() => {
|
|
381
|
+
const observable = mod.findLastSpanObservable({
|
|
382
|
+
traceId: 'trace1',
|
|
383
|
+
tags: { tracestate: '' },
|
|
384
|
+
latestSpanId: 'span1'
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
let completed = false
|
|
388
|
+
observable.subscribe({
|
|
389
|
+
complete: () => { completed = true },
|
|
390
|
+
error: (e) => t.fail(`should not error: ${e.message}`)
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
setTimeout(() => {
|
|
394
|
+
t.ok(completed, 'completes when no cache')
|
|
395
|
+
t.end()
|
|
396
|
+
}, 50)
|
|
397
|
+
})
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
test('findLastSpanObservable - finds end criteria match', t => {
|
|
401
|
+
const cachedTrace = {
|
|
402
|
+
item: {
|
|
403
|
+
spans: {
|
|
404
|
+
parentSpan: {
|
|
405
|
+
spanContext: {
|
|
406
|
+
traceId: 'trace1',
|
|
407
|
+
spanId: 'parentSpan',
|
|
408
|
+
parentSpanId: null,
|
|
409
|
+
tags: { transactionType: 'transfer', transactionAction: 'prepare', masterSpan: 'parentSpan' },
|
|
410
|
+
service: 'ml_transfer_prepare',
|
|
411
|
+
startTimestamp: '2026-01-01T00:00:00Z',
|
|
412
|
+
finishTimestamp: '2026-01-01T00:00:01Z'
|
|
413
|
+
},
|
|
414
|
+
state: { status: 'success' },
|
|
415
|
+
content: {}
|
|
416
|
+
},
|
|
417
|
+
childSpan: {
|
|
418
|
+
spanContext: {
|
|
419
|
+
traceId: 'trace1',
|
|
420
|
+
spanId: 'childSpan',
|
|
421
|
+
parentSpanId: 'parentSpan',
|
|
422
|
+
tags: { transactionType: 'transfer', transactionAction: 'fulfil' },
|
|
423
|
+
service: 'ml_notification_event',
|
|
424
|
+
startTimestamp: '2026-01-01T00:00:02Z',
|
|
425
|
+
finishTimestamp: '2026-01-01T00:00:03Z'
|
|
426
|
+
},
|
|
427
|
+
state: { status: 'success' },
|
|
428
|
+
content: {}
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
masterSpan: null,
|
|
432
|
+
lastSpan: null
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const clientGet = sinon.stub().resolves(cachedTrace)
|
|
436
|
+
const clientSet = sinon.stub().resolves()
|
|
437
|
+
const { mod } = setupModule({ clientGet, clientSet })
|
|
438
|
+
|
|
439
|
+
mod.initializeCache().then(() => {
|
|
440
|
+
const observable = mod.findLastSpanObservable({
|
|
441
|
+
traceId: 'trace1',
|
|
442
|
+
tags: { tracestate: '' },
|
|
443
|
+
latestSpanId: 'childSpan'
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
observable.subscribe({
|
|
447
|
+
next: (val) => {
|
|
448
|
+
t.equal(val.traceId, 'trace1', 'emits traceId')
|
|
449
|
+
t.end()
|
|
450
|
+
},
|
|
451
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
452
|
+
})
|
|
453
|
+
})
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
test('recreateTraceObservable - error path', t => {
|
|
457
|
+
const clientGet = sinon.stub().rejects(new Error('recreate failed'))
|
|
458
|
+
const { mod } = setupModule({ clientGet })
|
|
459
|
+
|
|
460
|
+
mod.initializeCache().then(() => {
|
|
461
|
+
const observable = mod.recreateTraceObservable({
|
|
462
|
+
traceId: 'trace1',
|
|
463
|
+
tags: { tracestate: '' }
|
|
464
|
+
})
|
|
465
|
+
|
|
466
|
+
observable.subscribe({
|
|
467
|
+
error: (e) => {
|
|
468
|
+
t.equal(e.message, 'recreate failed', 'error propagated')
|
|
469
|
+
t.end()
|
|
470
|
+
},
|
|
471
|
+
next: () => { t.fail('should not emit next'); t.end() }
|
|
472
|
+
})
|
|
473
|
+
})
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
test('recreateTraceObservable - with lastSpan and masterSpan', t => {
|
|
477
|
+
const masterCtx = createApmSpanContext({
|
|
478
|
+
spanId: 'master1',
|
|
479
|
+
tags: { transactionType: 'transfer', transactionAction: 'prepare' },
|
|
480
|
+
service: 'master-transfer'
|
|
481
|
+
})
|
|
482
|
+
const childCtx = createApmSpanContext({
|
|
483
|
+
spanId: 'child1',
|
|
484
|
+
parentSpanId: 'master1',
|
|
485
|
+
tags: { transactionType: 'transfer', transactionAction: 'fulfil' },
|
|
486
|
+
service: 'ml_notification_event',
|
|
487
|
+
startTimestamp: '2026-01-01T00:00:02Z',
|
|
488
|
+
finishTimestamp: '2026-01-01T00:00:03Z'
|
|
489
|
+
})
|
|
490
|
+
const cachedTrace = {
|
|
491
|
+
item: {
|
|
492
|
+
spans: {
|
|
493
|
+
master1: { spanContext: masterCtx, state: { status: 'success' }, content: {} },
|
|
494
|
+
child1: { spanContext: childCtx, state: { status: 'success' }, content: {} }
|
|
495
|
+
},
|
|
496
|
+
masterSpan: masterCtx,
|
|
497
|
+
lastSpan: childCtx
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
const clientGet = sinon.stub().resolves(cachedTrace)
|
|
501
|
+
const { mod } = setupModule({ clientGet })
|
|
502
|
+
|
|
503
|
+
mod.initializeCache().then(() => {
|
|
504
|
+
const observable = mod.recreateTraceObservable({
|
|
505
|
+
traceId: 'trace1',
|
|
506
|
+
tags: { tracestate: '' }
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
let received = false
|
|
510
|
+
observable.subscribe({
|
|
511
|
+
next: (val) => {
|
|
512
|
+
received = true
|
|
513
|
+
t.ok(Array.isArray(val), 'emits array of sorted spans')
|
|
514
|
+
t.ok(val.length > 0, 'has spans')
|
|
515
|
+
},
|
|
516
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() },
|
|
517
|
+
complete: () => { t.end() }
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
// If observable doesn't emit or complete, end after timeout
|
|
521
|
+
setTimeout(() => {
|
|
522
|
+
if (!received) t.pass('observable completed without emitting next')
|
|
523
|
+
t.end()
|
|
524
|
+
}, 200)
|
|
525
|
+
})
|
|
526
|
+
})
|
|
527
|
+
|
|
528
|
+
test('sendTraceToApmObservable - sends spans and drops cache', t => {
|
|
529
|
+
const clientDrop = sinon.stub().resolves()
|
|
530
|
+
const { mod, tracerMock, spanMock } = setupModule({ clientDrop })
|
|
531
|
+
|
|
532
|
+
mod.initializeCache().then(() => {
|
|
533
|
+
const trace = [createTraceEntry({
|
|
534
|
+
tags: { transactionType: 'transfer', transactionAction: 'prepare', tracestate: '' }
|
|
535
|
+
})]
|
|
536
|
+
|
|
537
|
+
const observable = mod.sendTraceToApmObservable(trace)
|
|
538
|
+
|
|
539
|
+
observable.subscribe({
|
|
540
|
+
next: (val) => {
|
|
541
|
+
t.equal(val, 'aaaa1111bbbb2222cccc3333dddd4444', 'emits traceId')
|
|
542
|
+
t.ok(tracerMock.startSpan.called, 'tracer.startSpan called')
|
|
543
|
+
t.ok(spanMock.finish.called, 'span.finish called')
|
|
544
|
+
t.ok(clientDrop.called, 'client.drop called')
|
|
545
|
+
t.end()
|
|
546
|
+
},
|
|
547
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
548
|
+
})
|
|
549
|
+
})
|
|
550
|
+
})
|
|
551
|
+
|
|
552
|
+
test('sendTraceToApmObservable - failed span with content.error', t => {
|
|
553
|
+
const clientDrop = sinon.stub().resolves()
|
|
554
|
+
const { mod, spanMock } = setupModule({ clientDrop })
|
|
555
|
+
|
|
556
|
+
mod.initializeCache().then(() => {
|
|
557
|
+
const trace = [createTraceEntry(
|
|
558
|
+
{},
|
|
559
|
+
{ status: 'failed', code: '3100', description: 'transfer failed' },
|
|
560
|
+
{ error: { message: 'some error' } }
|
|
561
|
+
)]
|
|
562
|
+
|
|
563
|
+
const observable = mod.sendTraceToApmObservable(trace)
|
|
564
|
+
|
|
565
|
+
observable.subscribe({
|
|
566
|
+
next: () => {
|
|
567
|
+
t.ok(spanMock.setTag.calledWith('error', true), 'error tag set')
|
|
568
|
+
t.ok(spanMock.log.called, 'span.log called for error')
|
|
569
|
+
t.end()
|
|
570
|
+
},
|
|
571
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
572
|
+
})
|
|
573
|
+
})
|
|
574
|
+
})
|
|
575
|
+
|
|
576
|
+
test('sendTraceToApmObservable - failed span with content.payload', t => {
|
|
577
|
+
const clientDrop = sinon.stub().resolves()
|
|
578
|
+
const { mod, spanMock } = setupModule({ clientDrop })
|
|
579
|
+
|
|
580
|
+
mod.initializeCache().then(() => {
|
|
581
|
+
const trace = [createTraceEntry(
|
|
582
|
+
{},
|
|
583
|
+
{ status: 'failed', code: '3100', description: 'transfer failed' },
|
|
584
|
+
{ payload: 'error payload data' }
|
|
585
|
+
)]
|
|
586
|
+
|
|
587
|
+
const observable = mod.sendTraceToApmObservable(trace)
|
|
588
|
+
|
|
589
|
+
observable.subscribe({
|
|
590
|
+
next: () => {
|
|
591
|
+
t.ok(spanMock.setTag.calledWith('error', true), 'error tag set')
|
|
592
|
+
t.ok(spanMock.log.called, 'span.log called with payload error')
|
|
593
|
+
t.end()
|
|
594
|
+
},
|
|
595
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
596
|
+
})
|
|
597
|
+
})
|
|
598
|
+
})
|
|
599
|
+
|
|
600
|
+
test('sendTraceToApmObservable - error path', t => {
|
|
601
|
+
const clientDrop = sinon.stub().rejects(new Error('drop failed'))
|
|
602
|
+
const { mod } = setupModule({ clientDrop })
|
|
603
|
+
|
|
604
|
+
mod.initializeCache().then(() => {
|
|
605
|
+
const observable = mod.sendTraceToApmObservable([createTraceEntry()])
|
|
606
|
+
|
|
607
|
+
observable.subscribe({
|
|
608
|
+
error: (e) => {
|
|
609
|
+
t.equal(e.message, 'drop failed', 'error propagated')
|
|
610
|
+
t.end()
|
|
611
|
+
},
|
|
612
|
+
next: () => { t.fail('should not emit next'); t.end() }
|
|
613
|
+
})
|
|
614
|
+
})
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
test('sendTraceToApmObservable - cleans up scheduler', t => {
|
|
618
|
+
const clientDrop = sinon.stub().resolves()
|
|
619
|
+
const clientGet = sinon.stub().resolves(null)
|
|
620
|
+
const clientSet = sinon.stub().resolves()
|
|
621
|
+
const { mod } = setupModule({ clientDrop, clientGet, clientSet })
|
|
622
|
+
|
|
623
|
+
mod.initializeCache().then(async () => {
|
|
624
|
+
cacheStartSpan(mod)
|
|
625
|
+
|
|
626
|
+
setTimeout(() => {
|
|
627
|
+
const trace = [createTraceEntry({
|
|
628
|
+
tags: { transactionType: 'transfer', transactionAction: 'prepare', tracestate: '' }
|
|
629
|
+
})]
|
|
630
|
+
|
|
631
|
+
mod.sendTraceToApmObservable(trace).subscribe({
|
|
632
|
+
next: (val) => {
|
|
633
|
+
t.equal(val, 'aaaa1111bbbb2222cccc3333dddd4444', 'emits traceId')
|
|
634
|
+
t.ok(clientDrop.called, 'client.drop called')
|
|
635
|
+
t.end()
|
|
636
|
+
},
|
|
637
|
+
error: (e) => { t.fail(`should not error: ${e.message}`); t.end() }
|
|
638
|
+
})
|
|
639
|
+
}, 100)
|
|
640
|
+
})
|
|
641
|
+
})
|
|
642
|
+
|
|
643
|
+
test('finishStaleTrace - processes stale spans via scheduler callback', t => {
|
|
644
|
+
const masterCtx = createApmSpanContext({
|
|
645
|
+
spanId: 'master1',
|
|
646
|
+
tags: { transactionType: 'transfer', transactionAction: 'prepare', tracestate: '' },
|
|
647
|
+
service: 'master-transfer'
|
|
648
|
+
})
|
|
649
|
+
const childCtx = createApmSpanContext({
|
|
650
|
+
spanId: 'child1',
|
|
651
|
+
parentSpanId: 'master1',
|
|
652
|
+
tags: { transactionType: 'transfer', transactionAction: 'fulfil', tracestate: '' },
|
|
653
|
+
service: 'ml_notification_event',
|
|
654
|
+
startTimestamp: '2026-01-01T00:00:01Z',
|
|
655
|
+
finishTimestamp: '2026-01-01T00:00:02Z'
|
|
656
|
+
})
|
|
657
|
+
|
|
658
|
+
const cachedTraceForFinish = {
|
|
659
|
+
item: {
|
|
660
|
+
spans: {
|
|
661
|
+
master1: { spanContext: { ...masterCtx }, state: { status: 'success' }, content: {} },
|
|
662
|
+
child1: { spanContext: { ...childCtx }, state: { status: 'success' }, content: {} }
|
|
663
|
+
},
|
|
664
|
+
masterSpan: masterCtx,
|
|
665
|
+
lastSpan: null
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// Set up clientGet to first return null (for initializeCache),
|
|
670
|
+
// then return the cached trace for finishStaleTrace
|
|
671
|
+
let callCount = 0
|
|
672
|
+
const clientGet = sinon.stub().callsFake(() => {
|
|
673
|
+
callCount++
|
|
674
|
+
if (callCount <= 1) return Promise.resolve(null)
|
|
675
|
+
return Promise.resolve(cachedTraceForFinish)
|
|
676
|
+
})
|
|
677
|
+
const clientDrop = sinon.stub().resolves()
|
|
678
|
+
const clientSet = sinon.stub().resolves()
|
|
679
|
+
|
|
680
|
+
const { mod, scheduledCallbacks, tracerMock } = setupModule({
|
|
681
|
+
clientGet, clientDrop, clientSet
|
|
682
|
+
})
|
|
683
|
+
|
|
684
|
+
mod.initializeCache().then(async () => {
|
|
685
|
+
cacheStartSpan(mod)
|
|
686
|
+
|
|
687
|
+
setTimeout(async () => {
|
|
688
|
+
t.ok(scheduledCallbacks.length > 0, 'scheduler callback was registered')
|
|
689
|
+
|
|
690
|
+
if (scheduledCallbacks.length > 0) {
|
|
691
|
+
const { fn, arg } = scheduledCallbacks[scheduledCallbacks.length - 1]
|
|
692
|
+
await fn(arg)
|
|
693
|
+
t.ok(tracerMock.startSpan.called, 'tracer.startSpan called for stale spans')
|
|
694
|
+
t.ok(clientDrop.called, 'client.drop called after finishStaleTrace')
|
|
695
|
+
}
|
|
696
|
+
t.end()
|
|
697
|
+
}, 150)
|
|
698
|
+
})
|
|
699
|
+
})
|
|
700
|
+
|
|
701
|
+
test('finishStaleTrace - no cache returns early', t => {
|
|
702
|
+
const clientGet = sinon.stub().resolves(null)
|
|
703
|
+
const clientSet = sinon.stub().resolves()
|
|
704
|
+
const clientDrop = sinon.stub().resolves()
|
|
705
|
+
|
|
706
|
+
const { mod, scheduledCallbacks } = setupModule({
|
|
707
|
+
clientGet, clientSet, clientDrop
|
|
708
|
+
})
|
|
709
|
+
|
|
710
|
+
mod.initializeCache().then(async () => {
|
|
711
|
+
cacheStartSpan(mod, { traceId: 'trace-empty' })
|
|
712
|
+
|
|
713
|
+
setTimeout(async () => {
|
|
714
|
+
if (scheduledCallbacks.length > 0) {
|
|
715
|
+
const { fn, arg } = scheduledCallbacks[scheduledCallbacks.length - 1]
|
|
716
|
+
await fn(arg)
|
|
717
|
+
t.pass('finishStaleTrace returned early with no cache')
|
|
718
|
+
}
|
|
719
|
+
t.end()
|
|
720
|
+
}, 100)
|
|
721
|
+
})
|
|
722
|
+
})
|
|
723
|
+
|
|
724
|
+
test('finishStaleTrace - error path logs error', t => {
|
|
725
|
+
const clientGet = sinon.stub()
|
|
726
|
+
.onFirstCall().resolves(null) // initializeCache
|
|
727
|
+
.onSecondCall().resolves(null) // cacheSpanContext
|
|
728
|
+
.onThirdCall().rejects(new Error('stale trace error'))
|
|
729
|
+
const clientSet = sinon.stub().resolves()
|
|
730
|
+
|
|
731
|
+
const { mod, scheduledCallbacks, loggerMock } = setupModule({
|
|
732
|
+
clientGet, clientSet
|
|
733
|
+
})
|
|
734
|
+
|
|
735
|
+
mod.initializeCache().then(() => {
|
|
736
|
+
cacheStartSpan(mod, { traceId: 'trace-err' })
|
|
737
|
+
|
|
738
|
+
setTimeout(async () => {
|
|
739
|
+
if (scheduledCallbacks.length > 0) {
|
|
740
|
+
const { fn, arg } = scheduledCallbacks[scheduledCallbacks.length - 1]
|
|
741
|
+
await fn(arg)
|
|
742
|
+
t.ok(loggerMock.error.called, 'Logger.error called on stale trace error')
|
|
743
|
+
}
|
|
744
|
+
t.end()
|
|
745
|
+
}, 100)
|
|
746
|
+
})
|
|
747
|
+
})
|
|
748
|
+
|
|
749
|
+
test('updateTraceToCache - unsubscribes existing scheduler', t => {
|
|
750
|
+
const cachedTrace = {
|
|
751
|
+
item: {
|
|
752
|
+
spans: {},
|
|
753
|
+
masterSpan: null,
|
|
754
|
+
lastSpan: null
|
|
755
|
+
},
|
|
756
|
+
ttl: 100000 // high ttl to trigger unsubscribe
|
|
757
|
+
}
|
|
758
|
+
const clientGet = sinon.stub().resolves(cachedTrace)
|
|
759
|
+
const clientSet = sinon.stub().resolves()
|
|
760
|
+
const { mod } = setupModule({ clientGet, clientSet })
|
|
761
|
+
|
|
762
|
+
mod.initializeCache().then(() => {
|
|
763
|
+
const spanContext = createStartSpanContext({ traceId: 'trace-resub' })
|
|
764
|
+
|
|
765
|
+
// First cache to create scheduler
|
|
766
|
+
const obs1 = mod.cacheSpanContextObservable({
|
|
767
|
+
spanContext,
|
|
768
|
+
state: { status: 'success' },
|
|
769
|
+
content: {}
|
|
770
|
+
})
|
|
771
|
+
obs1.subscribe({ next: () => {}, error: () => {} })
|
|
772
|
+
|
|
773
|
+
setTimeout(() => {
|
|
774
|
+
// Second cache to trigger unsubscribe on existing scheduler
|
|
775
|
+
const obs2 = mod.cacheSpanContextObservable({
|
|
776
|
+
spanContext: { ...spanContext, spanId: 'span2' },
|
|
777
|
+
state: { status: 'success' },
|
|
778
|
+
content: {}
|
|
779
|
+
})
|
|
780
|
+
obs2.subscribe({ next: () => {}, error: () => {} })
|
|
781
|
+
|
|
782
|
+
setTimeout(() => {
|
|
783
|
+
t.ok(clientSet.called, 'client.set called')
|
|
784
|
+
t.end()
|
|
785
|
+
}, 100)
|
|
786
|
+
}, 100)
|
|
787
|
+
})
|
|
788
|
+
})
|