Tag: fastapi

  • FastAPI Application Performance Monitoring with SigNoz

    What is SigNoz?

    SigNoz stands as an open-source alternative to Datadog or New Relic. It serves as a comprehensive solution for all your observability requirements, encompassing Application Performance Monitoring (APM), logs, metrics, exceptions, alerts, and dashboards, all enhanced by a robust query builder.

    Here is a quick guide to get up and running with FastAPI instrumentation with SigNoz really fast.

    Getting SigNoz

    I prefer using docker compose

    git clone -b main https://github.com/SigNoz/signoz.git && cd signoz/deploy/
    docker compose -f docker/clickhouse-setup/docker-compose.yaml up -d

    Testing SigNoz Installation

    http://localhost:3301

    If all went well, you should see a webpage asking you to create an account (yeah, that is correct)

    Getting test FastAPI App

    Here is a sample FastAPI app with basic instrumentation. I will use the same for this example.

    git clone https://github.com/SigNoz/sample-fastAPI-app.git
    cd sample-fastAPI-app
    docker build -t sample-fastapi-app .
    docker run -d --name fastapi-container \ 
    --net clickhouse-setup_default  \ 
    --link clickhouse-setup_otel-collector_1 \
    -e OTEL_METRICS_EXPORTER='none' \
    -e OTEL_RESOURCE_ATTRIBUTES='service.name=fastapiApp' \
    -e OTEL_EXPORTER_OTLP_ENDPOINT='http://clickhouse-setup_otel-collector_1:4317' \
    -e OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
    -p 5002:5002 sample-fastapi-app

    Generate Traffic Using Locust

    pip install locust
    locust -f locust.py --headless --users 10 --spawn-rate 1 -H http://localhost:5002:5002

    Done!

    Open-source application performance monitoring tools should offer cost efficiency, customization, and transparency. They provide community support, vendor independence, and continuous improvement through collaborative innovation. SigNoz is already very popular with the open-source community, let’s see how does it change the APM landscape.

  • Gunicorn: FastAPI in Production

    1. Gunicorn Configuration

    Gunicorn is a widely used WSGI server for running Python web applications. When deploying FastAPI, Gunicorn is often used in conjunction with Uvicorn to provide a production-ready server.

    Install Gunicorn using:

    pip install gunicorn

    Run Gunicorn with Uvicorn workers:

    gunicorn -k uvicorn.workers.UvicornWorker your_app:app -w 4 -b 0.0.0.0:8000

    Here:

    • -k uvicorn.workers.UvicornWorker specifies the worker class.
    • your_app:app points to your FastAPI application instance.
    • -w 4 sets the number of worker processes. Adjust this based on the available resources and expected load.

    2. Worker Processes

    The -w flag in the Gunicorn command determines the number of worker processes. The optimal number depends on factors like CPU cores, available memory, and the nature of your application.

    For example, on a machine with four CPU cores:

    gunicorn -k uvicorn.workers.UvicornWorker your_app:app -w 4 -b 0.0.0.0:8000

    If your application performs a significant amount of asynchronous I/O operations, you might increase the number of workers. However, keep in mind that too many workers can lead to resource contention.

    3. Load Balancing and Scaling

    In a production setting, deploying multiple instances of your FastAPI application and distributing incoming requests across them is essential for scalability and fault tolerance. The number of worker processes can impact the optimal scaling strategy.

    Consider using tools like nginx for load balancing or deploying your application in a container orchestration system like Kubernetes.

    4. Graceful Shutdown

    Ensure that Gunicorn handles signals gracefully. FastAPI applications may have asynchronous tasks or background jobs that need to complete before shutting down. Gunicorn’s --graceful-timeout option can be set to allow for graceful termination.

    gunicorn -k uvicorn.workers.UvicornWorker your_app:app -w 4 -b 0.0.0.0:8000 --graceful-timeout 60

    This allows Gunicorn to wait up to 60 seconds for workers to finish processing before shutting down.

    In conclusion, the choice of Gunicorn and worker processes is a crucial aspect of deploying FastAPI applications in a production environment. Fine-tuning the number of workers and configuring Gunicorn parameters according to your application’s characteristics and deployment environment ensures optimal performance and scalability.