Skip to content

Conversation

mydea
Copy link
Member

@mydea mydea commented Sep 11, 2025

This PR fixes the usage of the SDK in v10 with Prisma v5.
We used to require/recommend that users manually installed @prisma/instrumentation v5 and passed this to our prismaIntegration like this:

const { PrismaInstrumentation } = require('@prisma/instrumentation');

prismaIntegration({ prismaInstrumentation: new PrismaInstrumentation() });

However, this no longer works in v10 because that version pulls in v1 of @opentelemetry/sdk-trace-base, which is no longer compatible in v10 of our SDK 😢

So to fix this, this PR removes/deprecates the passing of prismaInstrumentation (this now no-ops). v5 is now supported out of the box!

To make this work, some hacks and workarounds were necessary 😬 The problem is that v5 of the prisma instrumentation relies on manually creating span instances with specified ID and parent IDs. This is no longer possible in OTEL v2, as all these APIs are no longer exported 😞 (it was also weird from prisma to do this in the first place, and they no longer do this in v6 of the instrumentation).

To get this to work, we manually overwrite the tracer._idGenerator with a mock that returns the IDs we need, kind of making this work as expected again. Hopefully this will not break in the future, we'll see - our tests should at least show us if that ever breaks.

While at this, I also re-wrote our integration tests to run in CJS and ESM properly and use the new test runner structure instead of manually calling yarn etc. in there.

Example trace before in v5 (without custom prismaInstrumentation, which broke at runtime):
https://sentry-sdks.sentry.io/explore/discover/trace/49e2095162e4bb571074e27653586643/?dataset=transactions&field=title&field=project&field=user.display&field=timestamp&name=All%20Errors&node=span-61995a9d80c6925f&project=4508330866769920&query=&queryDataset=transaction-like&sort=-timestamp&source=discover&statsPeriod=1h&timestamp=1757575919&yAxis=count%28%29

Example trace with this PR:
https://sentry-sdks.sentry.io/explore/discover/trace/deaaa1990ba6204085779aa09888dc2c/?dataset=transactions&field=title&field=project&field=user.display&field=timestamp&fov=0%2C813.7861328125&name=All%20Errors&node=span-8eb41978be76824e&project=4508330866769920&query=&queryDataset=transaction-like&sort=-timestamp&source=discover&statsPeriod=1h&timestamp=1757575780&yAxis=count%28%29

@mydea mydea self-assigned this Sep 11, 2025
Copy link
Member Author

Choose a reason for hiding this comment

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

not really related but noticed we had these checked in for no reason.

expect(spans).toContainEqual(
expect.objectContaining({
data: {
'db.statement': expect.stringContaining('SELECT'),
Copy link
Member Author

Choose a reason for hiding this comment

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

these tests were missing and this was actually what was not working in v5 without passing in the custom instrumentation, previously. this is now fixed on this branch.

Copy link
Contributor

github-actions bot commented Sep 11, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.17 kB - -
@sentry/browser - with treeshaking flags 22.75 kB - -
@sentry/browser (incl. Tracing) 40.17 kB - -
@sentry/browser (incl. Tracing, Replay) 78.52 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 68.28 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 83.2 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 95.41 kB - -
@sentry/browser (incl. Feedback) 40.91 kB - -
@sentry/browser (incl. sendFeedback) 28.82 kB - -
@sentry/browser (incl. FeedbackAsync) 33.77 kB - -
@sentry/react 25.89 kB - -
@sentry/react (incl. Tracing) 42.13 kB - -
@sentry/vue 28.66 kB - -
@sentry/vue (incl. Tracing) 41.97 kB - -
@sentry/svelte 24.2 kB - -
CDN Bundle 25.76 kB - -
CDN Bundle (incl. Tracing) 40.05 kB - -
CDN Bundle (incl. Tracing, Replay) 76.33 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 81.79 kB - -
CDN Bundle - uncompressed 75.33 kB - -
CDN Bundle (incl. Tracing) - uncompressed 118.56 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 233.66 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 246.42 kB - -
@sentry/nextjs (client) 44.17 kB - -
@sentry/sveltekit (client) 40.6 kB - -
@sentry/node-core 49.83 kB -0.01% -1 B 🔽
@sentry/node 151.1 kB +0.04% +49 B 🔺
@sentry/node - without tracing 91.72 kB -0.01% -1 B 🔽
@sentry/aws-serverless 105.17 kB - -

View base workflow run

Copy link
Contributor

github-actions bot commented Sep 11, 2025

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 8,832 - 9,150 -3%
GET With Sentry 1,247 14% 1,244 +0%
GET With Sentry (error only) 6,020 68% 6,000 +0%
POST Baseline 1,183 - 1,197 -1%
POST With Sentry 479 40% 482 -1%
POST With Sentry (error only) 1,026 87% 1,051 -2%
MYSQL Baseline 3,217 - 3,287 -2%
MYSQL With Sentry 266 8% 468 -43%
MYSQL With Sentry (error only) 2,569 80% 2,653 -3%

View base workflow run

@mydea mydea changed the title feat(node): Ensure prismaIntergation works with Prisma v5 feat(node): Ensure prismaIntegration works with Prisma v5 Sep 11, 2025
const spanId = engineSpan.span_id;
const traceId = engineSpan.trace_id;

const links: Link[] | undefined = engineSpan.links?.map(link => {
Copy link
Member

Choose a reason for hiding this comment

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

q: any idea what links prisma instrumentation would add to their spans? Just curious since this one of the first links usages I see in our code, no complaints :D

Copy link
Member Author

Choose a reason for hiding this comment

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

no idea, I just copied this from their internal implementation of this where they allow for this :D Maybe a good link to drop here, I tried to make this work based on this:

https://github.com/prisma/prisma/blob/5.22.0/packages/instrumentation/src/ActiveTracingHelper.ts#L54

},
};

tracer._idGenerator = temporaryIdGenerator;
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if this works reliably. Probably not because users can configure tools like terser to minify private fields (like we do in our CDN bundles). But then again, quite unlikely to happen in server builds. So let's roll with it :D

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah I had the same thought. this is def. not bullet proof 😬 for now I added a log about this, if this does not work then you will miss out on some spans (the db spans themselves) which is not great but I guess it is OK for this hacky compatibility work for the old version!

cursor[bot]

This comment was marked as outdated.

} finally {
// Ensure we always restore this at the end, even if something errors
tracer._idGenerator = initialIdGenerator;
}
Copy link

Choose a reason for hiding this comment

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

Bug: Prisma Span Missing Attributes, Tracer ID Conflict

Prisma v5 compatibility spans created in createEngineSpan are missing mandatory Sentry attributes like SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN and SEMANTIC_ATTRIBUTE_SENTRY_OP. Additionally, the temporary global override of tracer._idGenerator (lines 127-135) creates a race condition, potentially causing other concurrently created spans to receive incorrect IDs.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

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

this is correct in theory but we add the attributes etc. in a separate place.

@mydea mydea merged commit 2cc1ef6 into develop Sep 11, 2025
60 checks passed
@mydea mydea deleted the fn/prisma-5 branch September 11, 2025 11:06
mydea added a commit to getsentry/sentry-docs that referenced this pull request Sep 11, 2025
Once getsentry/sentry-javascript#17595 is merged
support will be even better, but even today passing the
`prismaInstrumentation` will actually fail, so not doing it is better
even if some spans are missing.

Also updating the docs to reflect that this is enabled by default now.
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.

3 participants