trace-tef: ?debug option to track spans

This commit is contained in:
Simon Cruanes 2026-01-17 20:54:07 -05:00
parent 254c7e0af9
commit f88cd7651c
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 35 additions and 14 deletions

View file

@ -3,4 +3,11 @@
(public_name trace-tef) (public_name trace-tef)
(synopsis (synopsis
"Simple and lightweight tracing using TEF/Catapult format, in-process") "Simple and lightweight tracing using TEF/Catapult format, in-process")
(libraries trace.core trace.util mtime mtime.clock.os unix threads)) (libraries
trace.core
trace.util
trace.debug
mtime
mtime.clock.os
unix
threads))

View file

@ -76,27 +76,38 @@ open struct
) )
end end
let setup ?(out = `Env) () = let setup ?(debug = false) ?(out = `Env) () =
register_atexit (); register_atexit ();
let setup_col c =
let c =
if debug then
Trace_debug.Track_spans.track c
else
c
in
Trace_core.setup_collector c
in
match out with match out with
| `Stderr -> Trace_core.setup_collector @@ collector ~out:`Stderr () | `Stderr -> setup_col @@ collector ~out:`Stderr ()
| `Stdout -> Trace_core.setup_collector @@ collector ~out:`Stdout () | `Stdout -> setup_col @@ collector ~out:`Stdout ()
| `File path -> Trace_core.setup_collector @@ collector ~out:(`File path) () | `File path -> setup_col @@ collector ~out:(`File path) ()
| `Env -> | `Env ->
(match Sys.getenv_opt "TRACE" with (match Sys.getenv_opt "TRACE" with
| Some ("1" | "true") -> | Some ("1" | "true") ->
let path = "trace.json" in let path = "trace.json" in
let c = collector ~out:(`File path) () in let c = collector ~out:(`File path) () in
Trace_core.setup_collector c setup_col c
| Some "stdout" -> Trace_core.setup_collector @@ collector ~out:`Stdout () | Some "stdout" -> setup_col @@ collector ~out:`Stdout ()
| Some "stderr" -> Trace_core.setup_collector @@ collector ~out:`Stderr () | Some "stderr" -> setup_col @@ collector ~out:`Stderr ()
| Some path -> | Some path ->
let c = collector ~out:(`File path) () in let c = collector ~out:(`File path) () in
Trace_core.setup_collector c setup_col c
| None -> ()) | None -> ())
let with_setup ?out () f = let with_setup ?debug ?out () f =
setup ?out (); setup ?debug ?out ();
Fun.protect ~finally:Trace_core.shutdown f Fun.protect ~finally:Trace_core.shutdown f
module Private_ = struct module Private_ = struct

View file

@ -19,7 +19,7 @@ val collector : out:[< output ] -> unit -> Trace_core.collector
(** Make a collector that writes into the given output. See {!setup} for more (** Make a collector that writes into the given output. See {!setup} for more
details. *) details. *)
val setup : ?out:[ output | `Env ] -> unit -> unit val setup : ?debug:bool -> ?out:[ output | `Env ] -> unit -> unit
(** [setup ()] installs the collector depending on [out]. (** [setup ()] installs the collector depending on [out].
@param out @param out
@ -31,9 +31,12 @@ val setup : ?out:[ output | `Env ] -> unit -> unit
- If it's set to "stdout", then logging happens on stdout (since 0.2) - If it's set to "stdout", then logging happens on stdout (since 0.2)
- If it's set to "stderr", then logging happens on stdout (since 0.2) - If it's set to "stderr", then logging happens on stdout (since 0.2)
- Otherwise, if it's set to a non empty string, the value is taken to be the - Otherwise, if it's set to a non empty string, the value is taken to be the
file path into which to write. *) file path into which to write.
val with_setup : ?out:[ output | `Env ] -> unit -> (unit -> 'a) -> 'a @param debug if true, use {!Trace_debug}. Default [false]. *)
val with_setup :
?debug:bool -> ?out:[ output | `Env ] -> unit -> (unit -> 'a) -> 'a
(** [with_setup () f] (optionally) sets a collector up, calls [f()], and makes (** [with_setup () f] (optionally) sets a collector up, calls [f()], and makes
sure to shutdown before exiting. since 0.2 a () argument was added. *) sure to shutdown before exiting. since 0.2 a () argument was added. *)