Typed runtime_events probes for OCaml
README.md

ocaml-probe #

Typed instrumentation over OCaml 5 runtime_events user events.

A library that reports what it does usually logs strings, and every consumer then has to parse those strings back. The OCaml 5 runtime already has a cheap ring buffer for events (Runtime_events), but its user events carry untyped payloads with no names. probe adds types on top. A library declares its events, spans and counters as values in its public API, and a consumer decodes them back into records without knowing which library produced them.

A payload is a flat map of scalar fields (string, int, int64, float, bool), so the same declaration can become trace arguments, JSON log fields or metric labels. The stream describes itself. Before a declaration's first data packet, the producer writes a descriptor with its name, documentation and field types, and it writes it again at intervals, so a consumer that attaches to a running process still learns what each packet means. Rings of different domains interleave in no fixed order, so the descriptor goes before the first packet on each domain's ring, and again after every 512 data packets of that declaration; a late consumer waits at most 512 packets before it can decode. A ring that overflows can still overwrite every descriptor of a declaration before a reader comes to them, so the name is also kept where no overwrite reaches it: each declaration registers a Runtime_events custom event named ocaml_probe.<id> <name>, which the runtime writes to a table in the ring file's header, and a reader in another process names a packet from there. A packet holds at most 1024 bytes. A string cut short to fit decodes as Truncated, which tells a consumer it is not simply a short value. Emitting does nothing while no ring or sink is active.

By default the span context is local to a domain. An Eio application calls Probe_eio.install () (library probe.eio) once to make it local to a fiber instead. Three more libraries sample the process. probe.diskio reads the kernel's per-process I/O counters, probe.energy records a cumulative energy counter on macOS, and on Linux where the RAPL counters can be read, and probe.instructions reads the instructions the process retired. RAPL (Running Average Power Limit) is a set of counters on Intel and recent AMD processors that measure the energy the package and its memory have used; Linux exposes them under /sys/class/powercap.

probe.tally counts what the process's own code does: a library declares a tally per operation (a registry read, a file open) and announces each one, and a program reads the totals before and after a piece of work, so the difference is that work's cost with no capture attached. While emission is on, each announcement is also a counter sample of the tally's total. probe.handed and probe.taken, the bytes handed to writes and taken from reads, are two such tallies.

Installation #

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install probe

Usage #

A declaration is built once, at module level, and emitted many times. The field list is checked against the record it encodes, down to the number and the order of the fields.

type fetch = { url : string; status : int }

let fetch =
  Probe.event "oci.blob.fetch"
    Probe.Fields.(
      obj (fun url status -> { url; status })
      |> field "url" string ~enc:(fun t -> t.url)
      |> field "status" int ~enc:(fun t -> t.status)
      |> seal)

let () = Probe.emit fetch { url = "https://example.invalid/layer"; status = 200 }

A consumer registers Probe.Consumer.packet_type with Runtime_events.Callbacks.add_user_event and decodes each packet with Probe.Consumer.decode. In a test, or any other run with a clear end, Probe.Consumer.collect f runs f and returns the records it emitted.

Codec events #

For a codec that reports its encode and decode operations, Probe.Event.v generates an event, a span and a byte counter for one operation type, along with helpers that use them.

type operation = Encode | Decode

let to_string = function Encode -> "encode" | Decode -> "decode"

module Event = (val Probe.Event.v "foo.codec" ~doc:"Foo codec operations" to_string)

let () = Event.emit_probe ~operation:Encode ~bytes:42 ~ok:true

Licence #

ISC. See LICENSE.md.