Skip to content

Commit 3691767

Browse files
committed
Prometheus: Implement suggestions by CodeRabbit
1 parent fbd11f7 commit 3691767

File tree

1 file changed

+73
-42
lines changed

1 file changed

+73
-42
lines changed
Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,87 @@
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

13-
[Prometheus](https://prometheus.io/docs/introduction/overview/) is a monitoring software for collecting metrics data from applications and infrastructures. Its focus lies on collecting big amounts of concise event data from the monitored system, roughly timestamp points with key-value pairs.
15+
[Prometheus] is a monitoring system that collects metrics from applications and
16+
infrastructure. It focuses on ingesting large volumes of concise time‑series
17+
events—timestamped points with key‑value labels.
1418

15-
Such data is very useful to track the state and trajectory of a system, so storing this data for the long term is a common need for Prometheus users.
19+
This data helps you track a system’s state and trajectory. Many Prometheus users
20+
want to retain it long term.
1621

17-
This is where [CrateDB](https://cratedb.com/database) comes into place. With the [CrateDB Prometheus Adapter](https://github.com/crate/cratedb-prometheus-adapter), one can easily store the collected metrics data in CrateDB and take advantage of its high ingestion and query speed and friendly UI to massively scale-out Prometheus.
22+
[CrateDB] helps here. With the [CrateDB Prometheus Adapter], you can store metrics
23+
in CrateDB and use its fast ingestion and query performance to scale Prometheus.
1824

1925
## Set up Docker Compose
2026

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.
27+
Run CrateDB, Prometheus, and the CrateDB Prometheus Adapter as [Docker containers].
28+
Use [Docker Compose] to centralize container management so you can build and run
29+
all containers with one command and define their connections in a YAML file.
2230

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

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.
33+
Create a directory on your machine to host the configuration files.
34+
Create three YAML files with your preferred editor and save them with the `.yml` extension.
2735

2836
### Create `docker-compose.yml`
2937

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.
38+
Create `docker-compose.yml` to configure the three containers.
39+
Define services for CrateDB, Prometheus, and the adapter. Mount `config.yml`
40+
and `prometheus.yml` into the adapter and Prometheus containers, respectively.
41+
You will create these files in the following steps.
3342
```yaml
3443
services:
3544
cratedb:
36-
image: "crate"
45+
image: "crate:4.7.0"
3746
ports:
3847
- "4200:4200"
3948
- "5432:5432"
49+
volumes:
50+
- cratedb-data:/data
51+
restart: unless-stopped
4052
prometheus:
41-
image: "prom/prometheus"
53+
image: "prom/prometheus:v2.33.3"
4254
volumes:
4355
- ./prometheus.yml:/etc/prometheus/prometheus.yml
4456
ports:
4557
- "9090:9090"
58+
restart: unless-stopped
4659
cratedb-prometheus-adapter:
47-
image: "ghcr.io/crate/cratedb-prometheus-adapter"
60+
image: "ghcr.io/crate/cratedb-prometheus-adapter:0.5.8"
4861
volumes:
4962
- ./config.yml:/etc/cratedb-prometheus-adapter/config.yml
5063
ports:
5164
- "9268:9268"
65+
depends_on:
66+
- cratedb
67+
- prometheus
68+
restart: unless-stopped
69+
volumes:
70+
cratedb-data:
5271
```
5372
5473
### Create `prometheus.yml`
5574

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.
75+
Next, create `prometheus.yml` following the [Prometheus documentation].
76+
This file defines the services that Prometheus scrapes.
5777

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

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).
80+
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)).
6181

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`.
82+
Because the adapter runs in Docker, use the adapter service name from `docker-compose.yml` (`cratedb-prometheus-adapter`) instead of `localhost` in the URLs.
6383

64-
The resulting prometheus.yml looks then like this:
84+
Use the following `prometheus.yml`:
6585
```yaml
6686
global:
6787
scrape_interval: 15s # By default, scrape targets every 15 seconds.
@@ -81,7 +101,7 @@ scrape_configs:
81101
scrape_interval: 5s
82102

83103
static_configs:
84-
- targets: ['localhost:9090']
104+
- targets: ['prometheus:9090']
85105

86106
remote_write:
87107
- url: http://cratedb-prometheus-adapter:9268/write
@@ -90,9 +110,10 @@ remote_read:
90110
```
91111
### Create `config.yml`
92112

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.
113+
Finally, create `config.yml` following the [CrateDB Prometheus Adapter].
114+
This file defines the CrateDB endpoints that the adapter writes to.
94115

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.
116+
Set the host to `cratedb` (the service name in `docker-compose.yml`) instead of `localhost`. Keep the remaining variables at their defaults.
96117
```yaml
97118
cratedb_endpoints:
98119
- host: "cratedb" # Host to connect to (default: "localhost")
@@ -105,18 +126,18 @@ cratedb_endpoints:
105126
enable_tls: false # Whether to connect using TLS (default: false).
106127
allow_insecure_tls: false # Whether to allow insecure / invalid TLS certificates (default: false).
107128
```
108-
I make sure both `docker-compose.yml`, `config.yml`, and `prometheus.yml` are in the same directory in my local machine.
129+
Place `docker-compose.yml`, `config.yml`, and `prometheus.yml` in the same directory on your machine.
109130

110131
## Start the services
111132

112-
Finally, I navigate to my CrateDB-Prometheus directory in my terminal and start Docker Compose with the `docker-compose up` command
133+
Finally, navigate to your CrateDBPrometheus directory and start Docker Compose:
113134
```shell
114-
$ cd /Users/Path/To/Directory/CrateDB-Prometheus
115-
$ docker-compose up
135+
cd /Users/Path/To/Directory/CrateDB-Prometheus
136+
docker-compose up
116137
```
117138

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-
139+
After Docker Compose starts, open the CrateDB Admin UI at `http://localhost:4200`
140+
and create a `metrics` table to store metrics gathered by Prometheus.
120141
```sql
121142
CREATE TABLE "metrics" (
122143
"timestamp" TIMESTAMP,
@@ -128,24 +149,34 @@ CREATE TABLE "metrics" (
128149
PRIMARY KEY ("timestamp", "labels_hash", "day__generated")
129150
) PARTITIONED BY ("day__generated")
130151
```
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**
132152

133-
![im1|690x206](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/91223397b30bce2f7188617436ea12ceed83d83c.png)
153+
Navigate to `http://localhost:9090` to open the Prometheus UI. Go to **Status** → **Targets**.
154+
155+
![Prometheus Targets page showing the self-scrape target as UP](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/91223397b30bce2f7188617436ea12ceed83d83c.png)
156+
157+
Confirm that Prometheus monitors itself.
158+
159+
![Prometheus target details for the self-scrape job](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/57ccb5374b0ab524466de08feefbafde559dac87.png)
160+
161+
Return to the CrateDB Admin UI and select the `metrics` table you created.
134162

135-
And confirm that Prometheus is successfully monitoring itself.
163+
After a few minutes, Prometheus will have gathered hundreds of thousands of data points.
136164

137-
![im2|690x173](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/57ccb5374b0ab524466de08feefbafde559dac87.png)
165+
![CrateDB Admin UI showing the populated metrics table](https://us1.discourse-cdn.com/flex020/uploads/crate/original/1X/22e8c7d5a90ec9240a4cb4269774e143759aa92e.jpeg)
138166

139-
Lastly, I head back to the CrateDB Admin UI and select the `metrics` table I just created.
167+
Use CrateDB’s query engine to analyze and visualize this data with tools
168+
like {ref}`grafana`.
140169

141-
I see that only after a few minutes of running, Prometheus has gathered over 300k data points.
170+
Explore these related tutorials:
142171

143-
![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.
172+
* [Visualizing time‑series data with Grafana and CrateDB](https://cratedb.com/blog/visualizing-time-series-data-with-grafana-and-cratedb)
173+
* [Monitoring CrateDB with Prometheus and Grafana](https://cratedb.com/blog/monitoring-cratedb-with-prometheus-and-grafana)
147174

148-
Here are a few interesting tutorials on that matter:
149175

150-
* https://cratedb.com/blog/visualizing-time-series-data-with-grafana-and-cratedb
151-
* https://cratedb.com/blog/monitoring-cratedb-with-prometheus-and-grafana
176+
[CrateDB]: https://cratedb.com/database
177+
[CrateDB Prometheus Adapter]: https://github.com/crate/cratedb-prometheus-adapter
178+
[Docker Compose]: https://docs.docker.com/compose/
179+
[Docker containers]: https://www.docker.com/resources/what-container
180+
[Docker installation guide]: https://docs.docker.com/get-docker/
181+
[Prometheus]: https://prometheus.io/docs/introduction/overview/
182+
[Prometheus documentation]: https://prometheus.io/docs/prometheus/latest/getting_started/

0 commit comments

Comments
 (0)