Commit 86d1e6d9 authored by Krzysztof Mochocki's avatar Krzysztof Mochocki
Browse files

fixed prometheus and added broadcasting of memory data

parent 62f7792e
import os, psutil
from hive.utils.stats import PrometheusClient, BroadcastObject
def log_memory_usage(memtypes=["rss", "vms", "shared"]) -> str:
def log_memory_usage(memtypes=["rss", "vms", "shared"], broadcast = True) -> str:
"""
Logs current memory types
Logs current memory types, additionally broadcast if broadcast set to True (default)
Available memtypes: rss, vms, shared, text, lib, data, dirty
"""
......@@ -13,6 +14,8 @@ def log_memory_usage(memtypes=["rss", "vms", "shared"]) -> str:
human_readable = { "rss": "physical_memory", "vms": "virtual_memory", "shared": "shared_memory", "text": "used_by_executable", "lib": "used_by_shared_libraries" }
stats = psutil.Process(os.getpid()).memory_info() # docs: https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_info
if broadcast:
PrometheusClient.broadcast([ BroadcastObject(f'hivemind_memory_{key}', getattr(stats, key), 'b') for key in stats._fields ]) # broadcast to prometheus
return f"memory usage report: { ', '.join( [ f'{ human_readable.get(k, k) } = { format_bytes(getattr(stats, k)) }' for k in memtypes ] ) }"
def chunks(lst, n):
......
......@@ -49,7 +49,7 @@ class PrometheusClient:
gauges = {}
while pid_exists(pid):
value : BroadcastObject = PrometheusClient.logs_to_broadcast.get()
value : BroadcastObject = PrometheusClient.logs_to_broadcast.get(True)
value.debug()
value_name = value.name()
......@@ -75,11 +75,13 @@ class PrometheusClient:
log.warn("Failed to import prometheus client. Online stats disabled")
return
from threading import Thread
deamon = Thread(target=PrometheusClient.work, args=[ port, getpid() ], daemon=True)
deamon.start()
PrometheusClient.deamon = Thread(target=PrometheusClient.work, args=[ port, getpid() ], daemon=True)
PrometheusClient.deamon.start()
@staticmethod
def broadcast(obj):
if PrometheusClient.deamon is None:
return
if type(obj) == type(list()):
for v in obj:
PrometheusClient.broadcast(v)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment