-
Notifications
You must be signed in to change notification settings - Fork 842
fix(langchain): allow configuration of metadata key prefix #3367
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2aab614
feat(langchain): allow configuration of metadata key prefix
ianchi bd21607
fix(langchain): set error status message on span
ianchi fce3f53
fix(langchain): invalid semconv attribute
ianchi 565db56
test(langchain): restore tests
ianchi 46fe7c8
fix(langchain): update tests to expect AI choice events
nirga 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 hidden or 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 hidden or 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
2 changes: 2 additions & 0 deletions
2
...opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/config.py
This file contains hidden or 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,9 +1,11 @@ | ||
| from typing import Optional | ||
|
|
||
| from opentelemetry._logs import Logger | ||
| from opentelemetry.semconv_ai import SpanAttributes | ||
|
|
||
|
|
||
| class Config: | ||
| exception_logger = None | ||
| use_legacy_attributes = True | ||
| event_logger: Optional[Logger] = None | ||
| metadata_key_prefix: str = SpanAttributes.TRACELOOP_ASSOCIATION_PROPERTIES |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
🧩 Analysis chain
Potential issue: Class variable causes instance configuration conflict.
Config.metadata_key_prefixis a class variable shared across all instances. If multipleLangchainInstrumentorinstances are created with differentmetadata_key_prefixvalues, they will overwrite each other, causing unexpected behavior where one instance's configuration affects another's.Example scenario:
Consider whether:
metadata_key_prefixas an instance variable and pass it to the callback handlerRun this script to check if the codebase or documentation discusses multiple instrumentor instances:
🏁 Script executed:
Length of output: 2500
I need to examine the Config class and understand how
metadata_key_prefixis actually used to verify if the class variable issue poses a real problem.Now I need to examine the actual code to understand how
Config.metadata_key_prefixis used and whether the class variable design actually causes problems.🏁 Script executed:
Length of output: 1512
I need to examine how the callback handler is instantiated and whether it captures
metadata_key_prefixat creation time or reads it dynamically.🏁 Script executed:
Length of output: 3660
Now let me check the README to understand the intended usage pattern and whether multiple instances are documented as supported or unsupported.
🏁 Script executed:
Length of output: 1180
Store
metadata_key_prefixas an instance variable to prevent configuration conflicts.The issue is confirmed:
Config.metadata_key_prefixis a class variable read dynamically by the callback handler at span creation time (callback_handler.py line 294). MultipleLangchainInstrumentorinstances with differentmetadata_key_prefixvalues will overwrite each other's configuration via line 55 in__init__.py.Currently, the codebase and README show only single-instance usage (
LangchainInstrumentor().instrument()), and no concurrent multi-instance scenarios exist. However, this design is a latent footgun: if future code creates multiple instances with different prefixes, spans will silently use the wrong prefix.Recommended fix: Pass
metadata_key_prefixtoTraceloopCallbackHandlerat instantiation (line 89) and have it store/use the instance-specific value instead of reading from the sharedConfigclass variable. Alternatively, document explicitly that only oneLangchainInstrumentorinstance should be active per process.🤖 Prompt for AI Agents