-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add get_chain_root_span
utility for langchain instrumentation
#1054
Merged
Merged
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
c200b4e
Use ContextVars to track langchain root spans
anticorrelator a6d6702
Do not explicitly manage a stack
anticorrelator 1f43388
Ruff 🐶
anticorrelator 9734103
Use separate reset token store
anticorrelator 298318a
Use better contextvar type annotation
anticorrelator 29263e1
Flesh out type annotation for Token object
anticorrelator 1af67a4
Add langchain instrumentor tests
anticorrelator 89a0d45
Use better type annotation
anticorrelator a0b14b4
Refactor tests
anticorrelator 3ddc798
Refactor root chain span propagation
anticorrelator 50ffb5a
Ruff 🐶
anticorrelator 45d1a0e
Update tests
anticorrelator 39bd069
Fix type annotations
anticorrelator 141ee9f
Remove unused type: ignore
anticorrelator 6c02b33
Simplify test to not use sequences
anticorrelator 965ce0f
Explicitly define type annotations for RunnableLambda
anticorrelator e1609ac
Use a RunnableSequence for more robust testing
anticorrelator aea1ae7
Remove root span tracking from Span attributes
anticorrelator 71a77b7
Track span tree manually
anticorrelator dd41d7c
Remove references to extra span attribute
anticorrelator 73a40b8
Remove redundant logic
anticorrelator 5880442
Properly test concurrency
anticorrelator ca6d3ba
Remove unused variable
anticorrelator 30ddc8f
Test root chain tree walking
anticorrelator 5bcc238
Get all ancestors
anticorrelator 611ed88
Only use run map
anticorrelator 7a35bec
Remove old bookkeeping
anticorrelator 6b82c94
Fix type annotations
anticorrelator a3e45e3
Add docstring
anticorrelator 26dbe41
Ignore unused ignores
anticorrelator 4a9e7b2
Update return types for consistency
anticorrelator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import logging | ||
from typing import TYPE_CHECKING, Any, Callable, Collection, Optional | ||
from typing import TYPE_CHECKING, Any, Callable, Collection, List, Optional | ||
from uuid import UUID | ||
|
||
from opentelemetry import trace as trace_api | ||
|
@@ -64,6 +64,26 @@ def _uninstrument(self, **kwargs: Any) -> None: | |
def get_span(self, run_id: UUID) -> Optional[Span]: | ||
return self._tracer.get_span(run_id) if self._tracer else None | ||
|
||
def get_ancestors(self, run_id: UUID) -> Optional[List[Span]]: | ||
ancestors = [] | ||
tracer = self._tracer | ||
assert tracer | ||
|
||
run = tracer.run_map.get(str(run_id)) | ||
if not run: | ||
return None | ||
|
||
run_id = run.parent_run_id # start with the first ancestor | ||
|
||
while run_id: | ||
span = self.get_span(run_id) | ||
if span: | ||
ancestors.append(span) | ||
|
||
run = tracer.run_map.get(str(run_id)) | ||
run_id = run.parent_run_id | ||
return ancestors if ancestors else None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for the simplicity of the typing (and maybe just my FP brain) - I think an empty array might be preferable to represent "none"? |
||
|
||
|
||
class _BaseCallbackManagerInit: | ||
__slots__ = ("_tracer",) | ||
|
@@ -104,3 +124,20 @@ def get_current_span() -> Optional[Span]: | |
if not run_id: | ||
return None | ||
return LangChainInstrumentor().get_span(run_id) | ||
|
||
|
||
def get_ancestor_spans() -> Optional[List[Span]]: | ||
import langchain_core | ||
|
||
run_id: Optional[UUID] = None | ||
config = langchain_core.runnables.config.var_child_runnable_config.get() | ||
if not isinstance(config, dict): | ||
return None | ||
for v in config.values(): | ||
if not isinstance(v, langchain_core.callbacks.BaseCallbackManager): | ||
continue | ||
if run_id := v.parent_run_id: | ||
break | ||
if not run_id: | ||
return None | ||
mikeldking marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return LangChainInstrumentor().get_ancestors(run_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to be explicit about the return type