From 7298f7489e775ac5d633335747477ea131ac2375 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 20 Feb 2025 17:29:33 +0100 Subject: [PATCH 1/4] fix(bun): Includes correct sdk metadata --- packages/bun/src/client.ts | 6 ++--- packages/bun/src/index.ts | 1 + packages/bun/src/sdk.ts | 15 ++++++++++--- packages/bun/src/types.ts | 9 -------- packages/bun/test/sdk.test.ts | 41 ++++++++++++++++++++++++++++++----- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/packages/bun/src/client.ts b/packages/bun/src/client.ts index 40e430dc2545..aa84106a0551 100644 --- a/packages/bun/src/client.ts +++ b/packages/bun/src/client.ts @@ -5,10 +5,8 @@ import { ServerRuntimeClient, applySdkMetadata } from '@sentry/core'; import type { BunClientOptions } from './types'; /** - * The Sentry Bun SDK Client. - * - * @see BunClientOptions for documentation on configuration options. - * @see SentryClient for usage documentation. + * @deprecated This client is no longer used in v9 + * It was likely mistakenly left after a refactor. */ export class BunClient extends ServerRuntimeClient { /** diff --git a/packages/bun/src/index.ts b/packages/bun/src/index.ts index 770cf2eb2ebe..c25c65487a47 100644 --- a/packages/bun/src/index.ts +++ b/packages/bun/src/index.ts @@ -141,6 +141,7 @@ export { export type { BunOptions } from './types'; +// eslint-disable-next-line deprecation/deprecation export { BunClient } from './client'; export { getDefaultIntegrations, init } from './sdk'; export { bunServerIntegration } from './integrations/bunserver'; diff --git a/packages/bun/src/sdk.ts b/packages/bun/src/sdk.ts index 8bded2d492af..c734a4a511db 100644 --- a/packages/bun/src/sdk.ts +++ b/packages/bun/src/sdk.ts @@ -1,4 +1,5 @@ import { + applySdkMetadata, functionToStringIntegration, inboundFiltersIntegration, linkedErrorsIntegration, @@ -17,8 +18,8 @@ import { onUncaughtExceptionIntegration, onUnhandledRejectionIntegration, } from '@sentry/node'; +import * as os from 'node:os'; -import { BunClient } from './client'; import { bunServerIntegration } from './integrations/bunserver'; import { makeFetchTransport } from './transports'; import type { BunOptions } from './types'; @@ -92,8 +93,16 @@ export function getDefaultIntegrations(_options: Options): Integration[] { * * @see {@link BunOptions} for documentation on configuration options. */ -export function init(options: BunOptions = {}): NodeClient | undefined { - options.clientClass = BunClient; +export function init(userOptions: BunOptions = {}): NodeClient | undefined { + applySdkMetadata(userOptions, 'bun'); + + const options = { + ...userOptions, + platform: 'javascript', + runtime: { name: 'bun', version: Bun.version }, + serverName: userOptions.serverName || global.process.env.SENTRY_NAME || os.hostname(), + }; + options.transport = options.transport || makeFetchTransport; if (options.defaultIntegrations === undefined) { diff --git a/packages/bun/src/types.ts b/packages/bun/src/types.ts index fa0e2171214f..6cc08edd495d 100644 --- a/packages/bun/src/types.ts +++ b/packages/bun/src/types.ts @@ -1,6 +1,5 @@ import type { ClientOptions, Options, TracePropagationTargets } from '@sentry/core'; -import type { BunClient } from './client'; import type { BunTransportOptions } from './transports'; export interface BaseBunOptions { @@ -25,14 +24,6 @@ export interface BaseBunOptions { /** Sets an optional server name (device name) */ serverName?: string; - /** - * Specify a custom BunClient to be used. Must extend BunClient! - * This is not a public, supported API, but used internally only. - * - * @hidden - * */ - clientClass?: typeof BunClient; - /** Callback that is executed when a fatal global error occurs. */ onFatalError?(this: void, error: Error): void; } diff --git a/packages/bun/test/sdk.test.ts b/packages/bun/test/sdk.test.ts index 11870f30c101..5a1bd237e74c 100644 --- a/packages/bun/test/sdk.test.ts +++ b/packages/bun/test/sdk.test.ts @@ -1,20 +1,51 @@ import { describe, expect, test } from 'bun:test'; +import type { NodeClient } from '../src/index'; import { init } from '../src/index'; +import type { BaseTransportOptions, Envelope, Transport, TransportMakeRequestResponse, Event } from '@sentry/core'; + +const envelopes: Envelope[] = []; + +function testTransport(_options: BaseTransportOptions): Transport { + return { + send(request: Envelope): Promise { + envelopes.push(request); + return Promise.resolve({ statusCode: 200 }); + }, + flush(): PromiseLike { + return new Promise(resolve => setTimeout(() => resolve(true), 100)); + }, + }; +} describe('Bun SDK', () => { const initOptions = { dsn: 'https://00000000000000000000000000000000@o000000.ingest.sentry.io/0000000', tracesSampleRate: 1, + transport: testTransport, }; - test("calling init shouldn't fail", () => { + test('SDK works as expected', async () => { + let client: NodeClient | undefined; expect(() => { - init(initOptions); + client = init(initOptions); }).not.toThrow(); - }); - test('should return client from init', () => { - expect(init(initOptions)).not.toBeUndefined(); + expect(client).not.toBeUndefined(); + + client?.captureException(new Error('test')); + client?.flush(); + + await new Promise(resolve => setTimeout(resolve, 1000)); + + expect(envelopes.length).toBe(1); + + const envelope = envelopes[0]; + const event = envelope?.[1][0][1] as Event; + + expect(event.sdk?.name).toBe('sentry.javascript.bun'); + + expect(event.exception?.values?.[0]?.type).toBe('Error'); + expect(event.exception?.values?.[0]?.value).toBe('test'); }); }); From 8cbb0dde728f59753d69e00bd4eef72962464973 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 20 Feb 2025 17:30:52 +0100 Subject: [PATCH 2/4] doc --- packages/bun/src/client.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/bun/src/client.ts b/packages/bun/src/client.ts index aa84106a0551..a96e0c04e264 100644 --- a/packages/bun/src/client.ts +++ b/packages/bun/src/client.ts @@ -5,8 +5,7 @@ import { ServerRuntimeClient, applySdkMetadata } from '@sentry/core'; import type { BunClientOptions } from './types'; /** - * @deprecated This client is no longer used in v9 - * It was likely mistakenly left after a refactor. + * @deprecated This client is no longer used in v9. */ export class BunClient extends ServerRuntimeClient { /** From 97335eadee2f390ea2f8db0768c6b1c796836c9e Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 20 Feb 2025 17:44:21 +0100 Subject: [PATCH 3/4] Fix serve test --- .../bun/test/integrations/bunserver.test.ts | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/bun/test/integrations/bunserver.test.ts b/packages/bun/test/integrations/bunserver.test.ts index e448402c0479..66a66476f78d 100644 --- a/packages/bun/test/integrations/bunserver.test.ts +++ b/packages/bun/test/integrations/bunserver.test.ts @@ -1,13 +1,14 @@ import { afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test'; import type { Span } from '@sentry/core'; -import { getDynamicSamplingContextFromSpan, setCurrentClient, spanIsSampled, spanToJSON } from '@sentry/core'; +import { getDynamicSamplingContextFromSpan, spanIsSampled, spanToJSON } from '@sentry/core'; -import { BunClient } from '../../src/client'; +import { init } from '../../src'; +import type { NodeClient } from '../../src'; import { instrumentBunServe } from '../../src/integrations/bunserver'; import { getDefaultBunClientOptions } from '../helpers'; describe('Bun Serve Integration', () => { - let client: BunClient; + let client: NodeClient | undefined; // Fun fact: Bun = 2 21 14 :) let port: number = 22114; @@ -17,9 +18,7 @@ describe('Bun Serve Integration', () => { beforeEach(() => { const options = getDefaultBunClientOptions({ tracesSampleRate: 1 }); - client = new BunClient(options); - setCurrentClient(client); - client.init(); + client = init(options); }); afterEach(() => { @@ -31,7 +30,7 @@ describe('Bun Serve Integration', () => { test('generates a transaction around a request', async () => { let generatedSpan: Span | undefined; - client.on('spanEnd', span => { + client?.on('spanEnd', span => { generatedSpan = span; }); @@ -66,7 +65,7 @@ describe('Bun Serve Integration', () => { test('generates a post transaction', async () => { let generatedSpan: Span | undefined; - client.on('spanEnd', span => { + client?.on('spanEnd', span => { generatedSpan = span; }); @@ -103,7 +102,7 @@ describe('Bun Serve Integration', () => { let generatedSpan: Span | undefined; - client.on('spanEnd', span => { + client?.on('spanEnd', span => { generatedSpan = span; }); @@ -139,7 +138,7 @@ describe('Bun Serve Integration', () => { test('does not create transactions for OPTIONS or HEAD requests', async () => { let generatedSpan: Span | undefined; - client.on('spanEnd', span => { + client?.on('spanEnd', span => { generatedSpan = span; }); @@ -165,7 +164,7 @@ describe('Bun Serve Integration', () => { test('intruments the server again if it is reloaded', async () => { let serverWasInstrumented = false; - client.on('spanEnd', () => { + client?.on('spanEnd', () => { serverWasInstrumented = true; }); From 63fdd55d177567548523a602171e6ff03b957b5f Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 20 Feb 2025 18:02:22 +0100 Subject: [PATCH 4/4] Biome linting is seperate? --- packages/bun/src/sdk.ts | 2 +- packages/bun/test/sdk.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/bun/src/sdk.ts b/packages/bun/src/sdk.ts index c734a4a511db..96f1b63f902d 100644 --- a/packages/bun/src/sdk.ts +++ b/packages/bun/src/sdk.ts @@ -1,3 +1,4 @@ +import * as os from 'node:os'; import { applySdkMetadata, functionToStringIntegration, @@ -18,7 +19,6 @@ import { onUncaughtExceptionIntegration, onUnhandledRejectionIntegration, } from '@sentry/node'; -import * as os from 'node:os'; import { bunServerIntegration } from './integrations/bunserver'; import { makeFetchTransport } from './transports'; diff --git a/packages/bun/test/sdk.test.ts b/packages/bun/test/sdk.test.ts index 5a1bd237e74c..c4f55ddbb3bb 100644 --- a/packages/bun/test/sdk.test.ts +++ b/packages/bun/test/sdk.test.ts @@ -1,8 +1,8 @@ import { describe, expect, test } from 'bun:test'; +import type { BaseTransportOptions, Envelope, Event, Transport, TransportMakeRequestResponse } from '@sentry/core'; import type { NodeClient } from '../src/index'; import { init } from '../src/index'; -import type { BaseTransportOptions, Envelope, Transport, TransportMakeRequestResponse, Event } from '@sentry/core'; const envelopes: Envelope[] = [];