[Python] What causes unicorn to log: ERROR: Exception in ASGI application?

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to look at some logging of a fastapi application that is run with unicorn, when any exception happens it seems to log:

    ERROR: Exception in ASGI application


    Which is causing issues because it seems to happen outside of the ASGI application, so my OTEL trace/span ids are not being included in the log messages and I cant search logs for exceptions by trace id.

    I am trying to try and create a minimal reproducable example of a fastapi app that creates these messages on exceptions, but I am struggling to reproduce it.

    So far I have an app like this:

    from fastapi import FastAPI
    from opentelemetry import trace
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import BatchSpanProcessor
    from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
    from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
    from opentelemetry.sdk.resources import Resource
    import os
    from opentelemetry.instrumentation.requests import RequestsInstrumentor
    from opentelemetry.instrumentation.logging import LoggingInstrumentor
    from fastapi import Request
    import logging


    resource = Resource.create(attributes={"service.name": os.environ["SERVICE_NAME"]})
    tracer_provider = TracerProvider(resource=resource)
    trace.set_tracer_provider(tracer_provider)
    otlp_exporter = OTLPSpanExporter(insecure=True)
    tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))


    app = FastAPI()
    FastAPIInstrumentor().instrument_app(app, tracer_provider=tracer_provider)
    LoggingInstrumentor().instrument(set_logging_format=True)


    @app.get("/")
    async def runtime_error():
    raise RuntimeError("Random error")



    async def exception_handler(request: Request, exc: Exception):
    logging.exception(f"Unhandled exception: {exc}")
    raise exc
    # return JSONResponse(status_code=500, content={"message": "Internal server error"})


    app.add_exception_handler(Exception, exception_handler)


    But that doesnt seem to actually cause any issues with the logging, the logs still have the trace ids and the span ids and there is no Exception in ASGI application.

    When does uvicorn log to it's error logger?

    Continue reading...

Compartilhe esta Página