<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Grafana Alloy tutorials on Grafana Labs</title><link>https://grafana.com/docs/alloy/v1.15/tutorials/</link><description>Recent content in Grafana Alloy tutorials on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/alloy/v1.15/tutorials/index.xml" rel="self" type="application/rss+xml"/><item><title>Use Grafana Alloy to send logs to Loki</title><link>https://grafana.com/docs/alloy/v1.15/tutorials/send-logs-to-loki/</link><pubDate>Mon, 30 Mar 2026 15:47:22 +0000</pubDate><guid>https://grafana.com/docs/alloy/v1.15/tutorials/send-logs-to-loki/</guid><content><![CDATA[&lt;!-- INTERACTIVE page intro.md START --&gt;
&lt;h2 id=&#34;use-grafana-alloy-to-send-logs-to-loki&#34;&gt;Use Grafana Alloy to send logs to Loki&lt;/h2&gt;
&lt;p&gt;This tutorial shows you how to configure Alloy to collect logs from your local machine, filter non-essential log lines, send them to Loki, and use Grafana to explore the results.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;p&gt;To complete this tutorial:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You must have a basic understanding of Alloy and telemetry collection in general.&lt;/li&gt;
&lt;li&gt;You should be familiar with Prometheus, PromQL, Loki, LogQL, and basic Grafana navigation.&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- INTERACTIVE ignore START --&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;Alternatively, you can try out this example in the interactive learning environment: &lt;a href=&#34;https://killercoda.com/grafana-labs/course/alloy/send-logs-to-loki&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Sending Logs to Loki&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s a fully configured environment with all the dependencies already installed.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;/media/docs/alloy/Alloy-Interactive-Learning-Environment-%28Doc-Banner%29.png&#34;
  alt=&#34;Interactive&#34; width=&#34;800&#34;
     height=&#34;250&#34;/&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;!-- INTERACTIVE ignore END --&gt;
&lt;!-- INTERACTIVE page intro.md END --&gt;
&lt;!-- INTERACTIVE page step1.md START --&gt;
&lt;h2 id=&#34;install-alloy-and-start-the-service&#34;&gt;Install Alloy and start the service&lt;/h2&gt;
&lt;!-- INTERACTIVE ignore START --&gt;
&lt;p&gt;This tutorial requires a Linux or macOS environment with Docker installed.&lt;/p&gt;
&lt;!-- INTERACTIVE ignore END --&gt;

&lt;h3 id=&#34;linux&#34;&gt;Linux&lt;/h3&gt;
&lt;p&gt;Install and run Alloy on Linux.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&#34;../../set-up/install/linux/&#34;&gt;Install Alloy&lt;/a&gt;.
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../set-up/run/linux/&#34;&gt;Run Alloy&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;!-- INTERACTIVE ignore START --&gt;
&lt;h3 id=&#34;macos&#34;&gt;macOS&lt;/h3&gt;
&lt;p&gt;Install and run Alloy on macOS.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../set-up/install/macos/&#34;&gt;Install Alloy&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../set-up/run/macos/&#34;&gt;Run Alloy&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;!-- INTERACTIVE ignore END --&gt;
&lt;p&gt;You can access the Alloy UI at &lt;a href=&#34;http://localhost:12345&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:12345&lt;/a&gt;.&lt;/p&gt;
&lt;!-- INTERACTIVE page step1.md END --&gt;
&lt;!-- INTERACTIVE page step2.md START --&gt;
&lt;h2 id=&#34;set-up-a-local-grafana-instance&#34;&gt;Set up a local Grafana instance&lt;/h2&gt;
&lt;p&gt;In this tutorial, you configure Alloy to collect logs from your local machine and send them to Loki.
You can use the following Docker Compose file to set up a local Grafana instance.
This Docker Compose file includes Loki and Prometheus configured as data sources.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a directory and save the Docker Compose file as &lt;code&gt;docker-compose.yml&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;mkdir alloy-tutorial
cd alloy-tutorial
touch docker-compose.yml&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Copy the following Docker Compose file into &lt;code&gt;docker-compose.yml&lt;/code&gt;.
&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;version: &amp;#39;3&amp;#39;
services:
  loki:
    image: grafana/loki:3.0.0
    ports:
      - &amp;#34;3100:3100&amp;#34;
    command: -config.file=/etc/loki/local-config.yaml
  prometheus:
    image: prom/prometheus:v2.47.0
    command:
      - --web.enable-remote-write-receiver
      - --config.file=/etc/prometheus/prometheus.yml
    ports:
      - &amp;#34;9090:9090&amp;#34;
  grafana:
    environment:
      - GF_PATHS_PROVISIONING=/etc/grafana/provisioning
      - GF_AUTH_ANONYMOUS_ENABLED=true
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
    entrypoint:
      - sh
      - -euc
      - |
        mkdir -p /etc/grafana/provisioning/datasources
        cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/grafana/provisioning/datasources/ds.yaml
        apiVersion: 1
        datasources:
        - name: Loki
          type: loki
          access: proxy
          orgId: 1
          url: http://loki:3100
          basicAuth: false
          isDefault: false
          version: 1
          editable: false
        - name: Prometheus
          type: prometheus
          orgId: 1
          url: http://prometheus:9090
          basicAuth: false
          isDefault: true
          version: 1
          editable: false
        EOF
        /run.sh
    image: grafana/grafana:11.0.0
    ports:
      - &amp;#34;3000:3000&amp;#34;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To start the local Grafana instance, run the following command.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;docker compose up -d&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
 &lt;!-- INTERACTIVE ignore START --&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;If you encounter the following error when you start your Docker container, &lt;code&gt;docker: &#39;compose&#39; is not a docker command&lt;/code&gt;, use the command &lt;code&gt;docker-compose up&lt;/code&gt; to start your Docker container.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

 &lt;!-- INTERACTIVE ignore END --&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open &lt;a href=&#34;http://localhost:3000&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000&lt;/a&gt; in your browser to access the Grafana UI.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;!-- INTERACTIVE page step2.md END --&gt;
&lt;!-- INTERACTIVE page step3.md START --&gt;
&lt;h2 id=&#34;configure-alloy&#34;&gt;Configure Alloy&lt;/h2&gt;
&lt;p&gt;After the local Grafana instance is set up, the next step is to configure Alloy.
You use components in the &lt;code&gt;config.alloy&lt;/code&gt; file to tell Alloy which logs you want to scrape, how you want to process that data, and where you want the data sent.&lt;/p&gt;
&lt;p&gt;The examples run on a single host so that you can run them on your laptop or in a Virtual Machine.
You can try the examples using a &lt;code&gt;config.alloy&lt;/code&gt; file and experiment with the examples.&lt;/p&gt;
&lt;h3 id=&#34;create-a-configalloy-file&#34;&gt;Create a &lt;code&gt;config.alloy&lt;/code&gt; file&lt;/h3&gt;
&lt;p&gt;Create a &lt;code&gt;config.alloy&lt;/code&gt; file within your current working directory.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;touch config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;first-component-log-files&#34;&gt;First component: Log files&lt;/h3&gt;
&lt;p&gt;Copy and paste the following component configuration at the top of the file.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;local.file_match &amp;#34;local_files&amp;#34; {
  path_targets = [{&amp;#34;__path__&amp;#34; = &amp;#34;/var/log/*.log&amp;#34;}]
  sync_period = &amp;#34;5s&amp;#34;
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This configuration creates a &lt;a href=&#34;../../reference/components/local/local.file_match/&#34;&gt;local.file_match&lt;/a&gt; component named &lt;code&gt;local_files&lt;/code&gt; which does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It tells Alloy which files to source.&lt;/li&gt;
&lt;li&gt;It checks for new files every 5 seconds.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;second-component-scraping&#34;&gt;Second component: Scraping&lt;/h3&gt;
&lt;p&gt;Copy and paste the following component configuration below the previous component in your &lt;code&gt;config.alloy&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;loki.source.file &amp;#34;log_scrape&amp;#34; {
  targets    = local.file_match.local_files.targets
  forward_to = [loki.process.filter_logs.receiver]
  tail_from_end = true
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This configuration creates a &lt;a href=&#34;../../reference/components/loki/loki.source.file/&#34;&gt;&lt;code&gt;loki.source.file&lt;/code&gt;&lt;/a&gt; component named &lt;code&gt;log_scrape&lt;/code&gt; which does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It connects to the &lt;code&gt;local_files&lt;/code&gt; component as its source or target.&lt;/li&gt;
&lt;li&gt;It forwards the logs it scrapes to the receiver of another component called &lt;code&gt;filter_logs&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It provides extra attributes and options to tail the log files from the end so you don&amp;rsquo;t ingest the entire log file history.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;third-component-filter-non-essential-logs&#34;&gt;Third component: Filter non-essential logs&lt;/h3&gt;
&lt;p&gt;Filtering non-essential logs before sending them to a data source can help you manage log volumes to reduce costs.&lt;/p&gt;
&lt;p&gt;The following example demonstrates how you can filter out or drop logs before sending them to Loki.&lt;/p&gt;
&lt;p&gt;Copy and paste the following component configuration below the previous component in your &lt;code&gt;config.alloy&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;loki.process &amp;#34;filter_logs&amp;#34; {
  stage.drop {
    source = &amp;#34;&amp;#34;
    expression  = &amp;#34;.*Connection closed by authenticating user root&amp;#34;
    drop_counter_reason = &amp;#34;noisy&amp;#34;
  }
  forward_to = [loki.write.grafana_loki.receiver]
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;loki.process&lt;/code&gt; component allows you to transform, filter, parse, and enrich log data.
Within this component, you can define one or more processing stages to specify how you would like to process log entries before they&amp;rsquo;re stored or forwarded.&lt;/p&gt;
&lt;p&gt;This configuration creates a &lt;a href=&#34;../../reference/components/loki/loki.process/&#34;&gt;&lt;code&gt;loki.process&lt;/code&gt;&lt;/a&gt; component named &lt;code&gt;filter_logs&lt;/code&gt; which does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It receives scraped log entries from the default &lt;code&gt;log_scrape&lt;/code&gt; component.&lt;/li&gt;
&lt;li&gt;It uses the &lt;code&gt;stage.drop&lt;/code&gt; block to define what to drop from the scraped logs.&lt;/li&gt;
&lt;li&gt;It uses the &lt;code&gt;expression&lt;/code&gt; parameter to identify the specific log entries to drop.&lt;/li&gt;
&lt;li&gt;It uses an optional string label &lt;code&gt;drop_counter_reason&lt;/code&gt; to show the reason for dropping the log entries.&lt;/li&gt;
&lt;li&gt;It forwards the processed logs to the receiver of another component called &lt;code&gt;grafana_loki&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;a href=&#34;../../reference/components/loki/loki.process/&#34;&gt;&lt;code&gt;loki.process&lt;/code&gt;&lt;/a&gt; documentation provides more comprehensive information on processing logs.&lt;/p&gt;
&lt;h3 id=&#34;fourth-component-write-logs-to-loki&#34;&gt;Fourth component: Write logs to Loki&lt;/h3&gt;
&lt;p&gt;Copy and paste this component configuration below the previous component in your &lt;code&gt;config.alloy&lt;/code&gt; file.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;loki.write &amp;#34;grafana_loki&amp;#34; {
  endpoint {
    url = &amp;#34;http://localhost:3100/loki/api/v1/push&amp;#34;

    // basic_auth {
    //  username = &amp;#34;admin&amp;#34;
    //  password = &amp;#34;admin&amp;#34;
    // }
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This final component creates a &lt;a href=&#34;../../reference/components/loki/loki.write/&#34;&gt;&lt;code&gt;loki.write&lt;/code&gt;&lt;/a&gt; component named &lt;code&gt;grafana_loki&lt;/code&gt; that points to &lt;code&gt;http://localhost:3100/loki/api/v1/push&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This completes the simple configuration pipeline.&lt;/p&gt;
&lt;!-- INTERACTIVE ignore START --&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;The &lt;code&gt;basic_auth&lt;/code&gt; block is commented out because the local &lt;code&gt;docker compose&lt;/code&gt; stack doesn&amp;rsquo;t require it.
It&amp;rsquo;s included in this example to show how you can configure authorization for other environments.
For further authorization options, refer to the &lt;a href=&#34;../../reference/components/loki/loki.write/&#34;&gt;&lt;code&gt;loki.write&lt;/code&gt;&lt;/a&gt; component reference.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;!-- INTERACTIVE ignore END --&gt;

&lt;p&gt;With this configuration, Alloy connects directly to the Loki instance running in the Docker container.&lt;/p&gt;
&lt;!-- INTERACTIVE page step3.md END --&gt;
&lt;!-- INTERACTIVE page step4.md START --&gt;
&lt;h2 id=&#34;reload-the-configuration&#34;&gt;Reload the configuration&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Copy your local &lt;code&gt;config.alloy&lt;/code&gt; file into the default Alloy configuration file location.&lt;/p&gt;

&lt;!-- INTERACTIVE ignore START --&gt;



  

  






  

  



  &lt;div class=&#34;code&#34; x-data=&#34;app_code([&amp;#34;macos&amp;#34;,&amp;#34;linux&amp;#34;], false)&#34; x-init=&#34;init()&#34; data-codetoggle=&#34;true&#34;&gt;
    &lt;div class=&#34;toggle-toolbar &#34;&gt;
      &lt;div&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;macos&#39; }&#34; @click=&#34;$store.code.language = &#39;macos&#39;&#34;&gt;
              &lt;span&gt;macos&lt;/span&gt;
            &lt;/button&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;linux&#39; }&#34; @click=&#34;$store.code.language = &#39;linux&#39;&#34;&gt;
              &lt;span&gt;linux&lt;/span&gt;
            &lt;/button&gt;&lt;/div&gt;
      &lt;div class=&#34;d-flex&#34;&gt;&lt;span class=&#34;code-clipboard&#34; x-ref=&#34;tooltip&#34;&gt;
          &lt;button @click=&#34;copy()&#34;&gt;
            &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
            &lt;span&gt;Copy&lt;/span&gt;
          &lt;/button&gt;
        &lt;/span&gt;
      &lt;/div&gt;
      &lt;div class=&#34;toggle-toolbar__border&#34;&gt;&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#34;code-rendered&#34; &gt;
      
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;macos&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-macos&#34;&gt;sudo cp config.alloy $(brew --prefix)/etc/alloy/config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;linux&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-linux&#34;&gt;sudo cp config.alloy /etc/alloy/config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

    &lt;/div&gt;
  &lt;/div&gt;


&lt;!-- INTERACTIVE ignore END --&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Call the &lt;code&gt;/-/reload&lt;/code&gt; endpoint to tell Alloy to reload the configuration file without a system service restart.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;curl -X POST http://localhost:12345/-/reload&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- INTERACTIVE ignore START --&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;This step uses the Alloy UI on &lt;code&gt;localhost&lt;/code&gt; port &lt;code&gt;12345&lt;/code&gt;.
If you chose to run Alloy in a Docker container, make sure you use the &lt;code&gt;--server.http.listen-addr=0.0.0.0:12345&lt;/code&gt; argument.
If you don&amp;rsquo;t use this argument, the &lt;a href=&#34;../../troubleshoot/debug/#alloy-ui&#34;&gt;debugging UI&lt;/a&gt; won&amp;rsquo;t be available outside of the Docker container.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;!-- INTERACTIVE ignore END --&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Optional: You can do a system service restart Alloy and load the configuration file.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;!-- INTERACTIVE ignore START --&gt;



  

  






  

  



  &lt;div class=&#34;code&#34; x-data=&#34;app_code([&amp;#34;macos&amp;#34;,&amp;#34;linux&amp;#34;], false)&#34; x-init=&#34;init()&#34; data-codetoggle=&#34;true&#34;&gt;
    &lt;div class=&#34;toggle-toolbar &#34;&gt;
      &lt;div&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;macos&#39; }&#34; @click=&#34;$store.code.language = &#39;macos&#39;&#34;&gt;
              &lt;span&gt;macos&lt;/span&gt;
            &lt;/button&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;linux&#39; }&#34; @click=&#34;$store.code.language = &#39;linux&#39;&#34;&gt;
              &lt;span&gt;linux&lt;/span&gt;
            &lt;/button&gt;&lt;/div&gt;
      &lt;div class=&#34;d-flex&#34;&gt;&lt;span class=&#34;code-clipboard&#34; x-ref=&#34;tooltip&#34;&gt;
          &lt;button @click=&#34;copy()&#34;&gt;
            &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
            &lt;span&gt;Copy&lt;/span&gt;
          &lt;/button&gt;
        &lt;/span&gt;
      &lt;/div&gt;
      &lt;div class=&#34;toggle-toolbar__border&#34;&gt;&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#34;code-rendered&#34; &gt;
      
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;macos&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-macos&#34;&gt;brew services restart grafana/grafana/alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;linux&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-linux&#34;&gt;sudo systemctl reload alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

    &lt;/div&gt;
  &lt;/div&gt;


&lt;!-- INTERACTIVE ignore END --&gt;
&lt;h2 id=&#34;inspect-your-configuration-in-the-alloy-ui&#34;&gt;Inspect your configuration in the Alloy UI&lt;/h2&gt;
&lt;p&gt;Open &lt;a href=&#34;http://localhost:12345&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:12345&lt;/a&gt; and click the &lt;strong&gt;Graph&lt;/strong&gt; tab at the top.
The graph should look similar to the following:&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 2016px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png&#34;data-srcset=&#34;/media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png?w=320 320w, /media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png?w=550 550w, /media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png?w=750 750w, /media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png?w=900 900w, /media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png?w=1040 1040w, /media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png?w=1240 1240w, /media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Your configuration in the Alloy UI&#34;width=&#34;2016&#34;height=&#34;1547&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/tutorial/Inspect-your-config-in-the-Alloy-UI-image.png&#34;
            alt=&#34;Your configuration in the Alloy UI&#34;width=&#34;2016&#34;height=&#34;1547&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;The Alloy UI shows you a visual representation of the pipeline you built with your Alloy component configuration.&lt;/p&gt;
&lt;p&gt;You can see that the components are healthy, and you are ready to explore the logs in Grafana.&lt;/p&gt;
&lt;!-- INTERACTIVE page step4.md END --&gt;
&lt;!-- INTERACTIVE page step5.md START --&gt;
&lt;h2 id=&#34;log-in-to-grafana-and-explore-loki-logs&#34;&gt;Log in to Grafana and explore Loki logs&lt;/h2&gt;
&lt;p&gt;Open &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; to access &lt;strong&gt;Explore&lt;/strong&gt; feature in Grafana.&lt;/p&gt;
&lt;p&gt;Select Loki as the data source and click the &lt;strong&gt;Label Browser&lt;/strong&gt; button to select a file that Alloy has sent to Loki.&lt;/p&gt;
&lt;p&gt;Here you can see that logs are flowing through to Loki as expected, and the end-to-end configuration was successful.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 2110px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/tutorial/loki-logs.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/tutorial/loki-logs.png&#34;data-srcset=&#34;/media/docs/alloy/tutorial/loki-logs.png?w=320 320w, /media/docs/alloy/tutorial/loki-logs.png?w=550 550w, /media/docs/alloy/tutorial/loki-logs.png?w=750 750w, /media/docs/alloy/tutorial/loki-logs.png?w=900 900w, /media/docs/alloy/tutorial/loki-logs.png?w=1040 1040w, /media/docs/alloy/tutorial/loki-logs.png?w=1240 1240w, /media/docs/alloy/tutorial/loki-logs.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Logs reported by Alloy in Grafana&#34;width=&#34;2110&#34;height=&#34;1344&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/tutorial/loki-logs.png&#34;
            alt=&#34;Logs reported by Alloy in Grafana&#34;width=&#34;2110&#34;height=&#34;1344&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;!-- INTERACTIVE page step5.md END --&gt;
&lt;!-- INTERACTIVE page finish.md START --&gt;
&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;p&gt;You have installed and configured Alloy, and sent logs from your local host to your local Grafana stack.&lt;/p&gt;
&lt;p&gt;In the &lt;a href=&#34;../send-metrics-to-prometheus/&#34;&gt;next tutorial&lt;/a&gt;, you learn more about configuration concepts and metrics.&lt;/p&gt;
&lt;!-- INTERACTIVE page finish.md END --&gt;
]]></content><description>&lt;!-- INTERACTIVE page intro.md START -->
&lt;h2 id="use-grafana-alloy-to-send-logs-to-loki">Use Grafana Alloy to send logs to Loki&lt;/h2>
&lt;p>This tutorial shows you how to configure Alloy to collect logs from your local machine, filter non-essential log lines, send them to Loki, and use Grafana to explore the results.&lt;/p></description></item><item><title>Use Grafana Alloy to send metrics to Prometheus</title><link>https://grafana.com/docs/alloy/v1.15/tutorials/send-metrics-to-prometheus/</link><pubDate>Mon, 30 Mar 2026 15:47:22 +0000</pubDate><guid>https://grafana.com/docs/alloy/v1.15/tutorials/send-metrics-to-prometheus/</guid><content><![CDATA[&lt;!-- INTERACTIVE page intro.md START --&gt;
&lt;h1 id=&#34;use-grafana-alloy-to-send-metrics-to-prometheus&#34;&gt;Use Grafana Alloy to send metrics to Prometheus&lt;/h1&gt;
&lt;p&gt;In the &lt;a href=&#34;../send-logs-to-loki/&#34;&gt;previous tutorial&lt;/a&gt;, you learned how to configure Alloy to collect and process logs from your local machine and send them to Loki.&lt;/p&gt;
&lt;p&gt;This tutorial shows you how to configure Alloy to collect and process metrics from your local machine, send them to Prometheus, and use Grafana to explore the results.&lt;/p&gt;
&lt;!-- INTERACTIVE ignore START --&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;p&gt;To complete this tutorial:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You must have a basic understanding of Alloy and telemetry collection in general.&lt;/li&gt;
&lt;li&gt;You should be familiar with Prometheus, PromQL, Loki, LogQL, and basic Grafana navigation.&lt;/li&gt;
&lt;li&gt;You must complete the &lt;a href=&#34;../send-logs-to-loki/&#34;&gt;previous tutorial&lt;/a&gt; to prepare the following prerequisites:
&lt;ul&gt;
&lt;li&gt;Install Alloy and start the service in your environment.&lt;/li&gt;
&lt;li&gt;Set up a local Grafana instance.&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;config.alloy&lt;/code&gt; file.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- INTERACTIVE ignore START --&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;Alternatively, you can try out this example in the interactive learning environment: &lt;a href=&#34;https://killercoda.com/grafana-labs/course/alloy/send-metrics-to-prometheus&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Sending metrics to Prometheus&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s a fully configured environment with all the dependencies already installed.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;/media/docs/alloy/Alloy-Interactive-Learning-Environment-%28Doc-Banner%29.png&#34;
  alt=&#34;Interactive&#34; width=&#34;800&#34;
     height=&#34;250&#34;/&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;!-- INTERACTIVE ignore END --&gt;

&lt;!-- INTERACTIVE page intro.md END --&gt;
&lt;!-- INTERACTIVE page step1.md START --&gt;
&lt;h2 id=&#34;configure-alloy&#34;&gt;Configure Alloy&lt;/h2&gt;
&lt;p&gt;In this tutorial, you configure Alloy to collect metrics and send them to Prometheus.&lt;/p&gt;
&lt;p&gt;You add components to your &lt;code&gt;config.alloy&lt;/code&gt; file to tell Alloy which metrics you want to scrape, how you want to process that data, and where you want the data sent.&lt;/p&gt;
&lt;p&gt;The following steps build on the &lt;code&gt;config.alloy&lt;/code&gt; file you created in the previous tutorial.&lt;/p&gt;

&lt;h3 id=&#34;first-component-scraping&#34;&gt;First component: Scraping&lt;/h3&gt;
&lt;p&gt;Paste the following component configuration at the top of your &lt;code&gt;config.alloy&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;prometheus.exporter.unix &amp;#34;local_system&amp;#34; { }

prometheus.scrape &amp;#34;scrape_metrics&amp;#34; {
  targets         = prometheus.exporter.unix.local_system.targets
  forward_to      = [prometheus.relabel.filter_metrics.receiver]
  scrape_interval = &amp;#34;10s&amp;#34;
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This configuration creates a &lt;a href=&#34;../../reference/components/prometheus/prometheus.scrape/&#34;&gt;&lt;code&gt;prometheus.scrape&lt;/code&gt;&lt;/a&gt; component named &lt;code&gt;scrape_metrics&lt;/code&gt; which does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It connects to the &lt;code&gt;local_system&lt;/code&gt; component as its source or target.&lt;/li&gt;
&lt;li&gt;It forwards the metrics it scrapes to the receiver of another component called &lt;code&gt;filter_metrics&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It tells Alloy to scrape metrics every 10 seconds.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;second-component-filter-metrics&#34;&gt;Second component: Filter metrics&lt;/h3&gt;
&lt;p&gt;Filtering non-essential metrics before sending them to a data source can help you reduce costs and allow you to focus on the data that matters most.&lt;/p&gt;
&lt;p&gt;The following example demonstrates how you can filter out or drop metrics before sending them to Prometheus.&lt;/p&gt;
&lt;p&gt;Paste the following component configuration below the previous component in your &lt;code&gt;config.alloy&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;prometheus.relabel &amp;#34;filter_metrics&amp;#34; {
  rule {
    action        = &amp;#34;drop&amp;#34;
    source_labels = [&amp;#34;env&amp;#34;]
    regex         = &amp;#34;dev&amp;#34;
  }

  forward_to = [prometheus.remote_write.metrics_service.receiver]
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;a href=&#34;../../reference/components/prometheus/prometheus.relabel/&#34;&gt;&lt;code&gt;prometheus.relabel&lt;/code&gt;&lt;/a&gt; component is commonly used to filter Prometheus metrics or standardize the label set passed to one or more downstream receivers.
You can use this component to rewrite the label set of each metric sent to the receiver.
Within this component, you can define rule blocks to specify how you would like to process metrics before they&amp;rsquo;re stored or forwarded.&lt;/p&gt;
&lt;p&gt;This configuration creates a &lt;a href=&#34;../../reference/components/prometheus/prometheus.relabel/&#34;&gt;&lt;code&gt;prometheus.relabel&lt;/code&gt;&lt;/a&gt; component named &lt;code&gt;filter_metrics&lt;/code&gt; which does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It receives scraped metrics from the &lt;code&gt;scrape_metrics&lt;/code&gt; component.&lt;/li&gt;
&lt;li&gt;It tells Alloy to drop metrics that have an &lt;code&gt;&amp;quot;env&amp;quot;&lt;/code&gt; label equal to &lt;code&gt;&amp;quot;dev&amp;quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It forwards the processed metrics to the receiver of another component called &lt;code&gt;metrics_service&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;third-component-write-metrics-to-prometheus&#34;&gt;Third component: Write metrics to Prometheus&lt;/h3&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Prometheus typically scrapes metrics from targets using a pull mechanism.
This component pushes metrics to Prometheus using the remote write protocol.
To accept these remote write requests, start Prometheus with the &lt;code&gt;--web.enable-remote-write-receiver&lt;/code&gt; flag.
For more information, refer to the &lt;a href=&#34;https://prometheus.io/docs/prometheus/latest/storage/#overview&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Remote storage integrations&lt;/a&gt; topic in the Prometheus documentation.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;Paste the following component configuration below the previous component in your &lt;code&gt;config.alloy&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;prometheus.remote_write &amp;#34;metrics_service&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:9090/api/v1/write&amp;#34;

        // basic_auth {
        //   username = &amp;#34;admin&amp;#34;
        //   password = &amp;#34;admin&amp;#34;
        // }
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This final component creates a &lt;a href=&#34;../../reference/components/prometheus/prometheus.remote_write/&#34;&gt;&lt;code&gt;prometheus.remote_write&lt;/code&gt;&lt;/a&gt; component named &lt;code&gt;metrics_service&lt;/code&gt; that points to &lt;code&gt;http://localhost:9090/api/v1/write&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This completes the simple configuration pipeline.&lt;/p&gt;
&lt;!-- INTERACTIVE ignore START --&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;The &lt;code&gt;basic_auth&lt;/code&gt; is commented out because the local &lt;code&gt;docker compose&lt;/code&gt; stack doesn&amp;rsquo;t require it.
It&amp;rsquo;s included in this example to show how you can configure authorization for other environments.&lt;/p&gt;
&lt;p&gt;For further authorization options, refer to the &lt;a href=&#34;../../reference/components/prometheus/prometheus.remote_write/&#34;&gt;&lt;code&gt;prometheus.remote_write&lt;/code&gt;&lt;/a&gt; component documentation.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;!-- INTERACTIVE ignore END --&gt;

&lt;p&gt;This connects directly to the Prometheus instance running in the Docker container.&lt;/p&gt;
&lt;!-- INTERACTIVE page step1.md END --&gt;
&lt;!-- INTERACTIVE page step2.md START --&gt;
&lt;h2 id=&#34;reload-the-configuration&#34;&gt;Reload the configuration&lt;/h2&gt;
&lt;p&gt;Copy your local &lt;code&gt;config.alloy&lt;/code&gt; file into the default Alloy configuration file location.&lt;/p&gt;

&lt;!-- INTERACTIVE ignore START --&gt;



  

  






  

  



  &lt;div class=&#34;code&#34; x-data=&#34;app_code([&amp;#34;macos&amp;#34;,&amp;#34;linux&amp;#34;], false)&#34; x-init=&#34;init()&#34; data-codetoggle=&#34;true&#34;&gt;
    &lt;div class=&#34;toggle-toolbar &#34;&gt;
      &lt;div&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;macos&#39; }&#34; @click=&#34;$store.code.language = &#39;macos&#39;&#34;&gt;
              &lt;span&gt;macos&lt;/span&gt;
            &lt;/button&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;linux&#39; }&#34; @click=&#34;$store.code.language = &#39;linux&#39;&#34;&gt;
              &lt;span&gt;linux&lt;/span&gt;
            &lt;/button&gt;&lt;/div&gt;
      &lt;div class=&#34;d-flex&#34;&gt;&lt;span class=&#34;code-clipboard&#34; x-ref=&#34;tooltip&#34;&gt;
          &lt;button @click=&#34;copy()&#34;&gt;
            &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
            &lt;span&gt;Copy&lt;/span&gt;
          &lt;/button&gt;
        &lt;/span&gt;
      &lt;/div&gt;
      &lt;div class=&#34;toggle-toolbar__border&#34;&gt;&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#34;code-rendered&#34; &gt;
      
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;macos&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-macos&#34;&gt;sudo cp config.alloy $(brew --prefix)/etc/alloy/config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;linux&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-linux&#34;&gt;sudo cp config.alloy /etc/alloy/config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

    &lt;/div&gt;
  &lt;/div&gt;


&lt;!-- INTERACTIVE ignore END --&gt;
&lt;p&gt;Call the &lt;code&gt;/-/reload&lt;/code&gt; endpoint to tell Alloy to reload the configuration file without a system service restart.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;curl -X POST http://localhost:12345/-/reload&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- INTERACTIVE ignore START --&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;This step uses the Alloy UI, on &lt;code&gt;localhost&lt;/code&gt; port &lt;code&gt;12345&lt;/code&gt;.
If you choose to run Alloy in a Docker container, make sure you use the &lt;code&gt;--server.http.listen-addr=0.0.0.0:12345&lt;/code&gt; argument.&lt;/p&gt;
&lt;p&gt;If you don&amp;rsquo;t use this argument, the &lt;a href=&#34;../../troubleshoot/debug/#alloy-ui&#34;&gt;debugging UI&lt;/a&gt; won&amp;rsquo;t be available outside of the Docker container.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;!-- INTERACTIVE ignore END --&gt;

&lt;p&gt;Optional: You can do a system service restart Alloy and load the configuration file:&lt;/p&gt;

&lt;!-- INTERACTIVE ignore START --&gt;



  

  






  

  



  &lt;div class=&#34;code&#34; x-data=&#34;app_code([&amp;#34;macos&amp;#34;,&amp;#34;linux&amp;#34;], false)&#34; x-init=&#34;init()&#34; data-codetoggle=&#34;true&#34;&gt;
    &lt;div class=&#34;toggle-toolbar &#34;&gt;
      &lt;div&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;macos&#39; }&#34; @click=&#34;$store.code.language = &#39;macos&#39;&#34;&gt;
              &lt;span&gt;macos&lt;/span&gt;
            &lt;/button&gt;&lt;button class=&#34;toggle-toolbar__item&#34; :class=&#34;{ &#39;toggle-toolbar__item-active&#39;: active === &#39;linux&#39; }&#34; @click=&#34;$store.code.language = &#39;linux&#39;&#34;&gt;
              &lt;span&gt;linux&lt;/span&gt;
            &lt;/button&gt;&lt;/div&gt;
      &lt;div class=&#34;d-flex&#34;&gt;&lt;span class=&#34;code-clipboard&#34; x-ref=&#34;tooltip&#34;&gt;
          &lt;button @click=&#34;copy()&#34;&gt;
            &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
            &lt;span&gt;Copy&lt;/span&gt;
          &lt;/button&gt;
        &lt;/span&gt;
      &lt;/div&gt;
      &lt;div class=&#34;toggle-toolbar__border&#34;&gt;&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#34;code-rendered&#34; &gt;
      
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;macos&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-macos&#34;&gt;brew services restart grafana/grafana/alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;linux&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-linux&#34;&gt;sudo systemctl reload alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

    &lt;/div&gt;
  &lt;/div&gt;


&lt;!-- INTERACTIVE ignore END --&gt;
&lt;!-- INTERACTIVE page step2.md END --&gt;
&lt;!-- INTERACTIVE page step3.md START --&gt;
&lt;h2 id=&#34;inspect-your-configuration-in-the-alloy-ui&#34;&gt;Inspect your configuration in the Alloy UI&lt;/h2&gt;
&lt;p&gt;Open &lt;a href=&#34;http://localhost:12345&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:12345&lt;/a&gt; and click the &lt;strong&gt;Graph&lt;/strong&gt; tab at the top.
The graph should look similar to the following:&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 1512px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/tutorial/Metrics-inspect-your-config.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/tutorial/Metrics-inspect-your-config.png&#34;data-srcset=&#34;/media/docs/alloy/tutorial/Metrics-inspect-your-config.png?w=320 320w, /media/docs/alloy/tutorial/Metrics-inspect-your-config.png?w=550 550w, /media/docs/alloy/tutorial/Metrics-inspect-your-config.png?w=750 750w, /media/docs/alloy/tutorial/Metrics-inspect-your-config.png?w=900 900w, /media/docs/alloy/tutorial/Metrics-inspect-your-config.png?w=1040 1040w, /media/docs/alloy/tutorial/Metrics-inspect-your-config.png?w=1240 1240w, /media/docs/alloy/tutorial/Metrics-inspect-your-config.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Your configuration in the Alloy UI&#34;width=&#34;1512&#34;height=&#34;971&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/tutorial/Metrics-inspect-your-config.png&#34;
            alt=&#34;Your configuration in the Alloy UI&#34;width=&#34;1512&#34;height=&#34;971&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;The Alloy UI shows you a visual representation of the pipeline you built with your Alloy component configuration.&lt;/p&gt;
&lt;p&gt;You can see that the components are healthy, and you are ready to explore the metrics in Grafana.&lt;/p&gt;
&lt;!-- INTERACTIVE page step3.md END --&gt;
&lt;!-- INTERACTIVE page step4.md START --&gt;
&lt;h2 id=&#34;log-into-grafana-and-explore-metrics-in-prometheus&#34;&gt;Log into Grafana and explore metrics in Prometheus&lt;/h2&gt;
&lt;p&gt;Open &lt;a href=&#34;http://localhost:3000/explore/metrics/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore/metrics/&lt;/a&gt; to access the &lt;strong&gt;Metrics Drilldown&lt;/strong&gt; feature in Grafana.&lt;/p&gt;
&lt;p&gt;From here you can visually explore the metrics sent to Prometheus by Alloy.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 3170px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/explore-metrics.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/explore-metrics.png&#34;data-srcset=&#34;/media/docs/alloy/explore-metrics.png?w=320 320w, /media/docs/alloy/explore-metrics.png?w=550 550w, /media/docs/alloy/explore-metrics.png?w=750 750w, /media/docs/alloy/explore-metrics.png?w=900 900w, /media/docs/alloy/explore-metrics.png?w=1040 1040w, /media/docs/alloy/explore-metrics.png?w=1240 1240w, /media/docs/alloy/explore-metrics.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Explore Metrics App&#34;width=&#34;3170&#34;height=&#34;1740&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/explore-metrics.png&#34;
            alt=&#34;Explore Metrics App&#34;width=&#34;3170&#34;height=&#34;1740&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;You can also build PromQL queries manually to explore the data further.&lt;/p&gt;
&lt;p&gt;Open &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; to access the &lt;strong&gt;Explore&lt;/strong&gt; feature in Grafana.&lt;/p&gt;
&lt;p&gt;Select Prometheus as the data source and click the &lt;strong&gt;Metrics Browser&lt;/strong&gt; button to select the metric, labels, and values for your labels.&lt;/p&gt;
&lt;p&gt;Here you can see that metrics are flowing through to Prometheus as expected, and the end-to-end configuration was successful.&lt;/p&gt;
&lt;!-- INTERACTIVE page step4.md END --&gt;
&lt;!-- INTERACTIVE page finish.md START --&gt;
&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;p&gt;You have configured Alloy to collect and process metrics from your local host and send them to your local Grafana stack.&lt;/p&gt;
&lt;!-- INTERACTIVE page finish.md END --&gt;
]]></content><description>&lt;!-- INTERACTIVE page intro.md START -->
&lt;h1 id="use-grafana-alloy-to-send-metrics-to-prometheus">Use Grafana Alloy to send metrics to Prometheus&lt;/h1>
&lt;p>In the &lt;a href="../send-logs-to-loki/">previous tutorial&lt;/a>, you learned how to configure Alloy to collect and process logs from your local machine and send them to Loki.&lt;/p></description></item><item><title>First components and the standard library in Grafana Alloy</title><link>https://grafana.com/docs/alloy/v1.15/tutorials/first-components-and-stdlib/</link><pubDate>Mon, 30 Mar 2026 15:47:22 +0000</pubDate><guid>https://grafana.com/docs/alloy/v1.15/tutorials/first-components-and-stdlib/</guid><content><![CDATA[&lt;h1 id=&#34;first-components-and-the-standard-library-in-grafana-alloy&#34;&gt;First components and the standard library in Grafana Alloy&lt;/h1&gt;
&lt;p&gt;This tutorial covers the basics of the Alloy configuration syntax and the standard library.
It introduces a basic pipeline that collects metrics from the host and sends them to Prometheus.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;p&gt;To complete this tutorial:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You must set up a &lt;a href=&#34;../send-logs-to-loki/#set-up-a-local-grafana-instance&#34;&gt;local Grafana instance&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;recommended-reading&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../get-started/configuration-syntax/&#34;&gt;Alloy configuration syntax&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;alloy-configuration-syntax-basics&#34;&gt;Alloy configuration syntax basics&lt;/h2&gt;
&lt;p&gt;An Alloy configuration file contains three elements:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Attributes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;key = value&lt;/code&gt; pairs used to configure individual settings.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;url = &amp;#34;http://localhost:9090&amp;#34;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Expressions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Expressions are used to compute values.
They can be constant values, for example &lt;code&gt;&amp;quot;localhost:9090&amp;quot;&lt;/code&gt;. They can be more complex, for example, referencing a component&amp;rsquo;s export: &lt;code&gt;prometheus.exporter.unix.targets&lt;/code&gt;.
They can also be a mathematical expression: &lt;code&gt;(1 &#43; 2) * 3&lt;/code&gt;, or a standard library function call: &lt;code&gt;sys.env(&amp;quot;HOME&amp;quot;)&lt;/code&gt;.
If you are curious, you can find a list of available standard library functions in the &lt;a href=&#34;../../reference/stdlib/&#34;&gt;Standard library documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Blocks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Blocks are used to configure components with groups of attributes or nested blocks.
The following example block can be used to configure the logging output of Alloy:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;logging {
    level  = &amp;#34;debug&amp;#34;
    format = &amp;#34;json&amp;#34;
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;The default log level is &lt;code&gt;info&lt;/code&gt; and the default log format is &lt;code&gt;logfmt&lt;/code&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;Try pasting this into &lt;code&gt;config.alloy&lt;/code&gt; and running &lt;code&gt;&amp;lt;BINARY_FILE_PATH&amp;gt; run config.alloy&lt;/code&gt; to see what happens. Replace &lt;em&gt;&lt;code&gt;&amp;lt;BINARY_FILE_PATH&amp;gt;&lt;/code&gt;&lt;/em&gt; with the path to the Alloy binary.&lt;/p&gt;
&lt;p&gt;Congratulations, you&amp;rsquo;ve just written your first Alloy configuration file.
This configuration won&amp;rsquo;t do anything. In the next step, you add some functionality to this configuration.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Comments in Alloy syntax are prefixed with &lt;code&gt;//&lt;/code&gt; and are single-line only. For example: &lt;code&gt;// This is a comment&lt;/code&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;components&#34;&gt;Components&lt;/h2&gt;
&lt;p&gt;Components are the building blocks of an Alloy configuration. They&amp;rsquo;re configured and linked to create pipelines that collect, process, and output your telemetry data. Components are configured with &lt;code&gt;Arguments&lt;/code&gt; and have &lt;code&gt;Exports&lt;/code&gt; that may be referenced by other components.&lt;/p&gt;
&lt;h3 id=&#34;recommended-reading-1&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../get-started/components/&#34;&gt;Components&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../get-started/configuration-syntax/components/&#34;&gt;Components configuration language&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../get-started/component_controller/&#34;&gt;Component controller&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;an-example-pipeline&#34;&gt;An example pipeline&lt;/h3&gt;
&lt;p&gt;Look at the following simple pipeline:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;local.file &amp;#34;example&amp;#34; {
    filename = sys.env(&amp;#34;HOME&amp;#34;) &amp;#43; &amp;#34;/file.txt&amp;#34;
}

prometheus.remote_write &amp;#34;local_prom&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:9090/api/v1/write&amp;#34;

        basic_auth {
            username = &amp;#34;admin&amp;#34;
            password = local.file.example.content
        }
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;A list of all available components can be found in the &lt;a href=&#34;../../reference/components/&#34;&gt;Component reference&lt;/a&gt;.
Each component has a link to its documentation, which contains a description of what the component does, its arguments, its exports, and examples.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;This pipeline has two components: &lt;code&gt;local.file&lt;/code&gt; and &lt;code&gt;prometheus.remote_write&lt;/code&gt;.
The &lt;code&gt;local.file&lt;/code&gt; component is configured with a single argument, &lt;code&gt;filename&lt;/code&gt;, which is set by calling the &lt;a href=&#34;../../reference/stdlib/sys/&#34;&gt;sys.env&lt;/a&gt; standard library function to retrieve the value of the &lt;code&gt;HOME&lt;/code&gt; environment variable and concatenating it with the string &lt;code&gt;&amp;quot;file.txt&amp;quot;&lt;/code&gt;.
The &lt;code&gt;local.file&lt;/code&gt; component has a single export, &lt;code&gt;content&lt;/code&gt;, which contains the contents of the file.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;prometheus.remote_write&lt;/code&gt; component is configured with an &lt;code&gt;endpoint&lt;/code&gt; block, containing the &lt;code&gt;url&lt;/code&gt; attribute and a &lt;code&gt;basic_auth&lt;/code&gt; block.
The &lt;code&gt;url&lt;/code&gt; attribute is set to the URL of the Prometheus remote write endpoint.
The &lt;code&gt;basic_auth&lt;/code&gt; block contains the &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt; attributes, which are set to the string &lt;code&gt;&amp;quot;admin&amp;quot;&lt;/code&gt; and the &lt;code&gt;content&lt;/code&gt; export of the &lt;code&gt;local.file&lt;/code&gt; component, respectively.
The &lt;code&gt;content&lt;/code&gt; export is referenced by using the syntax &lt;code&gt;local.file.example.content&lt;/code&gt;, where &lt;code&gt;local.file.example&lt;/code&gt; is the fully qualified name of the component (the component&amp;rsquo;s type &#43; its label) and &lt;code&gt;content&lt;/code&gt; is the name of the export.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 940px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/diagram-example-basic-alloy.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/diagram-example-basic-alloy.png&#34;data-srcset=&#34;/media/docs/alloy/diagram-example-basic-alloy.png?w=320 320w, /media/docs/alloy/diagram-example-basic-alloy.png?w=550 550w, /media/docs/alloy/diagram-example-basic-alloy.png?w=750 750w, /media/docs/alloy/diagram-example-basic-alloy.png?w=900 900w, /media/docs/alloy/diagram-example-basic-alloy.png?w=1040 1040w, /media/docs/alloy/diagram-example-basic-alloy.png?w=1240 1240w, /media/docs/alloy/diagram-example-basic-alloy.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Example pipeline with local.file and prometheus.remote_write components&#34;width=&#34;940&#34;height=&#34;170&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/diagram-example-basic-alloy.png&#34;
            alt=&#34;Example pipeline with local.file and prometheus.remote_write components&#34;width=&#34;940&#34;height=&#34;170&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;The &lt;code&gt;local.file&lt;/code&gt; component&amp;rsquo;s label is set to &lt;code&gt;&amp;quot;example&amp;quot;&lt;/code&gt;, so the fully qualified name of the component is &lt;code&gt;local.file.example&lt;/code&gt;.
The &lt;code&gt;prometheus.remote_write&lt;/code&gt; component&amp;rsquo;s label is set to &lt;code&gt;&amp;quot;local_prom&amp;quot;&lt;/code&gt;, so the fully qualified name of the component is &lt;code&gt;prometheus.remote_write.local_prom&lt;/code&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;This example pipeline still doesn&amp;rsquo;t do anything, so its time to add some more components to it.&lt;/p&gt;
&lt;h2 id=&#34;send-your-first-metrics&#34;&gt;Send your first metrics&lt;/h2&gt;
&lt;p&gt;Now that you have a simple pipeline, you can send your first metrics.&lt;/p&gt;
&lt;h3 id=&#34;recommended-reading-2&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/prometheus/prometheus.exporter.unix/&#34;&gt;&lt;code&gt;prometheus.exporter.unix&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/prometheus/prometheus.scrape/&#34;&gt;&lt;code&gt;prometheus.scrape&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/prometheus/prometheus.remote_write/&#34;&gt;&lt;code&gt;prometheus.remote_write&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;modify-your-pipeline-and-scrape-the-metrics&#34;&gt;Modify your pipeline and scrape the metrics&lt;/h3&gt;
&lt;p&gt;Make a simple pipeline with a &lt;code&gt;prometheus.exporter.unix&lt;/code&gt; component, a &lt;code&gt;prometheus.scrape&lt;/code&gt; component to scrape it, and a &lt;code&gt;prometheus.remote_write&lt;/code&gt; component to send the scraped metrics to Prometheus.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;prometheus.exporter.unix &amp;#34;localhost&amp;#34; {
    // This component exposes a lot of metrics by default, so we will keep all of the default arguments.
}

prometheus.scrape &amp;#34;default&amp;#34; {
    // Setting the scrape interval lower to make it faster to be able to see the metrics
    scrape_interval = &amp;#34;10s&amp;#34;

    targets    = prometheus.exporter.unix.localhost.targets
    forward_to = [
        prometheus.remote_write.local_prom.receiver,
    ]
}

prometheus.remote_write &amp;#34;local_prom&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:9090/api/v1/write&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run Alloy with the following command:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;&amp;lt;BINARY_FILE_PATH&amp;gt; run config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Replace the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;code&gt;&amp;lt;BINARY_FILE_PATH&amp;gt;&lt;/code&gt;&lt;/em&gt;: The path to the Alloy binary.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Navigate to &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; in your browser.
After ~15-20 seconds, you should be able to see the metrics from the &lt;code&gt;prometheus.exporter.unix&lt;/code&gt; component.
Try querying for &lt;code&gt;node_memory_Active_bytes&lt;/code&gt; to see the active memory of your host.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 2536px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/screenshot-memory-usage.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/screenshot-memory-usage.png&#34;data-srcset=&#34;/media/docs/alloy/screenshot-memory-usage.png?w=320 320w, /media/docs/alloy/screenshot-memory-usage.png?w=550 550w, /media/docs/alloy/screenshot-memory-usage.png?w=750 750w, /media/docs/alloy/screenshot-memory-usage.png?w=900 900w, /media/docs/alloy/screenshot-memory-usage.png?w=1040 1040w, /media/docs/alloy/screenshot-memory-usage.png?w=1240 1240w, /media/docs/alloy/screenshot-memory-usage.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Screenshot of node_memory_Active_bytes query in Grafana&#34;width=&#34;2536&#34;height=&#34;844&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/screenshot-memory-usage.png&#34;
            alt=&#34;Screenshot of node_memory_Active_bytes query in Grafana&#34;width=&#34;2536&#34;height=&#34;844&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;h2 id=&#34;visualize-the-relationship-between-components&#34;&gt;Visualize the relationship between components&lt;/h2&gt;
&lt;p&gt;The following diagram is an example pipeline:&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 1100px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png&#34;data-srcset=&#34;/media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png?w=320 320w, /media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png?w=550 550w, /media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png?w=750 750w, /media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png?w=900 900w, /media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png?w=1040 1040w, /media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png?w=1240 1240w, /media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Example pipeline with a prometheus.scrape, prometheus.exporter.unix, and prometheus.remote_write components&#34;width=&#34;1100&#34;height=&#34;420&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/diagram-example-pipeline-prometheus.scrape-alloy.png&#34;
            alt=&#34;Example pipeline with a prometheus.scrape, prometheus.exporter.unix, and prometheus.remote_write components&#34;width=&#34;1100&#34;height=&#34;420&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;Your pipeline configuration defines three components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;prometheus.scrape&lt;/code&gt; - A component that scrapes metrics from components that export targets.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;prometheus.exporter.unix&lt;/code&gt; - A component that exports metrics from the host, built around &lt;a href=&#34;https://github.com/prometheus/node_exporter&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;node_exporter&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;prometheus.remote_write&lt;/code&gt; - A component that sends metrics to a Prometheus remote-write compatible endpoint.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;prometheus.scrape&lt;/code&gt; component references the &lt;code&gt;prometheus.exporter.unix&lt;/code&gt; component&amp;rsquo;s targets export, which is a list of scrape targets.
The &lt;code&gt;prometheus.scrape&lt;/code&gt; component forwards the scraped metrics to the &lt;code&gt;prometheus.remote_write&lt;/code&gt; component.&lt;/p&gt;
&lt;p&gt;One rule is that components can&amp;rsquo;t form a cycle.
This means that a component can&amp;rsquo;t reference itself directly or indirectly.
This is to prevent infinite loops from forming in the pipeline.&lt;/p&gt;
&lt;h2 id=&#34;exercise&#34;&gt;Exercise&lt;/h2&gt;
&lt;p&gt;The following exercise guides you through modifying your pipeline to scrape metrics from Redis.&lt;/p&gt;
&lt;h3 id=&#34;recommended-reading-3&#34;&gt;Recommended Reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/prometheus/prometheus.exporter.redis/&#34;&gt;&lt;code&gt;prometheus.exporter.redis&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Start a container running Redis and configure Alloy to scrape the metrics.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;docker container run -d --name alloy-redis -p 6379:6379 --rm redis&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Try modifying the pipeline to scrape metrics from the Redis exporter.
You can refer to the &lt;a href=&#34;../../reference/components/prometheus/prometheus.exporter.redis/&#34;&gt;&lt;code&gt;prometheus.exporter.redis&lt;/code&gt;&lt;/a&gt; component documentation for more information on how to configure it.&lt;/p&gt;
&lt;p&gt;To give a visual hint, you want to create a pipeline that looks like this:&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 1300px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/diagram-example-pipeline-exercise-alloy.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/diagram-example-pipeline-exercise-alloy.png&#34;data-srcset=&#34;/media/docs/alloy/diagram-example-pipeline-exercise-alloy.png?w=320 320w, /media/docs/alloy/diagram-example-pipeline-exercise-alloy.png?w=550 550w, /media/docs/alloy/diagram-example-pipeline-exercise-alloy.png?w=750 750w, /media/docs/alloy/diagram-example-pipeline-exercise-alloy.png?w=900 900w, /media/docs/alloy/diagram-example-pipeline-exercise-alloy.png?w=1040 1040w, /media/docs/alloy/diagram-example-pipeline-exercise-alloy.png?w=1240 1240w, /media/docs/alloy/diagram-example-pipeline-exercise-alloy.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Exercise pipeline, with a scrape, unix_exporter, redis_exporter, and remote_write component&#34;width=&#34;1300&#34;height=&#34;480&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/diagram-example-pipeline-exercise-alloy.png&#34;
            alt=&#34;Exercise pipeline, with a scrape, unix_exporter, redis_exporter, and remote_write component&#34;width=&#34;1300&#34;height=&#34;480&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;Refer to the &lt;a href=&#34;../../reference/stdlib/array/&#34;&gt;&lt;code&gt;array.concat&lt;/code&gt;&lt;/a&gt; standard library function for information about combining lists of values into a single list.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;You can run Alloy with the new configuration file with the following command:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;&amp;lt;BINARY_FILE_PATH&amp;gt; run config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Replace the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;code&gt;&amp;lt;BINARY_FILE_PATH&amp;gt;&lt;/code&gt;&lt;/em&gt;: The path to the Alloy binary.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Navigate to &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; in your browser.
After the first scrape, you should be able to query for &lt;code&gt;redis&lt;/code&gt; metrics as well as &lt;code&gt;node&lt;/code&gt; metrics.&lt;/p&gt;
&lt;p&gt;To shut down the Redis container, run:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;docker container stop alloy-redis&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If you get stuck, you can always view a solution here:&lt;/p&gt;
&lt;div class=&#34;collapse&#34; x-data=&#34;app_collapse()&#34;&gt;
  &lt;button class=&#34;collapse-trigger&#34; @click=&#34;toggle()&#34;&gt;
    &lt;span class=&#34;body-large&#34;&gt;Solution&lt;/span&gt;
    &lt;span class=&#34;collapse-trigger__icon&#34; :class=&#34;{ &#39;collapse-trigger__icon-open&#39; : open }&#34;&gt;
      
  &lt;svg width=&#34;27&#34; height=&#34;26&#34; viewBox=&#34;0 0 27 26&#34; fill=&#34;none&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;path opacity=&#34;0.2&#34; d=&#34;M1.73047 12.8359C1.73047 19.4634 7.10305 24.8359 13.7305 24.8359C20.3579 24.8359 25.7305 19.4634 25.7305 12.8359C25.7305 6.20852 20.3579 0.835937 13.7305 0.835937C7.10305 0.835937 1.73047 6.20852 1.73047 12.8359Z&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M18.2344 12.8359L9.23438 12.8359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M13.7344 8.33594L13.7344 17.3359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;/svg&gt;


    &lt;/span&gt;
  &lt;/button&gt;
  &lt;div class=&#34;collapse-content&#34; x-ref=&#34;content&#34; hidden=&#34;until-found&#34;&gt;
    &lt;div class=&#34;collapse-content__inner&#34; x-ref=&#34;content-inner&#34;&gt;
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;// Configure your first components, learn about the standard library, and learn how to run Grafana Alloy

// prometheus.exporter.redis collects information about Redis and exposes
// targets for other components to use
prometheus.exporter.redis &amp;#34;local_redis&amp;#34; {
    redis_addr = &amp;#34;localhost:6379&amp;#34;
}

prometheus.exporter.unix &amp;#34;localhost&amp;#34; { }

// prometheus.scrape scrapes the targets that it is configured with and forwards
// the metrics to other components (typically prometheus.relabel or prometheus.remote_write)
prometheus.scrape &amp;#34;default&amp;#34; {
    // This is scraping too often for typical use-cases, but is easier for testing and demo-ing!
    scrape_interval = &amp;#34;10s&amp;#34;

    // Here, prometheus.exporter.redis.local_redis.targets refers to the &amp;#39;targets&amp;#39; export
    // of the prometheus.exporter.redis component with the label &amp;#34;local_redis&amp;#34;.
    //
    // If you have more than one set of targets that you would like to scrape, you can use
    // the &amp;#39;array.concat&amp;#39; function from the standard library to combine them.
    targets    = array.concat(prometheus.exporter.redis.local_redis.targets, prometheus.exporter.unix.localhost.targets)
    forward_to = [prometheus.remote_write.local_prom.receiver]
}

// prometheus.remote_write exports a &amp;#39;receiver&amp;#39;, which other components can forward
// metrics to and it will remote_write them to the configured endpoint(s)
prometheus.remote_write &amp;#34;local_prom&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:9090/api/v1/write&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;finish-up-and-next-steps&#34;&gt;Finish up and next steps&lt;/h2&gt;
&lt;p&gt;You might have noticed that running Alloy with the configurations created a directory called &lt;code&gt;data-alloy&lt;/code&gt; in the directory you ran Alloy from.
This directory is where components can store data, such as the &lt;code&gt;prometheus.exporter.unix&lt;/code&gt; component storing its Write Ahead Log (WAL).
If you look in the directory, do you notice anything interesting? The directory for each component is the fully qualified name.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;d like to store the data elsewhere, you can specify a different directory by supplying the &lt;code&gt;--storage.path&lt;/code&gt; flag to Alloy&amp;rsquo;s run command, for example, &lt;code&gt;&amp;lt;BINARY_FILE_PATH&amp;gt; run config.alloy --storage.path /etc/alloy&lt;/code&gt;. Replace &lt;em&gt;&lt;code&gt;&amp;lt;BINARY_FILE_PATH&amp;gt;&lt;/code&gt;&lt;/em&gt; with the path to the Alloy binary.
Generally, you can use a persistent directory for this, as some components may use the data stored in this directory to perform their function.&lt;/p&gt;
&lt;p&gt;In the next tutorial, you learn how to configure Alloy to collect logs from a file and send them to Loki.
You also learn how to use different components to process metrics and logs.&lt;/p&gt;
]]></content><description>&lt;h1 id="first-components-and-the-standard-library-in-grafana-alloy">First components and the standard library in Grafana Alloy&lt;/h1>
&lt;p>This tutorial covers the basics of the Alloy configuration syntax and the standard library.
It introduces a basic pipeline that collects metrics from the host and sends them to Prometheus.&lt;/p></description></item><item><title>Logs and relabeling basics in Grafana Alloy</title><link>https://grafana.com/docs/alloy/v1.15/tutorials/logs-and-relabeling-basics/</link><pubDate>Mon, 30 Mar 2026 15:47:22 +0000</pubDate><guid>https://grafana.com/docs/alloy/v1.15/tutorials/logs-and-relabeling-basics/</guid><content><![CDATA[&lt;h1 id=&#34;logs-and-relabeling-basics-in-grafana-alloy&#34;&gt;Logs and relabeling basics in Grafana Alloy&lt;/h1&gt;
&lt;p&gt;This tutorial covers some basic metric relabeling, and shows you how to send logs to Loki.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;p&gt;To complete this tutorial:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You must complete the &lt;a href=&#34;../first-components-and-stdlib/&#34;&gt;First components and the standard library&lt;/a&gt; tutorial.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;relabel-metrics&#34;&gt;Relabel metrics&lt;/h2&gt;
&lt;p&gt;Now that you have built a basic pipeline and scraped some metrics, you can use the &lt;code&gt;prometheus.relabel&lt;/code&gt; component to relabel metrics.&lt;/p&gt;
&lt;h3 id=&#34;recommended-reading&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/prometheus/prometheus.relabel/&#34;&gt;&lt;code&gt;prometheus.relabel&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;add-a-prometheusrelabel-component-to-your-pipeline&#34;&gt;Add a &lt;code&gt;prometheus.relabel&lt;/code&gt; component to your pipeline&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;prometheus.relabel&lt;/code&gt; component allows you to perform Prometheus relabeling on metrics and is similar to the &lt;code&gt;relabel_configs&lt;/code&gt; section of a Prometheus scrape configuration.&lt;/p&gt;
&lt;p&gt;Add a &lt;code&gt;prometheus.relabel&lt;/code&gt; component to a basic pipeline and add labels.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;prometheus.exporter.unix &amp;#34;localhost&amp;#34; { }

prometheus.scrape &amp;#34;default&amp;#34; {
    scrape_interval = &amp;#34;10s&amp;#34;

    targets    = prometheus.exporter.unix.localhost.targets
    forward_to = [
        prometheus.relabel.example.receiver,
    ]
}

prometheus.relabel &amp;#34;example&amp;#34; {
    forward_to = [
        prometheus.remote_write.local_prom.receiver,
    ]

    rule {
        action       = &amp;#34;replace&amp;#34;
        target_label = &amp;#34;os&amp;#34;
        replacement  = constants.os
    }
}

prometheus.remote_write &amp;#34;local_prom&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:9090/api/v1/write&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You have created the following pipeline:&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 1520px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/diagram-example-relabel-alloy.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/diagram-example-relabel-alloy.png&#34;data-srcset=&#34;/media/docs/alloy/diagram-example-relabel-alloy.png?w=320 320w, /media/docs/alloy/diagram-example-relabel-alloy.png?w=550 550w, /media/docs/alloy/diagram-example-relabel-alloy.png?w=750 750w, /media/docs/alloy/diagram-example-relabel-alloy.png?w=900 900w, /media/docs/alloy/diagram-example-relabel-alloy.png?w=1040 1040w, /media/docs/alloy/diagram-example-relabel-alloy.png?w=1240 1240w, /media/docs/alloy/diagram-example-relabel-alloy.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Diagram of pipeline that scrapes prometheus.exporter.unix, relabels the metrics, and remote_writes them&#34;width=&#34;1520&#34;height=&#34;480&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/diagram-example-relabel-alloy.png&#34;
            alt=&#34;Diagram of pipeline that scrapes prometheus.exporter.unix, relabels the metrics, and remote_writes them&#34;width=&#34;1520&#34;height=&#34;480&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;This pipeline has a &lt;code&gt;prometheus.relabel&lt;/code&gt; component that has a single rule.
This rule has the &lt;code&gt;replace&lt;/code&gt; action, which replaces the value of the &lt;code&gt;os&lt;/code&gt; label with a special value: &lt;code&gt;constants.os&lt;/code&gt;.
This value is a special constant that&amp;rsquo;s replaced with the OS of the host Alloy is running on.
You can see the other available constants in the &lt;a href=&#34;../../reference/stdlib/constants/&#34;&gt;constants&lt;/a&gt; documentation.
This example has one rule block, but you can have as many as you want.
Each rule block is applied in order.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Multiple &lt;code&gt;keep&lt;/code&gt; rules are applied sequentially.
If a target fails the first &lt;code&gt;keep&lt;/code&gt;, it’s dropped immediately, so two separate &lt;code&gt;keep&lt;/code&gt; rules act like a logical AND.
To implement OR logic across multiple criteria, you can combine them in one rule using multiple &lt;code&gt;source_labels&lt;/code&gt; and a compound regular expression.&lt;/p&gt;
&lt;p&gt;The following example demonstrates how you can implement OR in one rule:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;rule {
 source_labels = [&amp;#34;__meta_kubernetes_pod_annotation_A&amp;#34;, &amp;#34;__meta_kubernetes_pod_label_B&amp;#34;]
 regex         = &amp;#34;(true;.*|.*;true)&amp;#34;
 action        = &amp;#34;keep&amp;#34;
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;If you run Alloy and navigate to &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt;, you can see the &lt;code&gt;os&lt;/code&gt; label on the metrics.
Try querying for &lt;code&gt;node_context_switches_total&lt;/code&gt; and look at the labels.&lt;/p&gt;
&lt;p&gt;Relabeling uses the same rules as Prometheus. You can always refer to the &lt;a href=&#34;../../reference/components/prometheus/prometheus.relabel/#rule-block&#34;&gt;&lt;code&gt;prometheus.relabel rule-block&lt;/code&gt;&lt;/a&gt; documentation for a full list of available options.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;You can forward multiple components to one &lt;code&gt;prometheus.relabel&lt;/code&gt; component. This allows you to apply the same relabeling rules to multiple pipelines.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;



&lt;div class=&#34;admonition admonition-warning&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Warning&lt;/p&gt;&lt;p&gt;There is an issue commonly faced when relabeling and using labels that start with &lt;code&gt;__&lt;/code&gt; (double underscore).
These labels are considered internal and are dropped before relabeling rules from a &lt;code&gt;prometheus.relabel&lt;/code&gt; component are applied.
If you would like to keep or act on these kinds of labels, use a &lt;a href=&#34;../../reference/components/discovery/discovery.relabel/&#34;&gt;&lt;code&gt;discovery.relabel&lt;/code&gt;&lt;/a&gt; component.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h2 id=&#34;send-logs-to-loki&#34;&gt;Send logs to Loki&lt;/h2&gt;
&lt;p&gt;Now that you&amp;rsquo;ve created components and chained them together, you can collect some logs and send them to Loki.&lt;/p&gt;
&lt;h3 id=&#34;recommended-reading-1&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/local/local.file_match/&#34;&gt;&lt;code&gt;local.file_match&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/loki/loki.source.file/&#34;&gt;&lt;code&gt;loki.source.file&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/loki/loki.write/&#34;&gt;&lt;code&gt;loki.write&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;find-and-collect-the-logs&#34;&gt;Find and collect the logs&lt;/h3&gt;
&lt;p&gt;You can use the &lt;code&gt;local.file_match&lt;/code&gt; component to perform file discovery, the &lt;code&gt;loki.source.file&lt;/code&gt; to collect the logs, and the &lt;code&gt;loki.write&lt;/code&gt; component to send the logs to Loki.&lt;/p&gt;
&lt;p&gt;Before doing this, make sure you have a log file to scrape.
You can use the &lt;code&gt;echo&lt;/code&gt; command to create a file with some log content.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;mkdir -p /tmp/alloy-logs
echo &amp;#34;This is a log line&amp;#34; &amp;gt; /tmp/alloy-logs/log.log&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that you have a log file, you can create a pipeline to scrape it.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;local.file_match &amp;#34;tmplogs&amp;#34; {
    path_targets = [{&amp;#34;__path__&amp;#34; = &amp;#34;/tmp/alloy-logs/*.log&amp;#34;}]
}

loki.source.file &amp;#34;local_files&amp;#34; {
    targets    = local.file_match.tmplogs.targets
    forward_to = [loki.write.local_loki.receiver]
}

loki.write &amp;#34;local_loki&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:3100/loki/api/v1/push&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The rough flow of this pipeline is:&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 900px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/diagram-example-logs-loki-alloy.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/diagram-example-logs-loki-alloy.png&#34;data-srcset=&#34;/media/docs/alloy/diagram-example-logs-loki-alloy.png?w=320 320w, /media/docs/alloy/diagram-example-logs-loki-alloy.png?w=550 550w, /media/docs/alloy/diagram-example-logs-loki-alloy.png?w=750 750w, /media/docs/alloy/diagram-example-logs-loki-alloy.png?w=900 900w, /media/docs/alloy/diagram-example-logs-loki-alloy.png?w=1040 1040w, /media/docs/alloy/diagram-example-logs-loki-alloy.png?w=1240 1240w, /media/docs/alloy/diagram-example-logs-loki-alloy.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Diagram of pipeline that collects logs from /tmp/alloy-logs and writes them to a local Loki instance&#34;width=&#34;900&#34;height=&#34;519&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/diagram-example-logs-loki-alloy.png&#34;
            alt=&#34;Diagram of pipeline that collects logs from /tmp/alloy-logs and writes them to a local Loki instance&#34;width=&#34;900&#34;height=&#34;519&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;If you navigate to &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; and switch the data source to &lt;code&gt;Loki&lt;/code&gt;, you can query for &lt;code&gt;{filename=&amp;quot;/tmp/alloy-logs/log.log&amp;quot;}&lt;/code&gt; and see the log line you created earlier.
Try running the following command to add more logs to the file.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;echo &amp;#34;This is another log line!&amp;#34; &amp;gt;&amp;gt; /tmp/alloy-logs/log.log&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If you re-execute the query, you can see the new log lines.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 2531px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/screenshot-log-lines.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/screenshot-log-lines.png&#34;data-srcset=&#34;/media/docs/alloy/screenshot-log-lines.png?w=320 320w, /media/docs/alloy/screenshot-log-lines.png?w=550 550w, /media/docs/alloy/screenshot-log-lines.png?w=750 750w, /media/docs/alloy/screenshot-log-lines.png?w=900 900w, /media/docs/alloy/screenshot-log-lines.png?w=1040 1040w, /media/docs/alloy/screenshot-log-lines.png?w=1240 1240w, /media/docs/alloy/screenshot-log-lines.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Grafana Explore view of example log lines&#34;width=&#34;2531&#34;height=&#34;905&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/screenshot-log-lines.png&#34;
            alt=&#34;Grafana Explore view of example log lines&#34;width=&#34;2531&#34;height=&#34;905&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;If you are curious how Alloy keeps track of where it&amp;rsquo;s in a log file, you can look at &lt;code&gt;data-alloy/loki.source.file.local_files/positions.yml&lt;/code&gt;.
If you delete this file, Alloy starts reading from the beginning of the file again, which is why keeping the Alloy&amp;rsquo;s data directory in a persistent location is desirable.&lt;/p&gt;
&lt;h2 id=&#34;exercise&#34;&gt;Exercise&lt;/h2&gt;
&lt;p&gt;The following exercise guides you through adding a label to the logs, and filtering the results.&lt;/p&gt;
&lt;h3 id=&#34;recommended-reading-2&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../reference/components/loki/loki.relabel/&#34;&gt;&lt;code&gt;loki.relabel&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../reference/components/loki/loki.process/&#34;&gt;&lt;code&gt;loki.process&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;add-a-label-to-logs&#34;&gt;Add a Label to Logs&lt;/h3&gt;
&lt;p&gt;This exercise has two parts, and builds on the previous example.
Start by adding an &lt;code&gt;os&lt;/code&gt; label (just like the Prometheus example) to all of the logs you collect.&lt;/p&gt;
&lt;p&gt;Modify the following snippet to add the label &lt;code&gt;os&lt;/code&gt; with the value of the &lt;code&gt;os&lt;/code&gt; constant.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;local.file_match &amp;#34;tmplogs&amp;#34; {
    path_targets = [{&amp;#34;__path__&amp;#34; = &amp;#34;/tmp/alloy-logs/*.log&amp;#34;}]
}

loki.source.file &amp;#34;local_files&amp;#34; {
    targets    = local.file_match.tmplogs.targets
    forward_to = [loki.write.local_loki.receiver]
}

loki.write &amp;#34;local_loki&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:3100/loki/api/v1/push&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;You can use the &lt;a href=&#34;../../reference/components/loki/loki.relabel/&#34;&gt;&lt;code&gt;loki.relabel&lt;/code&gt;&lt;/a&gt; component to relabel and add labels, just like you can with the &lt;a href=&#34;../../reference/components/prometheus/prometheus.relabel/&#34;&gt;&lt;code&gt;prometheus.relabel&lt;/code&gt;&lt;/a&gt; component.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;Run Alloy and execute the following:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;echo &amp;#39;level=info msg=&amp;#34;INFO: This is an info level log!&amp;#34;&amp;#39; &amp;gt;&amp;gt; /tmp/alloy-logs/log.log
echo &amp;#39;level=warn msg=&amp;#34;WARN: This is a warn level log!&amp;#34;&amp;#39; &amp;gt;&amp;gt; /tmp/alloy-logs/log.log
echo &amp;#39;level=debug msg=&amp;#34;DEBUG: This is a debug level log!&amp;#34;&amp;#39; &amp;gt;&amp;gt; /tmp/alloy-logs/log.log&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Navigate to &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; and switch the data source to &lt;code&gt;Loki&lt;/code&gt;.
Try querying for &lt;code&gt;{filename=&amp;quot;/tmp/alloy-logs/log.log&amp;quot;}&lt;/code&gt; and see if you can find the new label.&lt;/p&gt;
&lt;p&gt;Now that you have added new labels, you can also filter on them. Try querying for &lt;code&gt;{os!=&amp;quot;&amp;quot;}&lt;/code&gt;.
You should only see the lines you added in the previous step.&lt;/p&gt;
&lt;div class=&#34;collapse&#34; x-data=&#34;app_collapse()&#34;&gt;
  &lt;button class=&#34;collapse-trigger&#34; @click=&#34;toggle()&#34;&gt;
    &lt;span class=&#34;body-large&#34;&gt;Solution&lt;/span&gt;
    &lt;span class=&#34;collapse-trigger__icon&#34; :class=&#34;{ &#39;collapse-trigger__icon-open&#39; : open }&#34;&gt;
      
  &lt;svg width=&#34;27&#34; height=&#34;26&#34; viewBox=&#34;0 0 27 26&#34; fill=&#34;none&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;path opacity=&#34;0.2&#34; d=&#34;M1.73047 12.8359C1.73047 19.4634 7.10305 24.8359 13.7305 24.8359C20.3579 24.8359 25.7305 19.4634 25.7305 12.8359C25.7305 6.20852 20.3579 0.835937 13.7305 0.835937C7.10305 0.835937 1.73047 6.20852 1.73047 12.8359Z&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M18.2344 12.8359L9.23438 12.8359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M13.7344 8.33594L13.7344 17.3359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;/svg&gt;


    &lt;/span&gt;
  &lt;/button&gt;
  &lt;div class=&#34;collapse-content&#34; x-ref=&#34;content&#34; hidden=&#34;until-found&#34;&gt;
    &lt;div class=&#34;collapse-content__inner&#34; x-ref=&#34;content-inner&#34;&gt;
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;// Let&amp;#39;s learn about relabeling and send logs to Loki!

local.file_match &amp;#34;tmplogs&amp;#34; {
    path_targets = [{&amp;#34;__path__&amp;#34; = &amp;#34;/tmp/alloy-logs/*.log&amp;#34;}]
}

loki.source.file &amp;#34;local_files&amp;#34; {
    targets    = local.file_match.tmplogs.targets
    forward_to = [loki.relabel.add_static_label.receiver]
}

loki.relabel &amp;#34;add_static_label&amp;#34; {
    forward_to = [loki.write.local_loki.receiver]

    rule {
        target_label = &amp;#34;os&amp;#34;
        replacement  = constants.os
    }
}

loki.write &amp;#34;local_loki&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:3100/loki/api/v1/push&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;h3 id=&#34;extract-and-add-a-label-from-logs&#34;&gt;Extract and add a Label from Logs&lt;/h3&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;This exercise is more challenging than the previous one.
If you are having trouble, skip it and move to the next section, which covers some of the concepts used here.
You can always come back to this exercise later.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;This exercise builds on the previous one, though it&amp;rsquo;s more involved.&lt;/p&gt;
&lt;p&gt;Assume you want to extract the &lt;code&gt;level&lt;/code&gt; from the logs and add it as a label. As a starting point, look at &lt;a href=&#34;../../reference/components/loki/loki.process/&#34;&gt;&lt;code&gt;loki.process&lt;/code&gt;&lt;/a&gt;.
This component allows you to perform processing on logs, including extracting values from log contents.&lt;/p&gt;
&lt;p&gt;Try modifying your configuration from the previous section to extract the &lt;code&gt;level&lt;/code&gt; from the logs and add it as a label.
If needed, you can find a solution to the previous exercise at the end of the &lt;a href=&#34;#add-a-label-to-logs&#34;&gt;previous section&lt;/a&gt;.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-tip&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Tip&lt;/p&gt;&lt;p&gt;The &lt;code&gt;stage.logfmt&lt;/code&gt; and &lt;code&gt;stage.labels&lt;/code&gt; blocks for &lt;code&gt;loki.process&lt;/code&gt; may be helpful.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;Run Alloy and execute the following:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;echo &amp;#39;level=info msg=&amp;#34;INFO: This is an info level log!&amp;#34;&amp;#39; &amp;gt;&amp;gt; /tmp/alloy-logs/log.log
echo &amp;#39;level=warn msg=&amp;#34;WARN: This is a warn level log!&amp;#34;&amp;#39; &amp;gt;&amp;gt; /tmp/alloy-logs/log.log
echo &amp;#39;level=debug msg=&amp;#34;DEBUG: This is a debug level log!&amp;#34;&amp;#39; &amp;gt;&amp;gt; /tmp/alloy-logs/log.log&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Navigate to &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; and switch the data source to &lt;code&gt;Loki&lt;/code&gt;.
Try querying for &lt;code&gt;{level!=&amp;quot;&amp;quot;}&lt;/code&gt; to see the new labels in action.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 2528px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/screenshot-log-line-levels.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/screenshot-log-line-levels.png&#34;data-srcset=&#34;/media/docs/alloy/screenshot-log-line-levels.png?w=320 320w, /media/docs/alloy/screenshot-log-line-levels.png?w=550 550w, /media/docs/alloy/screenshot-log-line-levels.png?w=750 750w, /media/docs/alloy/screenshot-log-line-levels.png?w=900 900w, /media/docs/alloy/screenshot-log-line-levels.png?w=1040 1040w, /media/docs/alloy/screenshot-log-line-levels.png?w=1240 1240w, /media/docs/alloy/screenshot-log-line-levels.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;Grafana Explore view of example log lines, now with the extracted &amp;#39;level&amp;#39; label&#34;width=&#34;2528&#34;height=&#34;902&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/screenshot-log-line-levels.png&#34;
            alt=&#34;Grafana Explore view of example log lines, now with the extracted &amp;#39;level&amp;#39; label&#34;width=&#34;2528&#34;height=&#34;902&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;div class=&#34;collapse&#34; x-data=&#34;app_collapse()&#34;&gt;
  &lt;button class=&#34;collapse-trigger&#34; @click=&#34;toggle()&#34;&gt;
    &lt;span class=&#34;body-large&#34;&gt;Solution&lt;/span&gt;
    &lt;span class=&#34;collapse-trigger__icon&#34; :class=&#34;{ &#39;collapse-trigger__icon-open&#39; : open }&#34;&gt;
      
  &lt;svg width=&#34;27&#34; height=&#34;26&#34; viewBox=&#34;0 0 27 26&#34; fill=&#34;none&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;path opacity=&#34;0.2&#34; d=&#34;M1.73047 12.8359C1.73047 19.4634 7.10305 24.8359 13.7305 24.8359C20.3579 24.8359 25.7305 19.4634 25.7305 12.8359C25.7305 6.20852 20.3579 0.835937 13.7305 0.835937C7.10305 0.835937 1.73047 6.20852 1.73047 12.8359Z&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M18.2344 12.8359L9.23438 12.8359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M13.7344 8.33594L13.7344 17.3359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;/svg&gt;


    &lt;/span&gt;
  &lt;/button&gt;
  &lt;div class=&#34;collapse-content&#34; x-ref=&#34;content&#34; hidden=&#34;until-found&#34;&gt;
    &lt;div class=&#34;collapse-content__inner&#34; x-ref=&#34;content-inner&#34;&gt;
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;// Let&amp;#39;s learn about relabeling and send logs to Loki!

local.file_match &amp;#34;tmplogs&amp;#34; {
    path_targets = [{&amp;#34;__path__&amp;#34; = &amp;#34;/tmp/alloy-logs/*.log&amp;#34;}]
}

loki.source.file &amp;#34;local_files&amp;#34; {
    targets    = local.file_match.tmplogs.targets
    forward_to = [loki.process.add_new_label.receiver]
}

loki.process &amp;#34;add_new_label&amp;#34; {
    // Extract the value of &amp;#34;level&amp;#34; from the log line and add it to the extracted map as &amp;#34;extracted_level&amp;#34;
    // You could also use &amp;#34;level&amp;#34; = &amp;#34;&amp;#34;, which would extract the value of &amp;#34;level&amp;#34; and add it to the extracted map as &amp;#34;level&amp;#34;
    // but to make it explicit for this example, we will use a different name.
    //
    // The extracted map will be covered in more detail in the next section.
    stage.logfmt {
        mapping = {
            &amp;#34;extracted_level&amp;#34; = &amp;#34;level&amp;#34;,
        }
    }

    // Add the value of &amp;#34;extracted_level&amp;#34; from the extracted map as a &amp;#34;level&amp;#34; label
    stage.labels {
        values = {
            &amp;#34;level&amp;#34; = &amp;#34;extracted_level&amp;#34;,
        }
    }

    forward_to = [loki.relabel.add_static_label.receiver]
}

loki.relabel &amp;#34;add_static_label&amp;#34; {
    forward_to = [loki.write.local_loki.receiver]

    rule {
        target_label = &amp;#34;os&amp;#34;
        replacement  = constants.os
    }
}

loki.write &amp;#34;local_loki&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:3100/loki/api/v1/push&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;finish-up-and-next-steps&#34;&gt;Finish up and next steps&lt;/h2&gt;
&lt;p&gt;You have learned the concepts of components, attributes, and expressions.
You have also seen how to use some standard library components to collect metrics and logs.
In the next tutorial, you learn more about how to use the &lt;code&gt;loki.process&lt;/code&gt; component to extract values from logs and use them.&lt;/p&gt;
]]></content><description>&lt;h1 id="logs-and-relabeling-basics-in-grafana-alloy">Logs and relabeling basics in Grafana Alloy&lt;/h1>
&lt;p>This tutorial covers some basic metric relabeling, and shows you how to send logs to Loki.&lt;/p>
&lt;h2 id="before-you-begin">Before you begin&lt;/h2>
&lt;p>To complete this tutorial:&lt;/p></description></item><item><title>Process logs with Grafana Alloy</title><link>https://grafana.com/docs/alloy/v1.15/tutorials/processing-logs/</link><pubDate>Mon, 30 Mar 2026 15:47:22 +0000</pubDate><guid>https://grafana.com/docs/alloy/v1.15/tutorials/processing-logs/</guid><content><![CDATA[&lt;h1 id=&#34;process-logs-with-grafana-alloy&#34;&gt;Process logs with Grafana Alloy&lt;/h1&gt;
&lt;p&gt;This tutorial assumes you are familiar with setting up and connecting components.
It covers using &lt;code&gt;loki.source.api&lt;/code&gt; to receive logs over HTTP, processing and filtering them, and sending them to Loki.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;p&gt;To complete this tutorial:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You must complete the &lt;a href=&#34;../logs-and-relabeling-basics/&#34;&gt;Logs and relabeling basics&lt;/a&gt; tutorial.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;receive-logs-over-http-and-process&#34;&gt;Receive logs over HTTP and Process&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;loki.source.api&lt;/code&gt; component can receive logs over HTTP.
It can be useful for receiving logs from other Alloys or collectors, or directly from applications that can send logs over HTTP, and then processing them centrally.&lt;/p&gt;
&lt;h3 id=&#34;recommended-reading&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Optional: &lt;a href=&#34;../../reference/components/loki/loki.source.api/&#34;&gt;&lt;code&gt;loki.source.api&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;set-up-the-lokisourceapi-component&#34;&gt;Set up the &lt;code&gt;loki.source.api&lt;/code&gt; component&lt;/h3&gt;
&lt;p&gt;Your pipeline is going to look like this:&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 1320px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/alloy/example-logs-pipeline-alloy.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/alloy/example-logs-pipeline-alloy.png&#34;data-srcset=&#34;/media/docs/alloy/example-logs-pipeline-alloy.png?w=320 320w, /media/docs/alloy/example-logs-pipeline-alloy.png?w=550 550w, /media/docs/alloy/example-logs-pipeline-alloy.png?w=750 750w, /media/docs/alloy/example-logs-pipeline-alloy.png?w=900 900w, /media/docs/alloy/example-logs-pipeline-alloy.png?w=1040 1040w, /media/docs/alloy/example-logs-pipeline-alloy.png?w=1240 1240w, /media/docs/alloy/example-logs-pipeline-alloy.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;An example logs pipeline&#34;width=&#34;1320&#34;height=&#34;170&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/alloy/example-logs-pipeline-alloy.png&#34;
            alt=&#34;An example logs pipeline&#34;width=&#34;1320&#34;height=&#34;170&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;Start by setting up the &lt;code&gt;loki.source.api&lt;/code&gt; component:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;loki.source.api &amp;#34;listener&amp;#34; {
    http {
        listen_address = &amp;#34;127.0.0.1&amp;#34;
        listen_port    = 9999
    }

    labels = { source = &amp;#34;api&amp;#34; }

    forward_to = [loki.process.process_logs.receiver]
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This is a simple configuration.
You are configuring the &lt;code&gt;loki.source.api&lt;/code&gt; component to listen on &lt;code&gt;127.0.0.1:9999&lt;/code&gt; and attach a &lt;code&gt;source=&amp;quot;api&amp;quot;&lt;/code&gt; label to the received log entries, which are then forwarded to the &lt;code&gt;loki.process.process_logs&lt;/code&gt; component&amp;rsquo;s exported receiver.&lt;/p&gt;
&lt;h2 id=&#34;process-and-write-logs&#34;&gt;Process and Write Logs&lt;/h2&gt;
&lt;h3 id=&#34;recommended-reading-1&#34;&gt;Recommended reading&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../reference/components/loki/loki.process/#stagedrop&#34;&gt;&lt;code&gt;loki.process#stage.drop&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../reference/components/loki/loki.process/#stagejson&#34;&gt;&lt;code&gt;loki.process#stage.json&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../reference/components/loki/loki.process/#stagelabels&#34;&gt;&lt;code&gt;loki.process#stage.labels&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;configure-the-lokiprocess-and-lokiwrite-components&#34;&gt;Configure the &lt;code&gt;loki.process&lt;/code&gt; and &lt;code&gt;loki.write&lt;/code&gt; components&lt;/h3&gt;
&lt;p&gt;Now that you have set up the &lt;code&gt;loki.source.api&lt;/code&gt; component, you can configure the &lt;code&gt;loki.process&lt;/code&gt; and &lt;code&gt;loki.write&lt;/code&gt; components.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;// Let&amp;#39;s send and process more logs!

loki.source.api &amp;#34;listener&amp;#34; {
    http {
        listen_address = &amp;#34;127.0.0.1&amp;#34;
        listen_port    = 9999
    }

    labels = { &amp;#34;source&amp;#34; = &amp;#34;api&amp;#34; }

    forward_to = [loki.process.process_logs.receiver]
}

loki.process &amp;#34;process_logs&amp;#34; {

    // Stage 1
    stage.json {
        expressions = {
            log = &amp;#34;&amp;#34;,
            ts  = &amp;#34;timestamp&amp;#34;,
        }
    }

    // Stage 2
    stage.timestamp {
        source = &amp;#34;ts&amp;#34;
        format = &amp;#34;RFC3339&amp;#34;
    }

    // Stage 3
    stage.json {
        source = &amp;#34;log&amp;#34;

        expressions = {
            is_secret = &amp;#34;&amp;#34;,
            level     = &amp;#34;&amp;#34;,
            log_line  = &amp;#34;message&amp;#34;,
        }
    }

    // Stage 4
    stage.drop {
        source = &amp;#34;is_secret&amp;#34;
        value  = &amp;#34;true&amp;#34;
    }

    // Stage 5
    stage.labels {
        values = {
            level = &amp;#34;&amp;#34;,
        }
    }

    // Stage 6
    stage.output {
        source = &amp;#34;log_line&amp;#34;
    }

    // This stage adds static values to the labels on the log line
    stage.static_labels {
        values = {
            source = &amp;#34;demo-api&amp;#34;,
        }
    }

    forward_to = [loki.write.local_loki.receiver]
}

loki.write &amp;#34;local_loki&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:3100/loki/api/v1/push&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;collapse&#34; x-data=&#34;app_collapse()&#34;&gt;
  &lt;button class=&#34;collapse-trigger&#34; @click=&#34;toggle()&#34;&gt;
    &lt;span class=&#34;body-large&#34;&gt;How the stages work&lt;/span&gt;
    &lt;span class=&#34;collapse-trigger__icon&#34; :class=&#34;{ &#39;collapse-trigger__icon-open&#39; : open }&#34;&gt;
      
  &lt;svg width=&#34;27&#34; height=&#34;26&#34; viewBox=&#34;0 0 27 26&#34; fill=&#34;none&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;path opacity=&#34;0.2&#34; d=&#34;M1.73047 12.8359C1.73047 19.4634 7.10305 24.8359 13.7305 24.8359C20.3579 24.8359 25.7305 19.4634 25.7305 12.8359C25.7305 6.20852 20.3579 0.835937 13.7305 0.835937C7.10305 0.835937 1.73047 6.20852 1.73047 12.8359Z&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M18.2344 12.8359L9.23438 12.8359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M13.7344 8.33594L13.7344 17.3359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;/svg&gt;


    &lt;/span&gt;
  &lt;/button&gt;
  &lt;div class=&#34;collapse-content&#34; x-ref=&#34;content&#34; hidden=&#34;until-found&#34;&gt;
    &lt;div class=&#34;collapse-content__inner&#34; x-ref=&#34;content-inner&#34;&gt;&lt;p&gt;Many of the &lt;code&gt;stage.*&lt;/code&gt; blocks in &lt;code&gt;loki.process&lt;/code&gt; act on reading or writing a shared map of values extracted from the logs.
You can think of this extracted map as a hashmap or table that each stage has access to, and it&amp;rsquo;s referred to as the &amp;ldquo;extracted map&amp;rdquo; from here on.
In subsequent stages, you can use the extracted map to filter logs, add, or remove labels, or even modify the log line.&lt;/p&gt;
&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;&lt;code&gt;stage.*&lt;/code&gt; blocks are executed in the order they appear in the component, top down.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;
&lt;p&gt;You can use an example log line to illustrate this, then go stage by stage, showing the contents of the extracted map.
Here is the example log line:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JSON&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-json&#34;&gt;{
    &amp;#34;log&amp;#34;: {
        &amp;#34;is_secret&amp;#34;: &amp;#34;true&amp;#34;,
        &amp;#34;level&amp;#34;: &amp;#34;info&amp;#34;,
        &amp;#34;message&amp;#34;: &amp;#34;This is a secret message!&amp;#34;,
    },
    &amp;#34;timestamp&amp;#34;: &amp;#34;2023-11-16T06:01:50Z&amp;#34;,
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h4 id=&#34;stage-1&#34;&gt;Stage 1&lt;/h4&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;stage.json {
    expressions = {
        log = &amp;#34;&amp;#34;,
        ts  = &amp;#34;timestamp&amp;#34;,
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This stage parses the log line as JSON, extracts two values from it, &lt;code&gt;log&lt;/code&gt; and &lt;code&gt;timestamp&lt;/code&gt;, and puts them into the extracted map with keys &lt;code&gt;log&lt;/code&gt; and &lt;code&gt;ts&lt;/code&gt;, respectively.&lt;/p&gt;
&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Supplying an empty string is shorthand for using the same key as in the input log line, so &lt;code&gt;log = &amp;quot;&amp;quot;&lt;/code&gt; is the same as &lt;code&gt;log = &amp;quot;log&amp;quot;&lt;/code&gt;.
The &lt;em&gt;keys&lt;/em&gt; of the &lt;code&gt;expressions&lt;/code&gt; object end up as the keys in the extracted map, and the &lt;em&gt;values&lt;/em&gt; are used as keys to look up in the parsed log line.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;
&lt;p&gt;If this were Python, it would be roughly equivalent to:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Python&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-python&#34;&gt;extracted_map = {}
log_line      = {&amp;#34;log&amp;#34;: {&amp;#34;is_secret&amp;#34;: &amp;#34;true&amp;#34;, &amp;#34;level&amp;#34;: &amp;#34;info&amp;#34;, &amp;#34;message&amp;#34;: &amp;#34;This is a secret message!&amp;#34;}, &amp;#34;timestamp&amp;#34;: &amp;#34;2023-11-16T06:01:50Z&amp;#34;}

extracted_map[&amp;#34;log&amp;#34;] = log_line[&amp;#34;log&amp;#34;]
extracted_map[&amp;#34;ts&amp;#34;]  = log_line[&amp;#34;timestamp&amp;#34;]&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Extracted map &lt;em&gt;before&lt;/em&gt; performing this stage:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JSON&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-json&#34;&gt;{}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Extracted map &lt;em&gt;after&lt;/em&gt; performing this stage:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JSON&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-json&#34;&gt;{
    &amp;#34;log&amp;#34;: {
        &amp;#34;is_secret&amp;#34;: &amp;#34;true&amp;#34;,
        &amp;#34;level&amp;#34;: &amp;#34;info&amp;#34;,
        &amp;#34;message&amp;#34;: &amp;#34;This is a secret message!&amp;#34;,
    },
    &amp;#34;ts&amp;#34;: &amp;#34;2023-11-16T06:01:50Z&amp;#34;,
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h4 id=&#34;stage-2&#34;&gt;Stage 2&lt;/h4&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;stage.timestamp {
    source = &amp;#34;ts&amp;#34;
    format = &amp;#34;RFC3339&amp;#34;
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This stage acts on the &lt;code&gt;ts&lt;/code&gt; value in the map you extracted in the previous stage.
The value of &lt;code&gt;ts&lt;/code&gt; is parsed in the format of &lt;code&gt;RFC3339&lt;/code&gt; and added as the timestamp to be ingested by Loki.
This is useful if you want to use the timestamp present in the log itself, rather than the time the log is ingested.
This stage doesn&amp;rsquo;t modify the extracted map.&lt;/p&gt;
&lt;h4 id=&#34;stage-3&#34;&gt;Stage 3&lt;/h4&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;stage.json {
    source = &amp;#34;log&amp;#34;

    expressions = {
        is_secret = &amp;#34;&amp;#34;,
        level     = &amp;#34;&amp;#34;,
        log_line  = &amp;#34;message&amp;#34;,
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This stage acts on the &lt;code&gt;log&lt;/code&gt; value in the extracted map, which is a value that you extracted in the previous stage.
This value is also a JSON object, so you can extract values from it as well.
This stage extracts three values from the &lt;code&gt;log&lt;/code&gt; value, &lt;code&gt;is_secret&lt;/code&gt;, &lt;code&gt;level&lt;/code&gt;, and &lt;code&gt;log_line&lt;/code&gt;, and puts them into the extracted map with keys &lt;code&gt;is_secret&lt;/code&gt;, &lt;code&gt;level&lt;/code&gt;, and &lt;code&gt;log_line&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If this were Python, it would be roughly equivalent to:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Python&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-python&#34;&gt;extracted_map = {
    &amp;#34;log&amp;#34;: {
        &amp;#34;is_secret&amp;#34;: &amp;#34;true&amp;#34;,
        &amp;#34;level&amp;#34;: &amp;#34;info&amp;#34;,
        &amp;#34;message&amp;#34;: &amp;#34;This is a secret message!&amp;#34;,
    },
    &amp;#34;ts&amp;#34;: &amp;#34;2023-11-16T06:01:50Z&amp;#34;,
}

source = extracted_map[&amp;#34;log&amp;#34;]

extracted_map[&amp;#34;is_secret&amp;#34;] = source[&amp;#34;is_secret&amp;#34;]
extracted_map[&amp;#34;level&amp;#34;]     = source[&amp;#34;level&amp;#34;]
extracted_map[&amp;#34;log_line&amp;#34;]  = source[&amp;#34;message&amp;#34;]&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Extracted map &lt;em&gt;before&lt;/em&gt; performing this stage:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JSON&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-json&#34;&gt;{
    &amp;#34;log&amp;#34;: {
        &amp;#34;is_secret&amp;#34;: &amp;#34;true&amp;#34;,
        &amp;#34;level&amp;#34;: &amp;#34;info&amp;#34;,
        &amp;#34;message&amp;#34;: &amp;#34;This is a secret message!&amp;#34;,
    },
    &amp;#34;ts&amp;#34;: &amp;#34;2023-11-16T06:01:50Z&amp;#34;,
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Extracted map &lt;em&gt;after&lt;/em&gt; performing this stage:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JSON&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-json&#34;&gt;{
    &amp;#34;log&amp;#34;: {
        &amp;#34;is_secret&amp;#34;: &amp;#34;true&amp;#34;,
        &amp;#34;level&amp;#34;: &amp;#34;info&amp;#34;,
        &amp;#34;message&amp;#34;: &amp;#34;This is a secret message!&amp;#34;,
    },
    &amp;#34;ts&amp;#34;: &amp;#34;2023-11-16T06:01:50Z&amp;#34;,
    &amp;#34;is_secret&amp;#34;: &amp;#34;true&amp;#34;,
    &amp;#34;level&amp;#34;: &amp;#34;info&amp;#34;,
    &amp;#34;log_line&amp;#34;: &amp;#34;This is a secret message!&amp;#34;,
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h4 id=&#34;stage-4&#34;&gt;Stage 4&lt;/h4&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;stage.drop {
    source = &amp;#34;is_secret&amp;#34;
    value  = &amp;#34;true&amp;#34;
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This stage acts on the &lt;code&gt;is_secret&lt;/code&gt; value in the extracted map, which is a value that you extracted in the previous stage.
This stage drops the log line if the value of &lt;code&gt;is_secret&lt;/code&gt; is &lt;code&gt;&amp;quot;true&amp;quot;&lt;/code&gt; and doesn&amp;rsquo;t modify the extracted map.
There are many other ways to filter logs, but this is a simple example.
Refer to the [&lt;code&gt;loki.process&lt;/code&gt;][loki.process#stage.drop] stage.drop documentation for more information.&lt;/p&gt;
&lt;h4 id=&#34;stage-5&#34;&gt;Stage 5&lt;/h4&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;stage.labels {
    values = {
        level = &amp;#34;&amp;#34;,
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This stage adds a label to the log using the same shorthand as before, so this is equivalent to using &lt;code&gt;values = { level = &amp;quot;level&amp;quot; }&lt;/code&gt;.
This stage adds a label with key &lt;code&gt;level&lt;/code&gt; and the value of &lt;code&gt;level&lt;/code&gt; in the extracted map to the log (&lt;code&gt;&amp;quot;info&amp;quot;&lt;/code&gt; from the example log line).
This stage doesn&amp;rsquo;t modify the extracted map.&lt;/p&gt;
&lt;h4 id=&#34;stage-6&#34;&gt;Stage 6&lt;/h4&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;stage.output {
    source = &amp;#34;log_line&amp;#34;
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This stage uses the &lt;code&gt;log_line&lt;/code&gt; value in the extracted map to set the actual log line that&amp;rsquo;s forwarded to Loki.
Rather than sending the entire JSON blob to Loki, you are only sending &lt;code&gt;original_log_line[&amp;quot;log&amp;quot;][&amp;quot;message&amp;quot;]&lt;/code&gt;, along with some labels that you attached.&lt;/p&gt;
&lt;p&gt;This stage doesn&amp;rsquo;t modify the extracted map.&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;h2 id=&#34;put-it-all-together&#34;&gt;Put it all together&lt;/h2&gt;
&lt;p&gt;Now that you have all of the pieces, you can run Alloy and send some logs to it.
Modify &lt;code&gt;config.alloy&lt;/code&gt; with the configuration from the previous example and start Alloy with:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;&amp;lt;BINARY_FILE_PATH&amp;gt; run config.alloy&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Replace the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;code&gt;&amp;lt;BINARY_FILE_PATH&amp;gt;&lt;/code&gt;&lt;/em&gt;: The path to the Alloy binary.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Try executing the following which inserts the current timestamp:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;curl localhost:9999/loki/api/v1/raw -XPOST -H &amp;#34;Content-Type: application/json&amp;#34; -d &amp;#39;{&amp;#34;log&amp;#34;: {&amp;#34;is_secret&amp;#34;: &amp;#34;false&amp;#34;, &amp;#34;level&amp;#34;: &amp;#34;debug&amp;#34;, &amp;#34;message&amp;#34;: &amp;#34;This is a debug message!&amp;#34;}, &amp;#34;timestamp&amp;#34;:  &amp;#34;&amp;#39;&amp;#34;$(date -u &amp;#43;&amp;#34;%Y-%m-%dT%H:%M:%SZ&amp;#34;)&amp;#34;&amp;#39;&amp;#34;}&amp;#39;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that you have sent some logs, its time to see how they look in Grafana.
Navigate to &lt;a href=&#34;http://localhost:3000/explore&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;http://localhost:3000/explore&lt;/a&gt; and switch the data source to &lt;code&gt;Loki&lt;/code&gt;.
Try querying for &lt;code&gt;{source=&amp;quot;demo-api&amp;quot;}&lt;/code&gt; and see if you can find the logs you sent.&lt;/p&gt;
&lt;p&gt;Try playing around with the values of &lt;code&gt;&amp;quot;level&amp;quot;&lt;/code&gt;, &lt;code&gt;&amp;quot;message&amp;quot;&lt;/code&gt;, &lt;code&gt;&amp;quot;timestamp&amp;quot;&lt;/code&gt;, and &lt;code&gt;&amp;quot;is_secret&amp;quot;&lt;/code&gt; and see how the logs change.
You can also try adding more stages to the &lt;code&gt;loki.process&lt;/code&gt; component to extract more values from the logs, or add more labels.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;/media/docs/alloy/screenshot-processed-log-lines.png&#34;
  alt=&#34;Example Loki Logs&#34; width=&#34;2529&#34;
     height=&#34;901&#34;/&gt;&lt;/p&gt;
&lt;h2 id=&#34;exercise&#34;&gt;Exercise&lt;/h2&gt;
&lt;p&gt;Since you are already using Docker and Docker exports logs, you can send those logs to Loki.
Refer to the &lt;a href=&#34;../../reference/components/discovery/discovery.docker/&#34;&gt;&lt;code&gt;discovery.docker&lt;/code&gt;&lt;/a&gt; and &lt;a href=&#34;../../reference/components/loki/loki.source.docker/&#34;&gt;&lt;code&gt;loki.source.docker&lt;/code&gt;&lt;/a&gt; documentation for more information.&lt;/p&gt;
&lt;p&gt;To ensure proper timestamps and other labels, make sure you use a &lt;code&gt;loki.process&lt;/code&gt; component to process the logs before sending them to Loki.&lt;/p&gt;
&lt;p&gt;Although you haven&amp;rsquo;t used it before, you can use a &lt;code&gt;discovery.relabel&lt;/code&gt; component to attach the container name as a label to the logs.
You can refer to the &lt;a href=&#34;../../reference/components/discovery/discovery.relabel/&#34;&gt;&lt;code&gt;discovery.relabel&lt;/code&gt;&lt;/a&gt; documentation for more information.
The &lt;code&gt;discovery.relabel&lt;/code&gt; component is very similar to the &lt;code&gt;prometheus.relabel&lt;/code&gt; component, but it relabels discovered targets rather than metrics.&lt;/p&gt;
&lt;div class=&#34;collapse&#34; x-data=&#34;app_collapse()&#34;&gt;
  &lt;button class=&#34;collapse-trigger&#34; @click=&#34;toggle()&#34;&gt;
    &lt;span class=&#34;body-large&#34;&gt;Solution&lt;/span&gt;
    &lt;span class=&#34;collapse-trigger__icon&#34; :class=&#34;{ &#39;collapse-trigger__icon-open&#39; : open }&#34;&gt;
      
  &lt;svg width=&#34;27&#34; height=&#34;26&#34; viewBox=&#34;0 0 27 26&#34; fill=&#34;none&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;
&lt;path opacity=&#34;0.2&#34; d=&#34;M1.73047 12.8359C1.73047 19.4634 7.10305 24.8359 13.7305 24.8359C20.3579 24.8359 25.7305 19.4634 25.7305 12.8359C25.7305 6.20852 20.3579 0.835937 13.7305 0.835937C7.10305 0.835937 1.73047 6.20852 1.73047 12.8359Z&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M18.2344 12.8359L9.23438 12.8359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;path d=&#34;M13.7344 8.33594L13.7344 17.3359&#34; stroke=&#34;black&#34; stroke-width=&#34;1.5&#34; stroke-linecap=&#34;round&#34; stroke-linejoin=&#34;round&#34;/&gt;
&lt;/svg&gt;


    &lt;/span&gt;
  &lt;/button&gt;
  &lt;div class=&#34;collapse-content&#34; x-ref=&#34;content&#34; hidden=&#34;until-found&#34;&gt;
    &lt;div class=&#34;collapse-content__inner&#34; x-ref=&#34;content-inner&#34;&gt;
&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Alloy&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-alloy&#34;&gt;// Discover docker containers to collect logs from
discovery.docker &amp;#34;docker_containers&amp;#34; {
    // Note that if you are using Docker Desktop Engine this may need to be changed to
    // something like &amp;#34;unix:///${HOME}/.docker/desktop/docker.sock&amp;#34;
    host = &amp;#34;unix:///var/run/docker.sock&amp;#34;
}

// Extract container name from __meta_docker_container_name label and add as label
discovery.relabel &amp;#34;docker_containers&amp;#34; {
    targets = discovery.docker.docker_containers.targets

    rule {
        source_labels = [&amp;#34;__meta_docker_container_name&amp;#34;]
        target_label  = &amp;#34;container&amp;#34;
    }
}

// Scrape logs from docker containers and send to be processed
loki.source.docker &amp;#34;docker_logs&amp;#34; {
    host    = &amp;#34;unix:///var/run/docker.sock&amp;#34;
    targets = discovery.relabel.docker_containers.output
    forward_to = [loki.process.process_logs.receiver]
}

// Process logs and send to Loki
loki.process &amp;#34;process_logs&amp;#34; {
    stage.docker { }

    forward_to = [loki.write.local_loki.receiver]
}

loki.write &amp;#34;local_loki&amp;#34; {
    endpoint {
        url = &amp;#34;http://localhost:3100/loki/api/v1/push&amp;#34;
    }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

]]></content><description>&lt;h1 id="process-logs-with-grafana-alloy">Process logs with Grafana Alloy&lt;/h1>
&lt;p>This tutorial assumes you are familiar with setting up and connecting components.
It covers using &lt;code>loki.source.api&lt;/code> to receive logs over HTTP, processing and filtering them, and sending them to Loki.&lt;/p></description></item></channel></rss>