refactor(loongsuite-instrumentation-langchain): GenAI Util Migration & ReAct Step Support#139
Conversation
- 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
…ndler - Integrate ExtendedTelemetryHandler for improved telemetry - Refactor LoongsuiteTracer with context propagation and content capture - Add ReAct step patch for langchain_classic - Refactor semconv and utility functions - Reorganize tests (chain, LLM, tool, retriever spans) - Update tox config for langchain testing Change-Id: I7c09df91dadd9d8ea75afba6502f6513fad800ad Co-developed-by: Cursor <noreply@cursor.com> Made-with: Cursor
Change-Id: I426ce98bc9e1eb2ebca2dfee5fbe0211d801809f Co-developed-by: Cursor <noreply@cursor.com>
…marking Add instrumentation for langchain.agents.create_agent (the new home of create_react_agent after LangGraph v1.0 deprecation). The wrapper marks the returned graph with _loongsuite_react_agent=True so that the langgraph Pregel stream/astream wrapper can inject metadata. Change-Id: Ie6a869ab107e0c91a55e6d2d79212f45d9234646 Co-developed-by: Cursor <noreply@cursor.com> Made-with: Cursor
Change-Id: Idff7ed598bdeb7c9846ffcee171abcce3af32994 Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: I78074851d283e36996c94a31bd51a00322f4325e Co-developed-by: Cursor <noreply@cursor.com>
Change-Id: Ie6441ccd1be48d60b3a8740774ad1a290afe8d3f Co-developed-by: Cursor <noreply@cursor.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors loongsuite-instrumentation-langchain to use opentelemetry-util-genai via ExtendedTelemetryHandler and adds ReAct step instrumentation for AgentExecutor and LangGraph agents. The legacy wrapt-based function wrapping is replaced with a BaseTracer callback mechanism, and old tests are removed in favor of a new test suite.
Changes:
- Replaced legacy tracer with
ExtendedTelemetryHandler-basedLoongsuiteTracer, adding support for LLM, Chain, Agent, Tool, Retriever, and ReAct Step spans with GenAI semantic conventions - Added monkey-patching of
AgentExecutor._iter_next_step/_aiter_next_stepfor ReAct step instrumentation, plus LangGraph agent detection via metadata propagation - Rewrote test suite (~107 tests) with shared
conftest.pyfixtures, oldest/latest dependency matrices, and updated CI workflows
Reviewed changes
Copilot reviewed 31 out of 32 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
tox-loongsuite.ini |
Enable langchain tests with oldest/latest matrix |
tests/conftest.py |
Shared pytest fixtures for instrumentation and export |
tests/test_basic.py |
Basic tracer injection and chain span tests |
tests/test_llm_spans.py |
LLM span creation and attribute tests |
tests/test_chain_spans.py |
Chain span creation and content tests |
tests/test_tool_spans.py |
Tool span creation and content tests |
tests/test_retriever_spans.py |
Retriever span creation and content tests |
tests/test_agent_spans.py |
Agent detection unit tests |
tests/test_react_step_patch.py |
ReAct step patch application and span tests |
tests/test_data_extraction.py |
Unit tests for data extraction helpers |
tests/test_instrumentor.py |
Instrumentor lifecycle tests |
tests/test_langchain_instrumentor.py |
Integration tests with RetrievalQA and ChatOpenAI |
tests/requirements.oldest.txt |
Oldest dependency pins for test matrix |
tests/requirements.latest.txt |
Latest dependency pins for test matrix |
internal/semconv.py |
Simplified to re-export from util-genai |
internal/_utils.py |
Refactored data extraction helpers |
internal/_tracer.py |
New LoongsuiteTracer based on BaseTracer and ExtendedTelemetryHandler |
internal/patch.py |
ReAct step monkey-patch for AgentExecutor |
__init__.py |
Instrumentor with agent executor and create_agent patching |
pyproject.toml |
Updated dependencies and metadata |
README.md |
Updated usage docs and traced operations table |
CHANGELOG.md |
Documented breaking changes and new features |
.github/workflows/loongsuite_test_0.yml |
CI test jobs for langchain oldest/latest |
.github/workflows/loongsuite_lint_0.yml |
CI lint job for langchain |
| Deleted old test files | Removed legacy test files under tests/instrumentation/langchain/ |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
instrumentation-loongsuite/loongsuite-instrumentation-langchain/tests/requirements.oldest.txt
Show resolved
Hide resolved
...loongsuite-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/__init__.py
Outdated
Show resolved
Hide resolved
...te-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/internal/_tracer.py
Outdated
Show resolved
Hide resolved
instrumentation-loongsuite/loongsuite-instrumentation-langchain/CHANGELOG.md
Outdated
Show resolved
Hide resolved
instrumentation-loongsuite/loongsuite-instrumentation-langchain/tests/test_retriever_spans.py
Outdated
Show resolved
Hide resolved
...te-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/internal/_tracer.py
Outdated
Show resolved
Hide resolved
...loongsuite-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/__init__.py
Outdated
Show resolved
Hide resolved
...te-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/internal/_tracer.py
Outdated
Show resolved
Hide resolved
| # _start_trace / _end_trace | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def _start_trace(self, run: Run) -> None: |
There was a problem hiding this comment.
不要调父类的_start_trace,如果没有需要的话在这里直接pass,调父类的_start_trace在之前导致过缓慢的内存泄漏。
| self._runs[run.id] = _Run(span=span, context=context) | ||
|
|
||
| def _end_trace(self, run: Run) -> None: | ||
| super()._end_trace(run) |
|
|
||
|
|
||
| def recursive_size(obj: Any, max_size: int = 10240) -> int: | ||
| """递归计算对象大小,超过阈值时快速返回""" |
There was a problem hiding this comment.
这块代码可以删掉了,处理逻辑太复杂了,半年前线上的代码已经注释掉了base64这块的逻辑
Description
Summary
Refactors
loongsuite-instrumentation-langchainto useopentelemetry-util-genaiand adds ReAct step instrumentation for AgentExecutor and LangGraph agents.Key Changes
Architecture
wrapt-based function wrapping with LangChainBaseTracercallback mechanismExtendedTelemetryHandlerfromopentelemetry-util-genaifor standardized GenAI semantic conventionsRLockfor thread safety in tracer callbacksReAct Step Instrumentation
AgentExecutor._iter_next_stepand_aiter_next_stepto instrument each ReAct iterationlangchain.agents(0.x) andlangchain_classic.agents(1.x) when availablegen_ai.span.kind=STEP,gen_ai.operation.name=react,gen_ai.react.round,gen_ai.react.finish_reasonLangGraph ReAct Agent Support
Run.metadata["_loongsuite_react_agent"](injected by langgraph instrumentation)langchain.agents.create_agentto mark ReAct graphs (langchain ≥ 1.x)inside_langgraph_reactpropagationinvoke_agent product_agent) instead of genericinvoke_agent LangGraph"agent"node within the graphOther Improvements
Breaking Changes
Testing & CI
oldest/latestdependency matricesFixes #136 (issue)
Type of change
Please delete options that are not relevant.
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
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.