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

This commit is contained in:
Karol Sójko 2024-01-05 12:21:24 +01:00
parent e253825da6
commit 1f4b26d269
No known key found for this signature in database
GPG key ID: C2F813669419D05F
3 changed files with 22 additions and 1 deletions

View file

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

View file

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

View file

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