Giter Site home page Giter Site logo

Comments (4)

quiqueg avatar quiqueg commented on June 14, 2024 2

@sats268842 Do you have any middleware installed that are catching exceptions and not re-raising them? I was running into the same issue, and it was due to this middleware: https://gist.github.com/nymous/f138c7f06062b7c43c060bf03759c29e, which does something like:

response = Response(status_code=500)
try:
    response = await call_next(request)
except Exception:
    # do something
    raise
finally:
    # do something else
    return response

The problem was specifically with the return response inside of the finally blockβ€”this swallows the exception, so ddtrace's TracingMiddleware never receives the exception and is therefore never able to add it to the trace.

I fixed it by moving the return response to outside of the finally block, and being OK with not returning my own Response(status_code=500) from this middleware. I'm handling it in an exception handler instead:

@app.exception_handler(Exception)
async def handle_exceptions(exc: Exception):
    return Response(status_code=500)

from dd-trace-py.

emmettbutler avatar emmettbutler commented on June 14, 2024

Thanks for the report @sats268842. We'll see if we can put together a demo app that reproduces this behavior. If you have one you can share, that would be helpful.

from dd-trace-py.

sats268842 avatar sats268842 commented on June 14, 2024

@emmettbutler Additionally, I've discovered that if you install both the auto instrumentation library injection and the ddtrace Python package, the tracing and metrics won't work. Also I will look into demo app, if time permits.

from dd-trace-py.

sats268842 avatar sats268842 commented on June 14, 2024

@emmettbutler Also got workaround for this issue using below code

from ddtrace.opentelemetry import TracerProvider
from opentelemetry.trace import set_tracer_provider
set_tracer_provider(TracerProvider())


@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc):
    import traceback
    current_span = trace.get_current_span()
    if (current_span is not None) and (current_span.is_recording()):
        print(exc.__class__.__name__)
        exc_type, exc_value, exc_traceback = sys.exc_info()
        filename, line_number, function_name, code = traceback.extract_tb(exc_traceback)[-1]
        attributes = {
            "error.message": str(exc.detail),
            "error.kind": exc.__class__.__name__,
            "error.type": exc.__class__.__name__,
            "error.stack": traceback.format_exc(),
            "error.file": filename,
            "error.line": line_number,
            "error.function": function_name,
            "http.status_code": exc.status_code,
        }
        current_span.set_status(StatusCode.ERROR, str(exc.detail))
        current_span.set_attributes(attributes)

    return ORJSONResponse(({"detail": str(exc.detail)}), status_code=exc.status_code)

from dd-trace-py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.