Skip to content

fix(bun): Ensure instrumentation of Bun.serve survives a server reload #15148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

nathankleyn
Copy link
Contributor

If #reload is called on an instance of Bun.serve, the Sentry intrumentation doesn't surive. This is because the Bun instrumentation works by using Proxy on the call to Bun.serve, which isn't called for a reload.

We can't wrap the serve created by calling Bun.serve with a Proxy as Bun seems to do some internal checks using instanceof which break if the instance is now reporting itself as a ProxyObject.

This fixes #15144.

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

import { getDynamicSamplingContextFromSpan, setCurrentClient, spanIsSampled, spanToJSON } from '@sentry/core';

import { BunClient } from '../../src/client';
import { instrumentBunServe } from '../../src/integrations/bunserver';
import { getDefaultBunClientOptions } from '../helpers';

// Fun fact: Bun = 2 21 14 :)
const DEFAULT_PORT = 22114;

describe('Bun Serve Integration', () => {
Copy link
Contributor Author

@nathankleyn nathankleyn Jan 23, 2025

Choose a reason for hiding this comment

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

I discovered that none of the tests in this file were actually working — all of the client.on('spanEnd', ...) were never being called and since all the expects were inside of them nothing failed.

I have restructured the tests to make sure that if the events ever stop firing again in the future, the tests will fail, then fixed all the tests.

describe('Bun Serve Integration', () => {
let client: BunClient;
// Fun fact: Bun = 2 21 14 :)
let port: number = 22114;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't reuse the port on each start since server.stop happens in the background and tests end up using each other's servers.

Comment on lines +6 to +9
const initOptions = {
dsn: 'https://[email protected]/0000000',
tracesSampleRate: 1,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It turns out that the SDK initialised in here is effecting the SDK being used by bunserver.test.ts above. I am not familiar enough with the Sentry codebase to know how to properly reset after these tests but for sure something is missing here as the tests are not properly isolated.

I've had to add tracesSampleRate: 1 for now to ensure that the SDK being reused doesn't totally break the other tests which depend on tracing happening for every single call.

@nathankleyn nathankleyn force-pushed the 15144-fix-bun-instrumentation-disappearing-after-reload branch from 16b41ad to 8fb5c8f Compare January 23, 2025 14:23
If `#reload` is called on an instance of `Bun.serve`, the Sentry
intrumentation doesn't surive. This is because the Bun instrumentation
works by using `Proxy` on the call to `Bun.serve`, which isn't called
for a reload.

We can't wrap the serve created by calling `Bun.serve` with a `Proxy` as
Bun seems to do some internal checks using `instanceof` which break if
the instance is now reporting itself as a `ProxyObject`.

This fixes getsentry#15144.
@nathankleyn nathankleyn force-pushed the 15144-fix-bun-instrumentation-disappearing-after-reload branch from 8fb5c8f to f946c81 Compare January 23, 2025 14:24
@AbhiPrasad
Copy link
Member

Hey @nathankleyn thanks for the PR! Assigning myself so that I can help get this merged in.

@AbhiPrasad AbhiPrasad self-requested a review January 23, 2025 18:12
@AbhiPrasad AbhiPrasad self-assigned this Jan 23, 2025
@AbhiPrasad AbhiPrasad changed the title Ensure instrumentation of Bun.serve survives a server reload fix(bun): Ensure instrumentation of Bun.serve survives a server reload Jan 23, 2025
@AbhiPrasad AbhiPrasad removed their assignment Jan 23, 2025
@AbhiPrasad AbhiPrasad merged commit 8e37842 into getsentry:develop Jan 24, 2025
31 checks passed
AbhiPrasad pushed a commit that referenced this pull request Jan 24, 2025
…oad (#15148)

If `#reload` is called on an instance of `Bun.serve`, the Sentry
intrumentation doesn't surive. This is because the Bun instrumentation
works by using `Proxy` on the call to `Bun.serve`, which isn't called
for a reload.

We can't wrap the serve created by calling `Bun.serve` with a `Proxy` as
Bun seems to do some internal checks using `instanceof` which break if
the instance is now reporting itself as a `ProxyObject`.
AbhiPrasad added a commit that referenced this pull request Jan 24, 2025
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #15148

---------

Co-authored-by: AbhiPrasad <[email protected]>
Co-authored-by: Abhijeet Prasad <[email protected]>
@nathankleyn nathankleyn deleted the 15144-fix-bun-instrumentation-disappearing-after-reload branch January 29, 2025 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bun instrumentation stops working if reload is called on the server
2 participants