-
Notifications
You must be signed in to change notification settings - Fork 78
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
Add APIs for getting the current logfire span #675
Open
dmontagu
wants to merge
4
commits into
main
Choose a base branch
from
dmontagu/get-current-logfire-span
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations as _annotations | ||
|
||
from opentelemetry.trace import get_current_span | ||
|
||
from logfire import LogfireSpan | ||
|
||
__all__ = ('current_span', 'current_logfire_span') | ||
|
||
|
||
current_span = get_current_span | ||
|
||
|
||
def current_logfire_span() -> LogfireSpan: | ||
"""Return the LogfireSpan corresponding to the current otel span. | ||
|
||
If the current otel span was not created as a LogfireSpan, we warn and return | ||
something API-compatible which delegates to the otel span as much as possible. | ||
|
||
Note: If we eventually rework the SDK so `opentelemetry.trace.get_current_span` returns a `LogfireSpan`, we should | ||
make this an alias for `current_span` and deprecate this method. There are some good reasons to do that, but there | ||
are also some good reasons not to, such as reducing overhead in calls made by third-party instrumentations. | ||
""" | ||
... |
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
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
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. are we sure this should be public. |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations as _annotations | ||
|
||
import warnings | ||
|
||
from opentelemetry.trace import get_current_span | ||
from opentelemetry.trace.span import Span | ||
|
||
from logfire._internal.config import OPEN_LOGFIRE_SPANS_BY_ID | ||
from logfire._internal.main import LogfireSpan, NoopSpan | ||
|
||
__all__ = ('current_span', 'current_logfire_span') | ||
|
||
|
||
class _BestEffortSpan: | ||
def __init__(self, span: Span): | ||
self.__span = span | ||
self.__noop_span = NoopSpan() | ||
|
||
def __getattr__(self, name: str): | ||
try: | ||
return getattr(self.__span, name) | ||
except AttributeError: | ||
value = getattr(self.__noop_span, name) | ||
# Emit the warning _after_ grabbing the value so we don't emit a warning if an AttributeError will be raised | ||
warnings.warn( | ||
'A logfire-specific attribute is being accessed on a non-logfire span,' | ||
' the value is not meaningful and method calls will not do anything.', | ||
stacklevel=2, | ||
) | ||
return value | ||
|
||
|
||
current_span = get_current_span | ||
|
||
|
||
def current_logfire_span() -> LogfireSpan: | ||
"""Return the LogfireSpan corresponding to the current otel span. | ||
|
||
If the current otel span was not created as a LogfireSpan, we warn and return | ||
something API-compatible which delegates to the otel span as much as possible. | ||
|
||
Note: If we eventually rework the SDK so `opentelemetry.trace.get_current_span` returns a `LogfireSpan`, we should | ||
make this an alias for `current_span` and deprecate this method. There are some good reasons to do that, but there | ||
are also some good reasons not to, such as reducing overhead in calls made by third-party instrumentations. | ||
""" | ||
otel_span = get_current_span() | ||
span_context = otel_span.get_span_context() | ||
logfire_span = OPEN_LOGFIRE_SPANS_BY_ID.get((span_context.trace_id, span_context.span_id)) | ||
if isinstance(logfire_span, LogfireSpan): | ||
return logfire_span | ||
return _BestEffortSpan(otel_span) # type: ignore |
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.
I think it could be confusing or cause weird side effects that the imported
current_span
overrides thecurrent_span
module.