-
Notifications
You must be signed in to change notification settings - Fork 709
Set status in start_as_current_span too #381
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
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 |
---|---|---|
|
@@ -538,6 +538,23 @@ def use_span( | |
self.source._current_span_slot.set( # pylint:disable=protected-access | ||
span_snapshot | ||
) | ||
|
||
except Exception as error: # pylint: disable=broad-except | ||
if ( | ||
span.status is None | ||
and span._set_status_on_exception # pylint:disable=protected-access # noqa | ||
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. Maybe you covered this in #297, but why not make 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. It can be made public, that would mean that we accept changing this behavior even after the span was created while specifically telling it to set or not its status on an exception being raised. Maybe we want to handle the exception in the code that surrounds the span and not bother setting a status if the exception is raised? 🤷♂️ This was specifically requested in the issue. 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. That's something I missed on the last review. We should not be capturing the exception here! try:
with tracer.start_as_current_span("foo") as span:
raise ValueError()
except ValueError as ex:
print("exception handling in the app")
else:
print("uh oh") # our context manager eats the error! 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. Responding to the other comments:
Or make it a read-only public property (which is easier in the SDK than the API).
I wonder if we want this to be a global setting instead of per-span. @Oberon00 what did you have in mind here? 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.
Good catch, I have committed a fix. 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. Thanks, looks good! 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 did have the per-span setting as it is now in mind. But thinking about it again, explicitly setting the status to OK does the same thing, so this might not be necessary. @c24t , @ocelotl: I was confused why we shouldn't capture (in my mind = record) the exception here, until I realized that you meant that we swallow the exception. 😄 |
||
): | ||
span.set_status( | ||
Status( | ||
canonical_code=StatusCanonicalCode.UNKNOWN, | ||
description="{}: {}".format( | ||
type(error).__name__, error | ||
), | ||
) | ||
) | ||
|
||
raise | ||
|
||
finally: | ||
if end_on_exit: | ||
span.end() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -641,16 +641,26 @@ def test_ended_span(self): | |
) | ||
|
||
def test_error_status(self): | ||
try: | ||
with trace.TracerSource().get_tracer(__name__).start_span( | ||
"root" | ||
) as root: | ||
raise Exception("unknown") | ||
except Exception: # pylint: disable=broad-except | ||
pass | ||
|
||
self.assertIs(root.status.canonical_code, StatusCanonicalCode.UNKNOWN) | ||
self.assertEqual(root.status.description, "Exception: unknown") | ||
def error_status_test(context): | ||
with self.assertRaises(AssertionError): | ||
with context as root: | ||
raise AssertionError("unknown") | ||
|
||
self.assertIs( | ||
root.status.canonical_code, StatusCanonicalCode.UNKNOWN | ||
) | ||
self.assertEqual( | ||
root.status.description, "AssertionError: unknown" | ||
) | ||
|
||
error_status_test( | ||
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. This should check that the exception escapes with Also FWIW I think this would be clearer without E.g. def test_error_status(self):
tracer = trace.TracerSource().get_tracer(__name__)
ex = Exception("unknown")
with self.assertRaises(Exception) as ec:
with tracer.start_span("root") as span:
raise ex
self.assertIs(ex, ec.exception)
self.assertIs(
span.status.canonical_code, StatusCanonicalCode.UNKNOWN
)
self.assertEqual(span.status.description, "Exception: unknown")
self.assertFalse(span.status.is_ok)
with self.assertRaises(Exception) as ec:
with tracer.start_as_current_span("root") as span:
raise ex
self.assertIs(ex, ec.exception)
self.assertIs(
span.status.canonical_code, StatusCanonicalCode.UNKNOWN
)
self.assertEqual(span.status.description, "Exception: unknown")
self.assertFalse(span.status.is_ok) 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. This is not only a matter of being DRY, there is something much more important. What the test does as it is now is that it quickly tells the reader another purpose of this test: we are testing that |
||
trace.TracerSource().get_tracer(__name__).start_span("root") | ||
) | ||
error_status_test( | ||
trace.TracerSource() | ||
.get_tracer(__name__) | ||
.start_as_current_span("root") | ||
) | ||
|
||
|
||
def span_event_start_fmt(span_processor_name, span_name): | ||
|
Uh oh!
There was an error while loading. Please reload this page.