Skip to content

Commit e08c640

Browse files
authored
fix(bun): Includes correct sdk metadata (#15459)
Extends the basic SDK test to ensure the metadata is correct and that we get the expected exception.
1 parent e5520aa commit e08c640

File tree

6 files changed

+60
-32
lines changed

6 files changed

+60
-32
lines changed

packages/bun/src/client.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import { ServerRuntimeClient, applySdkMetadata } from '@sentry/core';
55
import type { BunClientOptions } from './types';
66

77
/**
8-
* The Sentry Bun SDK Client.
9-
*
10-
* @see BunClientOptions for documentation on configuration options.
11-
* @see SentryClient for usage documentation.
8+
* @deprecated This client is no longer used in v9.
129
*/
1310
export class BunClient extends ServerRuntimeClient<BunClientOptions> {
1411
/**

packages/bun/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export {
141141

142142
export type { BunOptions } from './types';
143143

144+
// eslint-disable-next-line deprecation/deprecation
144145
export { BunClient } from './client';
145146
export { getDefaultIntegrations, init } from './sdk';
146147
export { bunServerIntegration } from './integrations/bunserver';

packages/bun/src/sdk.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import * as os from 'node:os';
12
import {
3+
applySdkMetadata,
24
functionToStringIntegration,
35
inboundFiltersIntegration,
46
linkedErrorsIntegration,
@@ -18,7 +20,6 @@ import {
1820
onUnhandledRejectionIntegration,
1921
} from '@sentry/node';
2022

21-
import { BunClient } from './client';
2223
import { bunServerIntegration } from './integrations/bunserver';
2324
import { makeFetchTransport } from './transports';
2425
import type { BunOptions } from './types';
@@ -92,8 +93,16 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
9293
*
9394
* @see {@link BunOptions} for documentation on configuration options.
9495
*/
95-
export function init(options: BunOptions = {}): NodeClient | undefined {
96-
options.clientClass = BunClient;
96+
export function init(userOptions: BunOptions = {}): NodeClient | undefined {
97+
applySdkMetadata(userOptions, 'bun');
98+
99+
const options = {
100+
...userOptions,
101+
platform: 'javascript',
102+
runtime: { name: 'bun', version: Bun.version },
103+
serverName: userOptions.serverName || global.process.env.SENTRY_NAME || os.hostname(),
104+
};
105+
97106
options.transport = options.transport || makeFetchTransport;
98107

99108
if (options.defaultIntegrations === undefined) {

packages/bun/src/types.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ClientOptions, Options, TracePropagationTargets } from '@sentry/core';
22

3-
import type { BunClient } from './client';
43
import type { BunTransportOptions } from './transports';
54

65
export interface BaseBunOptions {
@@ -25,14 +24,6 @@ export interface BaseBunOptions {
2524
/** Sets an optional server name (device name) */
2625
serverName?: string;
2726

28-
/**
29-
* Specify a custom BunClient to be used. Must extend BunClient!
30-
* This is not a public, supported API, but used internally only.
31-
*
32-
* @hidden
33-
* */
34-
clientClass?: typeof BunClient;
35-
3627
/** Callback that is executed when a fatal global error occurs. */
3728
onFatalError?(this: void, error: Error): void;
3829
}

packages/bun/test/integrations/bunserver.test.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
22
import type { Span } from '@sentry/core';
3-
import { getDynamicSamplingContextFromSpan, setCurrentClient, spanIsSampled, spanToJSON } from '@sentry/core';
3+
import { getDynamicSamplingContextFromSpan, spanIsSampled, spanToJSON } from '@sentry/core';
44

5-
import { BunClient } from '../../src/client';
5+
import { init } from '../../src';
6+
import type { NodeClient } from '../../src';
67
import { instrumentBunServe } from '../../src/integrations/bunserver';
78
import { getDefaultBunClientOptions } from '../helpers';
89

910
describe('Bun Serve Integration', () => {
10-
let client: BunClient;
11+
let client: NodeClient | undefined;
1112
// Fun fact: Bun = 2 21 14 :)
1213
let port: number = 22114;
1314

@@ -17,9 +18,7 @@ describe('Bun Serve Integration', () => {
1718

1819
beforeEach(() => {
1920
const options = getDefaultBunClientOptions({ tracesSampleRate: 1 });
20-
client = new BunClient(options);
21-
setCurrentClient(client);
22-
client.init();
21+
client = init(options);
2322
});
2423

2524
afterEach(() => {
@@ -31,7 +30,7 @@ describe('Bun Serve Integration', () => {
3130
test('generates a transaction around a request', async () => {
3231
let generatedSpan: Span | undefined;
3332

34-
client.on('spanEnd', span => {
33+
client?.on('spanEnd', span => {
3534
generatedSpan = span;
3635
});
3736

@@ -66,7 +65,7 @@ describe('Bun Serve Integration', () => {
6665
test('generates a post transaction', async () => {
6766
let generatedSpan: Span | undefined;
6867

69-
client.on('spanEnd', span => {
68+
client?.on('spanEnd', span => {
7069
generatedSpan = span;
7170
});
7271

@@ -103,7 +102,7 @@ describe('Bun Serve Integration', () => {
103102

104103
let generatedSpan: Span | undefined;
105104

106-
client.on('spanEnd', span => {
105+
client?.on('spanEnd', span => {
107106
generatedSpan = span;
108107
});
109108

@@ -139,7 +138,7 @@ describe('Bun Serve Integration', () => {
139138
test('does not create transactions for OPTIONS or HEAD requests', async () => {
140139
let generatedSpan: Span | undefined;
141140

142-
client.on('spanEnd', span => {
141+
client?.on('spanEnd', span => {
143142
generatedSpan = span;
144143
});
145144

@@ -165,7 +164,7 @@ describe('Bun Serve Integration', () => {
165164

166165
test('intruments the server again if it is reloaded', async () => {
167166
let serverWasInstrumented = false;
168-
client.on('spanEnd', () => {
167+
client?.on('spanEnd', () => {
169168
serverWasInstrumented = true;
170169
});
171170

packages/bun/test/sdk.test.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
import { describe, expect, test } from 'bun:test';
22

3+
import type { BaseTransportOptions, Envelope, Event, Transport, TransportMakeRequestResponse } from '@sentry/core';
4+
import type { NodeClient } from '../src/index';
35
import { init } from '../src/index';
46

7+
const envelopes: Envelope[] = [];
8+
9+
function testTransport(_options: BaseTransportOptions): Transport {
10+
return {
11+
send(request: Envelope): Promise<TransportMakeRequestResponse> {
12+
envelopes.push(request);
13+
return Promise.resolve({ statusCode: 200 });
14+
},
15+
flush(): PromiseLike<boolean> {
16+
return new Promise(resolve => setTimeout(() => resolve(true), 100));
17+
},
18+
};
19+
}
20+
521
describe('Bun SDK', () => {
622
const initOptions = {
723
dsn: 'https://[email protected]/0000000',
824
tracesSampleRate: 1,
25+
transport: testTransport,
926
};
1027

11-
test("calling init shouldn't fail", () => {
28+
test('SDK works as expected', async () => {
29+
let client: NodeClient | undefined;
1230
expect(() => {
13-
init(initOptions);
31+
client = init(initOptions);
1432
}).not.toThrow();
15-
});
1633

17-
test('should return client from init', () => {
18-
expect(init(initOptions)).not.toBeUndefined();
34+
expect(client).not.toBeUndefined();
35+
36+
client?.captureException(new Error('test'));
37+
client?.flush();
38+
39+
await new Promise(resolve => setTimeout(resolve, 1000));
40+
41+
expect(envelopes.length).toBe(1);
42+
43+
const envelope = envelopes[0];
44+
const event = envelope?.[1][0][1] as Event;
45+
46+
expect(event.sdk?.name).toBe('sentry.javascript.bun');
47+
48+
expect(event.exception?.values?.[0]?.type).toBe('Error');
49+
expect(event.exception?.values?.[0]?.value).toBe('test');
1950
});
2051
});

0 commit comments

Comments
 (0)