Send logs to Grafana Loki
config.alloy
file defines the pipeline for how Alloy should receive telemetry data, process or transform it, and export it to Prometheus, Loki, Tempo, or other platforms and databases.
The default configuration file location for Alloy is:
- Linux:
/etc/alloy/config.alloy
- macOS:
$(brew --prefix)/etc/alloy/config.alloy
- Windows:
%ProgramFiles%\GrafanaLabs\Alloy\config.alloy
You can open the configuration file using vim
or nano
. For this example,
VSCode is used with the
Alloy plugin installed, as it comes with the advantage of syntax support.
code $(brew --prefix)/etc/alloy/config.alloy
When you open the configuration file automatically generated by the setup guide in Grafana Cloud, a working pipeline is already set up by default that:
- Collects and exposes internal metrics about Alloy itself
- Adds useful labels to the metrics that will be scraped
- Starts a Prometheus scraping job that will scrape the internal metrics with a 60-second interval
- Filters the metrics to keep the useful ones
- Forwards them to Grafana Cloud Prometheus
- Forwards logs to Grafana Cloud Logs (Loki), but it’s not wired up yet.
With the configuration file still open, add the following components:
To configure Alloy to search for log files to source, copy and paste the following component configuration before the
loki.write
component.While not required, the recommended order of components provides for better organization and readability.
local.file_match "local_files" { path_targets = [{"__path__" = "/var/log/*.log"}] sync_period = "5s" }
This configuration creates a
local.file_match
component namedlocal_files
which does the following:- It tells Alloy which files to source.
- It checks for new files every 5 seconds.
To configure Alloy to read log entries from files and forward them to other
loki.*
components, copy and paste the following component configuration after thelocal.file_match
component.loki.source.file "log_scrape" { targets = local.file_match.local_files.targets forward_to = [loki.write.grafana_cloud_loki.receiver] }
This configuration creates a
loki.source.file
component namedlog_scrape
which does the following:- It connects to the
local_files
component as its source or target. - It forwards the logs it scrapes to the
loki.write
component calledgrafana_cloud_loki
.
- It connects to the
Run the following command to call the call the
/-/reload
endpoint to tell Alloy to reload the configuration file without a system service restart.curl -X POST http://localhost:12345/-/reload
You should see the message
config reloaded
printed on your terminal.Note
Run the reload command whenever you make changes to the configuration.
In the next milestone, you’re going to inspect your configuration file in the Alloy UI.