@evanion/nestjs-correlation-id 1.0.0 → 1.0.3
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/LICENSE +1 -1
- package/README.md +67 -0
- package/package.json +4 -3
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -77,6 +77,73 @@ export class AppModule implements NestModule {
|
|
|
77
77
|
}
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
+
#### Add `correlationId` to logs
|
|
81
|
+
|
|
82
|
+
In order to add the correlation ID to your logs, you can use the `CorrelationService` service to get the current correlationId.
|
|
83
|
+
|
|
84
|
+
In the following example, we are using the [@ntegral/nestjs-sentry](https://github.com/ntegral/nestjs-sentry) package, but you can use any package or provider you like.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { Injectable, NestMiddleware } from '@nestjs/common';
|
|
88
|
+
import { Request, Response } from 'express';
|
|
89
|
+
import { CorrelationService } from '@evanion/nestjs-correlation-id';
|
|
90
|
+
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry';
|
|
91
|
+
|
|
92
|
+
@Injectable()
|
|
93
|
+
export class SentryMiddleware implements NestMiddleware {
|
|
94
|
+
constructor(
|
|
95
|
+
private readonly correlationService: CorrelationService,
|
|
96
|
+
@InjectSentry() private readonly sentryService: SentryService,
|
|
97
|
+
) {}
|
|
98
|
+
|
|
99
|
+
async use(req: Request, res: Response, next) {
|
|
100
|
+
const correlationId = await this.correlationService.getCorrelationId();
|
|
101
|
+
this.sentryService.configureScope((scope) => {
|
|
102
|
+
scope.setTag('correlationId', correlationId);
|
|
103
|
+
});
|
|
104
|
+
next();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Then add it to your `AppModule`
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { Module } from '@nestjs-common';
|
|
113
|
+
import {
|
|
114
|
+
SentryModule,
|
|
115
|
+
ConfigModule,
|
|
116
|
+
ConfigService,
|
|
117
|
+
} from '@ntegral/nestjs-sentry';
|
|
118
|
+
import {
|
|
119
|
+
CorrelationModule,
|
|
120
|
+
CorrelationService,
|
|
121
|
+
} from '@evanion/nestjs-correlation-id';
|
|
122
|
+
|
|
123
|
+
@Module({
|
|
124
|
+
imports: [
|
|
125
|
+
CorrelationModule.forRoot(),
|
|
126
|
+
SentryModule.forRoot({
|
|
127
|
+
// ... your config
|
|
128
|
+
}),
|
|
129
|
+
],
|
|
130
|
+
})
|
|
131
|
+
export class AppModule implements NestModule {
|
|
132
|
+
configure(consumer: MiddlewareConsumer) {
|
|
133
|
+
consumer.apply(CorrelationIdMiddleware).forRoutes('*');
|
|
134
|
+
consumer.apply(SentryMiddleware).forRoutes('*');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
{
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
If you need to manually set the correlationId anywhere in your application. You can use the `CorrelationService` service to set the correlationId.
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
this.correlationService.setCorrelationId('some_correlation_id');
|
|
145
|
+
```
|
|
146
|
+
|
|
80
147
|
see [e2e tests](/test) for a fully working example
|
|
81
148
|
|
|
82
149
|
## Change Log
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evanion/nestjs-correlation-id",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Transparently forward or add correlation id to all requests",
|
|
5
5
|
"author": "Mikael Pettersson <evanion@icloud.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"test": "jest",
|
|
21
21
|
"test:watch": "jest --watch",
|
|
22
22
|
"test:cov": "jest --coverage",
|
|
23
|
-
"test:e2e": "jest --config ./test/jest-e2e.json"
|
|
23
|
+
"test:e2e": "jest --config ./test/jest-e2e.json",
|
|
24
|
+
"release": "release-it"
|
|
24
25
|
},
|
|
25
26
|
"keywords": [
|
|
26
27
|
"nestjs",
|
|
@@ -43,7 +44,6 @@
|
|
|
43
44
|
"@nestjs/axios": "^0.1.0",
|
|
44
45
|
"@nestjs/common": "^6.0.0"
|
|
45
46
|
},
|
|
46
|
-
"dependencies": {},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@nestjs/axios": "^0.1.0",
|
|
49
49
|
"@nestjs/common": "^9.0.1",
|
|
@@ -62,6 +62,7 @@
|
|
|
62
62
|
"jest": "28.1.2",
|
|
63
63
|
"prettier": "^2.7.1",
|
|
64
64
|
"reflect-metadata": "^0.1.13",
|
|
65
|
+
"release-it": "^15.1.1",
|
|
65
66
|
"rxjs": "^7.5.5",
|
|
66
67
|
"supertest": "6.2.4",
|
|
67
68
|
"ts-jest": "28.0.5",
|