Skip to content

Commit ca63521

Browse files
feat: add support for langchain embedding
1 parent 2917bdb commit ca63521

5 files changed

Lines changed: 928 additions & 0 deletions

File tree

instrumentation-loongsuite/loongsuite-instrumentation-langchain/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Langchain embedding span support([#155](https://github.com/alibaba/loongsuite-python-agent/pull/155))
1213
- Rerank / document-compressor span support
1314
([#149](https://github.com/alibaba/loongsuite-python-agent/pull/149))
1415

instrumentation-loongsuite/loongsuite-instrumentation-langchain/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ loongsuite-instrument <your_run_command>
130130
| ReAct Step | `STEP` | `gen_ai.operation.name=react`, `gen_ai.react.round`, `gen_ai.react.finish_reason` |
131131
| Tool | `TOOL` | `gen_ai.operation.name=execute_tool` |
132132
| Retriever | `RETRIEVER` | `gen_ai.operation.name=retrieval` |
133+
| Embedding | `EMBEDDING` | `gen_ai.operation.name=embeddings`, `gen_ai.request.model`, `gen_ai.provider.name`, `server.address`, `server.port`, `gen_ai.embeddings.dimension.count`, `gen_ai.request.encoding_formats` |
133134
| Reranker | `RERANKER` | `gen_ai.operation.name=rerank_documents`, `gen_ai.request.model`, `gen_ai.rerank.documents.count`, `gen_ai.request.top_k`, `gen_ai.rerank.input_documents`, `gen_ai.rerank.output_documents` (when content capture enabled) |
134135

135136
ReAct Step spans are created for each Reasoning-Acting iteration, with the hierarchy: Agent > ReAct Step > LLM/Tool. Supported agent types:

instrumentation-loongsuite/loongsuite-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ def _uninstrument_create_agent() -> None:
208208
_patched_create_agent_locations.clear()
209209

210210

211+
# ------------------------------------------------------------------
212+
# Embeddings patch
213+
# ------------------------------------------------------------------
214+
215+
216+
def _instrument_embeddings(handler: Any) -> None:
217+
"""Wrap all current and future ``Embeddings`` subclasses."""
218+
from opentelemetry.instrumentation.langchain.internal.patch_embedding import ( # noqa: PLC0415
219+
instrument_embeddings,
220+
)
221+
222+
instrument_embeddings(handler)
223+
224+
225+
def _uninstrument_embeddings() -> None:
226+
"""Restore original ``Embeddings`` methods."""
227+
from opentelemetry.instrumentation.langchain.internal.patch_embedding import ( # noqa: PLC0415
228+
uninstrument_embeddings,
229+
)
230+
231+
uninstrument_embeddings()
232+
233+
211234
# ------------------------------------------------------------------
212235
# BaseDocumentCompressor patch (rerank / compression)
213236
# ------------------------------------------------------------------
@@ -268,6 +291,7 @@ def _instrument(self, **kwargs: Any) -> None:
268291
_instrument_agent_executor()
269292
_instrument_create_agent()
270293
_instrument_document_compressor(handler)
294+
_instrument_embeddings(handler)
271295

272296
def _uninstrument(self, **kwargs: Any) -> None:
273297
try:
@@ -281,6 +305,7 @@ def _uninstrument(self, **kwargs: Any) -> None:
281305
_uninstrument_agent_executor()
282306
_uninstrument_create_agent()
283307
_uninstrument_document_compressor()
308+
_uninstrument_embeddings()
284309

285310

286311
class _BaseCallbackManagerInit:

0 commit comments

Comments
 (0)