Skip to content

feat(server-utils): Rewrite SentryLangGraphInstrumentation to orchestrion#22268

Merged
logaretm merged 3 commits into
developfrom
feat/langgraph-orchestrion
Jul 16, 2026
Merged

feat(server-utils): Rewrite SentryLangGraphInstrumentation to orchestrion#22268
logaretm merged 3 commits into
developfrom
feat/langgraph-orchestrion

Conversation

@logaretm

@logaretm logaretm commented Jul 14, 2026

Copy link
Copy Markdown
Member

Rewrites the LangGraph integration to a node:diagnostics_channel listener instead of InstrumentationBase, with orchestrion injecting the channels. The vendored OTel path stays as the fallback when orchestrion isn't injected.

closes #20916

@github-actions

github-actions Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.74 kB - -
@sentry/browser - with treeshaking flags 26.19 kB - -
@sentry/browser (incl. Tracing) 46.57 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.36 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.34 kB - -
@sentry/browser (incl. Tracing, Replay) 85.83 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.46 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.55 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 103.18 kB - -
@sentry/browser (incl. Feedback) 44.92 kB - -
@sentry/browser (incl. sendFeedback) 32.54 kB - -
@sentry/browser (incl. FeedbackAsync) 37.67 kB - -
@sentry/browser (incl. Metrics) 28.84 kB - -
@sentry/browser (incl. Logs) 29.07 kB - -
@sentry/browser (incl. Metrics & Logs) 29.76 kB - -
@sentry/react 29.54 kB - -
@sentry/react (incl. Tracing) 48.82 kB - -
@sentry/vue 33.17 kB - -
@sentry/vue (incl. Tracing) 48.55 kB - -
@sentry/svelte 27.77 kB - -
CDN Bundle 30.14 kB - -
CDN Bundle (incl. Tracing) 48.52 kB - -
CDN Bundle (incl. Logs, Metrics) 31.72 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.83 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.97 kB - -
CDN Bundle (incl. Tracing, Replay) 86.04 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.33 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.82 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 93.09 kB - -
CDN Bundle - uncompressed 89.85 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.66 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.56 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.64 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 219.28 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.86 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.82 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.56 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 283.52 kB - -
@sentry/nextjs (client) 51.38 kB - -
@sentry/sveltekit (client) 47 kB - -
@sentry/core/server 78.71 kB +0.11% +83 B 🔺
@sentry/core/browser 65.08 kB +0.15% +94 B 🔺
@sentry/node-core 63.21 kB +0.01% +1 B 🔺
@sentry/node 125.45 kB - -
@sentry/node (incl. diagnostics channel injection) 141.67 kB +0.36% +503 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 70.03 kB +0.01% +2 B 🔺
@sentry/node/light 51.33 kB - -
@sentry/node - without tracing 74.7 kB - -
@sentry/aws-serverless 83.92 kB -0.01% -5 B 🔽
@sentry/cloudflare (withSentry) - minified 182.1 kB - -
@sentry/cloudflare (withSentry) 450.9 kB - -

View base workflow run

@logaretm
logaretm marked this pull request as ready for review July 14, 2026 21:05
@logaretm
logaretm requested a review from a team as a code owner July 14, 2026 21:05
@logaretm
logaretm requested review from isaacs, mydea and nicohrubec and removed request for a team July 14, 2026 21:05
Comment thread packages/server-utils/src/integrations/tracing-channel/langgraph.ts
* injected into `@langchain/langgraph`'s `StateGraph.compile` and `createReactAgent`, so it requires
* the orchestrion runtime hook or bundler plugin.
*/
export const langGraphChannelIntegration = defineIntegration(_langGraphChannelIntegration);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing feat integration tests

Medium Severity

This is a feat PR that adds a new orchestrion diagnostics-channel LangGraph integration, but the diff includes no integration or E2E test covering the new channel path (StateGraph.compile / createReactAgent subscribe + invoke wrapping). Existing OTel suite coverage does not validate this rewrite. Flagged because the review rules require at least one integration or E2E test on feat PRs.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 5b8c0af. Configure here.

Comment thread packages/core/src/tracing/langgraph/index.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 90d1118. Configure here.

Comment thread packages/server-utils/src/integrations/tracing-channel/langgraph.ts
@linear-code

linear-code Bot commented Jul 14, 2026

Copy link
Copy Markdown

JS-2489


// `createReactAgent` compiles a `StateGraph` internally; suppress the `create_agent` span for that
// nested compile so a react agent gets a single `invoke_agent` span, matching the OTel path.
let insideCreateReactAgent = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: The module-level insideCreateReactAgent flag is not concurrency-safe, leading to race conditions where one request's state can cause incorrect span suppression in another unrelated request.
Severity: HIGH

Suggested Fix

Replace the module-level insideCreateReactAgent flag with a request-scoped state management solution, such as AsyncLocalStorage. This will ensure that the flag's state is isolated to the specific asynchronous context of each request, preventing interference between concurrent operations.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/server-utils/src/integrations/tracing-channel/langgraph.ts#L39

Potential issue: The `insideCreateReactAgent` flag, declared as a module-level
singleton, is modified by event subscribers for `reactAgentChannel`. In a concurrent
environment like a Node.js server, this creates a race condition. If one request calls
`createReactAgent` and sets the flag, a simultaneous, unrelated request to
`StateGraph.compile` will read the globally set flag and incorrectly suppress its
`create_agent` span. This happens because the event callbacks operate in a shared module
context, unlike the OTel path which uses a synchronous `try/finally` block to scope the
flag's state.

Also affects:

  • packages/server-utils/src/integrations/tracing-channel/langgraph.ts:63~63
  • packages/server-utils/src/integrations/tracing-channel/langgraph.ts:99~99
  • packages/server-utils/src/integrations/tracing-channel/langgraph.ts:102~102
  • packages/server-utils/src/integrations/tracing-channel/langgraph.ts:110~110

Did we get this right? 👍 / 👎 to inform future reviews.


// `createReactAgent` compiles a `StateGraph` internally; suppress the `create_agent` span for that
// nested compile so a react agent gets a single `invoke_agent` span, matching the OTel path.
let insideCreateReactAgent = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: afaik create_agent spans are no longer relevant for us, so that this is still emitted in the langgraph instrumentation seems like a leftover. so we could think about simplifying all of this? or if we want to keep parity for now that's also fine I think we should just add a task for the major to get rid of this I think

@logaretm logaretm Jul 16, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So is it safe to drop entirely? If it already gets dropped somewhere along the line then we should simplify sure. Otherwise, I can create an issue and park a PR for v11 for it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The OTel path still emits it but we will drop it eventually just didn't have the time yet, but I think it's fine to park this for v11 then we can align this more broadly.

logaretm added 3 commits July 16, 2026 11:02
Rewrites the LangGraph integration to a diagnostics-channel listener
instead of `InstrumentationBase`, with orchestrion injecting the channels
into `@langchain/langgraph`'s `StateGraph.compile` and `createReactAgent`.

The subscriber creates the `create_agent` span around `compile`, wraps the
returned compiled graph's `invoke` with the shared `invoke_agent`
instrumentation, and wraps react-agent tools, reusing the existing core
span builders so span output is identical. The OTel path stays as the
fallback when orchestrion isn't injected.
- Set the createReactAgent suppression flag only after wrapping tools, so a
  throw there can't leave it stuck on and permanently suppress create_agent spans.
- Route instrumentStateGraphCompile through _INTERNAL_getLangGraphCreateAgentSpanOptions
  so the create_agent span options have a single source of truth.
@logaretm
logaretm force-pushed the feat/langgraph-orchestrion branch from 07e668b to 1522c6f Compare July 16, 2026 15:05
@logaretm
logaretm requested a review from nicohrubec July 16, 2026 15:09
@logaretm
logaretm merged commit abacdbf into develop Jul 16, 2026
311 of 312 checks passed
@logaretm
logaretm deleted the feat/langgraph-orchestrion branch July 16, 2026 15:28
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.

Rewrite SentryLangGraphInstrumentation to orchestrion

2 participants