From e2c54b1fc4105c21352c486e79f52d753b996b68 Mon Sep 17 00:00:00 2001 From: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:53:51 +0300 Subject: [PATCH] chore: make rate limit logs verbose api-v2 (#17262) --- apps/api/v2/src/lib/throttler-guard.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/api/v2/src/lib/throttler-guard.ts b/apps/api/v2/src/lib/throttler-guard.ts index c7e4a3c5fb..2f773f0dce 100644 --- a/apps/api/v2/src/lib/throttler-guard.ts +++ b/apps/api/v2/src/lib/throttler-guard.ts @@ -56,7 +56,7 @@ export class CustomThrottlerGuard extends ThrottlerGuard { const request = context.switchToHttp().getRequest(); const response = context.switchToHttp().getResponse(); const tracker = await this.getTracker(request); - this.logger.log( + this.logger.verbose( `Tracker "${tracker}" generated based on: Bearer token "${request.get( "Authorization" )}", OAuth client ID "${request.get(X_CAL_CLIENT_ID)}" and IP "${request.ip}"` @@ -89,7 +89,7 @@ export class CustomThrottlerGuard extends ThrottlerGuard { private async handleNonApiKeyRequest(tracker: string, response: Response): Promise { const rateLimit = this.getDefaultRateLimit(tracker); - this.logger.log(`Tracker "${tracker}" uses default rate limits because it is not tracking api key: + this.logger.verbose(`Tracker "${tracker}" uses default rate limits because it is not tracking api key: ${JSON.stringify(rateLimit, null, 2)} `); @@ -135,7 +135,7 @@ export class CustomThrottlerGuard extends ThrottlerGuard { const cachedRateLimits = await this.storageService.redis.get(cacheKey); if (cachedRateLimits) { - this.logger.log(`Tracker "${tracker}" rate limits retrieved from redis cache: + this.logger.verbose(`Tracker "${tracker}" rate limits retrieved from redis cache: ${cachedRateLimits} `); return rateLimitsSchema.parse(JSON.parse(cachedRateLimits)); @@ -158,13 +158,13 @@ export class CustomThrottlerGuard extends ThrottlerGuard { }); if (rateLimits) { - this.logger.log(`Tracker "${tracker}" rate limits retrieved from database: + this.logger.verbose(`Tracker "${tracker}" rate limits retrieved from database: ${JSON.stringify(rateLimits, null, 2)}`); } if (!rateLimits || rateLimits.length === 0) { rateLimits = [this.getDefaultRateLimit(tracker)]; - this.logger.log(`Tracker "${tracker}" rate limits not found in database. Using default rate limits: + this.logger.verbose(`Tracker "${tracker}" rate limits not found in database. Using default rate limits: ${JSON.stringify(rateLimits, null, 2)}`); } @@ -194,10 +194,10 @@ export class CustomThrottlerGuard extends ThrottlerGuard { ); response.setHeader(`X-RateLimit-Reset-${nameFirstUpper}`, timeToBlockExpire || timeToExpire); - this.logger.log( + this.logger.verbose( `Tracker "${tracker}" rate limit "${name}" incremented. isBlocked ${isBlocked}, totalHits ${totalHits}, timeToExpire ${timeToExpire}, timeToBlockExpire ${timeToBlockExpire}` ); - this.logger.log( + this.logger.verbose( `Tracker "${tracker}" rate limit "${name}" response headers: X-RateLimit-Limit-${nameFirstUpper}: ${limit}, X-RateLimit-Remaining-${nameFirstUpper}: ${timeToBlockExpire ? 0 : Math.max(0, limit - totalHits)}, @@ -227,7 +227,7 @@ export class CustomThrottlerGuard extends ThrottlerGuard { return `ip_${request.ip}`; } - this.logger.log(`no tracker found: ${request.url}`); + this.logger.verbose(`no tracker found: ${request.url}`); return "unknown"; } }