Send logs to Grafana Loki

The 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.

bash
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:

  1. Collects and exposes internal metrics about Alloy itself
  2. Adds useful labels to the metrics that will be scraped
  3. Starts a Prometheus scraping job that will scrape the internal metrics with a 60-second interval
  4. Filters the metrics to keep the useful ones
  5. Forwards them to Grafana Cloud Prometheus
  6. Forwards logs to Grafana Cloud Logs (Loki), but it’s not wired up yet.

With the configuration file still open, add the following components:

  1. 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.

    alloy
    local.file_match "local_files" {
        path_targets = [{"__path__" = "/var/log/*.log"}]
        sync_period = "5s"
    }

    This configuration creates a local.file_match component named local_files which does the following:

    • It tells Alloy which files to source.
    • It checks for new files every 5 seconds.
  2. To configure Alloy to read log entries from files and forward them to other loki.* components, copy and paste the following component configuration after the local.file_match component.

    alloy
      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 named log_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 called grafana_cloud_loki.
  3. Run the following command to call the call the /-/reload endpoint to tell Alloy to reload the configuration file without a system service restart.

    bash
    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.


page 6 of 9