mirror of
https://github.com/ocaml-tracing/ocaml-trace.git
synced 2026-03-07 18:37:56 -05:00
change API
This commit is contained in:
parent
718cbf120f
commit
1fed3eba8e
5 changed files with 18 additions and 21 deletions
|
|
@ -10,12 +10,7 @@ open Types
|
||||||
let dummy_span : span = Int64.min_int
|
let dummy_span : span = Int64.min_int
|
||||||
|
|
||||||
module type S = sig
|
module type S = sig
|
||||||
val enabled : unit -> bool
|
val enter_span :
|
||||||
(** 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 :
|
|
||||||
?__FUNCTION__:string -> __FILE__:string -> __LINE__:int -> string -> span
|
?__FUNCTION__:string -> __FILE__:string -> __LINE__:int -> string -> span
|
||||||
|
|
||||||
val exit_span : span -> unit
|
val exit_span : span -> unit
|
||||||
|
|
|
||||||
|
|
@ -214,8 +214,6 @@ let collector ~out () : collector =
|
||||||
(** generator for span ids *)
|
(** generator for span ids *)
|
||||||
let span_id_gen_ = A.make 0
|
let span_id_gen_ = A.make 0
|
||||||
|
|
||||||
let enabled () = true
|
|
||||||
|
|
||||||
(* queue of messages to write *)
|
(* queue of messages to write *)
|
||||||
let events : event B_queue.t = B_queue.create ()
|
let events : event B_queue.t = B_queue.create ()
|
||||||
|
|
||||||
|
|
@ -234,7 +232,7 @@ let collector ~out () : collector =
|
||||||
else
|
else
|
||||||
Thread.id (Thread.self ())
|
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 span = Int64.of_int (A.fetch_and_add span_id_gen_ 1) in
|
||||||
let tid = get_tid_ () in
|
let tid = get_tid_ () in
|
||||||
let time_us = now_us () in
|
let time_us = now_us () in
|
||||||
|
|
|
||||||
16
src/trace.ml
16
src/trace.ml
|
|
@ -10,21 +10,21 @@ let collector : collector option A.t = A.make None
|
||||||
let[@inline] enabled () =
|
let[@inline] enabled () =
|
||||||
match A.get collector with
|
match A.get collector with
|
||||||
| None -> false
|
| 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
|
match A.get collector with
|
||||||
| None -> Collector.dummy_span
|
| 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 =
|
let[@inline] exit_span span : unit =
|
||||||
match A.get collector with
|
match A.get collector with
|
||||||
| None -> ()
|
| None -> ()
|
||||||
| Some (module C) -> C.exit_span span
|
| Some (module C) -> C.exit_span span
|
||||||
|
|
||||||
let with_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__ ~__LINE__
|
let with_span_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__
|
||||||
name f =
|
~__LINE__ name f =
|
||||||
let sp = C.create_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name in
|
let sp = C.enter_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name in
|
||||||
match f sp with
|
match f sp with
|
||||||
| x ->
|
| x ->
|
||||||
C.exit_span sp;
|
C.exit_span sp;
|
||||||
|
|
@ -34,13 +34,13 @@ let with_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__ ~__LINE__
|
||||||
C.exit_span sp;
|
C.exit_span sp;
|
||||||
Printexc.raise_with_backtrace exn bt
|
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
|
match A.get collector with
|
||||||
| None ->
|
| None ->
|
||||||
(* fast path: no collector, no span *)
|
(* fast path: no collector, no span *)
|
||||||
f Collector.dummy_span
|
f Collector.dummy_span
|
||||||
| Some collector ->
|
| 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 =
|
let[@inline] message ?__FUNCTION__ ~__FILE__ ~__LINE__ msg : unit =
|
||||||
match A.get collector with
|
match A.get collector with
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,17 @@ module Collector = Collector
|
||||||
(** {2 Tracing} *)
|
(** {2 Tracing} *)
|
||||||
|
|
||||||
val enabled : unit -> bool
|
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
|
?__FUNCTION__:string -> __FILE__:string -> __LINE__:int -> string -> span
|
||||||
|
|
||||||
val exit_span : span -> unit
|
val exit_span : span -> unit
|
||||||
|
|
||||||
val with_ :
|
val with_span :
|
||||||
?__FUNCTION__:string ->
|
?__FUNCTION__:string ->
|
||||||
__FILE__:string ->
|
__FILE__:string ->
|
||||||
__LINE__:int ->
|
__LINE__:int ->
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
let run () =
|
let run () =
|
||||||
for _i = 1 to 50 do
|
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
|
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)
|
Trace.messagef ~__FILE__ ~__LINE__ (fun k -> k "hello %d %d" _i _j)
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue