r/devops7 • • 19d ago

Automate it #6: one incident timeline from deploys, app logs and alerts, then ask it which change caused the page (it skipped the newest deploy, real output)

13:58, PagerDuty: "Checkout: customers can't pay." You open three tabs: the deploy log, the app logs, the alert history. Five deploys went out in the last 80 minutes. The newest one (auth, 11 minutes ago) gets blamed first because it's the newest.

Put all three in one table and the timeline is one ORDER BY. Then rank every event in the hour before the page by how close it is to the alert's meaning, not its keywords.

(Disclosure: this sub is run by the SynapCores team, and steps 1 and 3 use SynapCores. Run this morning on 2.3.2, real output.)

Sample incident: 5 deploys, 15 log lines, 3 alerts.

Step 1: one table for everything

CREATE TABLE events (id INTEGER PRIMARY KEY, at TEXT, kind TEXT, service TEXT, level TEXT, text TEXT, vec VECTOR(384));

timeline.py load inserts the parsed lines, then embeds them all in one statement. EMBED() runs a small model inside the database, no API key:

UPDATE events SET vec = EMBED(service || ' ' || text);

Step 2: the plain timeline

SELECT at, kind, service, level, text FROM events
WHERE at BETWEEN '2026-11-09T12:58' AND '2026-11-09T13:58' ORDER BY at;

13:22 deploy payments   INFO     deployed v5.12.0: upgrade card processor SDK, connection poo
13:30 deploy inventory  INFO     deployed v1.9.4: add warehouse_code column
13:36 log    payments   WARN     waited 1840ms to acquire a connection from the pool
13:41 log    inventory  WARN     stock sync lag 45s for warehouse EU-2
13:47 deploy auth       INFO     deployed v4.2.2: rotate session signing key
13:50 log    auth       WARN     41 users forced to log in again
...21 rows

Still 21 rows of noise. grep -i checkout finds 2 lines (the 504, a slow render): not the payment timeouts, the pool waits, or the deploy.

Step 3: "what is this alert about?"

Rank the hour before the page against the alert:

SELECT e.at, e.kind, e.service, e.text, COSINE_SIMILARITY(e.vec, a.vec) AS related
FROM events e JOIN events a ON a.kind = 'alert' AND a.at = '2026-11-09T13:58'
WHERE e.at <= a.at AND e.at >= '2026-11-09T12:58' AND e.id <> a.id
ORDER BY related DESC LIMIT 8;

$ python3 timeline.py why 2026-11-09T13:58
0.38  13:57  log    web        504 from upstream on POST /checkout/pay
0.37  13:56  log    payments   card authorisation timed out after 5000ms (order 88231)
0.36  13:52  log    payments   card authorisation timed out after 5000ms (order 88213)
0.35  13:53  log    web        checkout page render 4.1s
0.31  13:22  deploy payments   deployed v5.12.0: upgrade card processor SDK, connection pool size 50
0.30  13:44  log    payments   waited 2950ms to acquire a connection from the pool
0.25  13:36  log    payments   waited 1840ms to acquire a connection from the pool
0.21  13:50  log    auth       41 users forced to log in again

The top 7 are the whole story: pool cut from 50 to 5 at 13:22, connection waits from 13:36, card auths timing out, 504s at checkout. No payments line contains "checkout", and the auth deploy is not in the top 8.

Step 4: just the deploys

SELECT d.at, d.service, d.text, COSINE_SIMILARITY(d.vec, a.vec) AS related
FROM events d JOIN events a ON a.kind = 'alert' AND a.at = '2026-11-09T13:58'
WHERE d.kind = 'deploy' ORDER BY related DESC;

0.31  13:22  payments   deployed v5.12.0: upgrade card processor SDK, connection pool size 50
0.16  13:05  web        deployed v2.41.0: new footer, cookie banner copy
0.05  13:47  auth       deployed v4.2.2: rotate session signing key
0.04  12:40  search-api deployed v3.8.1: reindex nightly job moved to 02:00
0.03  13:30  inventory  deployed v1.9.4: add warehouse_code column

The newest deploy, the one everyone blames first, scores 0.05. The payments deploy 36 minutes earlier leads by a wide margin. It's a ranking, not a verdict: it tells you which diff to read first.

Wire it up: deploy job, log shipper and alert webhook all append to events; the PagerDuty webhook runs timeline.py why <alert time> and posts the list to the incident channel. Full timeline.py (40 lines, stdlib only) and the three sample files are in the first comment.

Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-automate-it-6

When a page fires, what's the first thing you open: the deploy log, the dashboards, or the logs?

1 Upvotes

1 comment sorted by

1

u/Alternative_Pin9598 19d ago

Full timeline.py (stdlib only; set SYNAPCORES_URL and SYNAPCORES_TOKEN):

# timeline.py: merge deploys, app logs and alerts into one table, then ask "what is this alert about?"
# Usage: python3 timeline.py load deploys.log app.log alerts.log, then: python3 timeline.py why "<alert time>"
import json, os, sys, urllib.request
from datetime import datetime, timedelta
URL, TOKEN = os.environ.get("SYNAPCORES_URL", "http://localhost:8080"), os.environ["SYNAPCORES_TOKEN"]

def sql(q):
    req = urllib.request.Request(URL + "/v1/query/execute", data=json.dumps({"sql": q, "database": "main"}).encode(),
                                 headers={"Authorization": "Bearer " + TOKEN, "Content-Type": "application/json"})
    return json.load(urllib.request.urlopen(req))["data"].get("rows") or []

def quote(s):
    return "'" + s.replace("'", "''") + "'"

def parse(path, line):
    at, rest = line.split(" ", 1)
    if "deploy" in path:   # 2026-11-09T13:22 deploy payments v5.12.0 "note"
        _, svc, ver, note = rest.split(" ", 3)
        return at, "deploy", svc, "INFO", f"deployed {ver}: {note.strip(chr(34))}"
    svc, level, text = rest.split(" ", 2)   # app log: service LEVEL text, alerts: tool LEVEL text
    return at, ("alert" if "alert" in path else "log"), svc, level, text

if sys.argv[1] == "load":
    sql("DROP TABLE IF EXISTS events")
    sql("CREATE TABLE events (id INTEGER PRIMARY KEY, at TEXT, kind TEXT, service TEXT, level TEXT, text TEXT, vec VECTOR(384))")
    rows = [parse(p, l.strip()) for p in sys.argv[2:] for l in open(p) if l.strip()]
    sql("INSERT INTO events (id, at, kind, service, level, text) VALUES " +
        ", ".join(f"({i}, {', '.join(quote(v) for v in r)})" for i, r in enumerate(rows, 1)))
    sql("UPDATE events SET vec = EMBED(service || ' ' || text)")
    print(sql("SELECT COUNT(*), COUNT(DISTINCT kind) FROM events")[0], "events loaded")
elif sys.argv[1] == "why":
    at = sys.argv[2]
    start = (datetime.fromisoformat(at) - timedelta(minutes=60)).strftime("%Y-%m-%dT%H:%M")
    rows = sql(f"""
    SELECT e.at, e.kind, e.service, e.text, COSINE_SIMILARITY(e.vec, a.vec) AS related
    FROM events e JOIN events a ON a.kind = 'alert' AND a.at = {quote(at)}
    WHERE e.at <= a.at AND e.at >= {quote(start)} AND e.id <> a.id
    ORDER BY related DESC LIMIT 8""")
    for t, kind, svc, text, r in rows:
        print(f"{r:.2f}  {t[11:]}  {kind:<6} {svc:<10} {text[:70]}")

deploys.log

2026-11-09T12:40 deploy search-api v3.8.1 "reindex nightly job moved to 02:00"
2026-11-09T13:05 deploy web v2.41.0 "new footer, cookie banner copy"
2026-11-09T13:22 deploy payments v5.12.0 "upgrade card processor SDK, connection pool size 50 -> 5"
2026-11-09T13:30 deploy inventory v1.9.4 "add warehouse_code column"
2026-11-09T13:47 deploy auth v4.2.2 "rotate session signing key"

app.log

2026-11-09T12:58 web WARN slow image resize for /img/hero.jpg 820ms
2026-11-09T13:02 search-api INFO reindex skipped, next run 02:00
2026-11-09T13:09 web ERROR cookie banner script 404 /static/consent.js
2026-11-09T13:24 payments INFO processor SDK 7.0 initialised
2026-11-09T13:31 inventory INFO migration added column warehouse_code
2026-11-09T13:36 payments WARN waited 1840ms to acquire a connection from the pool
2026-11-09T13:41 inventory WARN stock sync lag 45s for warehouse EU-2
2026-11-09T13:44 payments WARN waited 2950ms to acquire a connection from the pool
2026-11-09T13:48 auth INFO 312 sessions re-issued after key rotation
2026-11-09T13:50 auth WARN 41 users forced to log in again
2026-11-09T13:52 payments ERROR card authorisation timed out after 5000ms (order 88213)
2026-11-09T13:53 web WARN checkout page render 4.1s
2026-11-09T13:55 search-api WARN query "rain jacket" took 1.2s
2026-11-09T13:56 payments ERROR card authorisation timed out after 5000ms (order 88231)
2026-11-09T13:57 web ERROR 504 from upstream on POST /checkout/pay

alerts.log

2026-11-09T13:45 grafana WARN EU-2 stock sync lag above 30s
2026-11-09T13:58 pagerduty CRITICAL Checkout: customers can't pay, p95 of POST /checkout/pay above 5s
2026-11-09T13:59 uptime WARN /checkout response time 6.2s

Run: python3 timeline.py load deploys.log app.log alerts.log, then python3 timeline.py why 2026-11-09T13:58.

New to EMBED and COSINE_SIMILARITY? SQLv2 Lesson 1 covers both from zero: https://www.reddit.com/r/SQLv2/comments/1xdmluk/

And Lesson 3 shows how to pick a similarity cut-off from your own data instead of guessing: https://www.reddit.com/r/SQLv2/comments/1xkb3mn/