Skip to content

Commit

Permalink
fix(api): mid and after request logging (#6433)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChmaraX authored Sep 4, 2024
1 parent 163465b commit 4652d07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions apps/api/src/app/rate-limiting/guards/throttler.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ export class ApiRateLimitInterceptor extends ThrottlerGuard implements NestInter
)
);

res.rateLimitPolicy = {
limit,
windowDuration,
burstLimit,
algorithm,
apiRateLimitCategory,
apiRateLimitCost,
apiServiceLevel,
};

if (isDryRun) {
if (!success) {
Logger.warn(`[Dry run] ${THROTTLED_EXCEPTION_MESSAGE}`, 'ApiRateLimitInterceptor');
Expand Down
19 changes: 16 additions & 3 deletions libs/application-generic/src/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export function getLogLevel() {
if (loggingLevelArr.indexOf(logLevel) === -1) {
// eslint-disable-next-line no-console
console.log(
`${logLevel}is not a valid log level of ${
loggingLevelArr
}. Reverting to info.`,
`${logLevel}is not a valid log level of ${loggingLevelArr}. Reverting to info.`,
);

logLevel = 'info';
Expand Down Expand Up @@ -128,6 +126,21 @@ export function createNestLoggingModuleOptions(
},
transport,
autoLogging: !['test', 'local'].includes(process.env.NODE_ENV),
/**
* These custom props are only added to 'request completed' and 'request errored' logs.
* Logs generated during request processing won't have these props by default.
* To include these or any other custom props in mid-request logs,
* use `PinoLogger.assign(<props>)` explicitly before logging.
*/
customProps: (req: any, res: any) => ({
user: {
userId: req?.user?._id || null,
environmentId: req?.user?.environmentId || null,
organizationId: req?.user?.organizationId || null,
},
authScheme: req?.authScheme,
rateLimitPolicy: res?.rateLimitPolicy,
}),
},
};
}
Expand Down

0 comments on commit 4652d07

Please sign in to comment.