Skip to content

Commit

Permalink
feat(server): enable cls plugin to store request id
Browse files Browse the repository at this point in the history
POC
  • Loading branch information
fengmk2 committed Jan 17, 2025
1 parent aa21ac6 commit a990e20
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/backend/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"nanoid": "^5.0.9",
"nest-commander": "^3.15.0",
"nest-winston": "^1.9.7",
"nestjs-cls": "^4.5.0",
"nestjs-throttler-storage-redis": "^0.5.1",
"nodemailer": "^6.9.16",
"on-headers": "^1.0.2",
Expand Down
14 changes: 14 additions & 0 deletions packages/backend/server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { randomUUID } from 'node:crypto';

import {
DynamicModule,
ForwardReference,
Logger,
Module,
} from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import type { Request } from 'express';
import { get } from 'lodash-es';
import { ClsModule } from 'nestjs-cls';

import { AppController } from './app.controller';
import { getOptionalModuleMetadata } from './base';
Expand Down Expand Up @@ -42,6 +46,16 @@ import { REGISTERED_PLUGINS } from './plugins';
import { ENABLED_PLUGINS } from './plugins/registry';

export const FunctionalityModules = [
ClsModule.forRoot({
global: true,
middleware: {
mount: true,
generateId: true,
idGenerator(req: Request) {
return (req.headers['x-request-id'] as string) ?? randomUUID();
},
},
}),
ConfigModule.forRoot(),
RuntimeModule,
EventModule,
Expand Down
23 changes: 21 additions & 2 deletions packages/backend/server/src/base/logger/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { ConsoleLogger, Injectable, Scope } from '@nestjs/common';
import {
ConsoleLogger,
Injectable,
type LogLevel,
Scope,
} from '@nestjs/common';
import { ClsServiceManager } from 'nestjs-cls';

@Injectable({
scope: Scope.TRANSIENT,
})
export class AFFiNELogger extends ConsoleLogger {}
export class AFFiNELogger extends ConsoleLogger {
override stringifyMessage(message: unknown, logLevel: LogLevel) {
const messageString = super.stringifyMessage(message, logLevel);
return AFFiNELogger.formatMessageWithRequestId(messageString);
}

static formatMessageWithRequestId(message: string) {
const requestId = ClsServiceManager.getClsService()?.getId();
if (!requestId) {
return message;
}
return `<${requestId}> ${message}`;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LoggingWinston } from '@google-cloud/logging-winston';
import { LoggerService, Provider, Scope } from '@nestjs/common';
import { createLogger, transports } from 'winston';
import { createLogger, format, transports } from 'winston';

import { AFFiNELogger as LoggerProvide } from '../../../base/logger';
import { AFFiNELogger } from './logger';
Expand All @@ -17,6 +17,10 @@ export const loggerProvider: Provider<LoggerService> = {
// Add Cloud Logging
loggingWinston,
],
format: format.printf(info => {
// TODO(fengmk2): should add a unit test to make sure this works
return LoggerProvide.formatMessageWithRequestId(info.message as string);
}),
});
return new AFFiNELogger(instance);
},
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ __metadata:
nanoid: "npm:^5.0.9"
nest-commander: "npm:^3.15.0"
nest-winston: "npm:^1.9.7"
nestjs-cls: "npm:^4.5.0"
nestjs-throttler-storage-redis: "npm:^0.5.1"
nodemailer: "npm:^6.9.16"
nodemon: "npm:^3.1.7"
Expand Down Expand Up @@ -27269,6 +27270,18 @@ __metadata:
languageName: node
linkType: hard

"nestjs-cls@npm:^4.5.0":
version: 4.5.0
resolution: "nestjs-cls@npm:4.5.0"
peerDependencies:
"@nestjs/common": "> 7.0.0 < 11"
"@nestjs/core": "> 7.0.0 < 11"
reflect-metadata: "*"
rxjs: ">= 7"
checksum: 10/cbd807244aaf0e89480e5fa7d08415e27953d4f6264246318cd3aa5ec4817564988d5cbed4f700c79090f0ded6f72752887bc842543f7f0af49c515df41e937e
languageName: node
linkType: hard

"nestjs-throttler-storage-redis@npm:^0.5.1":
version: 0.5.1
resolution: "nestjs-throttler-storage-redis@npm:0.5.1"
Expand Down

0 comments on commit a990e20

Please sign in to comment.