Update exporter.py
All checks were successful
Build / build (push) Successful in 5s

This commit is contained in:
2026-03-18 10:45:04 +03:00
parent b61bfc9682
commit 7892c72279

View File

@@ -1,6 +1,7 @@
import time import time
import docker import docker
from prometheus_client import start_http_server, Gauge from prometheus_client import start_http_server, Gauge
from concurrent.futures import ThreadPoolExecutor
client = docker.from_env() client = docker.from_env()
@@ -15,8 +16,8 @@ def calc_cpu(stats):
cpus = stats["cpu_stats"].get("online_cpus", 1) cpus = stats["cpu_stats"].get("online_cpus", 1)
return (cd / sd) * cpus * 100 if sd > 0 else 0 return (cd / sd) * cpus * 100 if sd > 0 else 0
def collect(): def collect_one(c):
for c in client.containers.list(): try:
stats = c.stats(stream=False) stats = c.stats(stream=False)
name = c.name name = c.name
labels = c.labels labels = c.labels
@@ -29,8 +30,16 @@ def collect():
tx = sum(v["tx_bytes"] for v in net.values()) tx = sum(v["tx_bytes"] for v in net.values())
net_rx_gauge.labels(name, project, service).set(rx) net_rx_gauge.labels(name, project, service).set(rx)
net_tx_gauge.labels(name, project, service).set(tx) net_tx_gauge.labels(name, project, service).set(tx)
except Exception:
pass
def collect():
containers = client.containers.list()
with ThreadPoolExecutor(max_workers=10) as executor:
executor.map(collect_one, containers)
start_http_server(9338) start_http_server(9338)
while True: while True:
collect() collect()
time.sleep(15) time.sleep(15)