Skip to content

Commit

Permalink
docs: Sandboxed Metrics tutorial (#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayclifford345 authored Sep 5, 2024
1 parent 2c70be2 commit 0b1b5b6
Showing 1 changed file with 112 additions and 11 deletions.
123 changes: 112 additions & 11 deletions docs/sources/tutorials/send-metrics-to-prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@ description: Learn how to send metrics to Prometheus
title: Use Grafana Alloy to send metrics to Prometheus
menuTitle: Send metrics to Prometheus
weight: 150
killercoda:
title: Use Grafana Alloy to send metrics to Prometheus
description: Learn how to send metrics to Prometheus
details:
intro:
foreground: previous-tutorial-setup.sh
preprocessing:
substitutions:
- regexp: '{{[%<] *param *"FULL_PRODUCT_NAME" *[%>]}}'
replacement: Grafana Alloy
- regexp: '{{[%<] *param *"PRODUCT_NAME" *[%>]}}'
replacement: Alloy
- regexp: 'docker compose'
replacement: docker-compose
- regexp: '\.\./\.\./'
replacement: 'https://grafana.com/docs/alloy/latest/'
- regexp: '../send-logs-to-loki/'
replacement: 'https://grafana.com/docs/alloy/latest/tutorials/send-logs-to-loki/'

backend:
imageid: ubuntu
---

<!-- INTERACTIVE page intro.md START -->

# Use {{% param "FULL_PRODUCT_NAME" %}} to send metrics to Prometheus

In the [previous tutorial][], you learned how to configure {{< param "PRODUCT_NAME" >}} to collect and process logs from your local machine and send them to Loki.

This tutorial shows you how to configure {{< param "PRODUCT_NAME" >}} to collect and process metrics from your local machine, send them to Prometheus, and use Grafana to explore the results.

<!-- INTERACTIVE ignore START -->
## Before you begin

To complete this tutorial:
Expand All @@ -22,6 +47,24 @@ To complete this tutorial:
* Set up a local Grafana instance.
* Create a `config.alloy` file.

<!-- INTERACTIVE ignore START -->
{{< admonition type="tip" >}}
Alternatively, you can try out this example in our interactive learning environment: [Sending metrics to Prometheus](https://killercoda.com/grafana-labs/course/alloy/send-metrics-to-prometheus).

It's a fully configured environment with all the dependencies already installed.

![Interactive](/media/docs/alloy/Alloy-Interactive-Learning-Environment-(Doc-Banner).png)
{{< /admonition >}}
<!-- INTERACTIVE ignore END -->

{{< docs/ignore >}}
> Since this tutorial builds on the previous one, a setup script is automatically run to ensure you have the necessary prerequisites in place. This should take no longer than 1 minute to complete. You may begin the tutorial when you see this message: `Installation script has now been completed. You may now begin the tutorial.`
{{< /docs/ignore >}}
<!-- INTERACTIVE page intro.md END -->

<!-- INTERACTIVE page step1.md START -->

## Configure {{% param "PRODUCT_NAME" %}}

In this tutorial, you configure {{< param "PRODUCT_NAME" >}} to collect metrics and send them to Prometheus.
Expand All @@ -30,6 +73,10 @@ You add components to your `config.alloy` file to tell {{< param "PRODUCT_NAME"

The following steps build on the `config.alloy` file you created in the previous tutorial.

{{< docs/ignore >}}
> The interactive sandbox has a VSCode-like editor that allows you to access files and folders. To access this feature, click on the `Editor` tab. The editor also has a terminal that you can use to run commands. Since some commands assume you are within a specific directory, we recommend running the commands in `tab1`.
{{< /docs/ignore >}}
### First component: Scraping

Paste the following component configuration at the top of your `config.alloy` file:
Expand Down Expand Up @@ -101,6 +148,7 @@ This final component creates a [`prometheus.remote_write`][prometheus.remote_wri

This completes the simple configuration pipeline.

<!-- INTERACTIVE ignore START -->
{{< admonition type="tip" >}}
The `basic_auth` is commented out because the local `docker compose` stack doesn't require it.
It's included in this example to show how you can configure authorization for other environments.
Expand All @@ -109,13 +157,28 @@ For further authorization options, refer to the [`prometheus.remote_write`][prom

[prometheus.remote_write]: ../../reference/components/prometheus/prometheus.remote_write/
{{< /admonition >}}
<!-- INTERACTIVE ignore END -->

{{< docs/ignore >}}
> The `basic_auth` is commented out because the local `docker compose` stack doesn't require it. It's included in this example to show how you can configure authorization for other environments. For further authorization options, refer to the [`prometheus.remote_write`](../../reference/components/prometheus/prometheus.remote_write/) component documentation.
{{< /docs/ignore >}}
This connects directly to the Prometheus instance running in the Docker container.

<!-- INTERACTIVE page step1.md END -->

<!-- INTERACTIVE page step2.md START -->

## Reload the configuration

Copy your local `config.alloy` file into the default {{< param "PRODUCT_NAME" >}} configuration file location.

{{< docs/ignore >}}
```bash
sudo cp config.alloy /etc/alloy/config.alloy
```
{{< /docs/ignore >}}
<!-- INTERACTIVE ignore START -->
{{< code >}}

```macos
Expand All @@ -126,13 +189,15 @@ sudo cp config.alloy $(brew --prefix)/etc/alloy/config.alloy
sudo cp config.alloy /etc/alloy/config.alloy
```
{{< /code >}}
<!-- INTERACTIVE ignore END -->

Call the `/-/reload` endpoint to tell {{< param "PRODUCT_NAME" >}} to reload the configuration file without a system service restart.

```bash
curl -X POST http://localhost:12345/-/reload
curl -X POST http://localhost:12345/-/reload
```

<!-- INTERACTIVE ignore START -->
{{< admonition type="tip" >}}
This step uses the {{< param "PRODUCT_NAME" >}} UI, on `localhost` port `12345`.
If you choose to run Alloy in a Docker container, make sure you use the `--server.http.listen-addr=0.0.0.0:12345` argument.
Expand All @@ -141,21 +206,39 @@ If you don’t use this argument, the [debugging UI][debug] won’t be available

[debug]: ../../troubleshoot/debug/#alloy-ui
{{< /admonition >}}
<!-- INTERACTIVE ignore END -->

1. Optional: You can do a system service restart {{< param "PRODUCT_NAME" >}} and load the configuration file:
{{< docs/ignore >}}

{{< code >}}
> This step uses the {{< param "PRODUCT_NAME" >}} UI on `localhost` port `12345`. If you chose to run {{< param "PRODUCT_NAME" >}} in a Docker container, make sure you use the `--server.http.listen-addr=` argument. If you don’t use this argument, the [debugging UI](../../troubleshoot/debug/#alloy-ui) won’t be available outside of the Docker container.
```macos
brew services restart alloy
```
{{< /docs/ignore >}}

```linux
sudo systemctl reload alloy
```
Optional: You can do a system service restart {{< param "PRODUCT_NAME" >}} and load the configuration file:

{{< /code >}}
{{< docs/ignore >}}
```bash
sudo systemctl reload alloy
```
{{< /docs/ignore >}}

<!-- INTERACTIVE ignore START -->
{{< code >}}

```macos
brew services restart alloy
```

```linux
sudo systemctl reload alloy
```

{{< /code >}}
<!-- INTERACTIVE ignore END -->

<!-- INTERACTIVE page step2.md END -->

<!-- INTERACTIVE page step3.md START -->

## Inspect your configuration in the {{% param "PRODUCT_NAME" %}} UI

Expand All @@ -168,20 +251,38 @@ The {{< param "PRODUCT_NAME" >}} UI shows you a visual representation of the pip

You can see that the components are healthy, and you are ready to explore the metrics in Grafana.

<!-- INTERACTIVE page step3.md END -->

<!-- INTERACTIVE page step4.md START -->

## Log into Grafana and explore metrics in Prometheus

Open [http://localhost:3000/explore/metrics/](http://localhost:3000/explore/metrics/) to access the **Explore Metrics** feature in Grafana.

From here you can visually explore the metrics that are being sent to Prometheus by {{< param "PRODUCT_NAME" >}}.

{{< figure src="/media/docs/alloy/explore-metrics.png" alt="Explore Metrics App" >}}

You can also build promQL queries manually to explore the data further.

Open [http://localhost:3000/explore](http://localhost:3000/explore) to access the **Explore** feature in Grafana.

Select Prometheus as the data source and click the **Metrics Browser** button to select the metric, labels, and values for your labels.

Here you can see that metrics are flowing through to Prometheus as expected, and the end-to-end configuration was successful.

{{< figure src="/media/docs/alloy/tutorial/Metrics_visualization.png" alt="Your data flowing through Prometheus." >}}


<!-- INTERACTIVE page step4.md END -->

<!-- INTERACTIVE page finish.md START -->

## Summary

You have configured {{< param "PRODUCT_NAME" >}} to collect and process metrics from your local host and send them to your local Grafana stack.

<!-- INTERACTIVE page finish.md END -->

[previous tutorial]: ../send-logs-to-loki/
[prometheus.scrape]: ../../reference/components/prometheus/prometheus.scrape/
[prometheus.relabel]: ../../reference/components/prometheus/prometheus.relabel/
Expand Down

0 comments on commit 0b1b5b6

Please sign in to comment.