-
Notifications
You must be signed in to change notification settings - Fork 0
Add caller-supplied invocation_id and 0041 reserved keys #94
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
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Trace-id derivation helpers, SDK-independent. | ||
|
|
||
| Pure functions for mapping an OA ``invocation_id`` to the 32-char hex | ||
| Langfuse ``trace.id``. Imports only stdlib (``uuid``, ``hashlib``), so | ||
| this module is importable without the ``[langfuse]`` extras — | ||
| operators and tooling can compute trace ids without pulling in the | ||
| SDK. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import uuid as _uuid | ||
|
|
||
|
|
||
| def _is_uuid(value: str) -> bool: | ||
| try: | ||
| _uuid.UUID(value) | ||
| except (ValueError, AttributeError): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| # Per observability §8.4.1: Langfuse v4 trace ids are 128-bit values | ||
| # rendered as 32 lowercase hex. A UUID invocation_id maps to its hex | ||
| # form (dashes stripped). A non-UUID maps to the first 16 bytes of | ||
| # SHA-256(invocation_id) as 32 hex (the same derivation as Langfuse's | ||
| # create_trace_id(seed), so a consumer can reproduce it); the raw id is | ||
| # also written to trace.metadata.invocation_id (see the adapter's | ||
| # `trace`) for lookup. | ||
| def _to_otel_trace_id(trace_id: str) -> str: | ||
| """Return the 32-char hex Langfuse trace id for an OA invocation_id.""" | ||
| if _is_uuid(trace_id): | ||
| return _uuid.UUID(trace_id).hex | ||
| return hashlib.sha256(trace_id.encode("utf-8")).digest()[:16].hex() | ||
|
|
||
|
|
||
| def langfuse_trace_id(invocation_id: str) -> str: | ||
| """Return the Langfuse ``trace.id`` for an OA ``invocation_id``. | ||
|
|
||
| Public helper for mapping a logged ``invocation_id`` (a dashed UUID | ||
| or a caller-supplied non-UUID string) to the 32-char hex | ||
| ``trace.id`` Langfuse stores, e.g. to build a direct trace URL. | ||
| """ | ||
| return _to_otel_trace_id(invocation_id) |
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.
Uh oh!
There was an error while loading. Please reload this page.