Skip to content
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
11 changes: 11 additions & 0 deletions packages/node/src/sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ServerRuntimeClientOptions } from '@sentry/core/server';
import {
_INTERNAL_clearAiProviderSkips,
_INTERNAL_flushLogsBuffer,
_INTERNAL_flushMetricsBuffer,
_INTERNAL_setDeferSegmentSpanCapture,
applySdkMetadata,
debug,
Expand Down Expand Up @@ -38,6 +39,7 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
private _clientReportInterval: NodeJS.Timeout | undefined;
private _clientReportOnExitFlushListener: (() => void) | undefined;
private _logOnExitFlushListener: (() => void) | undefined;
private _metricsOnExitFlushListener: (() => void) | undefined;

public constructor(options: NodeClientOptions) {
const serverName =
Expand All @@ -63,6 +65,10 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
_INTERNAL_flushLogsBuffer(this);
};

this._metricsOnExitFlushListener = () => {
_INTERNAL_flushMetricsBuffer(this);
};

if (serverName) {
this.on('beforeCaptureLog', log => {
log.attributes = {
Expand All @@ -73,6 +79,7 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
}

process.on('beforeExit', this._logOnExitFlushListener);
process.on('beforeExit', this._metricsOnExitFlushListener);

// Enable deferred segment-span transaction capture here, in the constructor, rather than in
// `initOtel`. Every client runs its constructor exactly once, whereas `initOtel` only runs on
Expand Down Expand Up @@ -158,6 +165,10 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
process.off('beforeExit', this._logOnExitFlushListener);
}

if (this._metricsOnExitFlushListener) {
process.off('beforeExit', this._metricsOnExitFlushListener);
}

const allEventsSent = await super.close(timeout);
if (this.traceProvider) {
await this.traceProvider.shutdown();
Expand Down
38 changes: 21 additions & 17 deletions packages/node/test/sdk/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,35 +397,39 @@ describe('NodeClient', () => {
expect(forceFlushSpy).toHaveBeenCalledTimes(1);
});

it('stops client report tracking if it was started', async () => {
const processOffSpy = vi.spyOn(process, 'off');
const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval');

it('stops client report tracking when closed', async () => {
const originalListeners = process.listeners('beforeExit');
const client = new NodeClient(getDefaultNodeClientOptions({ sendClientReports: true }));
const listenersBeforeTracking = process.listeners('beforeExit');
const setIntervalSpy = vi.spyOn(globalThis, 'setInterval');
const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval');

client.startClientReportTracking();

const result = await client.close();
const addedListeners = process
.listeners('beforeExit')
.filter(listener => !listenersBeforeTracking.includes(listener));
const [intervalResult] = setIntervalSpy.mock.results;

expect(result).toBe(true);
await client.close();

expect(addedListeners).toHaveLength(1);
expect(process.listeners('beforeExit')).toEqual(originalListeners);
expect(setIntervalSpy).toHaveBeenCalledTimes(1);
expect(clearIntervalSpy).toHaveBeenCalledTimes(1);

// removes `_clientReportOnExitFlushListener`
expect(processOffSpy).toHaveBeenNthCalledWith(1, 'beforeExit', expect.any(Function));
expect(clearIntervalSpy).toHaveBeenCalledWith(intervalResult?.value);
});

it('stops log capture if it was started', async () => {
const processOffSpy = vi.spyOn(process, 'off');
it('removes log and metric exit listeners when closed', async () => {
const originalListeners = process.listeners('beforeExit');
const client = new NodeClient(getDefaultNodeClientOptions({ sendClientReports: false }));

const client = new NodeClient(getDefaultNodeClientOptions());
const addedListeners = process.listeners('beforeExit').filter(listener => !originalListeners.includes(listener));

const result = await client.close();

expect(result).toBe(true);
await client.close();

// removes `_logOnExitFlushListener`
expect(processOffSpy).toHaveBeenNthCalledWith(1, 'beforeExit', expect.any(Function));
expect(addedListeners).toHaveLength(2);
expect(process.listeners('beforeExit')).toEqual(originalListeners);
Comment thread
nicohrubec marked this conversation as resolved.
});
});

Expand Down
Loading