Files
mtusi2026lab7/migrations/0006_observability.sql

20 lines
1.4 KiB
SQL

-- Lab 6: trace context of a job and an index for the queue gauges.
--
-- jobs.traceparent / jobs.tracestate: W3C trace context of the request that created the job
-- (the upload, taken inside its HTTP server span). The relay publishes every outbox row of the
-- job (first attempt, retries, lease expiry, republish) as a PRODUCER span whose parent is this
-- context, and puts the producer's own context into the message (`traceparent` of
-- contracts/jobs.schema.json). One upload is therefore ONE trace: HTTP POST -> db enqueue ->
-- publish (relay) -> process (worker) -> S3/DB ... -> publish of the retry -> process ...
-- A job created by the operator CLI (jobctl replay/reprocess) has no context: NULL, and the relay
-- starts a new trace for it. The value is a 55-character technical id, never user data.
ALTER TABLE jobs
ADD COLUMN traceparent text COLLATE "C",
ADD COLUMN tracestate text COLLATE "C",
ADD CONSTRAINT jobs_traceparent CHECK (traceparent IS NULL OR traceparent ~ '^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$'),
ADD CONSTRAINT jobs_tracestate CHECK (tracestate IS NULL OR length(tracestate) <= 512);
-- Queue gauges of the relay (messenger.queue.depth / messenger.queue.oldest_age, every 5 s):
-- unfinished jobs by age. Partial index = only the few active rows, not the whole history.
CREATE INDEX jobs_active_age ON jobs (created_at) WHERE state IN ('queued', 'processing');