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

Error Tracking in ASGI (Hypercorn) Middleware with Connexion + Flask #11301

Open
h0rv opened this issue Nov 5, 2024 · 0 comments
Open

Error Tracking in ASGI (Hypercorn) Middleware with Connexion + Flask #11301

h0rv opened this issue Nov 5, 2024 · 0 comments

Comments

@h0rv
Copy link

h0rv commented Nov 5, 2024

I am encountering an issue with error tracking in a Connexion + Flask application using Hypercorn for ASGI, and I haven’t been able to resolve it.

Setup:

I initialize my app with ddtrace.auto and apply the ASGI middleware as shown below:

import ddtrace.auto
from ddtrace.contrib.asgi import TraceMiddleware
from main import init_app

connexion_app = init_app()
app = TraceMiddleware(connexion_app)

I also tried:

  • running ddtrace hypercorn ... instead of import ddtrace.auto, but the outcome is the same.
  • running with just import ddtrace.auto and no TraceMiddleware, but this resulted in context errors.

Problem:

  • In the generated flamegraphs, the top-level asgi.request span does not register as an error, even when exceptions are raised.
  • For errors to appear in error tracking, I need the root (top-level) span to reflect the error status.
  • Ideally, I’d like to have flask.request spans as the root spans for error tracking alongside asgi spans, if possible.

Here’s an example of the current flamegraph output (error span is missing at the top level):

Image

Attempted Solution:

I tried creating a custom exception handler to mark the root span as an error. However, it didn’t work as expected, and the logging statement in my handler wasn’t triggered (suggesting the handler was never called):

def custom_handle_exception_span(exc, span):
    import traceback
    from ddtrace.ext import http
    from ddtrace.constants import ERROR_MSG, ERROR_STACK, ERROR_TYPE
    from ddtrace import config

    logging.info(f"Using custom handle_exception_span for exception: {exc}")

    span.set_tag(http.STATUS_CODE, 500)
    span.error = 1
    span.set_tag_str(ERROR_TYPE, f"{exc.__class__.__module__}.{exc.__class__.__name__}")
    span.set_tag_str(ERROR_MSG, str(exc))
    span.set_tag_str(ERROR_STACK, "".join(traceback.format_exception(
        type(exc), exc, exc.__traceback__, limit=config._span_traceback_max_size
    )))

connexion_app = init_app()
app = TraceMiddleware(connexion_app, handle_exception_span=custom_handle_exception_span)

Question:

Am I missing something in my setup, or is this a known limitation of the ASGI middleware? My end goal is to enable “Error Tracking” for this Flask app. Any guidance would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant