Procházet zdrojové kódy

fix(syncing-server): add debug logs for checking traffic abuse

Karol Sójko před 1 rokem
rodič
revize
1f4b26d269

+ 1 - 0
packages/syncing-server/src/Bootstrap/Container.ts

@@ -967,6 +967,7 @@ export class ContainerConfigLoader {
         new CheckForTrafficAbuse(
           container.get<MetricsStoreInterface>(TYPES.Sync_MetricsStore),
           container.get<TimerInterface>(TYPES.Sync_Timer),
+          container.get<Logger>(TYPES.Sync_Logger),
         ),
       )
 

+ 6 - 1
packages/syncing-server/src/Domain/UseCase/Syncing/CheckForTrafficAbuse/CheckForTrafficAbuse.spec.ts

@@ -3,16 +3,21 @@ import { MetricsStoreInterface } from '../../../Metrics/MetricsStoreInterface'
 import { CheckForTrafficAbuse } from './CheckForTrafficAbuse'
 import { MetricsSummary } from '../../../Metrics/MetricsSummary'
 import { Metric } from '../../../Metrics/Metric'
+import { Logger } from 'winston'
 
 describe('CheckForTrafficAbuse', () => {
   let metricsStore: MetricsStoreInterface
   let timer: TimerInterface
   let timeframeLengthInMinutes: number
   let threshold: number
+  let logger: Logger
 
-  const createUseCase = () => new CheckForTrafficAbuse(metricsStore, timer)
+  const createUseCase = () => new CheckForTrafficAbuse(metricsStore, timer, logger)
 
   beforeEach(() => {
+    logger = {} as jest.Mocked<Logger>
+    logger.debug = jest.fn()
+
     const metricsSummary: MetricsSummary = {
       sum: 101,
       max: 0,

+ 15 - 0
packages/syncing-server/src/Domain/UseCase/Syncing/CheckForTrafficAbuse/CheckForTrafficAbuse.ts

@@ -5,14 +5,21 @@ import { CheckForTrafficAbuseDTO } from './CheckForTrafficAbuseDTO'
 import { MetricsStoreInterface } from '../../../Metrics/MetricsStoreInterface'
 import { Metric } from '../../../Metrics/Metric'
 import { MetricsSummary } from '../../../Metrics/MetricsSummary'
+import { Logger } from 'winston'
 
 export class CheckForTrafficAbuse implements UseCaseInterface<MetricsSummary> {
   constructor(
     private metricsStore: MetricsStoreInterface,
     private timer: TimerInterface,
+    private logger: Logger,
   ) {}
 
   async execute(dto: CheckForTrafficAbuseDTO): Promise<Result<MetricsSummary>> {
+    this.logger.debug(`Checking for traffic abuse for metric: ${dto.metricToCheck}.`, {
+      codeTag: 'CheckForTrafficAbuse',
+      userUuid: dto.userUuid,
+    })
+
     const userUuidOrError = Uuid.create(dto.userUuid)
     if (userUuidOrError.isFailed()) {
       return Result.fail(userUuidOrError.getError())
@@ -35,6 +42,14 @@ export class CheckForTrafficAbuse implements UseCaseInterface<MetricsSummary> {
       to: this.timer.getUTCDate(),
     })
 
+    this.logger.debug(
+      `Current traffic abuse metric ${dto.metricToCheck} value in timeframe of ${dto.timeframeLengthInMinutes} minutes is ${metricsSummary.sum}. The threshold is ${dto.threshold}`,
+      {
+        codeTag: 'CheckForTrafficAbuse',
+        userUuid: dto.userUuid,
+      },
+    )
+
     if (metricsSummary.sum > dto.threshold) {
       return Result.fail(
         `Traffic abuse detected for metric: ${metricToCheck.props.name}. Usage ${metricsSummary.sum} is greater than threshold ${dto.threshold}`,