Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit 0aadec7

Browse files
authored
Prometheus docker service (#77)
* feat: adjust docker-compose file * feat: add prometheus docker service * feat: add node-exporter docker service
1 parent abe96cb commit 0aadec7

File tree

8 files changed

+233
-41
lines changed

8 files changed

+233
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/docker/grafana/var/
99
*.log
1010
.DS_Store
11+
/docker/prometheus/var/

README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Visit the online dashboards at [https://ckbmonitor.bit.host/], and you can use t
1414
$ cp docker/.env.example docker/.env
1515

1616
$ docker-compose -f docker/docker-compose.yaml up -d
17+
18+
$ source docker/.env && psql "postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@127.0.0.1:${POSTGRES_PORT:-"5432"}" -f src/schema.sql
1719
```
1820

1921
### Install CKBAnalyzer
@@ -22,16 +24,7 @@ Download from [releases](https://github.com/keroro520/ckb-analyzer/releases).
2224

2325
### Run CKBAnalyzer
2426

25-
The following environment variables are required by CKBAnalyzer. They are mostly declared inside [`docker/.env.example`](./docker/.env.example). You can specify an environment file with `--envfile`.
26-
27-
| variable | required | description |
28-
| :--- | :--- | :--- |
29-
| `IPINFO_IO_TOKEN` | false | [ipinfo.io](https://ipinfo.ip) authentication token, is used to look up the geographical location by nodes' ip. |
30-
| `PGHOST` | true | Postgres host |
31-
| `PGPORT` | true | Postgres port |
32-
| `PGDATABASE` | true | Postgres database |
33-
| `PGUSER` | true | Postgres username |
34-
| `PGPASSWORD` | true | Postgres password |
27+
Mostly environment variables are declared inside [`docker/.env.example`](./docker/.env.example). You can specify an environment file with `--envfile`.
3528

3629
```shell
3730
ckb-analyzer \

docker/docker-compose.yaml

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,7 @@ services:
99
- "5432:5432"
1010
volumes:
1111
- "./postgresql/var/lib/postgresql/data:/var/lib/postgresql/data"
12-
13-
# # This short service is used to import database schema.
14-
# # Schema file locates at "../src/schema.sql"
15-
# postgresql-init-schema:
16-
# container_name: ckb-analyzer-postgresql-init-schema
17-
# image: timescale/timescaledb:latest-pg12
18-
# env_file: .env
19-
# volumes:
20-
# - "../src/schema.sql:/var/lib/postgresql/data/schema.sql"
21-
# entrypoint: [ "bash", "-c", "sleep 3 && psql postgres://ckb:ckb@postgresql:5432 -f /var/lib/postgresql/data/schema.sql" ]
22-
# restart: "no"
23-
# depends_on:
24-
# - postgresql
12+
restart: always
2513

2614
grafana:
2715
container_name: ckb-analyzer-grafana
@@ -35,3 +23,39 @@ services:
3523
- ./grafana/var/lib/grafana:/var/lib/grafana
3624
depends_on:
3725
- postgresql
26+
- prometheus
27+
restart: always
28+
29+
prometheus:
30+
container_name: ckb-analyzer-prometheus
31+
image: prom/prometheus:latest
32+
volumes:
33+
- ./prometheus/etc/prometheus:/etc/prometheus/
34+
- ./prometheus/var/lib/prometheus:/var/lib/prometheus
35+
command:
36+
- '--config.file=/etc/prometheus/prometheus.yml'
37+
- '--storage.tsdb.path=/var/lib/prometheus'
38+
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
39+
- '--web.console.templates=/usr/share/prometheus/consoles'
40+
ports:
41+
- "9091:9090" # FIXME
42+
extra_hosts:
43+
# use `host.docker.internal` as host DNS name
44+
- "host.docker.internal:host-gateway"
45+
restart: always
46+
47+
node-exporter:
48+
image: prom/node-exporter
49+
container_name: ckb-analyzer-node-exporter
50+
volumes:
51+
- /proc:/host/proc:ro
52+
- /sys:/host/sys:ro
53+
- /:/rootfs:ro
54+
command:
55+
- '--path.procfs=/host/proc'
56+
- '--path.sysfs=/host/sys'
57+
- --collector.filesystem.ignored-mount-points
58+
- "^/(sys|proc|dev|host|etc|rootfs/var/lib/docker/containers|rootfs/var/lib/docker/overlay2|rootfs/run/docker/netns|rootfs/var/lib/docker/aufs)($$|/)"
59+
ports:
60+
- 9100:9100
61+
restart: always

docker/grafana/etc/grafana/provisioning/dashboards/CKB/cell.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"gnetId": null,
2424
"graphTooltip": 0,
2525
"id": 23,
26-
"iteration": 1641288150895,
26+
"iteration": 1641991445924,
2727
"links": [],
2828
"liveNow": false,
2929
"panels": [
@@ -272,9 +272,9 @@
272272
{
273273
"allValue": null,
274274
"current": {
275-
"selected": true,
276-
"text": "ckb_testnet",
277-
"value": "ckb_testnet"
275+
"selected": false,
276+
"text": "ckb",
277+
"value": "ckb"
278278
},
279279
"description": "CKB network name",
280280
"error": null,
@@ -285,12 +285,12 @@
285285
"name": "network",
286286
"options": [
287287
{
288-
"selected": false,
288+
"selected": true,
289289
"text": "ckb",
290290
"value": "ckb"
291291
},
292292
{
293-
"selected": true,
293+
"selected": false,
294294
"text": "ckb_testnet",
295295
"value": "ckb_testnet"
296296
}
@@ -383,4 +383,4 @@
383383
"title": "Cell",
384384
"uid": "zxreLM5nk",
385385
"version": 7
386-
}
386+
}

docker/grafana/etc/grafana/provisioning/dashboards/CKB/miner.json

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"fiscalYearStartMonth": 0,
2323
"gnetId": null,
2424
"graphTooltip": 0,
25-
"id": 15,
26-
"iteration": 1639974772371,
25+
"id": 17,
26+
"iteration": 1641978245254,
2727
"links": [],
2828
"liveNow": false,
2929
"panels": [
@@ -326,6 +326,124 @@
326326
}
327327
],
328328
"type": "piechart"
329+
},
330+
{
331+
"datasource": "PostgreSQL-ckb",
332+
"fieldConfig": {
333+
"defaults": {
334+
"color": {
335+
"mode": "palette-classic"
336+
},
337+
"custom": {
338+
"axisLabel": "",
339+
"axisPlacement": "auto",
340+
"barAlignment": 0,
341+
"drawStyle": "line",
342+
"fillOpacity": 0,
343+
"gradientMode": "none",
344+
"hideFrom": {
345+
"legend": false,
346+
"tooltip": false,
347+
"viz": false
348+
},
349+
"lineInterpolation": "linear",
350+
"lineWidth": 1,
351+
"pointSize": 5,
352+
"scaleDistribution": {
353+
"type": "linear"
354+
},
355+
"showPoints": "auto",
356+
"spanNulls": false,
357+
"stacking": {
358+
"group": "A",
359+
"mode": "none"
360+
},
361+
"thresholdsStyle": {
362+
"mode": "off"
363+
}
364+
},
365+
"mappings": [],
366+
"thresholds": {
367+
"mode": "absolute",
368+
"steps": [
369+
{
370+
"color": "green",
371+
"value": null
372+
},
373+
{
374+
"color": "red",
375+
"value": 80
376+
}
377+
]
378+
}
379+
},
380+
"overrides": []
381+
},
382+
"gridPos": {
383+
"h": 9,
384+
"w": 15,
385+
"x": 9,
386+
"y": 9
387+
},
388+
"id": 5,
389+
"options": {
390+
"legend": {
391+
"calcs": [
392+
"sum"
393+
],
394+
"displayMode": "table",
395+
"placement": "right"
396+
},
397+
"tooltip": {
398+
"mode": "single"
399+
}
400+
},
401+
"pluginVersion": "8.2.2",
402+
"targets": [
403+
{
404+
"format": "time_series",
405+
"group": [],
406+
"metricColumn": "none",
407+
"rawQuery": true,
408+
"rawSql": "WITH timed_block AS (\n SELECT\n time_bucket(INTERVAL '$interval', time) AS bucket,\n miner_lock_args\n FROM $network.block\n WHERE $__timeFilter(time)\n)\n SELECT\n bucket AS time,\n miner_lock_args AS metric,\n COUNT(*)\n FROM timed_block\n GROUP BY bucket, miner_lock_args\n ORDER BY bucket\n",
409+
"refId": "A",
410+
"select": [
411+
[
412+
{
413+
"params": [
414+
"value"
415+
],
416+
"type": "column"
417+
}
418+
]
419+
],
420+
"timeColumn": "time",
421+
"where": [
422+
{
423+
"name": "$__timeFilter",
424+
"params": [],
425+
"type": "macro"
426+
}
427+
]
428+
}
429+
],
430+
"timeFrom": null,
431+
"timeShift": null,
432+
"title": "Miner Lock Args",
433+
"transformations": [
434+
{
435+
"id": "organize",
436+
"options": {
437+
"excludeByName": {},
438+
"indexByName": {},
439+
"renameByName": {
440+
"\u00124Vx�": "unknown",
441+
"Value": "none"
442+
}
443+
}
444+
}
445+
],
446+
"type": "timeseries"
329447
}
330448
],
331449
"refresh": false,
@@ -370,7 +488,7 @@
370488
"auto_count": 30,
371489
"auto_min": "10s",
372490
"current": {
373-
"selected": true,
491+
"selected": false,
374492
"text": "1d",
375493
"value": "1d"
376494
},
@@ -447,5 +565,5 @@
447565
"timezone": "",
448566
"title": "Miner",
449567
"uid": "ZPpJHDonz",
450-
"version": 8
451-
}
568+
"version": 2
569+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# config file version
2+
apiVersion: 1
3+
4+
# list of datasources to insert/update depending
5+
# what's available in the database
6+
datasources:
7+
# <string, required> name of the datasource. Required
8+
- name: Prometheus-ckb
9+
# <string, required> datasource type. Required
10+
type: prometheus
11+
# <int> org id. will default to orgId 1 if not specified
12+
orgId: 1
13+
# <string> url
14+
# defined service in docker-compose.yaml
15+
url: prometheus:9090
16+
# <string> database user, if used
17+
editable: true
18+
jsonData:
19+
manageAlerts: true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# my global config
2+
global:
3+
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
4+
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
5+
# scrape_timeout is set to the global default (10s).
6+
7+
# Alertmanager configuration
8+
alerting:
9+
alertmanagers:
10+
- static_configs:
11+
- targets:
12+
# - alertmanager:9093
13+
14+
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
15+
rule_files:
16+
# - "first_rules.yml"
17+
# - "second_rules.yml"
18+
19+
# A scrape configuration containing exactly one endpoint to scrape:
20+
# Here it's Prometheus itself.
21+
scrape_configs:
22+
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
23+
- job_name: "ckb"
24+
metrics_path: '/'
25+
static_configs:
26+
- targets:
27+
- "host.docker.internal:8100"
28+
29+
- job_name: "node-exporter"
30+
static_configs:
31+
- targets:
32+
- "host.docker.internal:9100"

src/main.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,32 @@ async fn main() {
3737

3838
let pg_config = {
3939
let host = env::var_os("PGHOST")
40-
.expect("requires environment variable \"PGHOST\"")
40+
.or_else(|| env::var_os("POSTGRES_HOST"))
41+
.expect("requires environment variable \"PGHOST\" or \"POSTGRES_HOST\"")
4142
.to_string_lossy()
4243
.to_string();
4344
let port = env::var_os("PGPORT")
45+
.or_else(|| env::var_os("POSTGRES_PORT"))
4446
.map(|raw| {
4547
raw.to_string_lossy()
4648
.to_string()
4749
.parse::<u16>()
48-
.expect("invalid environment variable \"PGPORT\"")
50+
.expect("invalid environment variable \"PGPORT\" or \"POSTGRES_PORT\"")
4951
})
50-
.expect("requires environment variable \"PGPORT\"");
52+
.expect("requires environment variable \"PGPORT\" or \"POSTGRES_PORT\"");
5153
let database = env::var_os("PGDATABASE")
52-
.expect("requires environment variable \"PGDATABASE\"")
54+
.or_else(|| env::var_os("POSTGRES_DB"))
55+
.expect("requires environment variable \"PGDATABASE\" or \"POSTGRES_DB\"")
5356
.to_string_lossy()
5457
.to_string();
5558
let user = env::var_os("PGUSER")
56-
.expect("requires environment variable \"PGUSER\"")
59+
.or_else(|| env::var_os("POSTGRES_USER"))
60+
.expect("requires environment variable \"PGUSER\" or \"POSTGRES_USER\"")
5761
.to_string_lossy()
5862
.to_string();
5963
let password = env::var_os("PGPASSWORD")
60-
.expect("requires environment variable \"PGPASSWORD\"")
64+
.or_else(|| env::var_os("POSTGRES_PASSWORD"))
65+
.expect("requires environment variable \"PGPASSWORD\" or \"POSTGRES_PASSWORD\"")
6166
.to_string_lossy()
6267
.to_string();
6368
let mut config = tokio_postgres::Config::new();

0 commit comments

Comments
 (0)