Watching your workflows

Observations

From docs/observations.md at commit 548821a.

Spinto 0.1.0. These docs follow the main branch. Spinto isn't on crates.io yet, so you install it from a clone.

Introduction

The workflow runtime keeps one row per journal entry and overwrites it on every try. It forgets earlier tries’ errors and times, it doesn’t count a try that a killed worker cut short, and it keeps no history of queued jobs or list of workers. Spinto records those things itself, from the moment it is installed, through the runtime’s public hooks:

  • JobProcessed and JobFailed listeners record each job attempt a worker settles.
  • A tracing layer, added with app.logging().layer(..), reads the runtime’s job, workflow and step spans and its “will be tried again” warning, and records each advance and each step try.
  • RunCompleted, RunFailed and RunCancelled listeners record how each run ended.
  • A tick in each queue:work process records that the worker is alive.

The provider registers all of it. Spinto never replaces the application’s trace exporter, never calls tracer_provider, and never reads the runtime’s private state.

What Spinto records

Two tables hold everything. Rows are only inserted, never updated, except a worker’s last_seen_at.

spinto_observations, keyed by (kind, subject, part, attempt, at):

kind subject part attempt Other columns
job-attempt connection:job_id empty the job attempt name, queue, outcome as the queue reports it, duration_ms
job-failed connection:job_id empty the job attempt name, queue, error_line
advance the run ID empty 0 job, job_attempt, name (the workflow), outcome, duration_ms
step-started the run ID step:name the try job, job_attempt, name
step-try the run ID step:name the try job, job_attempt, name, outcome (succeeded, retrying or failed), duration_ms, error_line
run-ended the run ID empty 0 name, outcome (completed, failed or cancelled), error_line

Every row may carry a trace_id. Every error_line is the error’s first line after the same redaction the pages apply, so no credential reaches the table.

spinto_worker_sightings, keyed by (host, pid, started_at), holds each worker process’s connection, queues and last_seen_at.

Installing the tables

Spinto ships its migrations and registers them from its provider, so the application’s own migrate creates both tables:

cargo run -- migrate

Until the tables exist, the observer drops what it records and logs a warning at most once a minute. Jobs and workflows run as usual, and run pages show an observations evidence check that says the table couldn’t be read.

Configuration

Variable Default What it does
SPINTO_RETENTION_DAYS 7 How many days spinto:prune keeps observations and worker check-ins.
SPINTO_TRACE_URL unset A trace viewer’s page for one trace, with {trace_id} where the ID goes, such as http://localhost:16686/trace/{trace_id}.

The flush interval (2 seconds), the buffer size (10,000 observations) and the check-in interval (30 seconds) are fixed.

Recording never slows a job

The listeners and the layer do no I/O. Each observation goes into a bounded buffer in the process’s memory, under a lock held for one push. When the buffer is full, the observation is dropped and counted.

A tick named spinto:observations runs every 2 seconds in the server, in every queue:work process and in every command that runs ticks. It writes the buffer with insert or ignore, 500 rows at a time, so writing the same observation twice keeps one row. When a write fails, for example because the table is missing, that batch is dropped and counted. Drops are logged at warn, at most once a minute, with the count and the last error. The tick also runs when the process shuts down. A one-off command such as workflow:cancel runs no ticks, so its listeners write their observation right away.

A worker killed with SIGKILL loses at most the last two seconds of observations. A step try that started within those two seconds then leaves no step-started row, and its run shows the gap rather than a try it can’t prove.

Queue workers

Each queue:work process checks in when it starts and every 30 seconds after, under the tick name spinto:check-in. A worker counts as not seen once its last check-in is more than 60 seconds old: it stopped, was killed, or stalled. The workers list is Spinto’s own observation; the runtime keeps no list of workers.

Retention

spinto:prune deletes observations whose at and check-ins whose last_seen_at are older than SPINTO_RETENTION_DAYS. Spinto schedules it hourly, so it runs wherever the application runs schedule:work or schedule:run. Run it by hand with:

cargo run -- spinto:prune

Throughput windows start no earlier than retention does, and say so in their aggregation.

How runs use observations

The reader merges what Spinto observed into every run it returns, to the pages and to MCP alike:

  • Tries. A step’s earlier tries take their times and redacted errors from step-try. A try backed by an observation has source: observation and startedAt, the time of its step-started, which the store doesn’t keep; the last try keeps the store’s outcome and error. A counted try Spinto never saw stays unrecorded and says why.
  • Tries a worker cut short. A step-started with no step-try in the same job attempt is interrupted once that job attempt ended (the queue settled it, the same job ran again, or something else advanced the run) or once the run’s lease lapsed. It goes before the counted try that repeated it, with the same number, because the runtime doesn’t count it. Before then it is running, and an unfinished run’s frontier names the step. Its timeline event keeps when that job attempt was first seen to have ended, as earlier.until, so a replay shows the try as started, not cut short, until then.
  • Timeline. Observed tries, advances and earlier run endings join the timeline with source: observation. A step with counted tries Spinto never saw gets a gap event instead of tries joined across the hole.
  • Rerun entries. workflow:retry deletes a failed entry, and its rerun counts from 1 again. Tries up to the one that failed for good before the new entry began belong to the deleted entry, so they stay in the timeline and out of the new entry’s tries.
  • Evidence. An observations check passes with what was merged, is notRun when Spinto observed nothing of the run, and is blocked when the table can’t be read.

Activity also uses the clock and the waits table: a waiting run whose wake time passed is overdue, a waiting run whose value a resume delivered (workflow_waits.resumed_at is set) is awaitingWorker, and a running run whose leased_until passed is leaseLapsed.

Reading observations

spinto::workflows::Reader reads them, and src/contracts/observations.rs defines the records:

Method Record What it holds
job_attempts(&JobFilters) Paginated<JobAttempt> Settled job attempts, newest first, with the runs each advanced.
job_throughput(range) JobThroughput Attempts in a window by name and outcome, attempts per minute, and duration percentiles.
job(job) JobHistory One job’s attempts in order, including numbers Spinto never saw, its final error, and its run links.
workers() Workers Every worker’s check-in and whether it is still seen.
attempt_tree(run_id) AttemptTree A run’s job attempts, the advance in each, and the step tries in each advance.

Job names, workers’ hosts and queues, and error lines go through the payload policy’s value patterns before they’re shown, so a credential in one reads [redacted]. A name filter that the patterns would change matches nothing, so it can’t confirm a guess.

The MCP server offers the same records through list_job_history, inspect_job, list_queue_workers and inspect_run_attempts. inspect_workflow_run and inspect_workflow_step include observed tries because the reader merges them.

Traces

When the application exports traces, each observation keeps the trace ID of the span it came from, read from the current OpenTelemetry context. With SPINTO_TRACE_URL set, job attempts and the attempt tree link to the trace viewer. Without a trace ID, or without the URL, the record says tracing isn’t configured rather than showing an empty link.

What the observer can’t see

  • Anything before Spinto was installed, or older than the retention window.
  • Processes that don’t register Spinto’s provider, and jobs on the sync connection.
  • A job payload a worker couldn’t decode, which fires no job event.
  • The error of a try that failed for good, beyond the one the entry keeps: the runtime logs a warning only for tries it will retry.
  • When the runtime scheduled each retry. Spinto records when each try ended.
  • Which process ran a step try: observations name the job attempt, not the worker.

Testing

tests/observations_derive.rs covers the rules, and tests/observations_sqlite.rs the tables, the prune command and the missing-table path. tests/observations_postgres.rs kills a real worker with SIGKILL in the middle of a step, starts another, and checks the reader and the MCP tools against the result. It needs PostgreSQL and the example binary:

cargo build
SPINTO_OBSERVATIONS_DATABASE_URL=postgres://127.0.0.1/spinto_observations \
    cargo test --test observations_postgres -- --ignored --nocapture

With SPINTO_WRITE_FIXTURES=1 it rewrites tests/fixtures/observations.*.json. The fixtures replace the machine’s hostname with worker-host.