forked from mtusi2026/mtusi2026lab6
лаба на 4
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import os
|
||||
import resource
|
||||
import time
|
||||
from threading import Lock
|
||||
|
||||
from opentelemetry import metrics
|
||||
from opentelemetry.metrics import Observation
|
||||
|
||||
|
||||
_lock = Lock()
|
||||
_busy = 0
|
||||
_backlog = 0
|
||||
_oldest = 0.0
|
||||
_queue_updated = 0.0
|
||||
_initialized = False
|
||||
_last_cpu = time.process_time()
|
||||
_last_wall = time.monotonic()
|
||||
|
||||
|
||||
def cpu_usage(options):
|
||||
global _last_cpu, _last_wall
|
||||
|
||||
with _lock:
|
||||
cpu = time.process_time()
|
||||
wall = time.monotonic()
|
||||
|
||||
value = (cpu - _last_cpu) / max(wall - _last_wall, 0.001)
|
||||
|
||||
_last_cpu = cpu
|
||||
_last_wall = wall
|
||||
|
||||
return [Observation(max(0, value) * 100)]
|
||||
|
||||
|
||||
def memory_usage(options):
|
||||
with open("/proc/self/statm", encoding="utf-8") as file:
|
||||
resident = int(file.read().split()[1])
|
||||
|
||||
return [Observation(resident * os.sysconf("SC_PAGE_SIZE"))]
|
||||
|
||||
|
||||
def worker_started():
|
||||
global _busy
|
||||
|
||||
with _lock:
|
||||
_busy += 1
|
||||
|
||||
|
||||
def worker_finished():
|
||||
global _busy
|
||||
|
||||
with _lock:
|
||||
_busy -= 1
|
||||
|
||||
|
||||
def update_queue(depth, oldest):
|
||||
global _backlog, _oldest, _queue_updated
|
||||
|
||||
with _lock:
|
||||
_backlog = int(depth)
|
||||
_oldest = float(oldest)
|
||||
_queue_updated = time.monotonic()
|
||||
|
||||
|
||||
def queue_observation(field):
|
||||
def callback(options):
|
||||
with _lock:
|
||||
if not _queue_updated:
|
||||
return []
|
||||
|
||||
if time.monotonic() - _queue_updated > 15:
|
||||
return []
|
||||
|
||||
value = _backlog if field == "depth" else _oldest
|
||||
|
||||
return [
|
||||
Observation(
|
||||
value,
|
||||
{"queue_name": "attachment.process.v1"},
|
||||
)
|
||||
]
|
||||
|
||||
return callback
|
||||
|
||||
|
||||
def setup_runtime_metrics(service_name):
|
||||
global _initialized
|
||||
|
||||
if _initialized:
|
||||
return
|
||||
|
||||
from src.database import engine, POOL_SIZE, MAX_OVERFLOW
|
||||
|
||||
meter = metrics.get_meter("messenger.runtime")
|
||||
pool = engine.sync_engine.pool
|
||||
|
||||
meter.create_observable_gauge(
|
||||
"messenger_process_cpu_percent",
|
||||
callbacks=[cpu_usage],
|
||||
)
|
||||
|
||||
meter.create_observable_gauge(
|
||||
"messenger_process_memory_bytes",
|
||||
callbacks=[memory_usage],
|
||||
)
|
||||
|
||||
meter.create_observable_gauge(
|
||||
"messenger_db_pool_used",
|
||||
callbacks=[
|
||||
lambda options: [Observation(pool.checkedout())]
|
||||
],
|
||||
)
|
||||
|
||||
meter.create_observable_gauge(
|
||||
"messenger_db_pool_capacity",
|
||||
callbacks=[
|
||||
lambda options: [
|
||||
Observation(POOL_SIZE + MAX_OVERFLOW)
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
meter.create_observable_gauge(
|
||||
"messenger_db_pool_waiting",
|
||||
callbacks=[
|
||||
lambda options: [
|
||||
Observation(pool.waiting_count())
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
if service_name == "messenger-worker":
|
||||
meter.create_observable_gauge(
|
||||
"messenger_worker_busy",
|
||||
callbacks=[
|
||||
lambda options: [Observation(_busy)]
|
||||
],
|
||||
)
|
||||
|
||||
meter.create_observable_gauge(
|
||||
"messenger_worker_capacity",
|
||||
callbacks=[
|
||||
lambda options: [Observation(1)]
|
||||
],
|
||||
)
|
||||
|
||||
if service_name == "messenger-dispatcher":
|
||||
meter.create_observable_gauge(
|
||||
"messenger_queue_depth",
|
||||
callbacks=[queue_observation("depth")],
|
||||
)
|
||||
|
||||
meter.create_observable_gauge(
|
||||
"messenger_queue_oldest_age_seconds",
|
||||
callbacks=[queue_observation("oldest")],
|
||||
)
|
||||
|
||||
_initialized = True
|
||||
Reference in New Issue
Block a user