Skip to content

Commit 659bb0d

Browse files
committed
added PromQL demos
1 parent aa4cbfe commit 659bb0d

File tree

16 files changed

+757
-0
lines changed

16 files changed

+757
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
$prometheus_script = <<-SCRIPT
2+
echo Downloading Prometheus
3+
wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz
4+
echo Getting files
5+
tar xvfz prometheus-2.32.1.linux-amd64.tar.gz
6+
SCRIPT
7+
8+
$exporter_script = <<-SCRIPT
9+
echo Installing prometheus node exporter linux
10+
apt-get -y install prometheus-node-exporter
11+
12+
SCRIPT
13+
14+
Vagrant.configure("2") do |config|
15+
config.vm.provision "shell", inline: <<-SHELL
16+
apt-get update -y
17+
echo "10.0.0.10 prometheus" >> /etc/hosts
18+
echo "10.0.0.11 linux-batch" >> /etc/hosts
19+
echo "10.0.0.12 linux-web" >> /etc/hosts
20+
SHELL
21+
22+
config.vm.define "prometheus" do |prometheus|
23+
prometheus.vm.box = "bento/ubuntu-20.04"
24+
prometheus.vm.hostname = "prometheus"
25+
prometheus.vm.network "private_network", ip: "10.0.0.10"
26+
prometheus.vm.provision "shell", inline: $prometheus_script
27+
prometheus.vm.synced_folder "prometheus-config/", "/opt/prometheus"
28+
prometheus.vm.provider "virtualbox" do |vb|
29+
vb.memory = 2024
30+
vb.cpus = 1
31+
end
32+
end
33+
34+
config.vm.define "linux-batch" do |linuxbatch|
35+
linuxbatch.vm.box = "bento/ubuntu-20.04"
36+
linuxbatch.vm.hostname = "linux-batch"
37+
linuxbatch.vm.network "private_network", ip: "10.0.0.11"
38+
linuxbatch.vm.provision "docker" do |d|
39+
d.run "batch", image: "jaimesalas/prom-batch", args: "-e PORT=80 -p 8080:80"
40+
d.run "batch2", image: "jaimesalas/prom-batch", args: "-e PORT=80 -p 8081:80"
41+
end
42+
linuxbatch.vm.provision "shell", inline: $exporter_script
43+
linuxbatch.vm.provider "virtualbox" do |vb|
44+
vb.memory = 1012
45+
vb.cpus = 1
46+
end
47+
end
48+
49+
config.vm.define "linux-web" do |linuxweb|
50+
linuxweb.vm.box = "bento/ubuntu-20.04"
51+
linuxweb.vm.hostname = "linux-web"
52+
linuxweb.vm.network "private_network", ip: "10.0.0.12"
53+
linuxweb.vm.provision "docker" do |d|
54+
d.run "web", image: "jaimesalas/prom-web", args: "-e PORT=80 -p 8080:80"
55+
d.run "web2", image: "jaimesalas/prom-web", args: "-e PORT=80 -p 8081:80"
56+
end
57+
linuxweb.vm.provision "shell", inline: $exporter_script
58+
linuxweb.vm.provider "virtualbox" do |vb|
59+
vb.memory = 1012
60+
vb.cpus = 1
61+
end
62+
end
63+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"targets": [
4+
"linux-batch:9100"
5+
],
6+
"labels": {
7+
"os": "linux",
8+
"runtime": "vm"
9+
}
10+
},
11+
{
12+
"targets": [
13+
"linux-batch:8080",
14+
"linux-batch:8081"
15+
],
16+
"labels": {
17+
"os": "linux",
18+
"runtime": "container"
19+
}
20+
}
21+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
global:
2+
scrape_interval: 60s
3+
4+
scrape_configs:
5+
- job_name: "prometheus"
6+
static_configs:
7+
- targets: ["localhost:9090"]
8+
9+
- job_name: "web"
10+
file_sd_configs:
11+
- files:
12+
- "web.json"
13+
metric_relabel_configs:
14+
- source_labels: [__name__]
15+
regex: "go_.*"
16+
action: drop
17+
18+
- job_name: "batch"
19+
file_sd_configs:
20+
- files:
21+
- "batch.json"
22+
relabel_configs:
23+
- source_labels: [runtime]
24+
regex: container
25+
target_label: runtime
26+
replacement: docker
27+
metric_relabel_configs:
28+
- source_labels: [__name__]
29+
regex: "go_.*"
30+
action: drop
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"targets": [
4+
"linux-web:9100"
5+
],
6+
"labels": {
7+
"os": "linux",
8+
"runtime": "vm"
9+
}
10+
},
11+
{
12+
"targets": [
13+
"linux-web:8080",
14+
"linux-web:8081"
15+
],
16+
"labels": {
17+
"os": "linux",
18+
"runtime": "docker"
19+
}
20+
}
21+
]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Querying Gauges and Counters
2+
3+
## Setup
4+
5+
```bash
6+
vagrant up
7+
```
8+
9+
After our environemnt gets up, we will have:
10+
11+
* `linux-batch`
12+
* Two docker instances running the batch process exposing on ports 8080 and 8081
13+
* Linux node exporter
14+
* `linux-web`
15+
* Two docker instances running the web server process exposing on ports 8080 and 8081
16+
* Linux node exporter
17+
18+
Now we can run `Prometheus` as follows:
19+
20+
```bash
21+
vagrant ssh prometheus
22+
```
23+
24+
```bash
25+
prometheus-2.32.1.linux-amd64/prometheus \
26+
--config.file="/opt/prometheus/prometheus.yaml"
27+
```
28+
29+
Verify targets at http://10.0.010:9090/targets.
30+
31+
32+
## 1. Gauge
33+
34+
- `worker_jobs_active`
35+
- The current active jobs gauge in batch processes.
36+
- `worker_jobs_active{instance="linux-batch:8080"}` - selector
37+
- If we want to see just the data of one instance we can use a selector
38+
- `sum (worker_jobs_active)` - aggregation operator
39+
- If we want to see dat of all instances
40+
- `sum without(instance) (worker_jobs_active)`
41+
- If we want to exclude one label but include all the others
42+
- `sum without(instance, job, os, runtime) (worker_jobs_active)`
43+
- If we exclude all the labels is the same as doing it over the entire metric
44+
- `avg without(instance, job, os, runtime) (worker_jobs_active)`
45+
- We can change the aggregation function to get the avarage
46+
- `max without(instance, job, os, runtime) (worker_jobs_active)`
47+
- Or even we can get the maximum value
48+
- `sum without(job, os, runtime) (worker_jobs_active)` - Graph
49+
- Here we can see how this value is changing over time
50+
51+
All these expressions use a gauge metric, which is just a snapshot at a point in time, and they are really easy to work with because you can aggregate the raw values.
52+
53+
## 2. Counter
54+
55+
Evalaute expressions over the processor's total jobs metric.
56+
57+
- `worker_jobs_total` - one much bigger, started earlier
58+
- Between two conatiners, we're producing for time series here (including OS)
59+
- `worker_jobs_total[5m]` - range vector
60+
- We can add a range `[5m]` to see the values over time. For each time series, we get all the samples in the last 5 minutes. This is the total value sine the metric began.
61+
- `rate(worker_jobs_total[5m])` - rates very similar
62+
- With counter we are more interested on change over time. This gives te rate of change of the counter over a 5 minute window, and is telling the increase per second. using the the rate function with a counter gives us gauge in the output, and we can aggregate that over time.
63+
- `sum without(instance, job, os, runtime) (rate(worker_jobs_total[5m]))`
64+
- This gives us the rate of change for each status.
65+
- `sum without(status, instance, job, os, runtime) (rate(worker_jobs_total[5m]))`
66+
- This gives us the rate of change of all jobs.
67+
- `sum without(instance, job, os, runtime) (rate(worker_jobs_total{status="failed"}[5m]))`
68+
- We can apply this to get just the failed jobs.
69+
- `sum without(job, os, runtime) (rate(worker_jobs_total{status="failed"}[5m]))` - Graph
70+
- `sum without(job, os, runtime) (rate(worker_jobs_total{status="processed"}[5m]))` - Graph
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
$prometheus_script = <<-SCRIPT
2+
echo Downloading Prometheus
3+
wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz
4+
echo Getting files
5+
tar xvfz prometheus-2.32.1.linux-amd64.tar.gz
6+
SCRIPT
7+
8+
$exporter_script = <<-SCRIPT
9+
echo Installing prometheus node exporter linux
10+
apt-get -y install prometheus-node-exporter
11+
12+
SCRIPT
13+
14+
Vagrant.configure("2") do |config|
15+
config.vm.provision "shell", inline: <<-SHELL
16+
apt-get update -y
17+
echo "10.0.0.10 prometheus" >> /etc/hosts
18+
echo "10.0.0.11 linux-batch" >> /etc/hosts
19+
echo "10.0.0.12 linux-web" >> /etc/hosts
20+
SHELL
21+
22+
config.vm.define "prometheus" do |prometheus|
23+
prometheus.vm.box = "bento/ubuntu-20.04"
24+
prometheus.vm.hostname = "prometheus"
25+
prometheus.vm.network "private_network", ip: "10.0.0.10"
26+
prometheus.vm.provision "shell", inline: $prometheus_script
27+
prometheus.vm.synced_folder "prometheus-config/", "/opt/prometheus"
28+
prometheus.vm.provider "virtualbox" do |vb|
29+
vb.memory = 2024
30+
vb.cpus = 1
31+
end
32+
end
33+
34+
config.vm.define "linux-batch" do |linuxbatch|
35+
linuxbatch.vm.box = "bento/ubuntu-20.04"
36+
linuxbatch.vm.hostname = "linux-batch"
37+
linuxbatch.vm.network "private_network", ip: "10.0.0.11"
38+
linuxbatch.vm.provision "docker" do |d|
39+
d.run "batch", image: "jaimesalas/prom-batch", args: "-e PORT=80 -p 8080:80"
40+
d.run "batch2", image: "jaimesalas/prom-batch", args: "-e PORT=80 -p 8081:80"
41+
end
42+
linuxbatch.vm.provision "shell", inline: $exporter_script
43+
linuxbatch.vm.provider "virtualbox" do |vb|
44+
vb.memory = 1012
45+
vb.cpus = 1
46+
end
47+
end
48+
49+
config.vm.define "linux-web" do |linuxweb|
50+
linuxweb.vm.box = "bento/ubuntu-20.04"
51+
linuxweb.vm.hostname = "linux-web"
52+
linuxweb.vm.network "private_network", ip: "10.0.0.12"
53+
linuxweb.vm.provision "docker" do |d|
54+
d.run "web", image: "jaimesalas/prom-web", args: "-e PORT=80 -p 8080:80"
55+
d.run "web2", image: "jaimesalas/prom-web", args: "-e PORT=80 -p 8081:80"
56+
end
57+
linuxweb.vm.provision "shell", inline: $exporter_script
58+
linuxweb.vm.provider "virtualbox" do |vb|
59+
vb.memory = 1012
60+
vb.cpus = 1
61+
end
62+
end
63+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
while :
3+
do
4+
for (( c=1; c<=$((1 + $RANDOM % 50)); c++ ))
5+
do
6+
curl -s -X GET "http://10.0.0.12:8080/quote" > /dev/null
7+
done
8+
9+
curl -s -X GET "http://10.0.0.12:8080/quote?slow=true" > /dev/null
10+
11+
for (( c=1; c<=$((1 + $RANDOM % 30)); c++ ))
12+
do
13+
curl -s -X GET "http://10.0.0.12:8081/quote" > /dev/null
14+
done
15+
16+
curl -s -X GET "http://10.0.0.12:8081/quote?slow=true" > /dev/null
17+
18+
done
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"targets": [
4+
"linux-batch:9100"
5+
],
6+
"labels": {
7+
"os": "linux",
8+
"runtime": "vm"
9+
}
10+
},
11+
{
12+
"targets": [
13+
"linux-batch:8080",
14+
"linux-batch:8081"
15+
],
16+
"labels": {
17+
"os": "linux",
18+
"runtime": "container"
19+
}
20+
}
21+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
global:
2+
scrape_interval: 60s
3+
4+
scrape_configs:
5+
- job_name: "prometheus"
6+
static_configs:
7+
- targets: ["localhost:9090"]
8+
9+
- job_name: "web"
10+
file_sd_configs:
11+
- files:
12+
- "web.json"
13+
metric_relabel_configs:
14+
- source_labels: [__name__]
15+
regex: "go_.*"
16+
action: drop
17+
18+
- job_name: "batch"
19+
file_sd_configs:
20+
- files:
21+
- "batch.json"
22+
relabel_configs:
23+
- source_labels: [runtime]
24+
regex: container
25+
target_label: runtime
26+
replacement: docker
27+
metric_relabel_configs:
28+
- source_labels: [__name__]
29+
regex: "go_.*"
30+
action: drop
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"targets": [
4+
"linux-web:9100"
5+
],
6+
"labels": {
7+
"os": "linux",
8+
"runtime": "vm"
9+
}
10+
},
11+
{
12+
"targets": [
13+
"linux-web:8080",
14+
"linux-web:8081"
15+
],
16+
"labels": {
17+
"os": "linux",
18+
"runtime": "docker"
19+
}
20+
}
21+
]

0 commit comments

Comments
 (0)