-
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
Remove double record exception #712
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1948,13 +1948,13 @@ def __init__(self, span: trace_api.Span) -> None: | |
self._token = context_api.attach(trace_api.set_span_in_context(self._span)) | ||
|
||
def __enter__(self) -> FastLogfireSpan: | ||
self._span.__enter__() | ||
return self | ||
|
||
@handle_internal_errors() | ||
def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: Any) -> None: | ||
context_api.detach(self._token) | ||
_exit_span(self._span, exc_value) | ||
self._span.end() | ||
self._span.__exit__(exc_type, exc_value, traceback) | ||
|
||
|
||
# Changes to this class may need to be reflected in `FastLogfireSpan` and `NoopSpan` as well. | ||
|
@@ -1990,6 +1990,7 @@ def __enter__(self) -> LogfireSpan: | |
attributes=self._otlp_attributes, | ||
links=self._links, | ||
) | ||
self._span.__enter__() | ||
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. Same note as above, this might not be necessary but seems fine to add and arguably more correct(?) |
||
if self._token is None: # pragma: no branch | ||
self._token = context_api.attach(trace_api.set_span_in_context(self._span)) | ||
|
||
|
@@ -1999,14 +2000,17 @@ def __enter__(self) -> LogfireSpan: | |
def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: Any) -> None: | ||
if self._token is None: # pragma: no cover | ||
return | ||
assert self._span is not None | ||
|
||
context_api.detach(self._token) | ||
self._token = None | ||
|
||
assert self._span is not None | ||
_exit_span(self._span, exc_value) | ||
|
||
self.end() | ||
if self._span.is_recording(): | ||
with handle_internal_errors(): | ||
if self._added_attributes: | ||
self._span.set_attribute( | ||
ATTRIBUTES_JSON_SCHEMA_KEY, attributes_json_schema(self._json_schema_properties) | ||
) | ||
Comment on lines
+2007
to
+2012
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 just moved the logic from |
||
self._span.__exit__(exc_type, exc_value, traceback) | ||
|
||
@property | ||
def message_template(self) -> str | None: # pragma: no cover | ||
|
@@ -2032,26 +2036,6 @@ def message(self) -> str: | |
def message(self, message: str): | ||
self._set_attribute(ATTRIBUTES_MESSAGE_KEY, message) | ||
|
||
def end(self, end_time: int | None = None) -> None: | ||
"""Sets the current time as the span's end time. | ||
|
||
The span's end time is the wall time at which the operation finished. | ||
|
||
Only the first call to this method is recorded, further calls are ignored so you | ||
can call this within the span's context manager to end it before the context manager | ||
exits. | ||
""" | ||
if self._span is None: # pragma: no cover | ||
raise RuntimeError('Span has not been started') | ||
if self._span.is_recording(): | ||
with handle_internal_errors(): | ||
if self._added_attributes: | ||
self._span.set_attribute( | ||
ATTRIBUTES_JSON_SCHEMA_KEY, attributes_json_schema(self._json_schema_properties) | ||
) | ||
|
||
self._span.end(end_time) | ||
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. We used to only call I can go back to only calling 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. import opentelemetry.trace
import logfire
logfire.configure()
tracer = opentelemetry.trace.get_tracer(__name__)
span = tracer.start_span('foo')
assert span.is_recording()
span.end()
assert not span.is_recording()
span.end() # Logs a warning: Calling end() on an ended span. So keeping the current behaviour seems best. |
||
|
||
@handle_internal_errors() | ||
def set_attribute(self, key: str, value: Any) -> None: | ||
"""Sets an attribute on the span. | ||
|
@@ -2183,16 +2167,6 @@ def is_recording(self) -> bool: | |
return False | ||
|
||
|
||
def _exit_span(span: trace_api.Span, exception: BaseException | None) -> None: | ||
if not span.is_recording(): | ||
return | ||
|
||
# record exception if present | ||
# isinstance is to ignore BaseException | ||
if isinstance(exception, Exception): | ||
record_exception(span, exception, escaped=True) | ||
|
||
|
||
AttributesValueType = TypeVar('AttributesValueType', bound=Union[Any, otel_types.AttributeValue]) | ||
|
||
|
||
|
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.
This is maybe unnecessary but seems reasonable to add, and I hope this class will be refactored/simplified soon anyway.
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.
Given how optimised this class is, I'd rather not add it here, but maybe it can stay in LogfireSpan.