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
16 changes: 16 additions & 0 deletions packages/cloudflare/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
applySdkMetadata,
debug,
spanIsSampled,
spanStreamingIntegration,
} from '@sentry/core';
import type { ServerRuntimeClientOptions } from '@sentry/core/server';
import { ServerRuntimeClient } from '@sentry/core/server';
Expand All @@ -15,6 +16,10 @@ import type { makeFlushLock } from './flush';
import type { CloudflareTransportOptions } from './transport';
import { getInvocationState, getInvocationWaitUntil } from './utils/invocationContext';

declare const __SENTRY_TRACING__: boolean;

const SPAN_STREAMING_INTEGRATION_NAME = 'SpanStreaming';

/**
* The Sentry Cloudflare SDK Client.
*
Expand Down Expand Up @@ -59,6 +64,17 @@ export class CloudflareClient extends ServerRuntimeClient {
_flushInterval: 0,
};

// workerd can't unref timers, so the default 500ms segment-end flush timer holds every
// Durable Object invocation open (and billed) until it fires, sampled or not. The boundary
// flush and the eager drain already deliver streamed spans here.
if (
(typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) &&
clientOptions.traceLifecycle !== 'static' &&
!clientOptions.integrations.some(i => i.name === SPAN_STREAMING_INTEGRATION_NAME)
) {
clientOptions.integrations.push(spanStreamingIntegration({ flushOnSegmentEnd: false }));
}

super(clientOptions);
this._flushLock = flushLock;
this.isCachedClient = options.cacheClient === true;
Expand Down
28 changes: 27 additions & 1 deletion packages/cloudflare/test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as SentryCore from '@sentry/core';
import type { Envelope, Integration } from '@sentry/core';
import { getClient } from '@sentry/core';
import { getClient, startSpan } from '@sentry/core';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import type { CloudflareOptions } from '../src/client';
import { CloudflareClient } from '../src/client';
Expand Down Expand Up @@ -69,6 +69,32 @@ describe('init', () => {
);
});

test('does not leave a timer pending when a segment span ends', () => {
vi.useFakeTimers();
try {
init({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 0 });

startSpan({ name: 'alarm' }, () => undefined);

// A pending timer keeps a Durable Object invocation open until it fires.
expect(vi.getTimerCount()).toBe(0);
} finally {
vi.useRealTimers();
}
});

test("doesn't add spanStreamingIntegration when tracing is excluded from the bundle", () => {
vi.stubGlobal('__SENTRY_TRACING__', false);
try {
const client = init({ dsn: 'https://public@dsn.ingest.sentry.io/1337' });
const integrations = client?.getOptions().integrations.filter(i => i.name === 'SpanStreaming');

expect(integrations).toEqual([]);
} finally {
vi.unstubAllGlobals();
}
});

type MarkedIntegration = Integration & { _custom?: boolean };

test("doesn't add spanStreamingIntegration if user added it manually", () => {
Expand Down
Loading