python-3.xhaproxyfastapiuvicorn

How do I capture X-Forwarded-For with FastAPI logging?


I am running FastApi with guvicorn in a function like this:

if __name__ == "__main__":

    uvicorn.run(
        app="app.main:app",
        host="HOSTIP",
        port=8000,
        reload=True,
        # log_config=None,
        log_config=log_config,
        log_level="info"
    )

This is what my log_config looks like:

log_config = {
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "default": {
            "()": "uvicorn.logging.DefaultFormatter",
            "fmt": "%(asctime)s::%(levelname)s::%(name)s::%(filename)s::%(funcName)s - %(message)s",
            "use_colors": None,
        },
        "access": {
            "()": "uvicorn.logging.AccessFormatter",
            "fmt": '%(asctime)s::%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s',
        },
    },
    "handlers":
    {
        "default":
        {
            "formatter": "default",
            # "class": 'logging.NullHandler',
            "class": 'logging.FileHandler',
            "filename": CONFIG[SECTION]["default"]
        },
        "error":
        {
            "formatter": "default",
            # "class": 'logging.NullHandler',
            "class": 'logging.FileHandler',
            "filename": CONFIG[SECTION]["error"]
        },
        "access":
        {
            "formatter": "access",
            # "class": 'logging.NullHandler',
            "class": 'logging.FileHandler',
            "filename": CONFIG[SECTION]["access"]
        },
    },
    "loggers":
    {
        "uvicorn": {"handlers": ["default"], "level": "INFO", "propagate": False},
        "uvicorn.error": {"handlers": ["error"], "level": "ERROR", "propagate": False},
        "uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False},
    }
}

I have 2 instances of fastapi on 2 servers, running behind haproxy. I was able to put in this option in haproxy to fwd client IP to my API:

option forwardfor

I am able to confirm with TCPDUMP on one of the API servers that I am infact getting some x-fwd headers coming in:

[user@server ~]# tcpdump -i INTERFACE host SERVERIP -AAA | grep -i IP OF MY LAPTOP
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on INTERFACE, link-type EN10MB (Ethernet), capture size 262144 bytes
*X-Forwarded-For: IP OF MY LAPTOP*

But in my logs, I only see the IP of the vip that the requests hit, even though HAproxy is fwding the IP of client, I am not able to log it.

Is there is a custom variable I can use for the log_config access section?

Thanks.


Solution

  • You can use proxy_headers=True to populate remote address info:

    From the documentation:

    --proxy-headers / --no-proxy-headers
                                      Enable/Disable X-Forwarded-Proto,
                                      X-Forwarded-For, X-Forwarded-Port to
                                      populate remote address info.
    

    https://www.uvicorn.org/deployment/#running-from-the-command-line