Background work and metrics
Status: built 2026-10-10. Both live in framework-core (no new dependency); the lock gate in
framework-locks; the Prometheus endpoint in framework-http (handler) and framework-http-jetty
(mounting). Java names are given where the shape matters.
1. Background loops
Section titled “1. Background loops”A loop is named work that runs again and again on its own virtual thread, owned by the
application’s lifecycle. Background is a service the core module provides (like Config, every
application has one).
app.get(Background.class) .loop("invoice-sweeper", LoopOptions.every(Duration.ofSeconds(30)), tick -> sweep(tick));- Registration:
loop(name, options, task)inbootorstarted(or later). Names match[a-z][a-z0-9-]*, at most 100 characters, unique (IllegalArgumentExceptionotherwise). Registering after the application began closing isIllegalStateException. - Start: loops registered before start-up finished begin after every module’s
startedhook ran, so a loop never sees a half-started application. Registered later, a loop begins at once. A command run bylaunchnever starts loops (noboot). - Schedule: wait
initialDelay(default zero), run, then waitintervalafter the run ended (fixed delay; runs never overlap), and so on.intervalis 10 ms to 24 h. Waiting uses real time and ends early when the loop is stopped. - The task is
void run(LoopTick tick) throws Exception.LoopTickgivesloop(the name),run(1, 2, …),fencingToken(OptionalLong, present when a lock gate admitted the run) andstopping(true once shutdown began; long tasks should check it and return). - Failures: an exception from a run is logged at ERROR with the loop name and run number, and
the loop continues with its next run. An
Erroris logged and ends the loop (its health check reports it). The interrupt flag is cleared between runs. - Gate (optional):
LoopOptions.gatedBy(LoopGate). Before each run the gate decides:Ran(the task ran, with the fencing token if any),Skipped(reason)(not run this time, for example another replica holds the lock) orLost(the task ran but lost exclusion while running).framework-locksprovidesLockGate.of(LockStore, LockKey, lease)andLockGate.service(Locks, name, lease)(default instance,LockKey.service(name)): each run takes the lock withwithLock(key, lease, 0), so a held lock skips the run instead of waiting, and the lease is refreshed while the task runs (locks contract section 4). A lock store that is down is a failed run. - Stop:
App.closestops every loop first, before any module’sshutdownhook, so a loop never runs against a service that has been shut down. All loops are signalled together; waiting loops end at once; a running task gets untilapp.shutdown.drainto return, then its thread is interrupted, logged at WARN as an overrun and left behind (as for shutdown hooks). - Health: each loop adds the check
background:<name>, informational by default (LoopOptions.criticalmakes it critical;app.health.*overrides apply). It isDownwhen the lastdownAfterFailuresruns (default 3, 1 to 1000) all failed (Lostcounts as a failure,Skippeddoes not), when the loop ended on anError, or whenstuckAfteris set and the current run has lasted longer. The reason names the count and the exception class only, never its message (health is served without authentication). - Metrics (section 3):
fw_background_runs_total{loop,outcome}with outcomesuccess,failure,skipped,lost;fw_background_run_duration_seconds{loop}(gated-out runs are not timed).
2. Metrics
Section titled “2. Metrics”Metrics is a service the core module provides. Instruments are families with fixed label
names; labels(values...) returns the series to record on (cache it on hot paths).
| Instrument | Create | Record |
|---|---|---|
| counter | metrics.counter(name, help, labelNames...) |
inc, inc(amount) (amount ≥ 0) |
| gauge | metrics.gauge(name, help, labelNames...) |
set(v), inc(d), dec(d) |
| gauge (callback) | metrics.gauge(name, help, DoubleSupplier) |
read when exported |
| timer | metrics.timer(name, help, labelNames...) or with explicit bucket bounds |
record(Duration), recordNanos(long), time(Callable) |
A family with no labels records directly on itself (counter.inc).
Drivers, chosen by app.metrics.driver (memory default, or noop):
memory: in-process, lock-free adders;snapshotreturns every series.noop: every instrument does nothing;snapshotis empty. Validation still runs, so a name that is wrong fails in tests with either driver.
A real exporter (OpenTelemetry, Micrometer) is a later driver; Metrics is an interface for that.
Naming and label rules (checked when an instrument is created; IllegalArgumentException)
Section titled “Naming and label rules (checked when an instrument is created; IllegalArgumentException)”- Names:
[a-z][a-z0-9_]*, at most 200 characters. Counters end in_total. Timers end in_secondsand record seconds. Gauges end in neither. Use a prefix per component; the framework’s own metrics start withfw_, which applications must not use. - Help: not blank, at most 500 characters.
- Label names:
[a-z][a-z0-9_]*, not starting with__, notleorquantile, unique within the family, at most 10. - Creating an instrument again with the same name, type, help and label names (and buckets) returns
the same family; with anything different it is
IllegalArgumentException. - Label values are runtime input and never make a recording throw: values longer than 200
characters are cut to 200;
nullbecomes the empty string. Only a wrong number of values throws (a programming error). - Cardinality is bounded: a family holds at most
app.metrics.max-seriesseries (default 1000, 1 to 100 000). A recording that would create one more is dropped, counted infw_metrics_dropped_series_total{metric}and logged at WARN once per family. Never use ids, paths, user input or exception messages as label values. - Timer buckets: strictly increasing, positive, finite, at most 50; default
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10seconds. A negative duration records zero.
Built-in metrics
Section titled “Built-in metrics”| Name | Type | Labels | From |
|---|---|---|---|
fw_http_server_requests_total |
counter | method, route, status |
every HTTP request |
fw_http_server_request_duration_seconds |
timer | method, route |
every HTTP request |
fw_usecase_runs_total |
counter | usecase, outcome |
every UseCases.run |
fw_usecase_duration_seconds |
timer | usecase |
every UseCases.run |
fw_background_runs_total |
counter | loop, outcome |
section 1 |
fw_background_run_duration_seconds |
timer | loop |
section 1 |
fw_metrics_dropped_series_total |
counter | metric |
the cardinality bound |
route is the matched route pattern (/orders/{id}), or <unmatched> when no route matched,
so request paths never become label values. status is the numeric status. Use-case outcome is
committed or the failure’s kind in lower case (validation, authorization, not_found,
conflict, business_rule, internal).
3. The Prometheus endpoint
Section titled “3. The Prometheus endpoint”PrometheusText.write(MetricsSnapshot)(core) renders the text exposition format 0.0.4: per family# HELP(backslash and newline escaped) and# TYPE(counter,gauge,histogram), then one line per series; label values escape backslash, double quote and newline; timers as histograms (_bucketwith cumulative counts andle, including+Inf, then_sum,_count). Families sorted by name, series by label values, so the output is stable. Numbers: whole-number values without a fraction, others as Java’s shortest round-trip form,+Inf,-Inf,NaN.MetricsEndpoint.handler(metrics, bearerToken)(framework-http) answers200with content typetext/plain; version=0.0.4; charset=utf-8. With a bearer token configured, a request withoutAuthorization: Bearer <token>(compared as SHA-256 digests, constant time) is401 UNAUTHENTICATEDwith the standard envelope.JettyHttpModulemounts it whenhttp.metrics.enabledis true (default false: metrics reveal routes and volumes, so exposing them is a decision). Settings:http.metrics.path(default/metrics, literal segments),http.metrics.bearer-token(secret, optional). No context is required. Start-up warns when it is enabled without a bearer token, and when the driver isnoop.
3a. As built (2026-10-10)
Section titled “3a. As built (2026-10-10)”MetricsandBackgroundare provided by the core module (app.get(...)always works; a second provider fails start-up as usual;AppBuilder.override(Metrics.class, ...)works in tests).Healthis now a seeded framework service (it cannot be provided or overridden) soBackgroundcan add its checks.Metrics.readablesays whethersnapshotreturns what was recorded (false fornoop); the Jetty module uses it for its start-up warning. A callback gauge can be registered once per name; a second registration, or a plain gauge of the same name, isIllegalArgumentException. A callback that throws is left out of the snapshot (WARN once).Routes.compile(resolver, clock, metrics)andTestClient.of(routes, resolver, metrics)are the overloads that record request metrics; the old ones record into a no-op registry. The status label of a request that ended in an uncaught exception is500.UseCaseshas a constructor takingMetricsand aMonotonicClock;UseCaseModuleuses it. The duration covers every attempt of a run, retries and their waits included.fw_metrics_dropped_series_totalexists from start (without series) in thememorydriver.LockGate.servicethrowsIllegalStateExceptionwhen no lock instance is configured (fromLocks.defaultInstance).
4. Tests
Section titled “4. Tests”Background: start after started, fixed delay without overlap, initial delay, failure keeps the
loop, consecutive-failure health and recovery, Error ends the loop, stuck run, stop before module
hooks, stop within the drain with an interrupt for a task that ignores the stop, stopping,
registration rules, gate outcomes (ran / skipped / lost) and the lock gate end to end on the memory
driver with two “replicas”. Metrics: every naming rule, idempotent re-creation, label truncation,
the cardinality bound and its counter, each instrument, histogram buckets, the noop driver, the exact
exposition bytes and escaping, the endpoint with and without the token, built-in HTTP, use-case and
background metrics.