With ActiveSupport::Notifications, how does monotonic_subscribe differ from subscribe, and which clock does a one-argument Event subscriber use?
answer
- Time objects vs Floats
- wall clocks can be stepped
- Process::CLOCK_MONOTONIC
- Event times itself monotonically
- duration in milliseconds
basics
~20 sFor five-argument subscribers, subscribe passes start and finish as wall-clock Time objects, while monotonic_subscribe passes Floats from Process::CLOCK_MONOTONIC, which never jump. A one-argument subscriber gets an Event timed on the monotonic clock either way; event.duration is milliseconds.
solid answer
~40 sThe two methods differ only for subscribers that take timing arguments. `subscribe` with a five-argument block gives `start` and `finish` as `Time` objects read from the wall clock, which an NTP correction can step forward or back, so `finish - start` can be wrong. `monotonic_subscribe` gives them as `Float` seconds from `Process::CLOCK_MONOTONIC`, which only moves forward — right for durations, meaningless as timestamps. A one-argument block or callable gets an `ActiveSupport::Notifications::Event` whichever method registered it, and the `Event` times itself with the monotonic clock in milliseconds: `event.duration` is safe, but `event.time` and `event.end` are monotonic readings, not times of day. Rails itself uses `monotonic_subscribe` for `ActiveRecord::RuntimeRegistry`, which sums query time for `db_runtime`. `subscribed(callback, pattern, monotonic: true) { ... }` is the block-scoped form.
code
ruby · 9 lines# five arguments, monotonic Floats in seconds: good for durations
ActiveSupport::Notifications.monotonic_subscribe("checkout.ticketing") do |_name, start, finish, _id, payload|
CheckoutLatency.observe((finish - start) * 1000.0)
end
# one argument: an Event, monotonic in milliseconds, plus the time of day read explicitly
ActiveSupport::Notifications.subscribe("checkout.ticketing") do |event|
Rails.logger.info("checkout at=#{Time.current.iso8601} ms=#{event.duration.round(1)}")
endgo deeper
Recall that monotonic_subscribe gives timing as numbers from a clock that never jumps, and that event.duration is in milliseconds.
Explain which subscriber shape gets which clock, why a one-argument block always gets an Event, and why event.time is not a timestamp.
Pick the cheapest correct form for hot events, as Rails does for RuntimeRegistry, and read Time.current explicitly when records need a time of day.
Standardise how services measure durations and stamp times, so latency data from in-process instrumentation is comparable across teams.
## Two kinds of clock - A **wall clock** (`Time.now`) tells the time of day. The operating system may **step** it — NTP corrections, manual changes, virtual machines resuming — so subtracting two readings can give a negative or inflated duration. - A **monotonic clock** (`Process.clock_gettime(Process::CLOCK_MONOTONIC)`) only moves forward at a steady rate. Its readings have no calendar meaning; only differences between them do. Ruby's own guidance on timing code with the monotonic clock belongs to the language; what matters here is which clock each Notifications subscriber sees. ## What each registration method hands over | Registration | Subscriber shape | `start` / `finish` | |---|---|---| | `subscribe(pattern) { \|name, start, finish, id, payload\| }` | five arguments | `Time` objects, wall clock | | `monotonic_subscribe(pattern) { \|name, start, finish, id, payload\| }` | five arguments | `Float` seconds, `CLOCK_MONOTONIC` | | either method with `{ \|event\| }` | one argument | an `Event`, timed internally on `CLOCK_MONOTONIC` in milliseconds | | `subscribed(callback, pattern, monotonic: true) { ... }` | block-scoped | as `monotonic_subscribe` while the block runs | The fanout chooses the subscriber type when you register: a callable with exactly one parameter becomes an **event-object** subscriber regardless of the `monotonic` flag; an object with `start` and `finish` methods gets those calls; otherwise `subscribe` creates a wall-clock **timed** subscriber and `monotonic_subscribe` a **monotonic timed** one. ## What the Event measures An `ActiveSupport::Notifications::Event` records, at start and at finish: - the monotonic clock in **milliseconds** — `duration` is `finish - start`; - **CPU time** for the thread — `cpu_time`, and `idle_time` as `duration - cpu_time`; - **GC time** — `gc_time`, in milliseconds; - **allocations** — `allocations`, from the VM's allocated-object counter. `event.time` and `event.end` return the monotonic readings converted to seconds, so they are not timestamps. If a subscriber needs the time of day — for a log line or a structured record — it should read `Time.current` itself. ## Choosing between them 1. **You want a duration and the payload:** a one-argument block is simplest and already monotonic. 2. **You want only a duration and care about overhead:** `monotonic_subscribe` with five arguments avoids building `Event` objects and the CPU, GC and allocation reads they do. 3. **You need wall-clock start and end times**, for example to line events up with external logs: `subscribe` with five arguments, accepting that durations computed from them can be skewed by clock steps. Rails takes route 2 for its own accounting: `ActiveRecord::RuntimeRegistry` is registered with `monotonic_subscribe("sql.active_record", ...)` and adds `finish - start` to the request's SQL runtime. ## A concrete comparison Suppose the system clock is stepped back by 2 seconds by an NTP correction while a 30 ms checkout is running: - A five-argument `subscribe` block computes `finish - start` from two `Time` values and gets about -1.97 seconds. - A five-argument `monotonic_subscribe` block computes the difference of two monotonic readings and reports about 0.03 s (30 ms). - A one-argument block reports `event.duration` of about 30 ms, because the `Event` used the monotonic clock internally. Real steps are rarer than this, but small slews and corrections are routine on long-running servers, and percentile metrics amplify them. ## Common mistakes - Treating `event.time` as the moment the query ran. - Using `subscribe` with five arguments to compute latency percentiles, then seeing negative durations after a clock step. - Assuming `monotonic_subscribe` changes what a one-argument block receives — it does not.
- What does Rails gain by registering ActiveRecord::RuntimeRegistry on sql.active_record with monotonic_subscribe and a five-argument call method?Its `call(name, start, finish, id, payload)` receives monotonic Floats, so the query time it adds to the request's SQL runtime cannot be skewed by a wall-clock step. Because it takes five arguments, no `Event` is built for it, so the per-query CPU-time, GC-time and allocation reads an `Event` performs are not paid on its behalf.
- monotonic_subscribe makes a one-argument block receive Floats instead of an Event
- event.time is the wall-clock time the instrumented block started
- subscribe's start and finish are monotonic, so they never jump
- Monotonic readings can be formatted as the time of day