Skip to content

fix(core): Make sure logs get flushed in server-runtime-client #16222

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

Merged
merged 1 commit into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class ServerRuntimeClient<
if (this._options._experiments?.enableLogs) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const client = this;

client.on('flushLogs', () => {
client._logWeight = 0;
clearTimeout(client._logFlushIdleTimeout);
Expand All @@ -72,6 +73,10 @@ export class ServerRuntimeClient<
}, DEFAULT_LOG_FLUSH_INTERVAL);
}
});

client.on('flush', () => {
_INTERNAL_flushLogsBuffer(client);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also cancel _logFlushIdleTimeout here?

Copy link
Member Author

@AbhiPrasad AbhiPrasad May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be done by the client.on('flushLogs' callback, which should be called as soon as the logs actually get flushed (from the _INTERNAL_flushLogsBuffer call).

The test validates this by showing the client['_logWeight'] is 0, which means we executed that code block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for the explanation.

});
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/core/test/lib/server-runtime-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,25 @@ describe('ServerRuntimeClient', () => {
expect(sendEnvelopeSpy).not.toHaveBeenCalled();
expect(client['_logWeight']).toBe(0);
});

it('flushes logs when flush event is triggered', () => {
const options = getDefaultClientOptions({
dsn: PUBLIC_DSN,
_experiments: { enableLogs: true },
});
client = new ServerRuntimeClient(options);

const sendEnvelopeSpy = vi.spyOn(client, 'sendEnvelope');

// Add some logs
_INTERNAL_captureLog({ message: 'test1', level: 'info' }, client);
_INTERNAL_captureLog({ message: 'test2', level: 'info' }, client);

// Trigger flush event
client.emit('flush');

expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
expect(client['_logWeight']).toBe(0); // Weight should be reset after flush
});
});
});
Loading