Grafana Alloy

Grafana Alloy collects telemetry (logs, metrics and traces) and sends it on. Here we’ll use it to read the application’s log files and send each new line to Loki.

note

Earlier versions of this guide used Promtail for this. Promtail is no longer developed, and reached end of life in March 2026: Grafana Alloy replaces it.

Alloy’s configuration is written in its own language, as a pipeline of components, each one passing its output to the next:

mkdir -p ~/volumes/alloy
~/volumes/alloy/config.alloytext
// Find the application's log files
local.file_match "fastapi" {
  path_targets = [{"__path__" = "/var/log/fastapi/*.log", "job" = "fastapi"}]
}

// Read new lines from them as they're written
loki.source.file "fastapi" {
  targets    = local.file_match.fastapi.targets
  forward_to = [loki.write.local.receiver]
}

// Send them to Loki
loki.write "local" {
  endpoint {
    url = "http://loki:3100/loki/api/v1/push"
  }
}
  • local.file_match finds the files to read. The log directory will be mounted at /var/log/fastapi in the container, and every line from these files gets the label job="fastapi".
  • loki.source.file reads the files, and remembers how far it got, so lines aren’t sent twice.
  • loki.write sends the lines to Loki, using the container’s name.

Check the configuration:

podman run --rm -v ~/volumes/alloy:/etc/alloy:Z docker.io/grafana/alloy:v1.22.0 validate /etc/alloy/config.alloy

Then run Alloy, with its configuration, and the log directory mounted read-only:

podman run -d --name alloy --network monitoring-network -p 12345:12345 \
  -v ~/volumes/alloy:/etc/alloy:Z \
  -v ~/volumes/logs:/var/log/fastapi:z,ro \
  docker.io/grafana/alloy:v1.22.0 \
  run --server.http.listen-addr=0.0.0.0:12345 --storage.path=/var/lib/alloy/data /etc/alloy/config.alloy

The lowercase :z on the log directory relabels it for SELinux so that both the application and the container can use it (an uppercase :Z would make it private to the container). Alloy has a web interface at http://localhost:12345, showing each component and whether it’s healthy.

Logs in Grafana#

In Grafana, go to Explore, choose the Loki data source, and query:

{job="fastapi"}

The application’s log lines appear, newest first. You can filter them, for example to only requests that failed with a 5xx status. |= keeps lines containing some text, and |~ lines matching a regular expression (in backticks, so the backslashes don’t need escaping):

{job="fastapi"} |= "uvicorn.access" |~ `" 5\d\d`