Skip to content

Commit f4b738b

Browse files
committed
feat: Attached promtheus/grafana services with docker
1 parent 11c2444 commit f4b738b

6 files changed

Lines changed: 62 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,4 @@ _build/
196196
# Attack simulation test results
197197
tests/security/attack_simulation/results/
198198
libp2p-forge
199+
libp2p-metrics

examples/metrics/coordinator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ async def command_executor(self, nursery):
5959
await self.host.connect(info)
6060
await self.ping_service.ping(info.peer_id, int(parts[2]))
6161

62-
# Then the rtts will be fed to the prometheus-metrics
63-
6462
if cmd == "local":
6563
maddr = self.host.get_addrs()[0]
6664
print(maddr)

libp2p/metrics/docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
services:
2+
prometheus:
3+
image: prom/prometheus:latest
4+
container_name: prometheus
5+
volumes:
6+
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
7+
ports:
8+
- "${PROMETHEUS_PORT}:9090"
9+
extra_hosts:
10+
- "host.docker.internal:host-gateway"
11+
12+
grafana:
13+
image: grafana/grafana:latest
14+
container_name: grafana
15+
ports:
16+
- "${GRAFANA_PORT}:3000"
17+
depends_on:
18+
- prometheus

libp2p/metrics/metrics.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
import socket
2+
13
from prometheus_client import start_http_server
24
import trio
5+
36
from libp2p.host.ping import PingEvent
47
from libp2p.metrics.ping import PingMetrics
5-
from libp2p.utils.address_validation import find_free_port
8+
9+
10+
def find_available_port(start_port: int = 8000, host: str = "127.0.0.1") -> int:
11+
port = start_port
12+
13+
while True:
14+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
15+
try:
16+
sock.bind((host, port))
17+
return port
18+
except OSError:
19+
port += 1
620

721

822
class Metrics:
@@ -15,15 +29,26 @@ async def start_prometheus_server(
1529
self,
1630
metric_recv_channel: trio.MemoryReceiveChannel,
1731
) -> None:
18-
19-
free_port = find_free_port()
20-
start_http_server(free_port)
21-
22-
print(f"Prometheus server started: http://localhost:{free_port}")
32+
metrics = find_available_port(8000)
33+
prometheus_dashboard = find_available_port(9000)
34+
grafana_dashboard = find_available_port(7000)
35+
36+
start_http_server(metrics)
37+
38+
print(f"\nPrometheus metrics visible at: http://localhost:{metrics}")
39+
print(
40+
f"Prometheus dashboard visible at: http://localhost:{prometheus_dashboard}"
41+
)
42+
print(f"Grafana dashboard visible at: http://localhost:{grafana_dashboard}\n")
43+
44+
print(
45+
"\nStart prometheus and grafana dashboard, for another terminal: \n"
46+
f"PROMETHEUS_PORT={prometheus_dashboard} GRAFANA_PORT={grafana_dashboard} docker compose up\n"
47+
)
2348

2449
while True:
2550
event = await metric_recv_channel.receive()
26-
51+
2752
match event:
2853
case PingEvent():
29-
self.ping.record(event)
54+
self.ping.record(event)

libp2p/metrics/prometheus.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
global:
2+
scrape_interval: 5s
3+
4+
scrape_configs:
5+
- job_name: "libp2p-python"
6+
static_configs:
7+
- targets:
8+
- "host.docker.internal:8000"

libp2p/utils/paths.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
AUTOTLS_CERT_PATH = Path("libp2p-forge/peer1/autotls-cert.pem")
1818
AUTOTLS_KEY_PATH = Path("libp2p-forge/peer1/autotls-key.pem")
1919

20+
METRICS_CONFIG_PATH = Path("libp2p-metrics/.config")
21+
2022

2123
def get_temp_dir() -> Path:
2224
"""

0 commit comments

Comments
 (0)