@edirect/trace 9.0.15 → 9.0.16

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 (44) hide show
  1. package/.editorconfig +14 -14
  2. package/.eslintignore +1 -1
  3. package/.eslintrc.js +44 -44
  4. package/.eslintrc.json +159 -159
  5. package/.prettierrc +4 -4
  6. package/README.md +353 -352
  7. package/dist/constants/headers.d.ts +4 -4
  8. package/dist/constants/headers.js +5 -5
  9. package/dist/constants/index.d.ts +1 -1
  10. package/dist/constants/index.js +8 -8
  11. package/dist/index.d.ts +3 -4
  12. package/dist/index.js +29 -30
  13. package/dist/index.js.map +1 -1
  14. package/dist/middlewares/CLS.d.ts +5 -5
  15. package/dist/middlewares/CLS.js +33 -33
  16. package/dist/middlewares/body.d.ts +5 -5
  17. package/dist/middlewares/body.js +40 -40
  18. package/dist/middlewares/index.d.ts +4 -4
  19. package/dist/middlewares/index.js +14 -14
  20. package/dist/middlewares/nextjs.d.ts +5 -5
  21. package/dist/middlewares/nextjs.js +37 -37
  22. package/dist/middlewares/trace.d.ts +5 -5
  23. package/dist/middlewares/trace.js +26 -26
  24. package/dist/otel.d.ts +1 -1
  25. package/dist/otel.js +86 -86
  26. package/dist/services/APM.d.ts +14 -14
  27. package/dist/services/APM.js +57 -57
  28. package/dist/services/index.d.ts +1 -1
  29. package/dist/services/index.js +8 -8
  30. package/dist/tsconfig.build.tsbuildinfo +1 -1
  31. package/dist/utils/CLS.d.ts +5 -5
  32. package/dist/utils/CLS.js +41 -41
  33. package/dist/utils/body-prop-tools.d.ts +5 -5
  34. package/dist/utils/body-prop-tools.js +23 -23
  35. package/dist/utils/index.d.ts +3 -3
  36. package/dist/utils/index.js +12 -12
  37. package/dist/utils/trace.d.ts +5 -5
  38. package/dist/utils/trace.js +12 -12
  39. package/package.json +41 -42
  40. package/tsconfig.build.json +9 -9
  41. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  42. package/.idea/modules.xml +0 -8
  43. package/.idea/trace.iml +0 -12
  44. package/.idea/vcs.xml +0 -6
package/README.md CHANGED
@@ -1,352 +1,353 @@
1
- # Open Telemetry Trace
2
-
3
- ## Background
4
-
5
- Based on application logs and a sales checker service that triggers alerts on a slack channel, the current solution offers some insight into the application but falls short when it comes to deep understanding of exceptions and the general business flow.
6
-
7
- Due to the sheer amount of logs and complex integrations between the application microservices, it is not uncommon to find scenarios where the development team can spend hours tracing the problem back to its origin.
8
-
9
- - Non-standard logs
10
- - The log format and content depends on implementation and log uniformity is a challenge by itself.
11
- - Logs might be outdated or absent
12
- - As services shift, evolve and so should logs. This can be a source for inconsistencies as logs may not be updated or can be wrongly removed during the feature implementation.
13
- - Logs are difficult to query and trace
14
- - Even with the help of tools such Elasticsearch, querying and tracing logs can be a difficult task due to complex interactions among different services in the application.
15
- - As an example, an error response from quote-engine but we only dispose of the policy number to find the root cause. The data provided is not enough for a complete investigation, which may lead to long research.
16
- - Not able to trace all exception occurrences
17
- - In the case shown by the previous item the error was not logged or at least not easily accessible.
18
- - Sales triggers are not flexible
19
- - Triggers are configured as code in the sales checker service. Any change to the logic must be implemented, tested and deployed.
20
- - Sales alert do not provide insight into root causes
21
- - The alert contains only the basic information necessary for an investigation to start. It contains the lead, partner, vertical and a brief description of the problem.
22
- - Limited business process visibility
23
- - As the alerts are based on sale statuses, we have limited insight into the process. With such a limited view we cannot verify which steps represent bottlenecks or are more prone to errors. Useful information for maintaining and improving the system.
24
- - No concise view of the business flow
25
- - Currently there are no dashboards or easily accessible views that can provide correlated information about the many steps that compose the Bolttech business flow.
26
-
27
- ## Detailed Design
28
-
29
- To fill the gaps that the current approach, the proposed solution must fulfill some requirements:
30
-
31
- - Requires minimal code and service changes
32
- - Must be flexible and configurable
33
- - Must capable of querying multiple data sources
34
- - Must be an evolutive tool that can help multiple types of users
35
- - Must have a dashboard that shows useful information about business flow
36
-
37
- Taking the requirements in consideration, the proposed solutions employs an application monitoring tool named Open Telemetry, a microservice responsible for correlating the data captured by the tool and a comprehensive dashboard for the final user.
38
-
39
- ![image](./docs/architecture.png)
40
-
41
- ## Usage
42
-
43
- - Tracing Apps
44
- - Datadog
45
- - Services and Observability
46
- - [Service Visibility Frontend](https://bitbucket.org/gofrank/service-visibility-frontend/src/main/)
47
- - [Service Visibility Backend](https://bitbucket.org/gofrank/service-visibility-backend/src/main/)
48
-
49
- ## Sample application
50
-
51
- - [Service Visibility Samples](https://bitbucket.org/gofrank/service-visibility-samples/src/master/)
52
-
53
- ## NestJS Applications
54
-
55
- NestJs applications __should be upgraded to version 9.__
56
-
57
- - Just the fact of upgrade the nest-app to version 9 will able the service to ship all logs to the service visibility
58
-
59
- As we applied the `@edirect/trace` to the nest-app, we dont need to have the trace part of code on nest-app applications with nest-app version 9
60
-
61
- In case of impossibility to upgrade, use the "Other" instructions using `@edirect/trace` directly
62
-
63
- Integration Service was not able to upgrade because it uses a react-ssr that does not have compatibility with nestjs 9, so, even as a nestjs application, we implemented the "Other" way
64
-
65
- - [@edirect/nest-app:9](https://bitbucket.org/gofrank/nest-app/src/master/)
66
-
67
- 1. Upgrade nest-app, all edirect dependencies and all nestjs dependencies
68
-
69
- 2. We should initialize our application with `otel` as a requirement for node startup command:
70
-
71
- ```bash
72
- node --require ./node_modules/@edirect/trace/dist/otel.js dist/main
73
- ```
74
-
75
- (dist/main or your application entrypoint)
76
-
77
- 3. Remove the old client initialization as we moved it to the node require
78
-
79
- If the service was using old trace version
80
-
81
- ```js
82
- const config = require('config');
83
- const { services: { APM }, middlewares: traceMiddlewares } = require('@edirect/trace');
84
- const apmConfig = config.get('apm');
85
-
86
- if (apmConfig.enabled) {
87
- const apm = new APM(apmConfig.service, apmConfig.url, apmConfig.traceServer);
88
- apm.startTrace();
89
- }
90
- ```
91
-
92
- 4. Remove the middleware usage
93
-
94
- If the service was using old trace version
95
-
96
- Remove it completely as we moved it to inside of `@edirect/trace`:
97
-
98
- ```js
99
- if (apmConfig.enabled) {
100
- app.use(new traceMiddlewares.Body().body);
101
- }
102
- ```
103
-
104
- ## NextJS Applications
105
- NextJS applications __should be upgraded to version 12 or above__.
106
-
107
- 1. Install the trace package:
108
- ```bash
109
- npm install --save @edirect/trace
110
- ```
111
-
112
- 2. Create a middleware
113
- When you upgrade the NextJS application to the version 12(or above) you will be able
114
- to use [Middlewares](https://nextjs.org/docs/advanced-features/middleware).
115
- Create a file called `middleware.ts(or .js)` inside of the `src` folder.
116
- ```ts
117
- import { NextResponse } from 'next/server';
118
- import type { NextRequest } from 'next/server';
119
- import NextJsTracer from '@edirect/trace/dist/middlewares/nextjs';
120
-
121
- // This function can be marked `async` if using `await` inside
122
- export function middleware(request: NextRequest) {
123
- // @edirect/trace
124
- new NextJsTracer().body(req as unknown as Request & Record<string, unknown>);
125
- NextResponse.next();
126
- }
127
- ```
128
- 3. Install dotenv and create a dotenv.config
129
- ```bash
130
- npm install --save dotenv
131
- ```
132
- `dotenv.config.js`
133
- ```js
134
- const dotenv = require('dotenv');
135
- const nodeEnv = process.env.NODE_ENV || 'development';
136
-
137
- dotenv.config({ path: `.${nodeEnv}.env` });
138
- ```
139
- 4. Add the environment variables to your NextJS app:
140
- __Example:__`.development.env`
141
- ```yml
142
- ## OPEN TELEMETRY
143
- TRACE_TAG_OWNER=OWNER
144
- TRACE_TAG_SCOPE=ie
145
- TRACE_TAG_SERVICENAME=frontend-v2
146
- TRACE_TAG_TENANT={STAGE}-{INSTANCE}
147
- TRACE_SERVER_URL={URL}
148
- TRACE_TAG_SERVICE=frontend-v2-{STAGE}-{INSTANCE}
149
- TRACE_TAG_VERSION=1.0.0
150
- TRACE_TAG_CLUSTER=stag-ie
151
- TRACE_TAG_ENV=ie-stag-broker
152
- ```
153
-
154
- 5. Serve your production build on the standalone mode. Add this line to your `next.config.js` file
155
- ```js
156
- module.exports = {
157
- // ...myOtherSettings
158
- output: 'standalone'
159
- }
160
- ```
161
-
162
- 6. Change the __start__ and __dev__ command:
163
- ```json
164
- {
165
- "scripts": {
166
- "dev": "cross-env NODE_ENV=development node --require ./dotenv.config.js --require ./node_modules/@edirect/trace/dist/otel.js ./node_modules/next/dist/bin/next -p 3100",
167
- "start": "cross-env NODE_ENV=production node --require ./dotenv.config.js --require ./node_modules/@edirect/trace/dist/otel.js ./build/client/standalone/server.js -p 3100"
168
- }
169
- }
170
- ```
171
-
172
-
173
- ## Other (Not nest-app:9)
174
-
175
- ### If you are unable to update to nest 9 or is not nest (express)
176
-
177
- - [@edirect/trace](https://bitbucket.org/gofrank/trace/src/master/)
178
-
179
- The usage of `@edirect/trace` will need your care to inject our middleware to log all requests from the app
180
-
181
- 1. Install the trace package
182
-
183
- ```bash
184
- npm install --save @edirect/trace
185
- ```
186
-
187
- 2. Import the trace package
188
-
189
- ```js
190
- import { middlewares as traceMiddlewares } from '@edirect/trace';
191
- ```
192
-
193
- 3. Add the trace middleware to your application routes
194
-
195
- Express:
196
-
197
- ```js
198
- app.use(new traceMiddlewares.Body().body);
199
- ```
200
-
201
- NestJs with nest-app:
202
-
203
- ```js
204
- consumer
205
- .apply(new traceMiddlewares.Body().body)
206
- .forRoutes({path: '/**', method: RequestMethod.ALL});
207
- ```
208
-
209
- 4. We should initialize our application with `otel` as a requirement for node startup command:
210
-
211
- ```bash
212
- node --require ./node_modules/@edirect/trace/dist/otel.js dist/main
213
- ```
214
-
215
- (dist/main or your application entrypoint)
216
-
217
- 5. Remove the old client initialization as we moved it to the node require
218
-
219
- If the service was using old trace version
220
-
221
- ```js
222
- const config = require('config');
223
- const { services: { APM }, middlewares: traceMiddlewares } = require('@edirect/trace');
224
- const apmConfig = config.get('apm');
225
-
226
- if (apmConfig.enabled) {
227
- const apm = new APM(apmConfig.service, apmConfig.url, apmConfig.traceServer);
228
- apm.startTrace();
229
- }
230
- ```
231
-
232
- 6. Clean the middleware usage
233
-
234
- If the service was using old trace version
235
-
236
- Replace:
237
-
238
- ```js
239
- if (apmConfig.enabled) {
240
- app.use(new traceMiddlewares.Body().body);
241
- }
242
- ```
243
-
244
- with:
245
-
246
- ```js
247
- app.use(new traceMiddlewares.Body().body);
248
- ```
249
-
250
- ## Applying k8s configurations (Required)
251
-
252
- We created a standard way to deploy our application configurations and simplify its configurations:
253
-
254
- - Sometimes it could be already done by another team and you don't need to manage the configuration, just generate the configurations and apply it
255
- - `bolttech-broker-asia\staging\config-map.yaml` (or your environment config-map)
256
-
257
- ```yaml
258
- auth: #or your service
259
- trace:
260
- tags:
261
- scope: ie #your tech center
262
- env: ie-stag-broker #current k8s cluster environment
263
- cluster: stag-ie #current k8s cluster
264
- tenant: ${namespace}
265
- service: ${name}-${namespace}
266
- servicename: ${name}
267
- version: "1.0.0"
268
- owner: OWNER
269
- server: opentelemetry-collector.stag.bolttechbroker.net #cluster opentelemetry server
270
- ```
271
-
272
- Then you should generate and apply your brand new configurations:
273
-
274
- 1. Generate
275
-
276
- This command will generate the new configuration files for the services.
277
-
278
- ```bash
279
- # Staging Clusters
280
- edi infra bolttech-broker-asia k8s generate <ENVIRONMENT> <SERVICE>
281
- edi infra bolttech-broker-asia k8s generate stage1-vnbroker rules-engine # VN Example
282
-
283
- # Production Clusters
284
- edi infra <PRODUCTION_BROKER> k8s generate <ENVIRONMENT> <SERVICE>
285
- edi infra bolttech-broker-asia-hk k8s generate live-hkbroker-a policy-issuing-service --prod # HK Example
286
- ```
287
-
288
- 2. Apply
289
-
290
- This command will apply the new configurations and __redeploy__ the service on the cluster.
291
-
292
- ```bash
293
- # Staging Clusters
294
- edi infra bolttech-broker-asia k8s apply staging <STAGE/RC> <SERVICE> --redeploy
295
- edi infra bolttech-broker-asia k8s apply staging rc-vnbroker frontend-service --redeploy # VN Example
296
-
297
- # Production Clusters
298
- edi infra <PRODUCTION_BROKER> k8s apply <CLUSTER> <ENVIRONMENT> <SERVICE>
299
- edi infra bolttech-broker-asia-hk k8s apply cluster-a live-hkbroker-a plan-service --prod --redeploy # HK Example
300
- ```
301
-
302
- - Key notes when using the infrastructure repositories:
303
- - Before running any command make sure you have completed the setup of the `edi-cli` tool and that you have all the related projects (`infrastructure` and `k8s-templates`) inside the same root folder.
304
- - Make sure to have the most recent version of the `master` branch before running any commands.
305
- - Remember to __ALWAYS COMMIT AND PUSH YOUR CHANGES TO THE REPOSITORIES, OTHERWISE THE NEW CONFIGURATIONS WILL NOT BE APPLIED TO SERVICES WHEN THEY ARE DEPLOYED USING THE JENKINS PIPELINES__.
306
- - To make things easier, make sure to check the helper script on `edi-cli` project. Located at the `edi-cli-cli` folder.
307
- - Consider hiding the folders of the brokers you will not need to work with. This will greatly improve your navigation inside the project.
308
-
309
- ## Debugging
310
-
311
- - How to debug it on vscode:
312
-
313
- As we don't debug using node command directly, we suggest you to use this in your `.vscode/launch.json`
314
-
315
- ```json
316
- {
317
- "version": "0.2.0",
318
- "configurations": [
319
- {
320
- "type": "node",
321
- "request": "launch",
322
- "name": "Debug with OTEL",
323
- "args": ["${workspaceFolder}/src/main.ts"],
324
- "runtimeArgs": [
325
- "--inspect",
326
- "-r",
327
- "./node_modules/@edirect/trace/dist/otel.js",
328
- "-r",
329
- "tsconfig-paths/register",
330
- "-r",
331
- "ts-node/register",
332
- ],
333
- "console": "integratedTerminal",
334
- "envFile": "${workspaceFolder}/.development.env"
335
- },
336
- ]
337
- }
338
- ```
339
-
340
- and append the OTEL environment variables to your local environments file:
341
-
342
- ```conf
343
- TRACE_TAG_OWNER=OWNER
344
- TRACE_TAG_SCOPE=pgw
345
- TRACE_TAG_SERVICENAME=payment-gateway
346
- TRACE_TAG_TENANT=local-paythbroker
347
- TRACE_SERVER_URL=opentelemetry-collector.stag.bolttechpay.net
348
- TRACE_TAG_SERVICE=payment-gateway-local-paythbroker
349
- TRACE_TAG_VERSION=1.0.0
350
- TRACE_TAG_CLUSTER=localhost
351
- TRACE_TAG_ENV=development
352
- ```
1
+ # Open Telemetry Trace
2
+
3
+ ## Background
4
+
5
+ Based on application logs and a sales checker service that triggers alerts on a slack channel, the current solution offers some insight into the application but falls short when it comes to deep understanding of exceptions and the general business flow.
6
+
7
+ Due to the sheer amount of logs and complex integrations between the application microservices, it is not uncommon to find scenarios where the development team can spend hours tracing the problem back to its origin.
8
+
9
+ - Non-standard logs
10
+ - The log format and content depends on implementation and log uniformity is a challenge by itself.
11
+ - Logs might be outdated or absent
12
+ - As services shift, evolve and so should logs. This can be a source for inconsistencies as logs may not be updated or can be wrongly removed during the feature implementation.
13
+ - Logs are difficult to query and trace
14
+ - Even with the help of tools such Elasticsearch, querying and tracing logs can be a difficult task due to complex interactions among different services in the application.
15
+ - As an example, an error response from quote-engine but we only dispose of the policy number to find the root cause. The data provided is not enough for a complete investigation, which may lead to long research.
16
+ - Not able to trace all exception occurrences
17
+ - In the case shown by the previous item the error was not logged or at least not easily accessible.
18
+ - Sales triggers are not flexible
19
+ - Triggers are configured as code in the sales checker service. Any change to the logic must be implemented, tested and deployed.
20
+ - Sales alert do not provide insight into root causes
21
+ - The alert contains only the basic information necessary for an investigation to start. It contains the lead, partner, vertical and a brief description of the problem.
22
+ - Limited business process visibility
23
+ - As the alerts are based on sale statuses, we have limited insight into the process. With such a limited view we cannot verify which steps represent bottlenecks or are more prone to errors. Useful information for maintaining and improving the system.
24
+ - No concise view of the business flow
25
+ - Currently there are no dashboards or easily accessible views that can provide correlated information about the many steps that compose the Bolttech business flow.
26
+
27
+ ## Detailed Design
28
+
29
+ To fill the gaps that the current approach, the proposed solution must fulfill some requirements:
30
+
31
+ - Requires minimal code and service changes
32
+ - Must be flexible and configurable
33
+ - Must capable of querying multiple data sources
34
+ - Must be an evolutive tool that can help multiple types of users
35
+ - Must have a dashboard that shows useful information about business flow
36
+
37
+ Taking the requirements in consideration, the proposed solutions employs an application monitoring tool named Open Telemetry, a microservice responsible for correlating the data captured by the tool and a comprehensive dashboard for the final user.
38
+
39
+ ![image](./docs/architecture.png)
40
+
41
+ ## Usage
42
+
43
+ - Tracing Apps
44
+ - Datadog
45
+ - Services and Observability
46
+ - [Service Visibility Frontend](https://bitbucket.org/gofrank/service-visibility-frontend/src/main/)
47
+ - [Service Visibility Backend](https://bitbucket.org/gofrank/service-visibility-backend/src/main/)
48
+
49
+ ## Sample application
50
+
51
+ - [Service Visibility Samples](https://bitbucket.org/gofrank/service-visibility-samples/src/master/)
52
+
53
+ ## NestJS Applications
54
+
55
+ NestJs applications __should be upgraded to version 9.__
56
+
57
+ - Just the fact of upgrade the nest-app to version 9 will able the service to ship all logs to the service visibility
58
+
59
+ As we applied the `@edirect/trace` to the nest-app, we dont need to have the trace part of code on nest-app applications with nest-app version 9
60
+
61
+ In case of impossibility to upgrade, use the "Other" instructions using `@edirect/trace` directly
62
+
63
+ Integration Service was not able to upgrade because it uses a react-ssr that does not have compatibility with nestjs 9, so, even as a nestjs application, we implemented the "Other" way
64
+
65
+ - [@edirect/nest-app:9](https://bitbucket.org/gofrank/nest-app/src/master/)
66
+
67
+ 1. Upgrade nest-app, all edirect dependencies and all nestjs dependencies
68
+
69
+ 2. We should initialize our application with `otel` as a requirement for node startup command:
70
+
71
+ ```bash
72
+ node --require ./node_modules/@edirect/trace/dist/otel.js dist/main
73
+ ```
74
+
75
+ (dist/main or your application entrypoint)
76
+
77
+ 3. Remove the old client initialization as we moved it to the node require
78
+
79
+ If the service was using old trace version
80
+
81
+ ```js
82
+ const config = require('config');
83
+ const { services: { APM }, middlewares: traceMiddlewares } = require('@edirect/trace');
84
+ const apmConfig = config.get('apm');
85
+
86
+ if (apmConfig.enabled) {
87
+ const apm = new APM(apmConfig.service, apmConfig.url, apmConfig.traceServer);
88
+ apm.startTrace();
89
+ }
90
+ ```
91
+
92
+ 4. Remove the middleware usage
93
+
94
+ If the service was using old trace version
95
+
96
+ Remove it completely as we moved it to inside of `@edirect/trace`:
97
+
98
+ ```js
99
+ if (apmConfig.enabled) {
100
+ app.use(new traceMiddlewares.Body().body);
101
+ }
102
+ ```
103
+
104
+ ## NextJS Applications
105
+ NextJS applications __should be upgraded to version 12 or above__.
106
+
107
+ 1. Install the trace package:
108
+ ```bash
109
+ npm install --save @edirect/trace
110
+ ```
111
+
112
+ 2. Create a middleware
113
+
114
+ * When you upgrade the NextJS application to the version 12(or above) you will be able
115
+ to use [Middlewares](https://nextjs.org/docs/advanced-features/middleware).
116
+ Create a file called `middleware.ts(or .js)` inside of the `src` folder.
117
+ ```ts
118
+ import { NextResponse } from 'next/server';
119
+ import type { NextRequest } from 'next/server';
120
+ import NextJsTracer from '@edirect/trace/dist/middlewares/nextjs';
121
+
122
+ // This function can be marked `async` if using `await` inside
123
+ export function middleware(request: NextRequest) {
124
+ // @edirect/trace
125
+ new NextJsTracer().body(req as unknown as Request & Record<string, unknown>);
126
+ NextResponse.next();
127
+ }
128
+ ```
129
+ 3. Install dotenv and create a dotenv.config
130
+ ```bash
131
+ npm install --save dotenv
132
+ ```
133
+ `dotenv.config.js`
134
+ ```js
135
+ const dotenv = require('dotenv');
136
+ const nodeEnv = process.env.NODE_ENV || 'development';
137
+
138
+ dotenv.config({ path: `.${nodeEnv}.env` });
139
+ ```
140
+ 4. Add the environment variables to your NextJS app:
141
+ __Example:__`.development.env`
142
+ ```yml
143
+ ## OPEN TELEMETRY
144
+ TRACE_TAG_OWNER=OWNER
145
+ TRACE_TAG_SCOPE=ie
146
+ TRACE_TAG_SERVICENAME=frontend-v2
147
+ TRACE_TAG_TENANT={STAGE}-{INSTANCE}
148
+ TRACE_SERVER_URL={URL}
149
+ TRACE_TAG_SERVICE=frontend-v2-{STAGE}-{INSTANCE}
150
+ TRACE_TAG_VERSION=1.0.0
151
+ TRACE_TAG_CLUSTER=stag-ie
152
+ TRACE_TAG_ENV=ie-stag-broker
153
+ ```
154
+
155
+ 5. Serve your production build on the standalone mode. Add this line to your `next.config.js` file
156
+ ```js
157
+ module.exports = {
158
+ // ...myOtherSettings
159
+ output: 'standalone'
160
+ }
161
+ ```
162
+
163
+ 6. Change the __start__ and __dev__ command:
164
+ ```json
165
+ {
166
+ "scripts": {
167
+ "dev": "cross-env NODE_ENV=development node --require ./dotenv.config.js --require ./node_modules/@edirect/trace/dist/otel.js ./node_modules/next/dist/bin/next -p 3100",
168
+ "start": "cross-env NODE_ENV=production node --require ./dotenv.config.js --require ./node_modules/@edirect/trace/dist/otel.js ./build/client/standalone/server.js -p 3100"
169
+ }
170
+ }
171
+ ```
172
+
173
+
174
+ ## Other (Not nest-app:9)
175
+
176
+ ### If you are unable to update to nest 9 or is not nest (express)
177
+
178
+ - [@edirect/trace](https://bitbucket.org/gofrank/trace/src/master/)
179
+
180
+ The usage of `@edirect/trace` will need your care to inject our middleware to log all requests from the app
181
+
182
+ 1. Install the trace package
183
+
184
+ ```bash
185
+ npm install --save @edirect/trace
186
+ ```
187
+
188
+ 2. Import the trace package
189
+
190
+ ```js
191
+ import { middlewares as traceMiddlewares } from '@edirect/trace';
192
+ ```
193
+
194
+ 3. Add the trace middleware to your application routes
195
+
196
+ Express:
197
+
198
+ ```js
199
+ app.use(new traceMiddlewares.Body().body);
200
+ ```
201
+
202
+ NestJs with nest-app:
203
+
204
+ ```js
205
+ consumer
206
+ .apply(new traceMiddlewares.Body().body)
207
+ .forRoutes({path: '/**', method: RequestMethod.ALL});
208
+ ```
209
+
210
+ 4. We should initialize our application with `otel` as a requirement for node startup command:
211
+
212
+ ```bash
213
+ node --require ./node_modules/@edirect/trace/dist/otel.js dist/main
214
+ ```
215
+
216
+ (dist/main or your application entrypoint)
217
+
218
+ 5. Remove the old client initialization as we moved it to the node require
219
+
220
+ If the service was using old trace version
221
+
222
+ ```js
223
+ const config = require('config');
224
+ const { services: { APM }, middlewares: traceMiddlewares } = require('@edirect/trace');
225
+ const apmConfig = config.get('apm');
226
+
227
+ if (apmConfig.enabled) {
228
+ const apm = new APM(apmConfig.service, apmConfig.url, apmConfig.traceServer);
229
+ apm.startTrace();
230
+ }
231
+ ```
232
+
233
+ 6. Clean the middleware usage
234
+
235
+ If the service was using old trace version
236
+
237
+ Replace:
238
+
239
+ ```js
240
+ if (apmConfig.enabled) {
241
+ app.use(new traceMiddlewares.Body().body);
242
+ }
243
+ ```
244
+
245
+ with:
246
+
247
+ ```js
248
+ app.use(new traceMiddlewares.Body().body);
249
+ ```
250
+
251
+ ## Applying k8s configurations (Required)
252
+
253
+ We created a standard way to deploy our application configurations and simplify its configurations:
254
+
255
+ - Sometimes it could be already done by another team and you don't need to manage the configuration, just generate the configurations and apply it
256
+ - `bolttech-broker-asia\staging\config-map.yaml` (or your environment config-map)
257
+
258
+ ```yaml
259
+ auth: #or your service
260
+ trace:
261
+ tags:
262
+ scope: ie #your tech center
263
+ env: ie-stag-broker #current k8s cluster environment
264
+ cluster: stag-ie #current k8s cluster
265
+ tenant: ${namespace}
266
+ service: ${name}-${namespace}
267
+ servicename: ${name}
268
+ version: "1.0.0"
269
+ owner: OWNER
270
+ server: opentelemetry-collector.stag.bolttechbroker.net #cluster opentelemetry server
271
+ ```
272
+
273
+ Then you should generate and apply your brand new configurations:
274
+
275
+ 1. Generate
276
+
277
+ This command will generate the new configuration files for the services.
278
+
279
+ ```bash
280
+ # Staging Clusters
281
+ edi infra bolttech-broker-asia k8s generate <ENVIRONMENT> <SERVICE>
282
+ edi infra bolttech-broker-asia k8s generate stage1-vnbroker rules-engine # VN Example
283
+
284
+ # Production Clusters
285
+ edi infra <PRODUCTION_BROKER> k8s generate <ENVIRONMENT> <SERVICE>
286
+ edi infra bolttech-broker-asia-hk k8s generate live-hkbroker-a policy-issuing-service --prod # HK Example
287
+ ```
288
+
289
+ 2. Apply
290
+
291
+ This command will apply the new configurations and __redeploy__ the service on the cluster.
292
+
293
+ ```bash
294
+ # Staging Clusters
295
+ edi infra bolttech-broker-asia k8s apply staging <STAGE/RC> <SERVICE> --redeploy
296
+ edi infra bolttech-broker-asia k8s apply staging rc-vnbroker frontend-service --redeploy # VN Example
297
+
298
+ # Production Clusters
299
+ edi infra <PRODUCTION_BROKER> k8s apply <CLUSTER> <ENVIRONMENT> <SERVICE>
300
+ edi infra bolttech-broker-asia-hk k8s apply cluster-a live-hkbroker-a plan-service --prod --redeploy # HK Example
301
+ ```
302
+
303
+ - Key notes when using the infrastructure repositories:
304
+ - Before running any command make sure you have completed the setup of the `edi-cli` tool and that you have all the related projects (`infrastructure` and `k8s-templates`) inside the same root folder.
305
+ - Make sure to have the most recent version of the `master` branch before running any commands.
306
+ - Remember to __ALWAYS COMMIT AND PUSH YOUR CHANGES TO THE REPOSITORIES, OTHERWISE THE NEW CONFIGURATIONS WILL NOT BE APPLIED TO SERVICES WHEN THEY ARE DEPLOYED USING THE JENKINS PIPELINES__.
307
+ - To make things easier, make sure to check the helper script on `edi-cli` project. Located at the `edi-cli-cli` folder.
308
+ - Consider hiding the folders of the brokers you will not need to work with. This will greatly improve your navigation inside the project.
309
+
310
+ ## Debugging
311
+
312
+ - How to debug it on vscode:
313
+
314
+ As we don't debug using node command directly, we suggest you to use this in your `.vscode/launch.json`
315
+
316
+ ```json
317
+ {
318
+ "version": "0.2.0",
319
+ "configurations": [
320
+ {
321
+ "type": "node",
322
+ "request": "launch",
323
+ "name": "Debug with OTEL",
324
+ "args": ["${workspaceFolder}/src/main.ts"],
325
+ "runtimeArgs": [
326
+ "--inspect",
327
+ "-r",
328
+ "./node_modules/@edirect/trace/dist/otel.js",
329
+ "-r",
330
+ "tsconfig-paths/register",
331
+ "-r",
332
+ "ts-node/register",
333
+ ],
334
+ "console": "integratedTerminal",
335
+ "envFile": "${workspaceFolder}/.development.env"
336
+ },
337
+ ]
338
+ }
339
+ ```
340
+
341
+ and append the OTEL environment variables to your local environments file:
342
+
343
+ ```conf
344
+ TRACE_TAG_OWNER=OWNER
345
+ TRACE_TAG_SCOPE=pgw
346
+ TRACE_TAG_SERVICENAME=payment-gateway
347
+ TRACE_TAG_TENANT=local-paythbroker
348
+ TRACE_SERVER_URL=opentelemetry-collector.stag.bolttechpay.net
349
+ TRACE_TAG_SERVICE=payment-gateway-local-paythbroker
350
+ TRACE_TAG_VERSION=1.0.0
351
+ TRACE_TAG_CLUSTER=localhost
352
+ TRACE_TAG_ENV=development
353
+ ```