31 lines
2.0 KiB
Python
31 lines
2.0 KiB
Python
"""DB-authoritative backlog and deadline SLI; pending work counts as bad after 5s."""
|
|
import signal
|
|
import threading
|
|
from app import store
|
|
from telemetry import meter,Observation,event
|
|
values={'depth':0,'oldest':0,'good':0,'bad':0,'db_up':0,'initialized':False}
|
|
stop=threading.Event()
|
|
for name,key,unit in [('messenger_queue_depth','depth',''),('messenger_queue_oldest','oldest','s'),
|
|
('messenger_ready_sli_good','good',''),('messenger_ready_sli_bad','bad',''),('messenger_db_up','db_up','')]:
|
|
instrument=meter.create_observable_counter if key in ('good','bad') else meter.create_observable_gauge
|
|
instrument(name,unit=unit,callbacks=[lambda _,key=key:[] if key in ('good','bad') and not values['initialized'] else [Observation(values[key],{'queue':'attachments.v1'})]])
|
|
|
|
if __name__=='__main__':
|
|
store.pool.open(wait=True,timeout=30)
|
|
for sig in (signal.SIGTERM,signal.SIGINT):signal.signal(sig,lambda *_:stop.set())
|
|
try:
|
|
while not stop.is_set():
|
|
try:
|
|
with store.connect() as conn:
|
|
depth,oldest=conn.execute("SELECT count(*),coalesce(max(extract(epoch FROM now()-created_at)),0) FROM jobs WHERE state IN ('queued','processing')").fetchone()
|
|
conn.execute("""INSERT INTO job_sli(job_id,outcome) SELECT id,
|
|
CASE WHEN state='ready' AND updated_at-created_at<=interval '5 seconds' THEN 'good' ELSE 'bad' END
|
|
FROM jobs WHERE state IN ('ready','failed','superseded') OR created_at<now()-interval '5 seconds'
|
|
ON CONFLICT DO NOTHING""")
|
|
good,bad=conn.execute("SELECT count(*) FILTER(WHERE outcome='good'),count(*) FILTER(WHERE outcome='bad') FROM job_sli").fetchone()
|
|
values.update(depth=depth,oldest=float(oldest),good=good,bad=bad,db_up=1,initialized=True)
|
|
except Exception:
|
|
values['db_up']=0;event('observer.db_unavailable')
|
|
stop.wait(2)
|
|
finally:store.pool.close()
|