Skip to content
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

Span.record_exception doesn't use the exception parameter to format the stacktrace #3762

Closed
alexmojaki opened this issue Mar 7, 2024 · 1 comment · Fixed by #3778
Closed
Labels
bug Something isn't working

Comments

@alexmojaki
Copy link

This line:

formats a stacktrace for the currently handled exception, which may be different from the exception passed to the method.

Here's an example of how this can go wrong:

from opentelemetry.sdk.trace import TracerProvider

span = TracerProvider().get_tracer(__name__).start_span('')

try:
    raise ValueError('bad')
except ValueError as e:
    # This happens to work correctly because `e` is the currently handled exception.
    span.record_exception(e)
    print(span.events[0].attributes['exception.stacktrace'])
    """
    Traceback (most recent call last):
      File "...", line ..., in <module>
        raise ValueError('bad')
    ValueError: bad
    """

    exc = e

# This doesn't work because there is no longer an exception being currently handled.
span.record_exception(exc)
print(span.events[1].attributes['exception.stacktrace'])
"""
NoneType: None
"""

try:
    raise TypeError('worse')
except TypeError:
    # Recording an old exception means that the type and message don't match the stacktrace.
    span.record_exception(exc)
    print(span.events[2].attributes['exception.type'])
    """
    ValueError
    """
    print(span.events[2].attributes['exception.stacktrace'])
    """
    Traceback (most recent call last):
      File "...", line ..., in <module>
        raise TypeError('worse')
    TypeError: worse
    """

Replacing the faulty line with this works correctly:

stacktrace = ''.join(traceback.format_exception(type(exception), exception, exception.__traceback__))
@alexmojaki alexmojaki added the bug Something isn't working label Mar 7, 2024
@xrmx
Copy link
Contributor

xrmx commented Mar 8, 2024

Care to open a PR?

xrmx added a commit to xrmx/opentelemetry-python that referenced this issue Mar 13, 2024
Make the stacktrace formatting not rely on being inside the except
scope.

Fixes open-telemetry#3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>
xrmx added a commit to xrmx/opentelemetry-python that referenced this issue Mar 13, 2024
…he except scope

Make the stacktrace formatting not rely on being inside the except
scope.

Fixes open-telemetry#3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>
xrmx added a commit to xrmx/opentelemetry-python that referenced this issue Mar 13, 2024
…he except scope

Make the stacktrace formatting not rely on being inside the except
scope.

Fixes open-telemetry#3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>
xrmx added a commit to xrmx/opentelemetry-python that referenced this issue Mar 14, 2024
…he except scope

Make the stacktrace formatting not rely on being inside the except
scope.

Fixes open-telemetry#3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>
xrmx added a commit to xrmx/opentelemetry-python that referenced this issue Mar 18, 2024
…he except scope

Make the stacktrace formatting not rely on being inside the except
scope.

Fixes open-telemetry#3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>
xrmx added a commit to xrmx/opentelemetry-python that referenced this issue Mar 19, 2024
…he except scope

Make the stacktrace formatting not rely on being inside the except
scope.

Fixes open-telemetry#3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>
ocelotl added a commit that referenced this issue Mar 21, 2024
…ope (#3778)

Make the stacktrace formatting not rely on being inside the except
scope.

Fixes #3762

Suggested-by: Alex Hall <alex.mojaki@gmail.com>

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants