Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Other Changes

- fix(core): Emit `flush` event in `Client.flush()` when client has no transport

## 10.48.0

### Important Changes
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,16 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
// @ts-expect-error - PromiseLike is a subset of Promise
public async flush(timeout?: number): PromiseLike<boolean> {
const transport = this._transport;

// Emit `flush` unconditionally so weight-based log/metric flushers drain
// their buffers and clear their idle timers, even when no transport is
// configured (e.g. no DSN).
this.emit('flush');

if (!transport) {
return true;
}

this.emit('flush');

const clientFinished = await this._isClientDoneProcessing(timeout);
const transportFlushed = await transport.flush(timeout);

Expand Down
44 changes: 44 additions & 0 deletions packages/core/test/lib/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3115,6 +3115,29 @@ describe('Client', () => {

safeUnrefSpy.mockRestore();
});

it('flush() drains the log buffer when client has no transport', async () => {
// Client without DSN — _transport is undefined
const options = getDefaultTestClientOptions({
enableLogs: true,
});
const client = new TestClient(options);
const scope = new Scope();
scope.setClient(client);

const flushLogsHandler = vi.fn();
client.on('flushLogs', flushLogsHandler);

// Capture a log which starts the weight-based flush timer
_INTERNAL_captureLog({ message: 'test log', level: 'info' }, scope);

expect(flushLogsHandler).not.toHaveBeenCalled();

// flush() should drain the buffer (and clear the timer) even without a transport
await client.flush();

expect(flushLogsHandler).toHaveBeenCalledTimes(1);
});
});

describe('metric weight-based flushing', () => {
Expand Down Expand Up @@ -3201,6 +3224,27 @@ describe('Client', () => {

safeUnrefSpy.mockRestore();
});

it('flush() drains the metric buffer when client has no transport', async () => {
// Client without DSN — _transport is undefined
const options = getDefaultTestClientOptions({});
const client = new TestClient(options);
const scope = new Scope();
scope.setClient(client);

const flushMetricsHandler = vi.fn();
client.on('flushMetrics', flushMetricsHandler);

// Capture a metric which starts the weight-based flush timer
_INTERNAL_captureMetric({ name: 'test_metric', value: 42, type: 'counter', attributes: {} }, { scope });

expect(flushMetricsHandler).not.toHaveBeenCalled();

// flush() should drain the buffer (and clear the timer) even without a transport
await client.flush();

expect(flushMetricsHandler).toHaveBeenCalledTimes(1);
});
});

describe('promise buffer usage', () => {
Expand Down
Loading