Repository navigation
feat!: record scheduled-job runs and add a gauge instrument to the metrics seam - #26
Merged
Merged
Conversation
…trics seam
SchedulerEntrypoint logged each run and recorded nothing, so a job that
stopped firing, failed every time or slowed down looked exactly like a
healthy one — and every consumer who filled the gap hit the same two traps:
a last-success timestamp stamped on every run, and an in-progress gauge
leaked on the error path.
Both scheduler adapters now take enable_metrics / metrics_prefix and record
every run through the app's metrics sink with SchedulerJobMetricsRecorder:
scheduler_job_runs_total{job_id,outcome,error_kind},
scheduler_job_duration_seconds, scheduler_job_last_success_timestamp_seconds
(moves only on ok) and scheduler_job_in_progress (released on every exit
path). Outcomes are ok / error / cancelled; error_kind carries the ErrorKind
of a ServiceError or "unexpected", so a job refused by a business rule and a
lost database connection are separate series.
Half of those are gauges, which the metrics seam did not have: GaugeProtocol
joins the contracts, gauge() joins MetricsSinkProtocol, the null sink, the
Prometheus sink and the author-facing MetricsSink ABC.
BREAKING CHANGE: MetricsSink.gauge() is abstract — a third-party metrics
backend must implement it (set / inc / dec) or fail at construction.
Closes #25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #25
Problem
SchedulerEntrypointlogs a run and opens a unit scope, and records nothing — no counter, no duration, no last success. A job that stopped firing, fails every time or slowed from two seconds to twenty is indistinguishable from a healthy one, and every consumer who fills the gap hits the same two traps: a last-success timestamp stamped on every run (a permanently failing job looks fresh), and an in-progress gauge leaked on the error path ("stuck" becomes permanently true).Design
Two layers, one of them more general than the request:
Kernel: a gauge instrument. Half of the proposed metrics are gauges and
MetricsSinkProtocolonly minted counters and histograms.GaugeProtocol(set/inc/dec) joinscore.contracts,gauge()joinsMetricsSinkProtocol,NullMetricsSink(NullGauge),PrometheusMetricsSink(PrometheusGauge, cached per registry like the others) and the author-facingMetricsSinkABC — abstract, so a backend that predates gauges fails at construction with the usual abstract-method message instead of silently recording nothing. That is the one breaking change (feat!, 0.x → minor).Adapters: the recorder, in the gRPC shape.
SchedulerJobMetricsRecorderinadapters/apscheduler4/metrics.pyandadapters/apscheduler3/metrics.py— the same code twice (the adapters share nothing; the conformance test now asserts the two modules are AST-identical), frozen names, buckets up to 1800 s.SchedulerEntrypoint(..., enable_metrics=True, metrics_prefix=...)andSchedulerPluginlikewise; the recorder is minted atbindfromctx.observability.metrics(an invalid prefix fails there, before the scheduler is entered)._dispatchwraps the unit of work inrecorder.track(job_id): in-progress up on entry; on any exit — normal, error, cancellation — in-progress down and the run recorded from the exception, which is re-raised untouched.Outcome vocabulary.
outcome ∈ {ok, error, cancelled};error_kindrefineserrorwith the library's own taxonomy — theErrorKindof aServiceError(validation,not_found, ...) orunexpectedfor any other exception (""when the run did not fail). The reporter's domain/infra split becomeserror_kind!="unexpected"with bounded cardinality and no application concept in the library.classify_outcomeis public for recorders that want the same vocabulary.Rejected: a generic "measure this entrypoint's unit of work" seam (a daemon's unit of work is its whole run; without an identity label the metric says nothing; per-adapter recorders are the pattern the library chose), documenting the pattern instead of shipping it, a counter-only approximation of last success, and a pluggable outcome classifier on the entrypoint (the existing error taxonomy covers the stated need; composing your own recorder from the instruments stays the escape hatch).
Docs:
adapters/scheduler.mdgains a Metrics section (names, semantics, the staleness and error-rate PromQL, the restart caveat),observability-backends.mddocuments the gauge in the ABC/protocols and the recorder, the worker blueprint enables metrics, ARCHITECTURE/checklist updated.Verification
make check+make test: ruff, format, mypy, import-linter clean; 703 passed, 14 skipped, coverage 92.78 %.make test-apscheduler3(own venv, APScheduler 3.x): 21 passed, 1 skipped — including the four new APScheduler 3 metrics tests.tests/unit/test_scheduler_metrics.py(13, over a real Prometheus registry): the two traps — a job failing every time never moveslast_success; a raising body releasesin_progressand re-raises — plus cancellation (counted, not timed, re-raised),ServiceError→error_kind, prefix, null sink.test_scheduler.py,test_scheduler_aps3.py), the Prometheus gauge, the ABC (a counter-only backend cannot be instantiated), conformance parity formetrics.pyandSchedulerPlugin.__init__.git archivetree):test_scheduler_metrics.pyand the gauge tests fail at collection (SchedulerJobMetricsRecorder/PrometheusGaugedo not exist); 7 of the 8 entrypoint metrics tests fail withTypeError: unexpected keyword argument 'enable_metrics'(the eighth — metrics disabled touches nothing — is trivially true on master).zensical build --clean— no issues; the recorder renders on the reference page.