add Emitter_limit_interval

This commit is contained in:
Simon Cruanes 2026-01-12 20:34:48 -05:00
parent 3ba0523227
commit d2d5b33751
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 42 additions and 0 deletions

View file

@ -1,3 +1,12 @@
(** Add a batch in front of an emitter.
The batch accumulates signals until it's full or too old, at which points
all the accumulated signals are emitted at once. Pushing into a batch is
generally very fast (amortized), in most cases; the slow path is only when
the batch needs to be emitted.
@since NEXT_RELEASE *)
open Opentelemetry_emitter
val wrap_emitter_with_batch : 'a Batch.t -> 'a Emitter.t -> 'a Emitter.t

View file

@ -0,0 +1,8 @@
open Common_.OTEL
let add_interval_limiter il (e : _ Emitter.t) : _ Emitter.t =
let emit xs = if Interval_limiter.make_attempt il then Emitter.emit e xs in
{ e with emit }
let limit_interval ~min_interval (e : _ Emitter.t) : _ Emitter.t =
add_interval_limiter (Interval_limiter.create ~min_interval ()) e

View file

@ -0,0 +1,25 @@
(** Limit frequency at which the emitter emits.
This puts a hard floor on the interval between two consecutive successful
[emit]. Attempts to emit too early are simply discarded.
The use case for this is metrics: it's possible, for a gauge, to just drop
some entries if we've been emitting them too frequently.
{b NOTE}: it's better to do [limit_interval ~min_interval (add_batching e)]
than [add_batching (limit_interval ~min_interval e)], because in the later
case we might be dismissing a whole large batch at ine
@since NEXT_RELEASE *)
open Common_.OTEL
val add_interval_limiter : Interval_limiter.t -> 'a Emitter.t -> 'a Emitter.t
(** [add_interval_limiter il e] is a new emitter [e'] that can only emit signals
less frequently than [Interval_limiter.min_interval il].
Trying to emit too early will simply drop the signal. *)
val limit_interval : min_interval:Mtime.span -> 'a Emitter.t -> 'a Emitter.t
(** [limit_interval ~min_interval e] is
[add_interval_limiter (Interval_limiter.create ~min_interval ()) e] *)