This commit is contained in:
13
exporter.py
13
exporter.py
@@ -1,6 +1,7 @@
|
||||
import time
|
||||
import docker
|
||||
from prometheus_client import start_http_server, Gauge
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
client = docker.from_env()
|
||||
|
||||
@@ -15,8 +16,8 @@ def calc_cpu(stats):
|
||||
cpus = stats["cpu_stats"].get("online_cpus", 1)
|
||||
return (cd / sd) * cpus * 100 if sd > 0 else 0
|
||||
|
||||
def collect():
|
||||
for c in client.containers.list():
|
||||
def collect_one(c):
|
||||
try:
|
||||
stats = c.stats(stream=False)
|
||||
name = c.name
|
||||
labels = c.labels
|
||||
@@ -29,8 +30,16 @@ def collect():
|
||||
tx = sum(v["tx_bytes"] for v in net.values())
|
||||
net_rx_gauge.labels(name, project, service).set(rx)
|
||||
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)
|
||||
while True:
|
||||
collect()
|
||||
time.sleep(15)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user