Grafana Promtail to Alloy migration
Promtail was a project I used as part of the Grafana Loki log collection ecosystem. It was deprecated as Grafana now recommends using the Grafana Alloy project to help collect telemetry data. Promtail made it relatively easy to collect logs from containers and files on a *nix system. I also used the Kubernetes helm chart to collect pod logs in Kubernetes and forward to Grafana Loki.
My last post about Grafana Loki was upgrading from v2 to v3. Now that my Loki server is upgraded, I am migrating my installations of promtail to use Alloy instead for log collection.
Grafana Alloy
Alloy is a distribution of the Opentelemetry project from Grafana labs. Opentelemetry is a growing set of APIs for collecting metrics, logs, and traces from your systems and applications. Grafana Alloy combines the strengths of the leading collectors into one place. Whether observing applications, infrastructure, or both, Grafana Alloy (and OTEL) can collect, process, and export telemetry signals to scale your observability approach. Official docs.
- It receives data from one or many inputs.
- It optionally transforms input data.
- Finally it transmits transformed inputs to one or many backends.
It covers my use cases:
- Collecting logs from the Linux journal
- Discover “targets” such as all pod logs in Kubernetes.
- Apply metadata to associate Logs, metrics, and traces.
- Relabel data before sending it to a remote backend.
- Compatible with Prometheus for metrics and Grafana Loki for logs.
Install on Docker hosts
On systems where you are running Docker as a container run time and want to collect telemetry such as logs, Alloy can run as a container along the others on the system with Docker. There are images for Linux containers using amd64 or arm64 CPU architecture.
You need Docker running. Check out a previous post to get started with containers if you are not familiar.
First step for the migration
My first step is to convert my promtail configuration from the original yaml file to a config file for Grafana Alloy. The maintainers provided a simple tool to assist the migration. This tool is using HCL instead of YAML.
Example usage:
|
|
To invoke the alloy
command, you need to have alloy on your system. Check out the Official docs to see how to install alloy
on your local system for example on Linux or MacOS. Here was the config that I used for promtail:
|
|
I do not know if all of these options should be ported to Alloy but the CLI tool produced this configuration for Alloy based on my config to collect docker container logs:
|
|
We can see if this worked by starting an allow container that has access to the Docker container log files. The container can be configured and started with a Docker Compose yaml template docker-compose.yaml
:
|
|
For now you need to configure the container to start a server on port 12345 by adding that flag to the run command for the container. Supply a compatible config file in the same directory as the docker-compose.yml
file alloy.hcl
.
Once I started the container with docker compose up -d
, my container logs were sent to Loki. It is worth noting that I saw the logs duplicated that were already collected by Promtail. Not worth fixing in my homelab but you may want to prepare your Docker hosts before starting up alloy.
Note that Alloy also supports collecting logs via the Docker daemon but I prefer this approach were we do not allow alloy to mount the Docker socket but instead just present the container log files for collection.
Install on macOS
Logs on macOS can also be collected with Alloy. To install Alloy on macOS, I recommend homebrew:
|
|
Configure on macOS
Alloy can be run as a homebrew service so the program will start when you log in:
|
|
Edit the default configuration file at $(brew --prefix)/etc/alloy/config.alloy
. Here is an example config file to collect the macOS system log file:
|
|
If you update the configuration you can restart alloy with brew:
|
|
And now the macOS logs should be forwarded to Loki. This was a big win for me cause I totally neglected to collect macOS logs with Promtail even though it was possible.
Some logs that Alloy picked up on my mac:
|
|
Collect Linux system journal logs
If you are on a Linux system, the setup to install the alloy
package may vary depending on your distribution. Check the Official docs for how to install as a package or download an executable binary.
This HCL config block will allow Grafana alloy to collect logs from the Linux journal daemon and ensure that there is a label applied that identifies the service:
|
|
Rather than scrape all .log
files on Linux like I did for macOS, this interacts with the systemd journal which may include some logs that are not logged to /var/log
.
Add journal as volume for containers
If using Docker/containers, we need to add the journal to the volume mounts for the container. Add this volume if using compose:
|
|
Note that when using Docker, I discovered that there is no container image for the armv7 cpu architecture found on a Raspberry Pi 3.
Alloy UI
You can view a basic debugging UI if you have access to port 12345
on the system where alloy runs. Navigate to http://localhost:12345
in your browser (or the IP/DNS name of a server where Alloy is running).