@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.
Files changed (52) hide show
  1. package/.circleci/config.yml +12 -0
  2. package/.editorconfig +18 -0
  3. package/.grype.yaml +37 -0
  4. package/.ncurc.yaml +7 -0
  5. package/.nvmrc +1 -0
  6. package/.nycrc.yml +19 -0
  7. package/CHANGELOG.md +89 -0
  8. package/CODEOWNERS +38 -0
  9. package/Dockerfile +49 -0
  10. package/LICENSE.md +9 -0
  11. package/README.md +178 -0
  12. package/audit-ci.jsonc +10 -0
  13. package/audit-resolve.json +21 -0
  14. package/config/default.json +133 -0
  15. package/config/policy-rollover-mojaloop.json +24 -0
  16. package/config/template-mojaloop.json +222 -0
  17. package/docker/configs/apm-server.docker.yml +1015 -0
  18. package/docker/docker-compose.yaml +170 -0
  19. package/docker/kafka-consume.sh +1 -0
  20. package/docker/kafka-produces-multi.sh +1 -0
  21. package/docker/kafka-produces-single.sh +1 -0
  22. package/docker/msg-example.single +1 -0
  23. package/docker/msg-examples.multi +8 -0
  24. package/package.json +124 -0
  25. package/sbom-v12.0.0-snapshot.25.csv +1233 -0
  26. package/src/index.js +44 -0
  27. package/src/lib/config.js +10 -0
  28. package/src/lib/efk/index.js +104 -0
  29. package/src/lib/healthCheck/subServiceHealth.js +85 -0
  30. package/src/lib/tracer/index.js +21 -0
  31. package/src/observables/apmTracerObservable.js +303 -0
  32. package/src/observables/elastic-logger.js +80 -0
  33. package/src/observables/fluentd-logger.js +20 -0
  34. package/src/observables/index.js +35 -0
  35. package/src/observables/testing_agents.-js +33 -0
  36. package/src/setup.js +128 -0
  37. package/src/templates/register.js +72 -0
  38. package/test/sample-data/messages.json +4327 -0
  39. package/test/unit/index.test.js +88 -0
  40. package/test/unit/lib/config.test.js +77 -0
  41. package/test/unit/lib/efk/index.test.js +187 -0
  42. package/test/unit/lib/healthCheck/subServiceHealth.test.js +119 -0
  43. package/test/unit/lib/tracer/index.test.js +59 -0
  44. package/test/unit/observables/apmTracerObservable.test.js +788 -0
  45. package/test/unit/observables/elastic-logger.test.js +187 -0
  46. package/test/unit/observables/fluentd-logger.test.js +101 -0
  47. package/test/unit/observables/index.test.js +47 -0
  48. package/test/unit/templates/register.test.js +66 -0
  49. package/test/util/scripts/docker-efk/docker-compose.yml +74 -0
  50. package/test/util/scripts/docker-efk/fluentd/Dockerfile +2 -0
  51. package/test/util/scripts/docker-efk/fluentd/conf/fluent.conf +18 -0
  52. package/test/util/scripts/docker-efk/misc/clients-td-agent.conf +14 -0
@@ -0,0 +1,88 @@
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
+ test('index.js calls setup() on require', t => {
34
+ const setupStub = sinon.stub()
35
+ const loggerMock = { error: sinon.stub() }
36
+
37
+ proxyquire('../../src/index', {
38
+ './setup': { setup: setupStub },
39
+ '@mojaloop/central-services-logger': loggerMock
40
+ })
41
+
42
+ t.ok(setupStub.calledOnce, 'setup() called')
43
+ t.end()
44
+ })
45
+
46
+ test('index.js catch block logs error when setup throws', t => {
47
+ const setupErr = new Error('setup failed')
48
+ setupErr.stack = 'setup failed stack'
49
+ const setupStub = sinon.stub().throws(setupErr)
50
+ const loggerMock = { error: sinon.stub() }
51
+
52
+ proxyquire('../../src/index', {
53
+ './setup': { setup: setupStub },
54
+ '@mojaloop/central-services-logger': loggerMock
55
+ })
56
+
57
+ t.ok(loggerMock.error.called, 'Logger.error called')
58
+ const errorArg = loggerMock.error.firstCall.args[0]
59
+ t.ok(errorArg.includes('setup failed stack'), 'logs error stack')
60
+ t.end()
61
+ })
62
+
63
+ test('index.js registers unhandledRejection handler', t => {
64
+ const setupStub = sinon.stub()
65
+ const loggerMock = { error: sinon.stub() }
66
+ const processOnStub = sinon.stub(process, 'on')
67
+
68
+ proxyquire('../../src/index', {
69
+ './setup': { setup: setupStub },
70
+ '@mojaloop/central-services-logger': loggerMock
71
+ })
72
+
73
+ const unhandledCall = processOnStub.getCalls().find(
74
+ c => c.args[0] === 'unhandledRejection'
75
+ )
76
+ t.ok(unhandledCall, 'unhandledRejection handler registered')
77
+
78
+ // Invoke the handler to test it
79
+ if (unhandledCall) {
80
+ const consoleErrorStub = sinon.stub(console, 'error')
81
+ unhandledCall.args[1]('test reason', Promise.resolve())
82
+ t.ok(loggerMock.error.called, 'Logger.error called in handler')
83
+ consoleErrorStub.restore()
84
+ }
85
+
86
+ processOnStub.restore()
87
+ t.end()
88
+ })
@@ -0,0 +1,77 @@
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 proxyquire = require('proxyquire').noCallThru()
31
+
32
+ test('config exports all expected keys', t => {
33
+ const mockConfig = {
34
+ PORT: 3082,
35
+ EFK_CLIENT: { host: 'localhost:9200', log: 'error', index: { name: 'mojaloop', template: '{{index}}-{{date}}' } },
36
+ APM: { serviceName: 'event-stream-processor' },
37
+ KAFKA: { CONSUMER: {}, PRODUCER: {} },
38
+ CACHE: { ttl: 300000, segment: 'trace' },
39
+ SPAN: { START_CRITERIA: {}, END_CRITERIA: {} }
40
+ }
41
+
42
+ const Config = proxyquire('../../../src/lib/config', {
43
+ 'parse-strings-in-object': () => mockConfig,
44
+ rc: () => mockConfig,
45
+ '../../config/default.json': {}
46
+ })
47
+
48
+ t.equal(Config.PORT, 3082, 'PORT is correct')
49
+ t.deepEqual(Config.EFK_CLIENT, mockConfig.EFK_CLIENT, 'EFK_CLIENT is correct')
50
+ t.deepEqual(Config.APM, mockConfig.APM, 'APM is correct')
51
+ t.deepEqual(Config.KAFKA_CONFIG, mockConfig.KAFKA, 'KAFKA_CONFIG maps to KAFKA')
52
+ t.deepEqual(Config.CACHE, mockConfig.CACHE, 'CACHE is correct')
53
+ t.deepEqual(Config.SPAN, mockConfig.SPAN, 'SPAN is correct')
54
+ t.end()
55
+ })
56
+
57
+ test('config PORT is a number', t => {
58
+ const mockConfig = {
59
+ PORT: 3082,
60
+ EFK_CLIENT: {},
61
+ APM: {},
62
+ KAFKA: {},
63
+ CACHE: { ttl: 300000, segment: 'trace' },
64
+ SPAN: {}
65
+ }
66
+
67
+ const Config = proxyquire('../../../src/lib/config', {
68
+ 'parse-strings-in-object': () => mockConfig,
69
+ rc: () => mockConfig,
70
+ '../../config/default.json': {}
71
+ })
72
+
73
+ t.equal(typeof Config.PORT, 'number', 'PORT is a number')
74
+ t.ok(Config.CACHE.ttl, 'CACHE has ttl')
75
+ t.ok(Config.CACHE.segment, 'CACHE has segment')
76
+ t.end()
77
+ })
@@ -0,0 +1,187 @@
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
+ const setupMocks = () => {
34
+ const eventHandlers = {}
35
+ const floggerMock = {
36
+ configure: sinon.stub(),
37
+ on: sinon.stub().callsFake((event, handler) => {
38
+ eventHandlers[event] = handler
39
+ })
40
+ }
41
+ const pingStub = sinon.stub().resolves(true)
42
+ const clientMock = { ping: pingStub }
43
+ const elasticsearchMock = {
44
+ Client: sinon.stub().returns(clientMock)
45
+ }
46
+ const configMock = {
47
+ EFK_CLIENT: {
48
+ host: 'localhost:9200',
49
+ log: 'error',
50
+ index: { name: 'mojaloop', template: '{{index}}-{{date}}' }
51
+ }
52
+ }
53
+ const loggerMock = {
54
+ info: sinon.stub(),
55
+ error: sinon.stub()
56
+ }
57
+
58
+ const createModule = () => proxyquire('../../../../src/lib/efk/index', {
59
+ 'fluent-logger': floggerMock,
60
+ elasticsearch: elasticsearchMock,
61
+ '../../lib/config': configMock,
62
+ '@mojaloop/central-services-logger': loggerMock,
63
+ mustache: require('mustache'),
64
+ moment: require('moment')
65
+ })
66
+
67
+ return { floggerMock, elasticsearchMock, configMock, loggerMock, clientMock, pingStub, eventHandlers, createModule }
68
+ }
69
+
70
+ test('initLogger calls flogger.configure and registers event handlers', async t => {
71
+ const { floggerMock, createModule } = setupMocks()
72
+ const efk = createModule()
73
+
74
+ await efk.initLogger('test-prefix', { host: 'localhost' })
75
+ t.ok(floggerMock.configure.calledOnce, 'flogger.configure called')
76
+ t.ok(floggerMock.configure.calledWith('test-prefix', { host: 'localhost' }), 'correct args')
77
+ t.ok(floggerMock.on.calledWith('connect'), 'connect handler registered')
78
+ t.ok(floggerMock.on.calledWith('error'), 'error handler registered')
79
+ t.end()
80
+ })
81
+
82
+ test('ElasticSearchClient.getIndex returns mojaloop-YYYY.MM.DD format', t => {
83
+ const { createModule } = setupMocks()
84
+ const efk = createModule()
85
+
86
+ const index = efk.ElasticSearchClient.getIndex()
87
+ t.ok(/^mojaloop-\d{4}\.\d{2}\.\d{2}$/.test(index), `index matches pattern: ${index}`)
88
+ t.end()
89
+ })
90
+
91
+ test('ElasticSearchClient.getIndex caches result on second call', t => {
92
+ const { createModule } = setupMocks()
93
+ const efk = createModule()
94
+
95
+ const index1 = efk.ElasticSearchClient.getIndex()
96
+ const index2 = efk.ElasticSearchClient.getIndex()
97
+ t.equal(index1, index2, 'same index returned on second call')
98
+ t.end()
99
+ })
100
+
101
+ test('ElasticSearchClient.getInstance creates client and pings', async t => {
102
+ const { elasticsearchMock, clientMock, pingStub, createModule } = setupMocks()
103
+ const efk = createModule()
104
+
105
+ const instance = await efk.ElasticSearchClient.getInstance()
106
+ t.ok(elasticsearchMock.Client.calledOnce, 'Client constructor called')
107
+ t.ok(pingStub.calledOnce, 'ping called')
108
+ t.equal(instance, clientMock, 'returns client instance')
109
+ t.end()
110
+ })
111
+
112
+ test('ElasticSearchClient.getInstance returns cached on second call', async t => {
113
+ const { elasticsearchMock, createModule } = setupMocks()
114
+ const efk = createModule()
115
+
116
+ const instance1 = await efk.ElasticSearchClient.getInstance()
117
+ const instance2 = await efk.ElasticSearchClient.getInstance()
118
+ t.equal(instance1, instance2, 'same instance returned')
119
+ t.equal(elasticsearchMock.Client.callCount, 1, 'Client constructor only called once')
120
+ t.end()
121
+ })
122
+
123
+ test('ElasticSearchClient.getInstance error path logs and rethrows', async t => {
124
+ const pingErr = new Error('ping failed')
125
+ pingErr.stack = 'ping failed stack'
126
+ const pingStub = sinon.stub().rejects(pingErr)
127
+ const clientMock = { ping: pingStub }
128
+ const elasticsearchMock = { Client: sinon.stub().returns(clientMock) }
129
+ const loggerMock = { info: sinon.stub(), error: sinon.stub() }
130
+ const configMock = {
131
+ EFK_CLIENT: { host: 'localhost:9200', log: 'error', index: { name: 'mojaloop', template: '{{index}}-{{date}}' } }
132
+ }
133
+ const floggerMock = { configure: sinon.stub(), on: sinon.stub() }
134
+
135
+ const efk = proxyquire('../../../../src/lib/efk/index', {
136
+ 'fluent-logger': floggerMock,
137
+ elasticsearch: elasticsearchMock,
138
+ '../../lib/config': configMock,
139
+ '@mojaloop/central-services-logger': loggerMock,
140
+ mustache: require('mustache'),
141
+ moment: require('moment')
142
+ })
143
+
144
+ try {
145
+ await efk.ElasticSearchClient.getInstance()
146
+ t.fail('should have thrown')
147
+ } catch (e) {
148
+ t.equal(e.message, 'ping failed', 'rethrows error')
149
+ t.ok(loggerMock.error.calledWith('ping failed stack'), 'logs error stack')
150
+ }
151
+ t.end()
152
+ })
153
+
154
+ test('initLogger connect handler executes', async t => {
155
+ const { eventHandlers, createModule } = setupMocks()
156
+ const efk = createModule()
157
+
158
+ await efk.initLogger('prefix', {})
159
+ t.ok(eventHandlers.connect, 'connect handler exists')
160
+ eventHandlers.connect()
161
+ t.pass('connect handler executed without error')
162
+ t.end()
163
+ })
164
+
165
+ test('initLogger error handler executes', async t => {
166
+ const { eventHandlers, createModule } = setupMocks()
167
+ const efk = createModule()
168
+
169
+ // Suppress unhandled rejection from Promise.reject in handler
170
+ const handler = () => {}
171
+ process.on('unhandledRejection', handler)
172
+ t.teardown(() => process.removeListener('unhandledRejection', handler))
173
+
174
+ await efk.initLogger('prefix', {})
175
+ t.ok(eventHandlers.error, 'error handler exists')
176
+ eventHandlers.error(new Error('test error'))
177
+ t.pass('error handler executed')
178
+ t.end()
179
+ })
180
+
181
+ test('logger export is the flogger mock', t => {
182
+ const { floggerMock, createModule } = setupMocks()
183
+ const efk = createModule()
184
+
185
+ t.equal(efk.logger, floggerMock, 'logger export is fluent-logger')
186
+ t.end()
187
+ })
@@ -0,0 +1,119 @@
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
+ const setupMocks = (opts = {}) => {
34
+ const consumerStubs = {}
35
+ const topics = opts.topics || ['topic-1', 'topic-2']
36
+
37
+ for (const topic of topics) {
38
+ consumerStubs[topic] = {
39
+ isHealthy: sinon.stub().resolves(opts.healthResults ? opts.healthResults[topic] : true)
40
+ }
41
+ }
42
+
43
+ if (opts.throwOnIsHealthy) {
44
+ consumerStubs[opts.throwOnIsHealthy].isHealthy = sinon.stub().rejects(new Error('health check failed'))
45
+ }
46
+
47
+ const ConsumerMock = {
48
+ getListOfTopics: opts.throwOnGetTopics
49
+ ? sinon.stub().throws(new Error('getListOfTopics failed'))
50
+ : sinon.stub().returns(topics),
51
+ getConsumer: sinon.stub().callsFake(topic => consumerStubs[topic])
52
+ }
53
+
54
+ const loggerMock = {
55
+ isWarnEnabled: true,
56
+ warn: sinon.stub(),
57
+ error: sinon.stub()
58
+ }
59
+
60
+ const mod = proxyquire('../../../../src/lib/healthCheck/subServiceHealth', {
61
+ '@mojaloop/central-services-shared': {
62
+ HealthCheck: {
63
+ HealthCheckEnums: {
64
+ statusEnum: { OK: 'OK', DOWN: 'DOWN' },
65
+ serviceName: { broker: 'broker' }
66
+ }
67
+ }
68
+ },
69
+ '@mojaloop/central-services-logger': loggerMock,
70
+ '@mojaloop/central-services-stream': {
71
+ Util: { Consumer: ConsumerMock }
72
+ }
73
+ })
74
+
75
+ return { mod, ConsumerMock, loggerMock }
76
+ }
77
+
78
+ test('getSubServiceHealthBroker - all healthy returns OK', async t => {
79
+ const { mod } = setupMocks()
80
+ const result = await mod.getSubServiceHealthBroker()
81
+ t.equal(result.name, 'broker', 'name is broker')
82
+ t.equal(result.status, 'OK', 'status is OK')
83
+ t.end()
84
+ })
85
+
86
+ test('getSubServiceHealthBroker - one unhealthy returns DOWN', async t => {
87
+ const { mod } = setupMocks({
88
+ healthResults: { 'topic-1': true, 'topic-2': false }
89
+ })
90
+ const result = await mod.getSubServiceHealthBroker()
91
+ t.equal(result.status, 'DOWN', 'status is DOWN')
92
+ t.end()
93
+ })
94
+
95
+ test('getSubServiceHealthBroker - isHealthy throws returns DOWN', async t => {
96
+ const { mod } = setupMocks({
97
+ throwOnIsHealthy: 'topic-1'
98
+ })
99
+ const result = await mod.getSubServiceHealthBroker()
100
+ t.equal(result.status, 'DOWN', 'status is DOWN when isHealthy throws')
101
+ t.end()
102
+ })
103
+
104
+ test('getSubServiceHealthBroker - getListOfTopics throws returns DOWN', async t => {
105
+ const { mod, loggerMock } = setupMocks({
106
+ throwOnGetTopics: true
107
+ })
108
+ const result = await mod.getSubServiceHealthBroker()
109
+ t.equal(result.status, 'DOWN', 'status is DOWN')
110
+ t.ok(loggerMock.warn.called, 'logs warning')
111
+ t.end()
112
+ })
113
+
114
+ test('getSubServiceHealthBroker - no topics returns OK', async t => {
115
+ const { mod } = setupMocks({ topics: [] })
116
+ const result = await mod.getSubServiceHealthBroker()
117
+ t.equal(result.status, 'OK', 'status is OK with no topics')
118
+ t.end()
119
+ })
@@ -0,0 +1,59 @@
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
+ test('tracer module starts agent and creates tracer', t => {
34
+ const agentInstance = {
35
+ captureError: sinon.stub()
36
+ }
37
+ const AgentMock = {
38
+ start: sinon.stub().returns(agentInstance)
39
+ }
40
+ const tracerInstance = { startSpan: sinon.stub() }
41
+ const TracerMock = sinon.stub().returns(tracerInstance)
42
+ const configMock = {
43
+ APM: { serviceName: 'test-service' }
44
+ }
45
+
46
+ const tracerModule = proxyquire('../../../../src/lib/tracer/index', {
47
+ '@mojaloop/elastic-apm-node': AgentMock,
48
+ '@mojaloop/elastic-apm-node-opentracing': TracerMock,
49
+ '../config': configMock
50
+ })
51
+
52
+ t.ok(AgentMock.start.calledOnce, 'Agent.start called')
53
+ t.ok(AgentMock.start.calledWith(configMock.APM), 'Agent.start called with APM config')
54
+ t.ok(agentInstance.captureError.calledOnce, 'captureError called')
55
+ t.equal(tracerModule.agent, agentInstance, 'exports agent')
56
+ t.ok(TracerMock.calledWith(agentInstance), 'Tracer created with agent')
57
+ t.equal(tracerModule.tracer, tracerInstance, 'exports tracer')
58
+ t.end()
59
+ })