On this page

FastAPI Observability: Logging, Metrics, Tracing & Grafana

Published on August 17, 2026

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Observability answers what a running system is doing from the outside, by examining its outputs. A fully observable FastAPI application generates three types of output — logs, metrics, and traces — and routes them to tools that make the data queryable and actionable. Logs answer what happened — discrete timestamped events with context. Metrics answer how much and how often — numeric measurements aggregated over time. Traces answer where the time went — the path a single request took through the system, broken into timed spans. This guide builds a complete observability stack for a FastAPI application: structured JSON logging with python-json-logger, automatic and custom Prometheus metrics via prometheus-fastapi-instrumentator and prometheus_client, OpenTelemetry distributed tracing with opentelemetry-instrumentation-fastapi, and a Grafana dashboard — all served by Docker Compose.


Installing Dependencies​

The following packages provide the complete observability stack:


pip install \

  python-json-logger \

  prometheus-client \

  prometheus-fastapi-instrumentator \

  opentelemetry-api \

  opentelemetry-sdk \

  opentelemetry-instrumentation-fastapi \

  opentelemetry-exporter-otlp-proto-grpc

python-json-logger formats log output as JSON objects. prometheus-fastapi-instrumentator automatically instruments every route with request count, latency, and status code metrics. opentelemetry-instrumentation-fastapi creates a trace span for every HTTP request.


Pillar 1: Structured Logging​

logging.basicConfig() is not appropriate for production FastAPI applications. It configures the root logger globally and can interfere with Uvicorn's own logging infrastructure. The correct approach is to configure a named application logger with its own handler, set logger.propagate = False to prevent double-logging, and format log output as JSON — making logs machine‑parseable and directly ingestible by log aggregators like Grafana Loki, Elasticsearch, or Datadog.

đź“„ core/logging_config.py


import logging

import sys

from pythonjsonlogger.json import JsonFormatter



def configure_logging() -> logging.Logger:

    logger = logging.getLogger("taskmanager")

    logger.setLevel(logging.INFO)



    if not logger.handlers:

        handler = logging.StreamHandler(sys.stdout)

        formatter = JsonFormatter(

            fmt="%(asctime)s %(levelname)s %(name)s %(message)s",

            datefmt="%Y-%m-%dT%H:%M:%S",

        )

        handler.setFormatter(formatter)

        logger.addHandler(handler)



    logger.propagate = False   # prevent double-logging via the root logger

    return logger



logger = configure_logging()

With JsonFormatter, every log line is a valid JSON object:


{

  "asctime": "2026-01-15T10:30:01",

  "levelname": "INFO",

  "name": "taskmanager",

  "message": "Task created",

  "task_id": 42,

  "user": "alice"

}

Import logger from this module wherever logging is needed:


from core.logging_config import logger



logger.info("Task created", extra={"task_id": created.id, "user": user.username})

logger.error("Database query failed", extra={"error": str(exc), "task_id": task_id})

The extra dictionary appends arbitrary key‑value pairs to the JSON output, making logs filterable by task_id, user, or any other field in a log aggregator.


Pillar 2: Metrics with Prometheus​

Prometheus is a pull‑based monitoring system. Rather than the application pushing data, Prometheus periodically sends an HTTP GET request to the application's /metrics endpoint and reads the response in the Prometheus exposition format — a standardised text format listing every metric name, its labels, and its current value.

Automatic instrumentation​

prometheus-fastapi-instrumentator wraps every route handler and records request count, latency, and status codes automatically, with no per‑route code:


from prometheus_fastapi_instrumentator import Instrumentator



instrumentator = Instrumentator()

The instrumentator must be initialised at module level — so it exists when the app is created — and activated inside the lifespan context manager:

đź“„ main.py


from contextlib import asynccontextmanager

from fastapi import FastAPI

from prometheus_fastapi_instrumentator import Instrumentator

from core.logging_config import logger



instrumentator = Instrumentator()



@asynccontextmanager

async def lifespan(app: FastAPI):

    instrumentator.instrument(app).expose(app, endpoint="/metrics")

    logger.info("Application started — metrics exposed at /metrics")

    yield

    logger.info("Application shutting down")



app = FastAPI(lifespan=lifespan)

After startup, GET /metrics returns the Prometheus exposition format. The defaults provide: a Counter for total requests with handler, status, and method labels; a Summary for request and response size; and a Histogram for request duration. The library automatically groups status codes into 2xx, 3xx, and so on.

Custom metrics​

Automatic metrics cover HTTP‑level data. Business‑level metrics — tasks created, authentication failures, database query durations — require manual instrumentation using Counter and Histogram from the prometheus_client library:

đź“„ core/metrics.py


from prometheus_client import Counter, Histogram



# Counter — monotonically increasing. Resets only on application restart.

TASKS_CREATED = Counter(

    "tasks_created_total",

    "Total number of tasks created",

    ["user_role"],           # label: differentiate by user role

)



AUTH_FAILURES = Counter(

    "auth_failures_total",

    "Total number of failed authentication attempts",

    ["reason"],              # label: "invalid_password", "expired_token", etc.

)



# Histogram — records distribution of values, used for latency and sizes.

# Buckets define the upper boundaries of the measurement ranges.

DB_QUERY_DURATION = Histogram(

    "db_query_duration_seconds",

    "Time spent executing database queries",

    ["operation"],           # label: "select", "insert", "update", "delete"

    buckets=[0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0],

)

A Counter is a number that only ever goes up. It is suitable for totals: requests served, errors raised, tasks created. A Histogram records how many observations fell into each of a set of predefined buckets. It is suitable for distributions: latency, file sizes, queue depths.

Use these metrics in service functions:


from core.metrics import TASKS_CREATED, DB_QUERY_DURATION

import time



def create_task(task: Task, session: Session, user: User) -> Task:

    start = time.perf_counter()



    session.add(task)

    session.commit()

    session.refresh(task)



    DB_QUERY_DURATION.labels(operation="insert").observe(

        time.perf_counter() - start

    )

    TASKS_CREATED.labels(user_role=user.role).inc()



    return task

Per‑request timer middleware​

For measuring the wall‑clock time of complete requests including response serialisation — which the service‑level timer does not capture — add a middleware:


import time

from fastapi import Request



@app.middleware("http")

async def request_timer_middleware(request: Request, call_next):

    start = time.perf_counter()

    response = await call_next(request)

    duration = time.perf_counter() - start

    logger.info(

        "Request completed",

        extra={

            "method":      request.method,

            "path":        request.url.path,

            "status_code": response.status_code,

            "duration_s":  round(duration, 4),

        },

    )

    return response

Pillar 3: Distributed Tracing with OpenTelemetry​

Traces consist of spans — named, timed units of work. FastAPIInstrumentor.instrument_app(app) creates one root span per HTTP request automatically. You add child spans manually for database queries, external HTTP calls, and any sub‑operation worth measuring individually.

đź“„ core/tracing.py


from opentelemetry import trace

from opentelemetry.sdk.trace import TracerProvider

from opentelemetry.sdk.trace.export import BatchSpanProcessor

from opentelemetry.sdk.resources import Resource



def configure_tracing(service_name: str = "taskmanager") -> None:

    resource = Resource.create({"service.name": service_name})

    provider = TracerProvider(resource=resource)



    # Development: export to console for visibility

    # Production: replace with OtlpGrpcExporter pointing to Jaeger or Tempo

    from opentelemetry.sdk.trace.export import ConsoleSpanExporter

    processor = BatchSpanProcessor(ConsoleSpanExporter())



    # Production exporter (uncomment for real deployments):

    # from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

    # processor = BatchSpanProcessor(

    #     OTLPSpanExporter(endpoint="http://jaeger:4317", insecure=True)

    # )



    provider.add_span_processor(processor)

    trace.set_tracer_provider(provider)



tracer = trace.get_tracer("taskmanager")

Activate tracing inside the lifespan context manager:


from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

from core.tracing import configure_tracing



@asynccontextmanager

async def lifespan(app: FastAPI):

    configure_tracing()

    FastAPIInstrumentor.instrument_app(app)

    instrumentator.instrument(app).expose(app, endpoint="/metrics")

    logger.info("Application started")

    yield

    logger.info("Application shutting down")

Add manual child spans for database operations in the service layer:


from core.tracing import tracer



def get_task_by_id(task_id: int, session: Session) -> Task:

    with tracer.start_as_current_span("db.get_task") as span:

        span.set_attribute("db.operation", "select")

        span.set_attribute("task.id", task_id)



        task = session.get(Task, task_id)

        if not task:

            span.set_attribute("error", True)

            span.set_attribute("error.message", f"Task {task_id} not found")

            raise ValueError(f"Task with id {task_id} not found")



        return task

Each span produced by this code appears as a timed segment in the trace for the parent HTTP request.


Health Check Endpoint​

A /health endpoint is not optional in a production application. Load balancers and container orchestrators poll it to determine whether the application is alive and ready to receive traffic. A health check that always returns a static success response passes even when the database is unreachable. The correct implementation executes a lightweight query — SELECT 1 — inside a try/except block:


from fastapi import APIRouter, Depends

from sqlmodel import Session, text

from db import get_session

from typing import Annotated



router = APIRouter(tags=["Health"])

SessionDep = Annotated[Session, Depends(get_session)]



@router.get("/health")

def health_check(session: SessionDep):

    try:

        session.exec(text("SELECT 1"))

        db_status = "ok"

    except Exception as exc:

        logger.error("Database health check failed", extra={"error": str(exc)})

        db_status = "error"



    return {

        "status": "ok" if db_status == "ok" else "degraded",

        "database": db_status,

    }

Docker Compose: Running the Full Stack​

Running Prometheus via a standalone docker run command and pointing it at localhost:8000 does not work. When Prometheus runs inside Docker, localhost refers to its own container, not the host machine. The correct architecture uses a shared Docker network where each service communicates by service name.

đź“„ docker-compose.yml


version: "3.8"



services:

  app:

    build: .

    container_name: taskmanager-app

    ports:

      - "8000:8000"

    networks:

      - monitoring

    environment:

      - PYTHONUNBUFFERED=1



  prometheus:

    image: prom/prometheus:latest

    container_name: taskmanager-prometheus

    volumes:

      - ./prometheus.yml:/etc/prometheus/prometheus.yml

    ports:

      - "9090:9090"

    networks:

      - monitoring

    depends_on:

      - app



  grafana:

    image: grafana/grafana:latest

    container_name: taskmanager-grafana

    ports:

      - "3000:3000"

    environment:

      - GF_SECURITY_ADMIN_USER=admin

      - GF_SECURITY_ADMIN_PASSWORD=admin

    volumes:

      - grafana-storage:/var/lib/grafana

    networks:

      - monitoring

    depends_on:

      - prometheus



networks:

  monitoring:

    driver: bridge



volumes:

  grafana-storage:

đź“„ prometheus.yml


global:

  scrape_interval: 15s

  evaluation_interval: 15s



scrape_configs:

  - job_name: "taskmanager"

    metrics_path: "/metrics"

    static_configs:

      - targets: ["app:8000"] # "app" is the Docker Compose service name

targets: ["app:8000"] uses the service name app — resolvable on the shared monitoring network — not localhost, which would point to the Prometheus container itself.

Start the full stack:


docker-compose up --build

Access each service:

ServiceURLFastAPI apphttp://localhost:8000API docshttp://localhost:8000/docsMetrics endpointhttp://localhost:8000/metricsPrometheus UIhttp://localhost:9090Grafanahttp://localhost:3000


Configuring Grafana​

Add Prometheus as a data source​

  1. Open http://localhost:3000 and log in with admin / admin
  2. Navigate to Connections → Data sources → Add data source
  3. Select Prometheus
  4. Set the URL to http://prometheus:9090 — use the Docker service name, not localhost
  5. Click Save & test

Essential dashboard panels​

Request rate (time series):


sum by (handler) (rate(http_requests_total[5m]))

Groups requests per second by endpoint. Shows traffic distribution and spikes.

Error rate percentage (stat panel):


100 * sum(rate(http_requests_total{status="5xx"}[5m])) /

      sum(rate(http_requests_total[5m]))

Single number showing percentage of server errors. Configure thresholds: green below 1%, yellow 1–5%, red above 5%.

P95 latency (time series):


histogram_quantile(

  0.95,

  sum(rate(http_request_duration_seconds_bucket[5m])) by (handler, le)

)

Shows the latency value that 95% of requests complete within, per endpoint.

Setting up alerts​

In Grafana, navigate to Alerting → Alert rules → New alert rule. A practical first alert:

This fires an alert when more than 5% of requests return 5xx status codes for two consecutive evaluation periods.


Common Mistakes to Avoid​

Using logging.basicConfig() in a FastAPI application. It configures the root logger globally, interferes with Uvicorn's logging, and produces plain‑text logs that log aggregators cannot parse. Use a named logger with JsonFormatter.

Pointing prometheus.yml at localhost:8000 when running in Docker. Inside a Docker container, localhost refers to the container itself, not the host machine. Use the Docker Compose service name as the scrape target.

Calling Instrumentator().instrument(app).expose(app) at module level. This runs before the app's lifespan is set up. Use the lifespan context manager to activate the instrumentator after the app is fully initialised.

Using ConsoleSpanExporter in production. Console output is only useful for local debugging. In production, configure the OTLP exporter to send traces to a backend such as Jaeger or Grafana Tempo.

Health check endpoints that do not verify the database. A health check that always returns {"status": "ok"} passes even when the database is unreachable. Always test the database connection inside the health check handler.

Not setting logger.propagate = False on a named logger. Without this, log records travel up to the root logger and are processed twice — once by your handler and once by Uvicorn's handler.


Complete main.py​


from contextlib import asynccontextmanager

from fastapi import FastAPI, Request

from prometheus_fastapi_instrumentator import Instrumentator

from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

from core.logging_config import logger

from core.tracing import configure_tracing

from db import create_db_and_tables

from routers.tasks import router as tasks_router

from routers.health import router as health_router

import time



instrumentator = Instrumentator()



@asynccontextmanager

async def lifespan(app: FastAPI):

    configure_tracing()

    FastAPIInstrumentor.instrument_app(app)

    instrumentator.instrument(app).expose(app, endpoint="/metrics")

    create_db_and_tables()

    logger.info("Application started", extra={"version": "1.0.0"})

    yield

    logger.info("Application shutting down")



app = FastAPI(title="Task Manager API", lifespan=lifespan)



@app.middleware("http")

async def request_timer_middleware(request: Request, call_next):

    start = time.perf_counter()

    response = await call_next(request)

    duration = time.perf_counter() - start

    logger.info(

        "Request completed",

        extra={

            "method":      request.method,

            "path":        request.url.path,

            "status_code": response.status_code,

            "duration_s":  round(duration, 4),

        },

    )

    return response



app.include_router(health_router)

app.include_router(tasks_router)

Practice Challenge​

Frequently Asked Questions​

What are the three pillars of observability in FastAPI?

The three pillars are logs, metrics, and traces. Logs answer what happened — discrete timestamped events with context about specific requests or errors. Metrics answer how much and how often — numeric measurements aggregated over time such as request rates, error percentages, and latency percentiles. Traces answer where the time went — the path a single request took through the system, broken into timed spans. Each pillar answers a different question and the three complement each other.

Why should I not use logging.basicConfig() in a FastAPI application?

logging.basicConfig() configures the root logger globally, which interferes with Uvicorn's own logging infrastructure and can produce duplicate log entries or suppress Uvicorn's output entirely. It also produces plain-text log lines that log aggregators cannot parse. The correct approach is to configure a named application logger with its own handler, set logger.propagate = False to prevent double-logging, and use a JSON formatter so log output is machine-parseable.

What is structured logging and why does it matter in production?

Structured logging formats log output as JSON objects rather than plain text strings. Each log line is a valid JSON document with named fields — level, message, timestamp, and any application-specific context like task_id or user. This makes logs directly ingestible and filterable by log aggregators such as Grafana Loki, Elasticsearch, and Datadog. With plain text logs, searching for all errors related to a specific task ID requires regex. With structured logs, it is a simple field query.

How does Prometheus collect metrics from a FastAPI application?

Prometheus is a pull-based system. The FastAPI application exposes a /metrics endpoint that returns metrics in the Prometheus exposition format — plain text listing every metric name, its labels, and its current value. Prometheus periodically sends a GET request to that endpoint and stores the time-series data in its own database. The scrape interval is configured in prometheus.yml, defaulting to 15 seconds. The application never pushes data to Prometheus.

What does prometheus-fastapi-instrumentator do?

It wraps every FastAPI route handler and automatically records three categories of metrics: http_requests_total (a counter of all requests labelled by handler, HTTP method, and status code), http_request_duration_seconds (a histogram of response times), and http_requests_in_progress (a gauge of concurrent requests). It exposes these at the /metrics endpoint with no per-route code required. Custom business metrics such as tasks created or authentication failures require additional manual instrumentation.

Where should I call Instrumentator().instrument(app).expose(app) in FastAPI?

Inside the lifespan context manager, not at module level. Calling it at module level runs before the application is fully initialised, which can cause issues with middleware ordering and application state. The correct pattern is to create the Instrumentator instance at module level — so it exists when the app object is created — and then call instrument(app).expose(app) inside the lifespan function before the yield statement.

What is the difference between a Counter and a Histogram in Prometheus?

A Counter is a number that only ever increases. It resets to zero when the application restarts. Counters are suitable for totals: requests served, errors raised, tasks created. In Grafana, the rate() function converts a raw counter total into a per-second rate. A Histogram records how many observations fell into each of a set of predefined value ranges called buckets. Histograms are suitable for distributions: latency, file sizes, queue depths. The histogram_quantile() PromQL function calculates percentiles like P95 and P99 from histogram data.

Why does pointing prometheus.yml at localhost:8000 fail when running in Docker?

When Prometheus runs inside a Docker container, localhost refers to that container's own network interface, not the host machine. A scrape target of localhost:8000 would attempt to connect to a service running inside the Prometheus container itself, not to the FastAPI application. The correct approach is to use a Docker Compose shared network and reference the FastAPI service by its service name — targets: ['app:8000'] — which Docker DNS resolves to the correct container's IP address.

How do I set up Prometheus and Grafana with Docker Compose for FastAPI?

Define three services in docker-compose.yml — app, prometheus, and grafana — all connected to a shared bridge network. Mount prometheus.yml into the Prometheus container and set the scrape target to the app service name rather than localhost. Set the Grafana admin credentials via environment variables. Add a grafana-storage volume to persist dashboards across restarts. Start everything with docker-compose up --build. In Grafana, add Prometheus as a data source using the URL http://prometheus:9090 — again using the service name, not localhost.

What is OpenTelemetry and how does it work with FastAPI?

OpenTelemetry is a vendor-neutral, open-source standard for generating and collecting telemetry data — primarily distributed traces. FastAPIInstrumentor.instrument_app(app) automatically creates one trace span per HTTP request, capturing the route, method, status code, and duration. You add child spans manually for sub-operations like database queries using tracer.start_as_current_span(). Spans are exported to a backend — Jaeger, Grafana Tempo, or any OTLP-compatible collector — where they can be visualised as request timelines.

What is ConsoleSpanExporter and when should I use it?

ConsoleSpanExporter prints completed trace spans as JSON to standard output. It is only useful during local development and debugging to verify that instrumentation is producing spans without setting up a trace backend. It should never be used in production — console output is not queryable, not persistent, and produces significant log noise. In production, replace it with OTLPSpanExporter pointing to a trace backend such as Jaeger or Grafana Tempo.

How do I add a custom Prometheus metric to a FastAPI service function?

Import Counter or Histogram from prometheus_client and define the metric at module level in a dedicated metrics.py file. Give it a name, a description, and optional label names. Call .inc() on a Counter to increment it, or .observe(value) on a Histogram to record a measurement. Call these inside the service function after the relevant operation completes. For example, call TASKS_CREATED.labels(user_role=user.role).inc() after a successful task insert and session commit.

Why should a FastAPI health check endpoint verify the database connection?

A health check that always returns a static success response passes even when the database is unreachable, causing load balancers and container orchestrators to route traffic to an application that cannot fulfil any data requests. The correct implementation executes a lightweight query — SELECT 1 — inside a try/except block and includes the database status in the response. When the database is down, the endpoint returns a degraded status, allowing the load balancer to stop routing traffic to that instance.

What PromQL query gives me the P95 latency for FastAPI endpoints?

Use histogram_quantile() with the http_request_duration_seconds_bucket metric: histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (handler, le)). This calculates the latency value that 95 percent of requests to each endpoint complete within, based on a five-minute rolling window. Change 0.95 to 0.99 for P99. Group by handler to see per-endpoint breakdowns.

What PromQL query shows the error rate percentage in Grafana?

Divide the rate of 5xx responses by the total request rate and multiply by 100: 100 * sum(rate(http_requests_total{status='5xx'}[5m])) / sum(rate(http_requests_total[5m])). This produces a percentage between 0 and 100. In Grafana, add thresholds to colour the panel — green below 1 percent, yellow between 1 and 5 percent, red above 5 percent — to make the current error rate immediately visible.

What is a trace span in OpenTelemetry?

A span is a named, timed unit of work within a trace. Every trace consists of one or more spans arranged in a parent-child hierarchy. The root span represents the entire HTTP request. Child spans represent sub-operations: a database query, an external HTTP call, a cache lookup. Each span records its name, start time, duration, and a set of key-value attributes. When visualised in a trace backend like Jaeger, spans appear as a timeline showing how the request time was distributed across operations.

How do I set up a Grafana alert for high error rates in FastAPI?

In Grafana navigate to Alerting, create a new alert rule, and enter the error rate PromQL query as the data source. Set the condition to fire when the result is above your threshold — for example 5 for 5 percent. Set the evaluation interval to one minute and require the condition to be true for two consecutive periods to reduce false positives from transient spikes. Connect the alert to a notification channel — email, Slack, PagerDuty — via a contact point configured in Grafana's alerting settings.

What is the request timer middleware pattern in FastAPI observability?

A middleware function that wraps every request, records the wall-clock start time before calling the next handler, captures the response, calculates the duration, and logs a structured JSON record containing the HTTP method, path, status code, and duration. Unlike service-level timers that measure only database or business logic time, the middleware captures the complete request lifecycle including serialisation and middleware processing. It complements rather than replaces the Prometheus histogram, which aggregates data for querying while the middleware produces per-request log entries for individual request investigation.

What is the resource attribute service.name used for in OpenTelemetry?

The service.name resource attribute identifies which application produced a trace. It is attached to the TracerProvider via Resource.create({'service.name': 'taskmanager'}) and is included in every span exported by that provider. In a distributed system where multiple services all export traces to the same backend, service.name is what allows the trace visualisation tool to colour-code and filter spans by their origin service, making it possible to follow a request as it crosses service boundaries.

Why are my custom Prometheus metrics not appearing in the /metrics endpoint?

Custom metrics defined using prometheus_client.Counter or Histogram are automatically registered with the default registry and appear in the /metrics endpoint when the Instrumentator exposes it. Check that you are importing the custom metrics module in your application so the metric definitions are executed at import time. Also verify that you are calling .inc() or .observe() on the metric instances in your service code. If the metric has labels, ensure you provide all required labels when incrementing.

How do I add custom labels to automatic prometheus-fastapi-instrumentator metrics?

Use the Instrumentator's built-in label injection mechanism. Define a function that returns a dictionary of label values based on the request and response, then pass it to instrumentator.add() before instrumentation. For example: `def request_label_injector(info): return {'user_agent': info.request.headers.get('User-Agent')}` and `instrumentator.add(request_label_injector)`. This adds the custom label to all auto-instrumented metrics.

Why am I getting duplicate log entries when using a named logger in FastAPI?

You likely have not set logger.propagate = False on your named logger. By default, log records propagate to the root logger, which also has a handler attached by Uvicorn. This results in two identical log lines for every log call. Set logger.propagate = False immediately after creating the logger to prevent propagation to the root logger while keeping your own handler active.

Can I use OpenTelemetry tracing with async database operations in FastAPI?

Yes. OpenTelemetry supports async contexts natively. When using async database drivers, wrap the await call inside a span context: `async with tracer.start_as_current_span('db.async_query') as span: span.set_attribute('db.operation', 'select'); result = await session.execute(query)`. The span will correctly measure the async operation's duration.

What is the recommended way to propagate tracing headers to downstream services in FastAPI?

Use the OpenTelemetry propagator to extract the trace context from incoming requests and inject it into outgoing requests. Configure `trace.set_tracer_provider(provider)` and use `propagators = [TraceContextPropagator()]` with the appropriate extract and inject functions. For HTTP clients like httpx, use `requests` or `httpx` instrumentation that automatically injects the current context into outgoing headers.

How do I set up Grafana with a different admin password in Docker Compose?

Override the GF_SECURITY_ADMIN_PASSWORD environment variable in the docker-compose.yml grafana service. You can also set GF_SECURITY_ADMIN_USER to change the username. For better security in production, use Docker Secrets or an external environment file. Example: `environment: - GF_SECURITY_ADMIN_USER=myuser - GF_SECURITY_ADMIN_PASSWORD=mysecurepassword`.

What should I do if my FastAPI app in Docker cannot connect to Prometheus or Grafana?

Check that all three services are on the same Docker network. In docker-compose.yml, ensure each service has `networks: - monitoring`. Verify that the app is exposing its /metrics endpoint on the port Prometheus expects — typically 8000. Confirm that prometheus.yml has the correct target: `targets: ['app:8000']` uses the service name. If the app is exposed on a different port in the container, adjust accordingly. Use `docker logs` to check for startup errors in each container.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

CI/CD and GitHub Actions

Next

Final Project: Production-Grade Task Manager API