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
33 changes: 32 additions & 1 deletion packages/deno/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { DenoClientOptions } from './types';

declare const __SENTRY_CHANNEL_INJECTION__: boolean | undefined;

const DEFAULT_CLIENT_REPORT_FLUSH_INTERVAL_MS = 60_000;

function getHostName(): string | undefined {
// Deno.permissions.querySync is not available on Deno Deploy
if (!Deno.permissions.querySync) {
Expand All @@ -27,6 +29,7 @@ function getHostName(): string | undefined {
*/
export class DenoClient extends ServerRuntimeClient<DenoClientOptions> {
private _onExitFlushListener: (() => void) | undefined;
private _clientReportInterval: number | undefined;

/**
* Creates a new Deno SDK instance.
Expand Down Expand Up @@ -57,9 +60,12 @@ export class DenoClient extends ServerRuntimeClient<DenoClientOptions> {
super(clientOptions);

// The log, metric and span buffers drain on `flush`. Their timers are unref'd, so a natural exit
// would drop what they hold.
// would drop what they hold, and the outcomes recorded since the last client report.
this._onExitFlushListener = () => {
this.emit('flush');
if (this.getOptions().sendClientReports) {
this._flushOutcomes();
}
};

if (serverName) {
Expand Down Expand Up @@ -98,11 +104,36 @@ export class DenoClient extends ServerRuntimeClient<DenoClientOptions> {
/** @inheritDoc */
// @ts-expect-error - PromiseLike is a subset of Promise
public async close(timeout?: number | undefined): PromiseLike<boolean> {
if (this._clientReportInterval !== undefined) {
clearInterval(this._clientReportInterval);
}

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

return super.close(timeout);
}

/**
* Starts sending client reports on an interval. The exit listener sends them too.
*
* `Sentry.init()` calls this; a directly constructed client must call it itself. It is not in the
* constructor, so that creating many clients does not leak intervals. Call `close()` on a client
* that is no longer used.
*/
Comment thread
sentry-junior[bot] marked this conversation as resolved.
Resolved
public startClientReportTracking(): void {
const options = this.getOptions();
if (!options.sendClientReports) {
return;
}

// `setInterval` returns a number in Deno. `@types/node` types it as `NodeJS.Timeout`.
this._clientReportInterval = setInterval(() => {
this._flushOutcomes();
}, options.clientReportFlushInterval ?? DEFAULT_CLIENT_REPORT_FLUSH_INTERVAL_MS) as unknown as number;
// An unref'd interval does not keep the process alive.
Deno.unrefTimer(this._clientReportInterval);
}
}
4 changes: 3 additions & 1 deletion packages/deno/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ function _init(options: DenoOptions, getDefaultIntegrationsImpl: (options: Optio

const clientOptions: ServerRuntimeClientOptions = {
...options,
sendClientReports: options.sendClientReports ?? true,
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
integrations: getIntegrationsToSetup({ integrations: options.integrations, defaultIntegrations }),
transport: options.transport || makeFetchTransport,
};

const client = initAndBind(DenoClient, clientOptions);
const client = initAndBind(DenoClient, clientOptions) as DenoClient;
client.startClientReportTracking();

// Set up OpenTelemetry compatibility to capture spans from libraries using @opentelemetry/api
// Note: This is separate from Deno's native OTEL support and doesn't capture auto-instrumented spans
Expand Down
5 changes: 5 additions & 0 deletions packages/deno/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface BaseDenoOptions {

/** Callback that is executed when a fatal global error occurs. */
onFatalError?(this: void, error: Error): void;

/**
* Configures in which interval client reports will be flushed. Defaults to `60_000` (milliseconds).
*/
clientReportFlushInterval?: number;
}

/**
Expand Down
73 changes: 73 additions & 0 deletions packages/deno/test/client-reports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { ClientReport, Envelope } from '@sentry/core';
import { forEachEnvelopeItem } from '@sentry/core';
import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts';
import { close, getCurrentScope, init } from '../build/esm/index.js';
import type { DenoOptions } from '../build/esm/index.js';
import { makeTestTransport } from './transport.ts';

function initWithClientReports(options: Partial<DenoOptions> = {}): ClientReport[] {
const clientReports: ClientReport[] = [];
getCurrentScope().setClient(undefined);
init({
dsn: 'https://233a45e5efe34c47a3536797ce15dafa@nothing.here/5650507',
defaultIntegrations: false,
beforeSend: () => null,
transport: makeTestTransport((envelope: Envelope) => {
forEachEnvelopeItem(envelope, (item, type) => {
if (type === 'client_report') {
clientReports.push(item[1] as ClientReport);
}
});
}),
...options,
});
return clientReports;
}

async function waitForClientReport(clientReports: ClientReport[]): Promise<void> {
const deadline = Date.now() + 5_000;
while (clientReports.length === 0) {
if (Date.now() > deadline) {
throw new Error('Timed out waiting for a client report');
}
await new Promise(resolve => setTimeout(resolve, 10));
}
}

Deno.test('sends client reports on the flush interval', async () => {
const clientReports = initWithClientReports({ clientReportFlushInterval: 50 });

getCurrentScope().captureMessage('dropped');
await waitForClientReport(clientReports);

assertEquals(
clientReports.map(report => report.discarded_events),
[[{ reason: 'before_send', category: 'error', quantity: 1 }]],
);

await close();
});

Deno.test('flushes client reports when unload fires', async () => {
const clientReports = initWithClientReports();

getCurrentScope().captureMessage('dropped');
globalThis.dispatchEvent(new Event('unload'));
await waitForClientReport(clientReports);

assertEquals(
clientReports.map(report => report.discarded_events),
[[{ reason: 'before_send', category: 'error', quantity: 1 }]],
);

await close();
});

Comment thread
sentry-junior[bot] marked this conversation as resolved.
Resolved
Deno.test('does not send client reports when sendClientReports is false', async () => {
const clientReports = initWithClientReports({ sendClientReports: false });

getCurrentScope().captureMessage('dropped');
await close();

assertEquals(clientReports, []);
});
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Disable path is not actually tested

Medium Severity

The sendClientReports: false test never hits the interval or unload send paths this PR adds. close() also does not flush outcomes here, so the empty assertion stays green even if those guards are missing. A drop plus a short interval or a synthetic unload would actually prove the flag disables sending.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit cf4db75. Configure here.

Loading