forked from mtusi2026/mtusi2026lab7
fix(tracing): preserve upload causality during watchdog republication
This commit is contained in:
File diff suppressed because one or more lines are too long
+7
-1
@@ -108,7 +108,13 @@ def event(conn, job_id, name, detail=None):
|
||||
def enqueue(conn, job_id):
|
||||
row = conn.execute("SELECT * FROM jobs WHERE id=%s", (job_id,)).fetchone()
|
||||
context = telemetry.carrier()
|
||||
if not context and row.get("traceparent"):
|
||||
retained = row.get("traceparent", "")
|
||||
# A watchdog DB span may start an unrelated trace. Keep the upload's
|
||||
# causal trace; an actual retry in that same trace may advance its parent.
|
||||
if retained and (
|
||||
context.get("traceparent", "").split("-")[1:2]
|
||||
!= retained.split("-")[1:2]
|
||||
):
|
||||
context = {"traceparent": row["traceparent"]}
|
||||
if row.get("tracestate"):
|
||||
context["tracestate"] = row["tracestate"]
|
||||
|
||||
@@ -145,3 +145,46 @@ def test_large_tracestate_cannot_break_durable_job_contract(signals):
|
||||
assert validate_job(message) == message
|
||||
assert retained["traceparent"].split("-")[1] == trace_id
|
||||
assert "tracestate" not in retained
|
||||
|
||||
|
||||
def test_watchdog_republication_keeps_original_upload_trace(signals):
|
||||
from datetime import UTC, datetime
|
||||
from uuid import uuid4
|
||||
|
||||
from src.pipeline import enqueue
|
||||
|
||||
exporter, _ = signals
|
||||
with telemetry.span("upload"):
|
||||
original = telemetry.carrier()
|
||||
row = {
|
||||
"id": uuid4(),
|
||||
"attachment_id": uuid4(),
|
||||
"pipeline_version": 1,
|
||||
"attempt": 0,
|
||||
"next_at": datetime.now(UTC),
|
||||
**original,
|
||||
}
|
||||
notifications = []
|
||||
|
||||
class Connection:
|
||||
def execute(self, statement, parameters):
|
||||
if statement.startswith("INSERT INTO outbox"):
|
||||
notifications.append(parameters[1].obj)
|
||||
return SimpleNamespace(fetchone=lambda: row)
|
||||
|
||||
with telemetry.span("watchdog.database"):
|
||||
enqueue(Connection(), row["id"])
|
||||
with telemetry.span("republished", parent=notifications[0]):
|
||||
pass
|
||||
spans = {s.name: s for s in exporter.get_finished_spans()}
|
||||
assert (
|
||||
spans["republished"].context.trace_id
|
||||
== spans["upload"].context.trace_id
|
||||
)
|
||||
assert (
|
||||
spans["republished"].context.trace_id
|
||||
!= spans["watchdog.database"].context.trace_id
|
||||
)
|
||||
assert (
|
||||
spans["republished"].parent.span_id == spans["upload"].context.span_id
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user