mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-08 03:47:59 -04:00
update exporters and emitter combinators in client
This commit is contained in:
parent
1ee298a1a3
commit
9dd15d109a
4 changed files with 32 additions and 82 deletions
|
|
@ -91,6 +91,7 @@ let[@inline] push' self elems = ignore (push self elems : [ `Dropped | `Ok ])
|
|||
open Opentelemetry_emitter
|
||||
|
||||
let wrap_emitter (self : _ t) (e : _ Emitter.t) : _ Emitter.t =
|
||||
let enabled () = e.enabled () in
|
||||
let closed () = e.closed () in
|
||||
let flush_and_close () =
|
||||
(* 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
|
||||
|
||||
let emit l =
|
||||
if l <> [] then (
|
||||
if l <> [] && e.enabled () then (
|
||||
push' self l;
|
||||
|
||||
(* 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
|
||||
|
||||
{ Emitter.closed; flush_and_close; tick; emit }
|
||||
{ Emitter.closed; enabled; flush_and_close; tick; emit }
|
||||
|
|
|
|||
|
|
@ -1,36 +1,31 @@
|
|||
open Common_
|
||||
open Opentelemetry_emitter
|
||||
|
||||
(** [debug exporter] behaves like [exporter], but will print signals on [stderr]
|
||||
before passing them to [exporter] *)
|
||||
class debug ?(out = Format.err_formatter) (exp : #OTEL.Exporter.t) :
|
||||
OTEL.Exporter.t =
|
||||
let debug ?(out = Format.err_formatter) (exp : OTEL.Exporter.t) :
|
||||
OTEL.Exporter.t =
|
||||
let open Proto in
|
||||
object
|
||||
method send_trace l =
|
||||
Format.fprintf out "SPANS: %a@." (Format.pp_print_list Trace.pp_span) l;
|
||||
exp#send_trace l
|
||||
|
||||
method send_metrics l =
|
||||
Format.fprintf out "METRICS: %a@."
|
||||
(Format.pp_print_list Metrics.pp_metric)
|
||||
l;
|
||||
exp#send_metrics l
|
||||
|
||||
method send_logs l =
|
||||
Format.fprintf out "LOGS: %a@."
|
||||
(Format.pp_print_list Logs.pp_log_record)
|
||||
l;
|
||||
exp#send_logs l
|
||||
|
||||
method tick () = exp#tick ()
|
||||
|
||||
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
|
||||
{
|
||||
emit_spans =
|
||||
Emitter.tap
|
||||
(fun sp -> Format.fprintf out "SPAN: %a@." Trace.pp_span sp)
|
||||
exp.emit_spans;
|
||||
emit_logs =
|
||||
Emitter.tap
|
||||
(fun log -> Format.fprintf out "LOG: %a@." Proto.Logs.pp_log_record log)
|
||||
exp.emit_logs;
|
||||
emit_metrics =
|
||||
Emitter.tap
|
||||
(fun m -> Format.fprintf out "METRIC: %a@." Metrics.pp_metric m)
|
||||
exp.emit_metrics;
|
||||
on_tick = exp.on_tick;
|
||||
cleanup =
|
||||
(fun ~on_done () ->
|
||||
Format.fprintf out "CLEANUP@.";
|
||||
exp.cleanup ~on_done ());
|
||||
}
|
||||
|
||||
(** Exporter that simply debugs on [stderr] *)
|
||||
let debug_only : OTEL.Exporter.t =
|
||||
new debug ~out:Format.err_formatter OTEL.Exporter.dummy
|
||||
debug ~out:Format.err_formatter @@ OTEL.Exporter.dummy ()
|
||||
|
|
|
|||
|
|
@ -34,13 +34,16 @@ let accept (self : t) : bool =
|
|||
open Opentelemetry_emitter
|
||||
|
||||
let wrap_emitter (self : t) (e : _ Emitter.t) : _ Emitter.t =
|
||||
let enabled () = e.enabled () in
|
||||
let closed () = Emitter.closed e in
|
||||
let flush_and_close () = Emitter.flush_and_close e in
|
||||
let tick ~now = Emitter.tick e ~now in
|
||||
|
||||
let emit l =
|
||||
let accepted = List.filter (fun _x -> accept self) l in
|
||||
if accepted <> [] then Emitter.emit e accepted
|
||||
if l <> [] && e.enabled () then (
|
||||
let accepted = List.filter (fun _x -> accept self) l in
|
||||
if accepted <> [] then Emitter.emit e accepted
|
||||
)
|
||||
in
|
||||
|
||||
{ Emitter.closed; flush_and_close; tick; emit }
|
||||
{ Emitter.closed; enabled; flush_and_close; tick; emit }
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue