diff --git a/src/collector.ml b/src/collector.ml index a04cb5e..4d65bc5 100644 --- a/src/collector.ml +++ b/src/collector.ml @@ -10,12 +10,7 @@ open Types let dummy_span : span = Int64.min_int module type S = sig - val enabled : unit -> bool - (** Is the collector enabled? This should be extremely fast so that - the traced program can check it before creating any span or - message *) - - val create_span : + val enter_span : ?__FUNCTION__:string -> __FILE__:string -> __LINE__:int -> string -> span val exit_span : span -> unit diff --git a/src/tef/trace_tef.ml b/src/tef/trace_tef.ml index bd697ee..2bd892f 100644 --- a/src/tef/trace_tef.ml +++ b/src/tef/trace_tef.ml @@ -214,8 +214,6 @@ let collector ~out () : collector = (** generator for span ids *) let span_id_gen_ = A.make 0 - let enabled () = true - (* queue of messages to write *) let events : event B_queue.t = B_queue.create () @@ -234,7 +232,7 @@ let collector ~out () : collector = else Thread.id (Thread.self ()) - let create_span ?__FUNCTION__:_ ~__FILE__:_ ~__LINE__:_ name : span = + let enter_span ?__FUNCTION__:_ ~__FILE__:_ ~__LINE__:_ name : span = let span = Int64.of_int (A.fetch_and_add span_id_gen_ 1) in let tid = get_tid_ () in let time_us = now_us () in diff --git a/src/trace.ml b/src/trace.ml index 8110e53..fdd45a3 100644 --- a/src/trace.ml +++ b/src/trace.ml @@ -10,21 +10,21 @@ let collector : collector option A.t = A.make None let[@inline] enabled () = match A.get collector with | None -> false - | Some (module C) -> C.enabled () + | Some _ -> true -let[@inline] create_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name : span = +let[@inline] enter_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name : span = match A.get collector with | None -> Collector.dummy_span - | Some (module C) -> C.create_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name + | Some (module C) -> C.enter_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name let[@inline] exit_span span : unit = match A.get collector with | None -> () | Some (module C) -> C.exit_span span -let with_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__ ~__LINE__ - name f = - let sp = C.create_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name in +let with_span_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__ + ~__LINE__ name f = + let sp = C.enter_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name in match f sp with | x -> C.exit_span sp; @@ -34,13 +34,13 @@ let with_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__ ~__LINE__ C.exit_span sp; Printexc.raise_with_backtrace exn bt -let[@inline] with_ ?__FUNCTION__ ~__FILE__ ~__LINE__ name f = +let[@inline] with_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name f = match A.get collector with | None -> (* fast path: no collector, no span *) f Collector.dummy_span | Some collector -> - with_collector_ collector ?__FUNCTION__ ~__FILE__ ~__LINE__ name f + with_span_collector_ collector ?__FUNCTION__ ~__FILE__ ~__LINE__ name f let[@inline] message ?__FUNCTION__ ~__FILE__ ~__LINE__ msg : unit = match A.get collector with diff --git a/src/trace.mli b/src/trace.mli index e437db6..2477b02 100644 --- a/src/trace.mli +++ b/src/trace.mli @@ -6,13 +6,17 @@ module Collector = Collector (** {2 Tracing} *) val enabled : unit -> bool +(** Is there a collector? + + This is fast, so that the traced program can check it before creating + any span or message *) -val create_span : +val enter_span : ?__FUNCTION__:string -> __FILE__:string -> __LINE__:int -> string -> span val exit_span : span -> unit -val with_ : +val with_span : ?__FUNCTION__:string -> __FILE__:string -> __LINE__:int -> diff --git a/test/t1.ml b/test/t1.ml index 7080766..7bf599d 100644 --- a/test/t1.ml +++ b/test/t1.ml @@ -1,8 +1,8 @@ let run () = for _i = 1 to 50 do - Trace.with_ ~__FUNCTION__ ~__FILE__ ~__LINE__ "outer.loop" @@ fun _sp -> + Trace.with_span ~__FUNCTION__ ~__FILE__ ~__LINE__ "outer.loop" @@ fun _sp -> for _j = 2 to 5 do - Trace.with_ ~__FILE__ ~__LINE__ "inner.loop" @@ fun _sp -> + Trace.with_span ~__FILE__ ~__LINE__ "inner.loop" @@ fun _sp -> Trace.messagef ~__FILE__ ~__LINE__ (fun k -> k "hello %d %d" _i _j) done done