diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index 588c8a62e97e..2a06ee28b535 100644 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -445,6 +445,7 @@ export abstract class Client { */ // @ts-expect-error - PromiseLike is a subset of Promise public async close(timeout?: number): PromiseLike { + _INTERNAL_flushLogsBuffer(this); const result = await this.flush(timeout); this.getOptions().enabled = false; this.emit('close'); diff --git a/packages/core/test/lib/client.test.ts b/packages/core/test/lib/client.test.ts index 014ffe8ebbe1..35438866c6c2 100644 --- a/packages/core/test/lib/client.test.ts +++ b/packages/core/test/lib/client.test.ts @@ -13,6 +13,7 @@ import { withMonitor, } from '../../src'; import * as integrationModule from '../../src/integration'; +import * as logsInternalModule from '../../src/logs/internal'; import { _INTERNAL_captureLog } from '../../src/logs/internal'; import { _INTERNAL_captureMetric } from '../../src/metrics/internal'; import * as traceModule from '../../src/tracing/trace'; @@ -2212,6 +2213,24 @@ describe('Client', () => { expect(getSentCount()).toBe(1); }); + test('close flushes the logs buffer', async () => { + vi.useRealTimers(); + + const flushLogsSpy = vi + .spyOn(logsInternalModule, '_INTERNAL_flushLogsBuffer') + .mockImplementation(() => undefined); + + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN }); + const client = new TestClient(options); + + await client.close(); + + expect(flushLogsSpy).toHaveBeenCalledTimes(1); + expect(flushLogsSpy).toHaveBeenCalledWith(client); + + flushLogsSpy.mockRestore(); + }); + test('multiple concurrent flush calls should just work', async () => { vi.useRealTimers(); expect.assertions(3);