Skip to content

Commit

Permalink
Merge pull request #117 from grafana/staging
Browse files Browse the repository at this point in the history
Chore: Merge Staging to Prod
  • Loading branch information
Jayclifford345 authored Sep 5, 2024
2 parents 052f889 + 3521eaf commit 9475d6f
Show file tree
Hide file tree
Showing 16 changed files with 432 additions and 71 deletions.
3 changes: 3 additions & 0 deletions alloy/send-metrics-to-prometheus/finish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Summary

You have configured Alloy to collect and process metrics from your local host and send them to your local Grafana stack.
30 changes: 30 additions & 0 deletions alloy/send-metrics-to-prometheus/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"title": "Use Grafana Alloy to send metrics to Prometheus",
"description": "Learn how to send metrics to Prometheus",
"details": {
"intro": {
"text": "intro.md",
"foreground": "previous-tutorial-setup.sh"
},
"steps": [
{
"text": "step1.md"
},
{
"text": "step2.md"
},
{
"text": "step3.md"
},
{
"text": "step4.md"
}
],
"finish": {
"text": "finish.md"
}
},
"backend": {
"imageid": "ubuntu"
}
}
7 changes: 7 additions & 0 deletions alloy/send-metrics-to-prometheus/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Use Grafana Alloy to send metrics to Prometheus

In the [previous tutorial](https://grafana.com/docs/alloy/latest/tutorials/send-logs-to-loki/), you learned how to configure Alloy to collect and process logs from your local machine and send them to Loki.

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.

> 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.`
111 changes: 111 additions & 0 deletions alloy/send-metrics-to-prometheus/previous-tutorial-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/bash

# Define the content of the docker-compose.yml file
compose_content=$(cat <<EOF
version: '3'
services:
loki:
image: grafana/loki:3.0.0
ports:
- "3100:3100"
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:
- "9090:9090"
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 <<EOS > /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
EOS
/run.sh
image: grafana/grafana:11.0.0
ports:
- "3000:3000"
EOF
)

# Define the content of the config.alloy file
alloy_content=$(cat <<EOF
local.file_match "local_files" {
path_targets = [{"__path__" = "/var/log/*.log"}]
sync_period = "5s"
}
loki.source.file "log_scrape" {
targets = local.file_match.local_files.targets
forward_to = [loki.process.filter_logs.receiver]
tail_from_end = true
}
loki.process "filter_logs" {
stage.drop {
source = ""
expression = ".*Connection closed by authenticating user root"
drop_counter_reason = "noisy"
}
forward_to = [loki.write.grafana_loki.receiver]
}
loki.write "grafana_loki" {
endpoint {
url = "http://localhost:3100/loki/api/v1/push"
}
}
EOF
)

# Create the docker-compose.yml file and add the content to it
echo "$compose_content" > docker-compose.yml
echo "docker-compose.yml has been created."

# Create the config.alloy file and add the content to it
echo "$alloy_content" > config.alloy
echo "config.alloy has been created."

# Docker-compose up
docker-compose up -d

# Install Alloy
sudo apt install gpg -y && \
sudo mkdir -p /etc/apt/keyrings/ && \
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null && \
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list && \
sudo apt-get update && \
sudo apt-get install alloy -y && \
# Modify the Alloy service configuration to listen on the desired port
sudo sed -i -e 's/CUSTOM_ARGS=""/CUSTOM_ARGS="--server.http.listen-addr=0.0.0.0:12345"/' /etc/default/alloy && \
# Enable and start the Alloy service
sudo systemctl enable alloy && \
sudo systemctl start alloy.service && \
clear && \
echo "Installation script has now been completed. You may now begin the tutorial."
86 changes: 86 additions & 0 deletions alloy/send-metrics-to-prometheus/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Configure Alloy

In this tutorial, you configure Alloy to collect metrics and send them to Prometheus.

You add components to your `config.alloy`{{copy}} file to tell Alloy which metrics you want to scrape, how you want to process that data, and where you want the data sent.

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

> 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`.
## First component: Scraping

Paste the following component configuration at the top of your `config.alloy`{{copy}} file:

```alloy
prometheus.exporter.unix "local_system" { }
prometheus.scrape "scrape_metrics" {
targets = prometheus.exporter.unix.local_system.targets
forward_to = [prometheus.relabel.filter_metrics.receiver]
scrape_interval = "10s"
}
```{{copy}}
This configuration creates a [`prometheus.scrape`{{copy}}](https://grafana.com/docs/alloy/latest/reference/components/prometheus/prometheus.scrape/) component named `scrape_metrics`{{copy}} which does the following:
- It connects to the `local_system`{{copy}} component as its source or target.
- It forwards the metrics it scrapes to the receiver of another component called `filter_metrics`{{copy}}.
- It tells Alloy to scrape metrics every 10 seconds.
## Second component: Filter metrics
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.
The following example demonstrates how you can filter out or drop metrics before sending them to Prometheus.
Paste the following component configuration below the previous component in your `config.alloy`{{copy}} file:
```alloy
prometheus.relabel "filter_metrics" {
rule {
action = "drop"
source_labels = ["env"]
regex = "dev"
}
forward_to = [prometheus.remote_write.metrics_service.receiver]
}
```{{copy}}
The [`prometheus.relabel`{{copy}}](https://grafana.com/docs/alloy/latest/reference/components/prometheus/prometheus.relabel/) 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’re stored or forwarded.
This configuration creates a [`prometheus.relabel`{{copy}}](https://grafana.com/docs/alloy/latest/reference/components/prometheus/prometheus.relabel/) component named `filter_metrics`{{copy}} which does the following:
- It receives scraped metrics from the `scrape_metrics`{{copy}} component.
- It tells Alloy to drop metrics that have an `"env"`{{copy}} label equal to `"dev"`{{copy}}.
- It forwards the processed metrics to the receiver of another component called `metrics_service`{{copy}}.
## Third component: Write metrics to Prometheus
Paste the following component configuration below the previous component in your `config.alloy`{{copy}} file:
```alloy
prometheus.remote_write "metrics_service" {
endpoint {
url = "http://localhost:9090/api/v1/write"
// basic_auth {
// username = "admin"
// password = "admin"
// }
}
}
```{{copy}}
This final component creates a [`prometheus.remote_write`{{copy}}](https://grafana.com/docs/alloy/latest/reference/components/prometheus/prometheus.remote_write/) component named `metrics_service`{{copy}} that points to `http://localhost:9090/api/v1/write`{{copy}}.
This completes the simple configuration pipeline.
> 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`](https://grafana.com/docs/alloy/latest/reference/components/prometheus/prometheus.remote_write/) component documentation.
This connects directly to the Prometheus instance running in the Docker container.
20 changes: 20 additions & 0 deletions alloy/send-metrics-to-prometheus/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Reload the configuration

Copy your local `config.alloy`{{copy}} file into the default Alloy configuration file location.

```bash
sudo cp config.alloy /etc/alloy/config.alloy
```{{exec}}
Call the `/-/reload`{{copy}} endpoint to tell Alloy to reload the configuration file without a system service restart.
```bash
curl -X POST http://localhost:12345/-/reload
```{{exec}}
> This step uses the Alloy UI on `localhost` port `12345`. If you chose to run Alloy in a Docker container, make sure you use the `--server.http.listen-addr=` argument. If you don’t use this argument, the [debugging UI](https://grafana.com/docs/alloy/latest/troubleshoot/debug/#alloy-ui) won’t be available outside of the Docker container.
Optional: You can do a system service restart Alloy and load the configuration file:
```bash
sudo systemctl reload alloy
```{{exec}}
10 changes: 10 additions & 0 deletions alloy/send-metrics-to-prometheus/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Inspect your configuration in the Alloy UI

Open [http://localhost:12345]({{TRAFFIC_HOST1_12345}}) and click the **Graph** tab at the top.
The graph should look similar to the following:

![Your configuration in the Alloy UI](https://grafana.com/media/docs/alloy/tutorial/Metrics-inspect-your-config.png)

The Alloy UI shows you a visual representation of the pipeline you built with your Alloy component configuration.

You can see that the components are healthy, and you are ready to explore the metrics in Grafana.
15 changes: 15 additions & 0 deletions alloy/send-metrics-to-prometheus/step4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Log into Grafana and explore metrics in Prometheus

Open [http://localhost:3000/explore/metrics/]({{TRAFFIC_HOST1_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 Alloy.

![Explore Metrics App](https://grafana.com/media/docs/alloy/explore-metrics.png)

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

Open [http://localhost:3000/explore]({{TRAFFIC_HOST1_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.
3 changes: 2 additions & 1 deletion alloy/structure.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"items": [
{ "path": "send-logs-to-loki", "title": "Use Grafana Alloy to send logs to Loki"}
{ "path": "send-logs-to-loki", "title": "Use Grafana Alloy to send logs to Loki"},
{ "path": "send-metrics-to-prometheus", "title": "Use Grafana Alloy to send metrics to Prometheus"}
]
}
Loading

0 comments on commit 9475d6f

Please sign in to comment.