Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [RUMF-1109] Add event rate limiters for loggers #1243

Merged
merged 5 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 62 additions & 56 deletions packages/logs/src/boot/startLogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,71 +339,77 @@ describe('logs', () => {
])
})
})
;[
{ status: StatusType.error, configuration: { maxErrorsPerMinute: 1 } },
{ status: StatusType.warn, configuration: { maxWarningsPerMinute: 1 } },
{ status: StatusType.info, configuration: { maxInfosPerMinute: 1 } },
{ status: StatusType.debug, configuration: { maxDebugsPerMinute: 1 } },
].forEach(({ status, configuration }) => {
describe(`${status} logs limitation`, () => {
let clock: Clock

beforeEach(() => {
clock = mockClock()
})

describe('error logs limitation', () => {
let clock: Clock

beforeEach(() => {
clock = mockClock()
})

afterEach(() => {
clock.cleanup()
})
afterEach(() => {
clock.cleanup()
})

it('stops sending error logs when reaching the limit', () => {
const sendLogSpy = jasmine.createSpy<(message: LogsMessage & { foo?: string }) => void>()
const sendLog = startLogs({ errorLogger: new Logger(sendLogSpy), configuration: { maxErrorsPerMinute: 1 } })
sendLog({ message: 'foo', status: StatusType.error }, {})
sendLog({ message: 'bar', status: StatusType.error }, {})
it(`stops sending ${status} logs when reaching the limit`, () => {
const sendLogSpy = jasmine.createSpy<(message: LogsMessage & { foo?: string }) => void>()
const sendLog = startLogs({ errorLogger: new Logger(sendLogSpy), configuration })
sendLog({ message: 'foo', status }, {})
sendLog({ message: 'bar', status }, {})

expect(server.requests.length).toEqual(1)
expect(getLoggedMessage(server, 0).message).toBe('foo')
expect(sendLogSpy).toHaveBeenCalledOnceWith({
message: 'Reached max number of errors by minute: 1',
status: StatusType.error,
error: {
origin: ErrorSource.AGENT,
kind: undefined,
stack: undefined,
},
date: Date.now(),
expect(server.requests.length).toEqual(1)
expect(getLoggedMessage(server, 0).message).toBe('foo')
expect(sendLogSpy).toHaveBeenCalledOnceWith({
message: `Reached max number of ${status}s by minute: 1`,
status: StatusType.error,
error: {
origin: ErrorSource.AGENT,
kind: undefined,
stack: undefined,
},
date: Date.now(),
})
})
})

it('does not take discarded errors into account', () => {
const sendLogSpy = jasmine.createSpy<(message: LogsMessage & { foo?: string }) => void>()
const sendLog = startLogs({
errorLogger: new Logger(sendLogSpy),
configuration: {
maxErrorsPerMinute: 1,
beforeSend(event) {
if (event.message === 'discard me') {
return false
}
it(`does not take discarded ${status}s into account`, () => {
const sendLogSpy = jasmine.createSpy<(message: LogsMessage & { foo?: string }) => void>()
const sendLog = startLogs({
errorLogger: new Logger(sendLogSpy),
configuration: {
...configuration,
beforeSend(event) {
if (event.message === 'discard me') {
return false
}
},
},
},
})
sendLog({ message: 'discard me', status }, {})
sendLog({ message: 'discard me', status }, {})
sendLog({ message: 'discard me', status }, {})
sendLog({ message: 'foo', status }, {})

expect(server.requests.length).toEqual(1)
expect(getLoggedMessage(server, 0).message).toBe('foo')
expect(sendLogSpy).not.toHaveBeenCalled()
})
sendLog({ message: 'discard me', status: StatusType.error }, {})
sendLog({ message: 'discard me', status: StatusType.error }, {})
sendLog({ message: 'discard me', status: StatusType.error }, {})
sendLog({ message: 'foo', status: StatusType.error }, {})

expect(server.requests.length).toEqual(1)
expect(getLoggedMessage(server, 0).message).toBe('foo')
expect(sendLogSpy).not.toHaveBeenCalled()
})

it('allows to send new errors after a minute', () => {
const sendLog = startLogs({ configuration: { maxErrorsPerMinute: 1 } })
sendLog({ message: 'foo', status: StatusType.error }, {})
sendLog({ message: 'bar', status: StatusType.error }, {})
clock.tick(ONE_MINUTE)
sendLog({ message: 'baz', status: StatusType.error }, {})
it(`allows to send new ${status}s after a minute`, () => {
const sendLog = startLogs({ configuration })
sendLog({ message: 'foo', status }, {})
sendLog({ message: 'bar', status }, {})
clock.tick(ONE_MINUTE)
sendLog({ message: 'baz', status }, {})

expect(server.requests.length).toEqual(2)
expect(getLoggedMessage(server, 0).message).toBe('foo')
expect(getLoggedMessage(server, 1).message).toBe('baz')
expect(server.requests.length).toEqual(2)
expect(getLoggedMessage(server, 0).message).toBe('foo')
expect(getLoggedMessage(server, 1).message).toBe('baz')
})
})
})
})
20 changes: 15 additions & 5 deletions packages/logs/src/boot/startLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,35 @@ export function buildAssemble(
configuration: LogsConfiguration,
reportError: (error: RawError) => void
) {
const errorRateLimiter = createEventRateLimiter(StatusType.error, configuration.maxErrorsPerMinute, reportError)
const logRateLimiters = {
[StatusType.error]: createEventRateLimiter(StatusType.error, configuration.maxErrorsPerMinute, reportError),
[StatusType.warn]: createEventRateLimiter(StatusType.warn, configuration.maxWarningsPerMinute, reportError),
[StatusType.info]: createEventRateLimiter(StatusType.info, configuration.maxInfosPerMinute, reportError),
[StatusType.debug]: createEventRateLimiter(StatusType.debug, configuration.maxDebugsPerMinute, reportError),
}
amortemousque marked this conversation as resolved.
Show resolved Hide resolved

return (message: LogsMessage, currentContext: Context) => {
const startTime = message.date ? getRelativeTime(message.date) : undefined
const session = sessionManager.findTrackedSession(startTime)

if (!session) {
return undefined
}

const contextualizedMessage = combine(
{ service: configuration.service, session_id: session.id },
currentContext,
getRUMInternalContext(startTime),
message
)
if (configuration.beforeSend && configuration.beforeSend(contextualizedMessage) === false) {
return undefined
}
if (contextualizedMessage.status === StatusType.error && errorRateLimiter.isLimitReached()) {

if (
configuration.beforeSend?.(contextualizedMessage) === false ||
logRateLimiters[contextualizedMessage.status]?.isLimitReached()
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
) {
return undefined
}

return contextualizedMessage as Context
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/logs/src/domain/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export type HybridInitConfiguration = Omit<LogsInitConfiguration, 'clientToken'>

export interface LogsConfiguration extends Configuration {
forwardErrorsToLogs: boolean

// Event limits
maxWarningsPerMinute: number
maxInfosPerMinute: number
maxDebugsPerMinute: number
}

export function validateAndBuildLogsConfiguration(
Expand All @@ -25,5 +30,8 @@ export function validateAndBuildLogsConfiguration(
...baseConfiguration,

forwardErrorsToLogs: !!initConfiguration.forwardErrorsToLogs,
maxWarningsPerMinute: 3000,
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
maxInfosPerMinute: 3000,
maxDebugsPerMinute: 3000,
}
}