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
18 changes: 17 additions & 1 deletion packages/deno/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ServerRuntimeClientOptions } from '@sentry/core/server';
import { _INTERNAL_flushLogsBuffer, SDK_VERSION } from '@sentry/core';
import { _INTERNAL_flushLogsBuffer, _INTERNAL_flushMetricsBuffer, SDK_VERSION } from '@sentry/core';
import { ServerRuntimeClient } from '@sentry/core/server';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils';
import process from 'node:process';
import type { DenoClientOptions } from './types';

function getHostName(): string | undefined {
Expand All @@ -22,6 +23,7 @@ function getHostName(): string | undefined {
*/
export class DenoClient extends ServerRuntimeClient<DenoClientOptions> {
private _logOnExitFlushListener: (() => void) | undefined;
private _metricsOnExitFlushListener: (() => void) | undefined;

/**
* Creates a new Deno SDK instance.
Expand Down Expand Up @@ -55,6 +57,10 @@ export class DenoClient extends ServerRuntimeClient<DenoClientOptions> {
_INTERNAL_flushLogsBuffer(this);
};

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

if (serverName) {
this.on('beforeCaptureLog', log => {
log.attributes = {
Expand All @@ -64,7 +70,11 @@ export class DenoClient extends ServerRuntimeClient<DenoClientOptions> {
});
}

// Unlike unload, beforeExit lets the transport finish asynchronous sends.
process.on('beforeExit', this._logOnExitFlushListener);
process.on('beforeExit', this._metricsOnExitFlushListener);
globalThis.addEventListener('unload', this._logOnExitFlushListener);
globalThis.addEventListener('unload', this._metricsOnExitFlushListener);
}

/** @inheritDoc */
Expand All @@ -81,9 +91,15 @@ export class DenoClient extends ServerRuntimeClient<DenoClientOptions> {
// @ts-expect-error - PromiseLike is a subset of Promise
public async close(timeout?: number | undefined): PromiseLike<boolean> {
if (this._logOnExitFlushListener) {
process.off('beforeExit', this._logOnExitFlushListener);
globalThis.removeEventListener('unload', this._logOnExitFlushListener);
}

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

return super.close(timeout);
}
}
17 changes: 13 additions & 4 deletions packages/deno/test/mod.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'node:process';
import type { Envelope, Event, Log } from '@sentry/core';
import { createStackParser, forEachEnvelopeItem } from '@sentry/core';
import { nodeStackLineParser } from '@sentry/core/server';
Expand Down Expand Up @@ -69,15 +70,15 @@ function expectCaptureExceptionEvent(event: Event | undefined): void {
filename: 'app:///test/mod.test.ts',
function: '?',
in_app: true,
lineno: 45,
lineno: 46,
},
{
colno: 12,
context_line: " return new Error('Some unhandled error');",
filename: 'app:///test/mod.test.ts',
function: 'something',
in_app: true,
lineno: 42,
lineno: 43,
},
],
);
Expand Down Expand Up @@ -231,7 +232,8 @@ Deno.test('preserves existing log attributes when adding server.address', () =>
assertEquals(log.attributes?.['server.address'], 'test-server');
});

Deno.test('close() removes unload listener', async () => {
Deno.test('close() removes log and metric exit listeners', async () => {
const originalListeners = process.listeners('beforeExit');
const removeEventListenerCalls: Array<string> = [];
const originalRemoveEventListener = globalThis.removeEventListener;
globalThis.removeEventListener = ((event: string, ...args: unknown[]) => {
Expand All @@ -248,9 +250,16 @@ Deno.test('close() removes unload listener', async () => {
transport: makeTestTransport(() => {}),
});

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

await client.close();

assertEquals(removeEventListenerCalls.includes('unload'), true);
assertEquals(addedListeners.length, 2);
assertEquals(process.listeners('beforeExit'), originalListeners);
assertEquals(
removeEventListenerCalls.filter(event => event === 'unload'),
['unload', 'unload'],
);
} finally {
globalThis.removeEventListener = originalRemoveEventListener;
}
Expand Down
Loading