Skip to content

fix(node): Prevent duplicate LangChain spans from double module patching#19684

Merged
nicohrubec merged 1 commit intodevelopfrom
nh/fix-duplicate-langchain-spans
Mar 6, 2026
Merged

fix(node): Prevent duplicate LangChain spans from double module patching#19684
nicohrubec merged 1 commit intodevelopfrom
nh/fix-duplicate-langchain-spans

Conversation

@nicohrubec
Copy link
Member

@nicohrubec nicohrubec commented Mar 6, 2026

The LangChain instrumentation registers both a module-level and a file-level hook for each provider package (e.g. @langchain/openai). Both hooks call _patch, which wraps the same prototype methods (invoke, stream, batch) with a new
proxy and callback handler. This results in every LangChain call producing duplicate gen_ai.chat spans. The fix adds a __sentry_patched__ guard on the prototype to skip patching if it's already been done.

Closes #19685 (added automatically)

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2026

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 25.63 kB - -
@sentry/browser - with treeshaking flags 24.13 kB - -
@sentry/browser (incl. Tracing) 42.43 kB - -
@sentry/browser (incl. Tracing, Profiling) 47.09 kB - -
@sentry/browser (incl. Tracing, Replay) 81.25 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.87 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 85.95 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 98.21 kB - -
@sentry/browser (incl. Feedback) 42.44 kB - -
@sentry/browser (incl. sendFeedback) 30.3 kB - -
@sentry/browser (incl. FeedbackAsync) 35.35 kB - -
@sentry/browser (incl. Metrics) 26.8 kB - -
@sentry/browser (incl. Logs) 26.94 kB - -
@sentry/browser (incl. Metrics & Logs) 27.61 kB - -
@sentry/react 27.38 kB - -
@sentry/react (incl. Tracing) 44.77 kB - -
@sentry/vue 30.08 kB - -
@sentry/vue (incl. Tracing) 44.3 kB - -
@sentry/svelte 25.66 kB - -
CDN Bundle 28.17 kB - -
CDN Bundle (incl. Tracing) 43.26 kB - -
CDN Bundle (incl. Logs, Metrics) 29.01 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 44.1 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 68.09 kB - -
CDN Bundle (incl. Tracing, Replay) 80.14 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 81 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 85.65 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.53 kB - -
CDN Bundle - uncompressed 82.35 kB - -
CDN Bundle (incl. Tracing) - uncompressed 128.07 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 85.19 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 130.9 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 208.85 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 244.95 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 247.77 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 257.86 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 260.67 kB - -
@sentry/nextjs (client) 47.18 kB - -
@sentry/sveltekit (client) 42.89 kB - -
@sentry/node-core 52.25 kB +0.02% +9 B 🔺
@sentry/node 174.73 kB +0.02% +20 B 🔺
@sentry/node - without tracing 97.4 kB +0.02% +12 B 🔺
@sentry/aws-serverless 113.2 kB +0.01% +8 B 🔺

View base workflow run

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2026

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,958 - 9,167 -2%
GET With Sentry 1,717 19% 1,740 -1%
GET With Sentry (error only) 6,012 67% 6,123 -2%
POST Baseline 1,158 - 1,196 -3%
POST With Sentry 586 51% 600 -2%
POST With Sentry (error only) 1,044 90% 1,068 -2%
MYSQL Baseline 3,224 - 3,344 -4%
MYSQL With Sentry 433 13% 510 -15%
MYSQL With Sentry (error only) 2,605 81% 2,729 -5%

View base workflow run

Copy link
Member

@andreiborza andreiborza left a comment

Choose a reason for hiding this comment

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

LGTM

@linear-code linear-code bot changed the title fix(node): Prevent duplicate LangChain spans from double module patching fix(node): Prevent duplicate LangChain spans from double module patching Mar 6, 2026
@nicohrubec nicohrubec merged commit 5e5487b into develop Mar 6, 2026
155 checks passed
@nicohrubec nicohrubec deleted the nh/fix-duplicate-langchain-spans branch March 6, 2026 14:58
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.

fix(node): Prevent duplicate LangChain spans from double module patching

2 participants