question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

trace.id is not available in fastapi exception handler

See original GitHub issue

Describe the bug: We have fastapi framework, and we add apm_client for starlette to the application, we want to generate trace.id to correlate logs when an exception happens. I have structlog package for the logging and import elasticapm.handlers.structlog.structlog_processor. We set exception handlers in fastapi application for our excpected handlers and also one for Exception class to capture the ones that we do not expect. I can get trace.id in child exceptions but not in the exception handler that is comparing to root.

My goal was to have log Exception types and have log correlation in them.

I am not sure if it is bug or feature request🤷‍♂️ To Reproduce

  1. create apm agent and fastapi app, add the apm agent to application
from fastapi import FastAPI
from elasticapm.contrib.starlette import make_apm_client
from elasticapm.contrib.starlette import ElasticAPM
apm = make_apm_client(config=config)
app = FastAPI()
app.add_middleware(ElasticAPM, client=apm)
  1. configure structlog
import structlog
from elasticapm.handlers.structlog import structlog_processor
def processor(logger, level, log_dict):
    return json.dumps(log_dict)

structlog.configure(processors=[structlog_processor, processor])

  1. create an child exceptions and root and child exception
class AuthException(Exception):
    def __ini__(self):
        super().__init__()


@app.exception_handler(Exception)
async def fallback_handler(request , exc: Exception):
    getLogger().info("Uncaught exception during request handling happend")
    return JSONResponse({"detail": "ROOT EXCEPTION"}, status_code=500)

@app.exception_handler(AuthException)
async def validation_exception_handler(request, exc: AuthException):
    getLogger().info(f"Request Validation exception happend{sys.exc_info()}")
    return JSONResponse({"detail": "CHILD EXCEPTION"}, status_code=404)
  1. create an endpoint to send request to see if logging formatter/processor works or not
@app.post("/")
async def post(body: Any = Body(...)):
    if body == 1: raise AuthException("child exception")
    if body == 2: raise ZeroDivisionError("root exception")
    return JSONResponse({"Response": "test SUCCESS"}, status_code=200)

Environment (please complete the following information)

  • OS: Win and Linux
  • Python version: 3.7
  • Framework and version [e.g. Django 2.1]: fastapi 0.63
  • APM Server version: _
  • Agent version: 6.*

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
basepicommented, Oct 19, 2022

I have a POC in #1674 but it’s not working yet. My code isn’t being hit, still trying to figure out why.

2reactions
basepicommented, Oct 18, 2022

@magichair I’m not sure how we missed this in our 8.6 planning, but I’m going to take a look. 😃

Edit: Oh, wait, just saw your edit. That sounds plausible. I’m still going to look into this, though, we should support handling the base Exception.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Errors - FastAPI
FastAPI has some default exception handlers. These handlers are in charge of returning the default JSON responses when you raise an HTTPException and...
Read more >
Catch `Exception` globally in FastAPI - Stack Overflow
I found a way to catch exceptions without the "Exception in ASGI application_" by using a middleware. Not sure if this has some...
Read more >
3 Ways to Handle Errors in FastAPI That You Need to Know
On the other hand, “detail” is a message that will show in the request's response. Here is a bigger example of how the...
Read more >
tiangolo/fastapi - Gitter
from fastapi import FastAPI, Depends from starlette.responses import JSONResponse app = FastAPI() class CustomException(Exception): pass def ...
Read more >
Not getting exception traceback : r/FastAPI - Reddit
But whenever there is a problem I am using : try… ... debug but still I am not seeing any traceback/helpful message, All...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found