Skip to content

Commit c7f4347

Browse files
committed
Prometheus: Implement suggestions by CodeRabbit
1 parent efb7776 commit c7f4347

File tree

1 file changed

+54
-33
lines changed

1 file changed

+54
-33
lines changed
Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
(prometheus-tutorial)=
22
# Storing long-term metrics with Prometheus in CrateDB
33

4-
In this tutorial, I show how to
4+
This tutorial shows how to:
55

66
* Set up Docker Compose to run CrateDB, Prometheus, and the CrateDB Prometheus Adapter
77
* Run the applications with Docker Compose
88

9-
*Note: this blog post uses CrateDB 4.7.0, Prometheus 2.33.3 and CrateDB Prometheus Adapter 0.4.0*
9+
:::{note}
10+
These examples use CrateDB 4.7.0, Prometheus 2.33.3, and the CrateDB Prometheus Adapter 0.5.8.
11+
:::
1012

1113
## Motivation
1214

@@ -18,50 +20,63 @@ This is where [CrateDB](https://cratedb.com/database) comes into place. With the
1820

1921
## Set up Docker Compose
2022

21-
Both CrateDB, Prometheus, and the CrateDB Prometheus Adapter applications can be run as [Docker containers](https://www.docker.com/resources/what-container). To then centralize the container management I use [Docker Compose](https://docs.docker.com/compose/), this way I can build and run all the containers with a single command and set up the connections between them in a YAML file.
23+
Run CrateDB, Prometheus, and the CrateDB Prometheus Adapter as [Docker containers].
24+
Use [Docker Compose] to centralize container management so you can build and run
25+
all containers with one command and define their connections in a YAML file.
2226

23-
Before anything else, I follow the [Docker Installation Tutorial](https://docs.docker.com/get-docker/) to get Docker in my local machine.
27+
Install Docker by following the [Docker installation guide].
2428

25-
Then, I create a directory in my local machine to host the necessary configuration files.
26-
I’ll have a total of three of them, all following the YAML format. They can be easily created using any [text editor](https://www.computerhope.com/jargon/e/editor.htm), like TextEdit on a Mac, and then saved with the `.yml` format.
29+
Create a directory on your machine to host the configuration files.
30+
Create three YAML files with your preferred editor and save them with the `.yml` extension.
2731

2832
### Create `docker-compose.yml`
2933

30-
The first YAML file I create is `docker-compose.yml`, which wraps up the configurations for the three containers.
31-
32-
I specify CrateDB, Prometheus, and Adapter as services. Then, I add `config.yml` and `prometheus.yml` files as volumes to the Adapter and Prometheus containers, respectively. These files will be created in the following steps.
34+
Create `docker-compose.yml` to configure the three containers.
35+
Define services for CrateDB, Prometheus, and the adapter. Mount `config.yml`
36+
and `prometheus.yml` into the adapter and Prometheus containers, respectively.
37+
You will create these files in the following steps.
3338
```yaml
3439
services:
3540
cratedb:
36-
image: "crate"
41+
image: "crate:4.7.0"
3742
ports:
3843
- "4200:4200"
3944
- "5432:5432"
45+
volumes:
46+
- cratedb-data:/data
47+
restart: unless-stopped
4048
prometheus:
41-
image: "prom/prometheus"
49+
image: "prom/prometheus:v2.33.3"
4250
volumes:
4351
- ./prometheus.yml:/etc/prometheus/prometheus.yml
4452
ports:
4553
- "9090:9090"
54+
restart: unless-stopped
4655
cratedb-prometheus-adapter:
47-
image: "ghcr.io/crate/cratedb-prometheus-adapter"
56+
image: "ghcr.io/crate/cratedb-prometheus-adapter:0.5.8"
4857
volumes:
4958
- ./config.yml:/etc/cratedb-prometheus-adapter/config.yml
5059
ports:
5160
- "9268:9268"
61+
depends_on:
62+
- cratedb
63+
- prometheus
64+
restart: unless-stopped
65+
volumes:
66+
cratedb-data:
5267
```
5368
5469
### Create `prometheus.yml`
5570

56-
Next, following the[ Prometheus Documentation](https://prometheus.io/docs/prometheus/latest/getting_started/), I create a `prometheus.yml` file, which holds the scraping configuration for whichever service Prometheus collects metrics from.
71+
Next, create `prometheus.yml` as described in the [Prometheus documentation](https://prometheus.io/docs/prometheus/latest/getting_started/). This file defines which services Prometheus scrapes.
5772

58-
To keep it simple, I follow the example in the Prometheus documentation and set it to monitor itself.
73+
To keep it simple, monitor Prometheus itself.
5974

60-
One last bit of configuration necessary to forward requests from Prometheus to the CrateDB Adapter is to set `remote_write` and `remote_read` to the Adapter URL, as stated in [CrateDB Prometheus Adapter Setup](https://github.com/crate/cratedb-prometheus-adapter).
75+
To forward samples to CrateDB, set `remote_write` and `remote_read` to the adapter URL (see the [CrateDB Prometheus Adapter setup](https://github.com/crate/cratedb-prometheus-adapter)).
6176

62-
As I’m running the Adapter on Docker instead of locally, the host in its URL will not be `localhost`, but rather however I called the Adapter service previously in my `docker-compose.yml` file, in this case, `cratedb-prometheus-adapter`.
77+
Because the adapter runs in Docker, use the adapter service name from `docker-compose.yml` (`cratedb-prometheus-adapter`) instead of `localhost` in the URLs.
6378

64-
The resulting prometheus.yml looks then like this:
79+
Use the following `prometheus.yml`:
6580
```yaml
6681
global:
6782
scrape_interval: 15s # By default, scrape targets every 15 seconds.
@@ -81,7 +96,7 @@ scrape_configs:
8196
scrape_interval: 5s
8297

8398
static_configs:
84-
- targets: ['localhost:9090']
99+
- targets: ['prometheus:9090']
85100

86101
remote_write:
87102
- url: http://cratedb-prometheus-adapter:9268/write
@@ -90,9 +105,9 @@ remote_read:
90105
```
91106
### Create `config.yml`
92107

93-
Finally, following the [CrateDB Prometheus Adapter setup instructions](https://github.com/crate/cratedb-prometheus-adapter), I create the `config.yml` file, which defines the CrateDB endpoints the Adapter writes to.
108+
Finally, create `config.yml` as described in the [CrateDB Prometheus Adapter setup](https://github.com/crate/cratedb-prometheus-adapter). This file defines the CrateDB endpoints the adapter writes to.
94109

95-
As I did previously in the `prometheus.yml` file, the host is set to `cratedb`, which is how I declared the CrateDB service on the `docker-compose.yml` file, instead of the default `localhost`. The remaining variables are set with their default values.
110+
Set the host to `cratedb` (the service name in `docker-compose.yml`) instead of `localhost`. Keep the remaining variables at their defaults.
96111
```yaml
97112
cratedb_endpoints:
98113
- host: "cratedb" # Host to connect to (default: "localhost")
@@ -105,18 +120,18 @@ cratedb_endpoints:
105120
enable_tls: false # Whether to connect using TLS (default: false).
106121
allow_insecure_tls: false # Whether to allow insecure / invalid TLS certificates (default: false).
107122
```
108-
I make sure both `docker-compose.yml`, `config.yml`, and `prometheus.yml` are in the same directory in my local machine.
123+
Place `docker-compose.yml`, `config.yml`, and `prometheus.yml` in the same directory on your machine.
109124

110125
## Start the services
111126

112-
Finally, I navigate to my CrateDB-Prometheus directory in my terminal and start Docker Compose with the `docker-compose up` command
127+
Finally, navigate to your CrateDBPrometheus directory and start Docker Compose:
113128
```shell
114-
$ cd /Users/Path/To/Directory/CrateDB-Prometheus
115-
$ docker-compose up
129+
cd /Users/Path/To/Directory/CrateDB-Prometheus
130+
docker-compose up
116131
```
117132

118-
With Docker Compose up and running, I follow the [CrateDB Prometheus Adapter setup instructions](https://github.com/crate/cratedb-prometheus-adapter), navigate to the [CrateDB Admin UI](https://www.google.com/search?client=safari&rls=en&q=cratedb+admin+ui&ie=UTF-8&oe=UTF-8) at [http://localhost:4200](http://localhost:4200/) and create a `metrics` table in CrateDB, which will store the metrics gathered by Prometheus.
119-
133+
After Docker Compose starts, open the CrateDB Admin UI at `http://localhost:4200`
134+
and create a `metrics` table to store metrics gathered by Prometheus.
120135
```sql
121136
CREATE TABLE "metrics" (
122137
"timestamp" TIMESTAMP,
@@ -128,24 +143,30 @@ CREATE TABLE "metrics" (
128143
PRIMARY KEY ("timestamp", "labels_hash", "day__generated")
129144
) PARTITIONED BY ("day__generated")
130145
```
131-
Then I navigate to [http://localhost:9090](http://localhost:9090/), where I find the Prometheus UI. There, I head to **Status** and then **Targets**
146+
147+
Navigate to `http://localhost:9090` to open the Prometheus UI. Go to **Status** → **Targets**.
132148

133149
![im1|690x206](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/91223397b30bce2f7188617436ea12ceed83d83c.png)
134150

135-
And confirm that Prometheus is successfully monitoring itself.
151+
Confirm that Prometheus monitors itself.
136152

137153
![im2|690x173](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/57ccb5374b0ab524466de08feefbafde559dac87.png)
138154

139-
Lastly, I head back to the CrateDB Admin UI and select the `metrics` table I just created.
155+
Return to the CrateDB Admin UI and select the `metrics` table you created.
140156

141-
I see that only after a few minutes of running, Prometheus has gathered over 300k data points.
157+
After a few minutes, Prometheus will have gathered hundreds of thousands of data points.
142158

143159
![im3|690x403](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/22e8c7d5a90ec9240a4cb4269774e143759aa92e.jpeg)
144-
145-
I can now enjoy CrateDB’s incredible query speed to analyze and visualize this
146-
data using tools like {ref}`grafana`, which works effortlessly with CrateDB.
160+
161+
Use CrateDB’s query engine to analyze and visualize this data with tools
162+
like {ref}`grafana`.
147163

148164
Here are a few interesting tutorials on that matter:
149165

150166
* https://cratedb.com/blog/visualizing-time-series-data-with-grafana-and-cratedb
151167
* https://cratedb.com/blog/monitoring-cratedb-with-prometheus-and-grafana
168+
169+
170+
[Docker Compose]: https://docs.docker.com/compose/
171+
[Docker containers]: https://www.docker.com/resources/what-container
172+
[Docker installation guide]: https://docs.docker.com/get-docker/

0 commit comments

Comments
 (0)