feat tef-tldrs: expose a subscriber

This commit is contained in:
Simon Cruanes 2024-09-09 14:44:23 -04:00
parent 59db458fec
commit 839eb3fcdf
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 30 additions and 12 deletions

View file

@ -1,6 +1,11 @@
(library
(name trace_tef_tldrs)
(public_name trace-tef.tldrs)
(synopsis "Multiprocess tracing using the `tldrs` daemon")
(libraries trace.core trace.private.util trace-tef unix threads))
(name trace_tef_tldrs)
(public_name trace-tef.tldrs)
(synopsis "Multiprocess tracing using the `tldrs` daemon")
(libraries
trace.core
trace.private.util
trace-subscriber
trace-tef
unix
threads))

View file

@ -84,7 +84,7 @@ let find_role ~out () : role =
| Some path -> Some (write_to_file path)
| None -> None))
let collector_ (client : as_client) : collector =
let subscriber_ (client : as_client) : Trace_subscriber.t =
(* connect to unix socket *)
let sock = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
(try Unix.connect sock (Unix.ADDR_UNIX client.socket)
@ -105,24 +105,31 @@ let collector_ (client : as_client) : collector =
in
fpf out "OPEN %s\n%!" client.trace_id;
Trace_tef.Internal_.collector_jsonl ~finally ~out:(`Output out) ()
Trace_tef.Private_.subscriber_jsonl ~finally ~out:(`Output out) ()
let subscriber ~out () =
let role = find_role ~out () in
match role with
| None -> assert false
| Some c -> subscriber_ c
let collector ~out () : collector =
let role = find_role ~out () in
match role with
| None -> assert false
| Some c -> collector_ c
| Some c -> subscriber_ c |> Trace_subscriber.collector
let setup ?(out = `Env) () =
let role = find_role ~out () in
match role with
| None -> ()
| Some c -> Trace_core.setup_collector @@ collector_ c
| Some c ->
Trace_core.setup_collector @@ Trace_subscriber.collector @@ subscriber_ c
let with_setup ?out () f =
setup ?out ();
Fun.protect ~finally:Trace_core.shutdown f
module Internal_ = struct
include Trace_tef.Internal_
module Private_ = struct
include Trace_tef.Private_
end

View file

@ -1,7 +1,13 @@
(** Emit traces by talking to the {{: https://github.com/imandra-ai/tldrs} tldrs} daemon *)
val collector : out:[ `File of string ] -> unit -> Trace_core.collector
(** Make a collector that writes into the given output.
See {!setup} for more details. *)
val subscriber : out:[ `File of string ] -> unit -> Trace_subscriber.t
(** Make a subscriber that writes into the given output.
@since NEXT_RELEASE *)
type output = [ `File of string ]
(** Output for tracing.
- [`File "foo"] will enable tracing and print events into file
@ -30,7 +36,7 @@ val with_setup : ?out:[ output | `Env ] -> unit -> (unit -> 'a) -> 'a
(**/**)
module Internal_ : sig
module Private_ : sig
val mock_all_ : unit -> unit
(** use fake, deterministic timestamps, TID, PID *)