functions to enter/exit implicit spans

This commit is contained in:
Simon Cruanes 2023-12-22 20:10:58 -05:00
parent a0874f2c31
commit ef15941936
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 53 additions and 0 deletions

View file

@ -31,6 +31,23 @@ module type S = sig
to be efficient to implement in async contexts.
@since 0.3 *)
val enter_span :
__FUNCTION__:string option ->
__FILE__:string ->
__LINE__:int ->
data:(string * user_data) list ->
string ->
span
(** Enter a new implicit span. For many uses cases, {!with_span} will
be easier to use.
@since NEXT_RELEASE *)
val exit_span : span -> unit
(** Exit span. This should be called on the same thread
as the corresponding {!enter_span}, and nest properly with
other calls to enter/exit_span and {!with_span}.
@since NEXT_RELEASE *)
val enter_manual_span :
parent:explicit_span option ->
flavor:[ `Sync | `Async ] option ->

View file

@ -29,6 +29,19 @@ let[@inline] with_span ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name f =
with_span_collector_ collector ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name
f
let[@inline] enter_span ?__FUNCTION__ ~__FILE__ ~__LINE__
?(data = data_empty_build_) name : span =
match A.get collector with
| None -> Collector.dummy_span
| Some (module C) ->
let data = data () in
C.enter_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name
let[@inline] exit_span sp : unit =
match A.get collector with
| None -> ()
| Some (module C) -> C.exit_span sp
let enter_explicit_span_collector_ (module C : Collector.S) ~parent ~flavor
?__FUNCTION__ ~__FILE__ ~__LINE__ ?(data = data_empty_build_) name :
explicit_span =

View file

@ -34,6 +34,16 @@ val with_span :
see {!enter_manual_span}.
*)
val enter_span :
?__FUNCTION__:string ->
__FILE__:string ->
__LINE__:int ->
?data:(unit -> (string * user_data) list) ->
string ->
span
val exit_span : span -> unit
val add_data_to_span : span -> (string * user_data) list -> unit
(** Add structured data to the given active span (see {!with_span}).
Behavior is not specified if the span has been exited.

View file

@ -386,6 +386,19 @@ let collector ~out () : collector =
else
Thread.id (Thread.self ())
let enter_span ~__FUNCTION__:fun_name ~__FILE__:_ ~__LINE__:_ ~data 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
B_queue.push events
(E_define_span { tid; name; time_us; id = span; fun_name; data });
span
let exit_span span : unit =
let time_us = now_us () in
B_queue.push events (E_exit_span { id = span; time_us })
let with_span ~__FUNCTION__:fun_name ~__FILE__:_ ~__LINE__:_ ~data name f =
let span = Int64.of_int (A.fetch_and_add span_id_gen_ 1) in
let tid = get_tid_ () in