From 0b1b5b6b01d5d31b1d278587eaf511cc1e96a1aa Mon Sep 17 00:00:00 2001 From: Jay Clifford <45856600+Jayclifford345@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:49:35 +0100 Subject: [PATCH] docs: Sandboxed Metrics tutorial (#1619) --- .../tutorials/send-metrics-to-prometheus.md | 123 ++++++++++++++++-- 1 file changed, 112 insertions(+), 11 deletions(-) diff --git a/docs/sources/tutorials/send-metrics-to-prometheus.md b/docs/sources/tutorials/send-metrics-to-prometheus.md index 8d81f9c468..67f2f44ce3 100644 --- a/docs/sources/tutorials/send-metrics-to-prometheus.md +++ b/docs/sources/tutorials/send-metrics-to-prometheus.md @@ -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 --- + + + # 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. + ## Before you begin To complete this tutorial: @@ -22,6 +47,24 @@ To complete this tutorial: * Set up a local Grafana instance. * Create a `config.alloy` file. + +{{< 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 >}} + + +{{< 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 >}} + + + + + ## Configure {{% param "PRODUCT_NAME" %}} In this tutorial, you configure {{< param "PRODUCT_NAME" >}} to collect metrics and send them to Prometheus. @@ -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: @@ -101,6 +148,7 @@ This final component creates a [`prometheus.remote_write`][prometheus.remote_wri This completes the simple configuration pipeline. + {{< 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. @@ -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 >}} + + +{{< 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. + + + + ## 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 >}} + {{< code >}} ```macos @@ -126,13 +189,15 @@ sudo cp config.alloy $(brew --prefix)/etc/alloy/config.alloy sudo cp config.alloy /etc/alloy/config.alloy ``` {{< /code >}} + 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 ``` + {{< 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. @@ -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 >}} + -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 >}} + + + {{< code >}} + + ```macos + brew services restart alloy + ``` + + ```linux + sudo systemctl reload alloy + ``` + {{< /code >}} + + + + + ## Inspect your configuration in the {{% param "PRODUCT_NAME" %}} UI @@ -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. + + + + ## 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." >}} + + + + + ## Summary You have configured {{< param "PRODUCT_NAME" >}} to collect and process metrics from your local host and send them to your local Grafana stack. + + [previous tutorial]: ../send-logs-to-loki/ [prometheus.scrape]: ../../reference/components/prometheus/prometheus.scrape/ [prometheus.relabel]: ../../reference/components/prometheus/prometheus.relabel/