@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,35 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2020-2025 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
+ - Name Surname <name.surname@mojaloop.io>
24
+ * Valentin Genev <valentin.genev@modusbox.com>
25
+ * Deon Botha <deon.botha@modusbox.com>
26
+ --------------
27
+ ******/
28
+
29
+ 'use strict'
30
+
31
+ module.exports = {
32
+ fluentdObservable: require('./fluentd-logger').FluentdLoggerObservable,
33
+ TraceObservable: require('./apmTracerObservable'),
34
+ elasticsearchClientObservable: require('./elastic-logger').elasticsearchClientObservable
35
+ }
@@ -0,0 +1,33 @@
1
+ const Agent = require('@mojaloop/elastic-apm-node')
2
+ const Tracer = require('@mojaloop/elastic-apm-node-opentracing')
3
+
4
+ const agentConf = {
5
+ "serviceName": "test for agent",
6
+ "serverUrl": "http://dev1-apm.mojaloop.live:80"
7
+ }
8
+
9
+ const agent = Agent.start(agentConf)
10
+ const tracer = new Tracer(agent)
11
+
12
+ const pause = async (ms) => {
13
+ return new Promise((resolve, reject) => {
14
+ setTimeout(() => {
15
+ resolve()
16
+ }, ms)
17
+ })
18
+ }
19
+
20
+ const doRun = async () => {
21
+ let span = tracer.startSpan('parent-transaction')
22
+ await pause(15000)
23
+ let span2 = tracer.startSpan('child-span', { childOf: span.context() })
24
+ await pause(15000)
25
+ span2.finish()
26
+ let span3 = tracer.startSpan('child2-span', { childOf: span.context() })
27
+ await pause(15000)
28
+ span3.finish()
29
+ await pause(15000)
30
+ span.finish()
31
+ }
32
+
33
+ doRun()
package/src/setup.js ADDED
@@ -0,0 +1,128 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2020-2025 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
+ - Name Surname <name.surname@mojaloop.io>
24
+ * Valentin Genev <valentin.genev@modusbox.com>
25
+ * Deon Botha <deon.botha@modusbox.com>
26
+ --------------
27
+ ******/
28
+
29
+ 'use strict'
30
+
31
+ /**
32
+ * @module src/setup
33
+ */
34
+
35
+ const Kafka = require('@mojaloop/central-services-shared').Util.Kafka
36
+ const Util = require('@mojaloop/central-services-stream').Util
37
+ const Consumer = Util.Consumer
38
+ const Enum = require('@mojaloop/central-services-shared').Enum
39
+ const Logger = require('@mojaloop/central-services-logger')
40
+ const Rx = require('rxjs')
41
+ const { share, filter, flatMap, catchError } = require('rxjs/operators')
42
+ const Config = require('./lib/config')
43
+ const HealthCheck = require('@mojaloop/central-services-shared').HealthCheck.HealthCheck
44
+ const { createHealthCheckServer, defaultHealthHandler } = require('@mojaloop/central-services-health')
45
+ const packageJson = require('../package.json')
46
+ const { getSubServiceHealthBroker } = require('./lib/healthCheck/subServiceHealth')
47
+ const Observables = require('./observables')
48
+ const { initializeCache } = Observables.TraceObservable
49
+
50
+ const setup = async () => {
51
+ Rx.config.onUnhandledError = (err) => {
52
+ console.warn(err)
53
+ }
54
+ Rx.config.useDeprecatedNextContext = true
55
+ await registerEventHandler()
56
+ await initializeCache(Config.CACHE_CONFIG)
57
+ const topicName = Kafka.transformGeneralTopicName(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, Enum.Events.Event.Action.EVENT)
58
+ const consumer = Consumer.getConsumer(topicName)
59
+ const healthCheck = new HealthCheck(packageJson, [
60
+ getSubServiceHealthBroker
61
+ ])
62
+ await createHealthCheckServer(Config.PORT, defaultHealthHandler(healthCheck))
63
+
64
+ const topicObservable = Rx.Observable.create((observer) => {
65
+ consumer.on('message', async (message) => {
66
+ Logger.debug(`Central-Event-Processor :: Topic ${topicName} :: Payload: \n${JSON.stringify(message.value, null, 2)}`)
67
+ observer.next({ message })
68
+ if (!Consumer.isConsumerAutoCommitEnabled(topicName)) {
69
+ consumer.commitMessageSync(message)
70
+ }
71
+ })
72
+ })
73
+
74
+ const sharedMessageObservable = topicObservable.pipe(share(), catchError(e => { return Rx.onErrorResumeNext(sharedMessageObservable) }))
75
+
76
+ sharedMessageObservable.subscribe(async props => {
77
+ Observables.elasticsearchClientObservable(props).subscribe({
78
+ next: v => Logger.debug(v),
79
+ error: (e) => Logger.error(e.stack),
80
+ completed: () => Logger.debug('elastic API log completed')
81
+ })
82
+ })
83
+
84
+ const tracingObservable = sharedMessageObservable.pipe(
85
+ filter(props => props.message.value.metadata.event.type === 'trace'),
86
+ flatMap(Observables.TraceObservable.extractContextObservable),
87
+ flatMap(Observables.TraceObservable.cacheSpanContextObservable),
88
+ flatMap(Observables.TraceObservable.findLastSpanObservable),
89
+ flatMap(Observables.TraceObservable.recreateTraceObservable),
90
+ flatMap(Observables.TraceObservable.sendTraceToApmObservable),
91
+ catchError(e => { return Rx.onErrorResumeNext(tracingObservable) }))
92
+
93
+ tracingObservable.subscribe({
94
+ next: traceId => {
95
+ Logger.debug(`traceId ${traceId} sent to APM`)
96
+ },
97
+ error: (e) => Logger.error(e.stack),
98
+ completed: () => Logger.debug('trace info sent')
99
+ })
100
+ }
101
+
102
+ /**
103
+ * @function registerEventHandler
104
+ *
105
+ * @description This is used to register the handler for the Event topic according to a specified Kafka congfiguration
106
+ *
107
+ * @returns true
108
+ * @throws {Error} - if handler failed to create
109
+ */
110
+ const registerEventHandler = async () => {
111
+ try {
112
+ const EventHandler = {
113
+ topicName: Kafka.transformGeneralTopicName(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, Enum.Events.Event.Action.EVENT),
114
+ config: Kafka.getKafkaConfig(Config.KAFKA_CONFIG, Enum.Kafka.Config.CONSUMER, Enum.Events.Event.Type.EVENT.toUpperCase())
115
+
116
+ }
117
+ EventHandler.config.rdkafkaConf['client.id'] = EventHandler.topicName
118
+ await Consumer.createHandler(EventHandler.topicName, EventHandler.config)
119
+ return true
120
+ } catch (e) {
121
+ Logger.error(e.stack)
122
+ throw e
123
+ }
124
+ }
125
+
126
+ module.exports = {
127
+ setup
128
+ }
@@ -0,0 +1,72 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2020-2025 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
+ - Name Surname <name.surname@mojaloop.io>
24
+
25
+ * ModusBox
26
+ - Miguel de Barros <migue.debarros@modusbox.com>
27
+ --------------
28
+ ******/
29
+ 'use strict'
30
+
31
+ const Logger = require('@mojaloop/central-services-logger')
32
+ const Config = require('../lib/config').util.toObject()
33
+
34
+ const elasticsearch = require('elasticsearch')
35
+ const client = new elasticsearch.Client({
36
+ host: Config.efkClient.host,
37
+ log: Config.efkClient.log
38
+ })
39
+
40
+ /**
41
+ * Function to register Mojaloop Templates for a Mojaloop specific Index on Elasticsearch.
42
+ * This is required as the event.content model needs to be `flattened` to support the dynamic content.
43
+ * Note that the content field must be of type Object (not a stirng, etc) to be correctly indexed.
44
+ *
45
+ * TODO: To be added to an init or migration script on startup. This file is manually executed at this moment. The init-script would need to first check if the template exists, and/if not it would then create the template.
46
+ *
47
+ * One can also run the following curl commands from project root:
48
+ * curl -X PUT "http://localhost:9200/_template/mojatemplate?pretty" -H 'Content-Type: application/json' -d @config/template-mojaloop.json'
49
+ * curl -X DELETE "http://localhost:9200/_template/mojatemplate"
50
+ * curl -X GET "http://localhost:9200/_template/mojatemplate" | jq
51
+ *
52
+ * @returns {Promise<void>}
53
+ */
54
+ const registerMojaloopTemplate = async () => {
55
+ const resultPing = await client.ping({
56
+ // ping usually has a 3000ms timeout
57
+ requestTimeout: 1000
58
+ })
59
+
60
+ Logger.debug(`connection result=${resultPing}`)
61
+
62
+ const templateMojaloop = require('../../config/template-mojaloop.json')
63
+ const resultTemplate = await client.indices.putTemplate({
64
+ name: 'mojatemplate',
65
+ create: false,
66
+ body: templateMojaloop
67
+ })
68
+
69
+ Logger.debug(`template create result=${JSON.stringify(resultTemplate)}`)
70
+ }
71
+
72
+ registerMojaloopTemplate()