update exporters and emitter combinators in client

This commit is contained in:
Simon Cruanes 2025-12-04 10:57:02 -05:00
parent 1ee298a1a3
commit 9dd15d109a
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 32 additions and 82 deletions

View file

@ -91,6 +91,7 @@ let[@inline] push' self elems = ignore (push self elems : [ `Dropped | `Ok ])
open Opentelemetry_emitter open Opentelemetry_emitter
let wrap_emitter (self : _ t) (e : _ Emitter.t) : _ Emitter.t = let wrap_emitter (self : _ t) (e : _ Emitter.t) : _ Emitter.t =
let enabled () = e.enabled () in
let closed () = e.closed () in let closed () = e.closed () in
let flush_and_close () = let flush_and_close () =
(* FIXME: we need to close the batch first, to prevent (* FIXME: we need to close the batch first, to prevent
@ -118,7 +119,7 @@ let wrap_emitter (self : _ t) (e : _ Emitter.t) : _ Emitter.t =
in in
let emit l = let emit l =
if l <> [] then ( if l <> [] && e.enabled () then (
push' self l; push' self l;
(* TODO: it'd be nice if we checked only for size here, not (* TODO: it'd be nice if we checked only for size here, not
@ -129,4 +130,4 @@ let wrap_emitter (self : _ t) (e : _ Emitter.t) : _ Emitter.t =
) )
in in
{ Emitter.closed; flush_and_close; tick; emit } { Emitter.closed; enabled; flush_and_close; tick; emit }

View file

@ -1,36 +1,31 @@
open Common_ open Common_
open Opentelemetry_emitter
(** [debug exporter] behaves like [exporter], but will print signals on [stderr] (** [debug exporter] behaves like [exporter], but will print signals on [stderr]
before passing them to [exporter] *) before passing them to [exporter] *)
class debug ?(out = Format.err_formatter) (exp : #OTEL.Exporter.t) : let debug ?(out = Format.err_formatter) (exp : OTEL.Exporter.t) :
OTEL.Exporter.t = OTEL.Exporter.t =
let open Proto in let open Proto in
object {
method send_trace l = emit_spans =
Format.fprintf out "SPANS: %a@." (Format.pp_print_list Trace.pp_span) l; Emitter.tap
exp#send_trace l (fun sp -> Format.fprintf out "SPAN: %a@." Trace.pp_span sp)
exp.emit_spans;
method send_metrics l = emit_logs =
Format.fprintf out "METRICS: %a@." Emitter.tap
(Format.pp_print_list Metrics.pp_metric) (fun log -> Format.fprintf out "LOG: %a@." Proto.Logs.pp_log_record log)
l; exp.emit_logs;
exp#send_metrics l emit_metrics =
Emitter.tap
method send_logs l = (fun m -> Format.fprintf out "METRIC: %a@." Metrics.pp_metric m)
Format.fprintf out "LOGS: %a@." exp.emit_metrics;
(Format.pp_print_list Logs.pp_log_record) on_tick = exp.on_tick;
l; cleanup =
exp#send_logs l (fun ~on_done () ->
Format.fprintf out "CLEANUP@.";
method tick () = exp#tick () exp.cleanup ~on_done ());
}
method add_on_tick_callback cb = exp#add_on_tick_callback cb
method cleanup ~on_done () =
Format.fprintf out "CLEANUP@.";
exp#cleanup ~on_done ()
end
(** Exporter that simply debugs on [stderr] *) (** Exporter that simply debugs on [stderr] *)
let debug_only : OTEL.Exporter.t = let debug_only : OTEL.Exporter.t =
new debug ~out:Format.err_formatter OTEL.Exporter.dummy debug ~out:Format.err_formatter @@ OTEL.Exporter.dummy ()

View file

@ -34,13 +34,16 @@ let accept (self : t) : bool =
open Opentelemetry_emitter open Opentelemetry_emitter
let wrap_emitter (self : t) (e : _ Emitter.t) : _ Emitter.t = let wrap_emitter (self : t) (e : _ Emitter.t) : _ Emitter.t =
let enabled () = e.enabled () in
let closed () = Emitter.closed e in let closed () = Emitter.closed e in
let flush_and_close () = Emitter.flush_and_close e in let flush_and_close () = Emitter.flush_and_close e in
let tick ~now = Emitter.tick e ~now in let tick ~now = Emitter.tick e ~now in
let emit l = let emit l =
let accepted = List.filter (fun _x -> accept self) l in if l <> [] && e.enabled () then (
if accepted <> [] then Emitter.emit e accepted let accepted = List.filter (fun _x -> accept self) l in
if accepted <> [] then Emitter.emit e accepted
)
in in
{ Emitter.closed; flush_and_close; tick; emit } { Emitter.closed; enabled; flush_and_close; tick; emit }

View file

@ -1,49 +0,0 @@
(** A simple exporter that prints on stdout *)
open Common_
open Opentelemetry_util
open struct
let pp_span out (sp : OTEL.Span.t) =
let open OTEL in
Format.fprintf out
"@[<2>SPAN@ trace_id: %a@ span_id: %a@ name: %S@ start: %a@ end: %a@]@."
Trace_id.pp
(Trace_id.of_bytes sp.trace_id)
Span_id.pp
(Span_id.of_bytes sp.span_id)
sp.name Timestamp_ns.pp_debug sp.start_time_unix_nano
Timestamp_ns.pp_debug sp.end_time_unix_nano
let pp_vlist mutex pp out l =
if l != [] then (
let@ () = Util_mutex.protect mutex in
Format.fprintf out "@[<v>";
List.iteri
(fun i x ->
if i > 0 then Format.fprintf out "@,";
pp out x)
l;
Format.fprintf out "@]@."
)
end
class stdout : OTEL.Exporter.t =
let open Opentelemetry_util in
let out = Format.std_formatter in
let mutex = Mutex.create () in
let tick_cbs = Cb_set.create () in
object
method send_trace l = pp_vlist mutex pp_span out l
method send_metrics l = pp_vlist mutex Proto.Metrics.pp_metric out l
method send_logs l = pp_vlist mutex Proto.Logs.pp_log_record out l
method tick () = Cb_set.trigger tick_cbs
method add_on_tick_callback cb = Cb_set.register tick_cbs cb
method cleanup ~on_done () = on_done ()
end