forked from mtusi2026/mtusi2026lab7
feat(security): add scoped identities, TLS boundaries and OpenBao bootstrap
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
"""Bounded DB polling; pending tasks missing the deadline count as bad SLIs."""
|
||||
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
from src import telemetry
|
||||
from src.pipeline import database
|
||||
|
||||
|
||||
def poll(db, epoch):
|
||||
with db.connection() as conn, conn.transaction():
|
||||
# Evaluate unfinished jobs at their deadline, not after completion.
|
||||
# Exclude decided IDs before LIMIT to avoid starving newer jobs.
|
||||
conn.execute(
|
||||
"INSERT INTO job_slo_observations(job_id,outcome) "
|
||||
"SELECT j.id,CASE WHEN e.committed_at<=j.created_at+interval '10 seconds' "
|
||||
"THEN 'good' ELSE 'bad' END FROM jobs j "
|
||||
"LEFT JOIN effect_publications e ON e.job_id=j.id "
|
||||
"WHERE NOT EXISTS(SELECT 1 FROM job_slo_observations o WHERE o.job_id=j.id) "
|
||||
"AND j.state<>'obsolete' AND (e.committed_at IS NOT NULL "
|
||||
"OR j.state='failed' OR j.created_at<=clock_timestamp()-interval '10 seconds') "
|
||||
"ORDER BY j.created_at LIMIT 128 ON CONFLICT DO NOTHING"
|
||||
)
|
||||
queue = conn.execute(
|
||||
"SELECT count(*) FILTER(WHERE state='queued') depth,"
|
||||
"COALESCE(max(EXTRACT(epoch FROM clock_timestamp()-created_at)) "
|
||||
"FILTER(WHERE state IN ('queued','processing')),0) oldest FROM jobs"
|
||||
).fetchone()
|
||||
decisions = conn.execute(
|
||||
"SELECT outcome,count(*) n FROM job_slo_observations GROUP BY outcome"
|
||||
).fetchall()
|
||||
completed = conn.execute(
|
||||
"SELECT event,count(*) n FROM pipeline_events "
|
||||
"WHERE event IN ('effect_committed','failed') GROUP BY event"
|
||||
).fetchall()
|
||||
ready = conn.execute(
|
||||
"SELECT o.job_id,EXTRACT(epoch FROM e.committed_at-j.created_at) seconds "
|
||||
"FROM job_slo_observations o JOIN effect_publications e ON e.job_id=o.job_id "
|
||||
"JOIN jobs j ON j.id=o.job_id WHERE o.histogram_epoch IS DISTINCT FROM %s::uuid "
|
||||
"ORDER BY e.committed_at LIMIT 128 FOR UPDATE OF o SKIP LOCKED",
|
||||
(epoch,),
|
||||
).fetchall()
|
||||
for row in ready:
|
||||
conn.execute(
|
||||
"UPDATE job_slo_observations SET histogram_epoch=%s WHERE job_id=%s",
|
||||
(epoch, row["job_id"]),
|
||||
)
|
||||
values = {
|
||||
"queue_depth": queue["depth"],
|
||||
"oldest_job_age": float(queue["oldest"]),
|
||||
"slo_good": 0,
|
||||
"slo_bad": 0,
|
||||
"completed_ready": 0,
|
||||
"completed_failed": 0,
|
||||
}
|
||||
values.update({"slo_" + row["outcome"]: row["n"] for row in decisions})
|
||||
values.update(
|
||||
{
|
||||
"completed_"
|
||||
+ (
|
||||
"ready" if row["event"] == "effect_committed" else "failed"
|
||||
): row["n"]
|
||||
for row in completed
|
||||
}
|
||||
)
|
||||
telemetry.observe(values)
|
||||
for row in ready:
|
||||
telemetry.duration(
|
||||
"time_to_ready",
|
||||
float(row["seconds"]),
|
||||
{"job_type": "attachment.process.v1", "outcome": "ready"},
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
epoch = str(uuid4())
|
||||
telemetry.configure("messenger-observer", instance=epoch)
|
||||
db = database()
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
poll(db, epoch)
|
||||
except Exception:
|
||||
telemetry.log(
|
||||
"observer.unavailable",
|
||||
error=True,
|
||||
error_code="database_unavailable",
|
||||
)
|
||||
time.sleep(2)
|
||||
finally:
|
||||
db.close()
|
||||
telemetry.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user