forked from mtusi2026/mtusi2026lab6
Реализовать минимум наблюдаемости: OTLP, хранилища и Grafana
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""Internal exporter: durable backlog, including jobs not yet published to broker."""
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from src.storage import storage
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def log_message(self, *args):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
try:
|
||||
with storage.pool.connection() as conn:
|
||||
row = conn.execute("SELECT count(*) AS depth, COALESCE(EXTRACT(EPOCH FROM "
|
||||
"clock_timestamp()-min(created_at)),0) AS age FROM attachment_jobs "
|
||||
"WHERE state IN ('queued','processing')").fetchone()
|
||||
windows = {}
|
||||
for window, seconds in [('30s',30),('1m',60),('2m',120),('5m',300)]:
|
||||
result = conn.execute("SELECT count(*) AS total, count(*) FILTER (WHERE "
|
||||
"state <> 'ready' OR updated_at-created_at > interval '5 seconds') AS bad "
|
||||
"FROM attachments WHERE created_at <= clock_timestamp()-interval '5 seconds' "
|
||||
"AND created_at > clock_timestamp()-(%s+5)*interval '1 second'", (seconds,)).fetchone()
|
||||
windows[window] = result
|
||||
values = {'messenger_queue_depth': row['depth'], 'messenger_queue_oldest_seconds': row['age'],
|
||||
'messenger_diagnostics_db_up': 1}
|
||||
data = ''.join(f'{key} {value}\n' for key, value in values.items()).encode()
|
||||
for window, result in windows.items():
|
||||
for kind in ('bad','total'):
|
||||
data += f'messenger_ready_window_{kind}{{window="{window}"}} {result[kind]}\n'.encode()
|
||||
except Exception:
|
||||
data = b'messenger_diagnostics_db_up 0\n'
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'text/plain; version=0.0.4')
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
storage.pool.open(wait=True)
|
||||
try:
|
||||
HTTPServer(('0.0.0.0',9099), Handler).serve_forever()
|
||||
finally:
|
||||
storage.pool.close()
|
||||
Reference in New Issue
Block a user