Skip to content

feat(loongsuite-instrumentation-langgraph): Add langgraph instrumentation to identify react agent#143

Merged
Cirilla-zmh merged 16 commits intoalibaba:mainfrom
Cirilla-zmh:feat/langgraph-instrumentation
Mar 11, 2026
Merged

feat(loongsuite-instrumentation-langgraph): Add langgraph instrumentation to identify react agent#143
Cirilla-zmh merged 16 commits intoalibaba:mainfrom
Cirilla-zmh:feat/langgraph-instrumentation

Conversation

@Cirilla-zmh
Copy link
Collaborator

@Cirilla-zmh Cirilla-zmh commented Mar 11, 2026

Description

Summary

This PR introduces OpenTelemetry instrumentation for LangGraph, enabling proper Agent and ReAct Step span creation when used together with the LangChain instrumentation.

Changes

New: LangGraph Instrumentation Package (loongsuite-instrumentation-langgraph)

  • Patch create_react_agent to set _loongsuite_react_agent = True on CompiledStateGraph
  • Patch Pregel.stream / Pregel.astream to inject metadata["_loongsuite_react_agent"] into RunnableConfig, so LangChain instrumentation can detect ReAct agents via callback metadata
  • Use wrapt.wrap_function_wrapper (aligned with loongsuite-instrumentation-langchain)
  • Add unit tests for the instrumentor, patches, and ReAct step spans

CI & Tooling

  • Add loongsuite_lint_0.yml and loongsuite_test_0.yml workflows
  • Add LangGraph instrumentation to tox-loongsuite.ini
  • Add copilot-instructions.md

Fixes #136 (issue)

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Add unit tests

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

- Add ExtendedTelemetryHandler with entry span and ReAct span support
- Add propagation of session id and user id
- Add _extended_common for shared types and utilities
- Update gen_ai_extended_attributes for new span types
- Add loongsuite workflows for CI

Change-Id: I461f3febc206aea281741a01d00f38931483a4dc
Co-developed-by: Cursor <noreply@cursor.com>
Made-with: Cursor
Change-Id: I426ce98bc9e1eb2ebca2dfee5fbe0211d801809f
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: Idff7ed598bdeb7c9846ffcee171abcce3af32994
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: I78074851d283e36996c94a31bd51a00322f4325e
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: I186113d97e1ca400434021fc487c4e587f05a1fa
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: Icb53d8e58729c407a9f8f3caf547f32e562210cf
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: Ib4bdeb22467225d136627702cbba86e6ff7c2425
Co-developed-by: Cursor <noreply@cursor.com>
- Add ExtendedTelemetryHandler with entry span and ReAct span support
- Add propagation of session id and user id
- Add _extended_common for shared types and utilities
- Update gen_ai_extended_attributes for new span types
- Add loongsuite workflows for CI

Change-Id: I461f3febc206aea281741a01d00f38931483a4dc
Co-developed-by: Cursor <noreply@cursor.com>
Made-with: Cursor
- Add loongsuite-instrumentation-langgraph package
- Patch create_react_agent to set _loongsuite_react_agent flag
- Patch Pregel.stream/astream to inject metadata for LangChain tracer
- Add tests for instrumentor, patch, and ReAct step spans
- Update tox config for langgraph testing

Change-Id: I8f1cba4f3f0e99842820815167fb6b03670cd184
Co-developed-by: Cursor <noreply@cursor.com>
Made-with: Cursor
…attr

Replace hardcoded string literals with the REACT_AGENT_METADATA_KEY
constant for both the graph attribute and metadata injection, improving
consistency. The constant and wrapper are kept local to the langgraph
package (mirrored in langchain instrumentation) to avoid cross-package
dependencies.

Change-Id: Iea2171ac506269dfd3fac81c8663e2ecb0c9edfd
Co-developed-by: Cursor <noreply@cursor.com>
Made-with: Cursor
@Cirilla-zmh Cirilla-zmh added documentation Improvements or additions to documentation enhancement New feature or request instrumentaion The instrumentation label represents issues related to instrumentation. genai The genai label represents issues related to generative AI. labels Mar 11, 2026
Change-Id: I062fab9993f7bfa8a09233f3f3f6f95b72c5387e
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: I55a91317730763f6b76060550be0d63f333800a5
Co-developed-by: Cursor <noreply@cursor.com>
@ralf0131 ralf0131 requested a review from Copilot March 11, 2026 08:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new loongsuite-instrumentation-langgraph package that patches LangGraph’s ReAct agent entry points so LangChain instrumentation can correctly detect ReAct agents and emit Agent / ReAct Step spans.

Changes:

  • Introduces a LangGraph instrumentor that wraps create_react_agent and Pregel.stream/astream to propagate ReAct detection metadata.
  • Adds pytest coverage for patching behavior and ReAct-step span hierarchy.
  • Extends tox and GitHub Actions workflows to lint/test the new instrumentation across supported Python versions.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tox-loongsuite.ini Adds tox envs/deps/commands for langgraph instrumentation tests + lint.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/tests/test_react_step_spans.py Integration-style tests validating Agent/Step/LLM span creation and parenting.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/tests/test_patch.py Unit tests for wrapper patching / unpatching behavior.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/tests/test_instrumentor.py Basic lifecycle/dependencies tests for the new instrumentor.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/tests/requirements.oldest.txt Oldest test dependency set for the new package.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/tests/requirements.latest.txt Latest test dependency set for the new package.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/tests/conftest.py Test fixtures wiring OTel providers + installing both instrumentors.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/tests/init.py Test package marker (license header).
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/src/opentelemetry/instrumentation/langgraph/version.py Introduces package version module.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/src/opentelemetry/instrumentation/langgraph/package.py Declares _instruments metadata for the instrumentor.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/src/opentelemetry/instrumentation/langgraph/internal/patch.py Implements wrapt wrappers for ReAct flagging + metadata injection.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/src/opentelemetry/instrumentation/langgraph/internal/init.py Internal package marker (license header).
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/src/opentelemetry/instrumentation/langgraph/init.py Implements LangGraphInstrumentor patch/unpatch logic.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/pyproject.toml Defines new distribution metadata and OTel entry point.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/README.md Documents installation, usage, and the patching data flow.
instrumentation-loongsuite/loongsuite-instrumentation-langgraph/CHANGELOG.md Adds initial changelog entry for the new instrumentation.
copilot-instructions.md Adds guidance around PLC0415 inline imports and naming conventions.
.github/workflows/loongsuite_test_0.yml Adds CI matrix jobs for langgraph instrumentation tests.
.github/workflows/loongsuite_lint_0.yml Adds CI job for langgraph instrumentation linting.
Comments suppressed due to low confidence (1)

instrumentation-loongsuite/loongsuite-instrumentation-langgraph/README.md:1

  • The README shows calling instrument() and uninstrument() on two different LangGraphInstrumentor() instances. It’s safer and more idiomatic to use the same instance for both (especially if patch state becomes instance-scoped as suggested), e.g. create instrumentor = LangGraphInstrumentor() then call instrumentor.instrument() / instrumentor.uninstrument().
# LongSuite LangGraph Instrumentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Change-Id: Ic0a6f4856fbde936feb7c6e6d5e2038d1d72849a
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: Icb403e9db5e214c5a726c71a92538ef990201998
Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: I7d2c209080387374091a02c1cd5355621b0415d4
Co-developed-by: Cursor <noreply@cursor.com>
@Cirilla-zmh Cirilla-zmh merged commit 9db22cc into alibaba:main Mar 11, 2026
97 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request genai The genai label represents issues related to generative AI. instrumentaion The instrumentation label represents issues related to instrumentation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Refactor loongsuite-instrumentation-langchain for the latest OTel/LoongSuite semantic conventions.

4 participants