wip: opentelemetry.emitter with same time

a bit like a buffered writer for any data
This commit is contained in:
Simon Cruanes 2025-12-04 01:06:52 -05:00
parent 959cf724fd
commit 08be80b74b
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 35 additions and 0 deletions

5
src/emitter/dune Normal file
View file

@ -0,0 +1,5 @@
(library
(name opentelemetry_emitter)
(public_name opentelemetry.emitter)
(libraries mtime mtime.clock.os)
(synopsis "Modular emitters for a single signal at a time"))

30
src/emitter/emitter.ml Normal file
View file

@ -0,0 +1,30 @@
(** Emitters *)
exception Closed
type 'a t = {
emit: 'a list -> unit;
(** Emit signals. @raise Closed if the emitter is closed. *)
tick: now:Mtime.t -> unit;
(** Call regularly to ensure background work is done *)
closed: unit -> bool; (** True if the emitter was closed *)
flush_and_close: unit -> unit;
(** Flush internal buffered signals, then close *)
}
(** An emitter for values of type ['a]. *)
let[@inline] emit (self : _ t) l : unit = if l <> [] then self.emit l
let[@inline] tick (self : _ t) ~now : unit = self.tick ~now
let[@inline] closed self : bool = self.closed ()
let[@inline] flush_and_close (self : _ t) : unit = self.flush_and_close ()
let map (f : 'a -> 'b) (self : 'b t) : 'a t =
{ self with emit = (fun l -> self.emit (List.map f l)) }
(* TODO: batching, either regular or sharded to reduce contention *)
(* TODO: sampling *)
(* TODO: use in Opentelemetry, and also for Tracer, Logger, etc. *)