rename explicit span API

This commit is contained in:
Simon Cruanes 2023-08-03 14:54:46 -04:00
parent 4ca766166f
commit 50b4691ab6
5 changed files with 48 additions and 28 deletions

View file

@ -31,15 +31,15 @@ module type S = sig
to be efficient to implement in async contexts. to be efficient to implement in async contexts.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
val enter_explicit_span : val enter_manual_span :
surrounding:explicit_span option -> parent:explicit_span option ->
?__FUNCTION__:string -> ?__FUNCTION__:string ->
__FILE__:string -> __FILE__:string ->
__LINE__:int -> __LINE__:int ->
data:(string * user_data) list -> data:(string * user_data) list ->
string -> string ->
explicit_span explicit_span
(** Enter an explicit span. Surrounding scope is provided by [surrounding], (** Enter an explicit span. Surrounding scope, if any, is provided by [parent],
and this function can store as much metadata as it wants in the hmap and this function can store as much metadata as it wants in the hmap
in the {!explicit_span}'s [meta] field. in the {!explicit_span}'s [meta] field.
@ -48,7 +48,7 @@ module type S = sig
everything can be transmitted in the {!explicit_span}. everything can be transmitted in the {!explicit_span}.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
val exit_explicit_span : explicit_span -> unit val exit_manual_span : explicit_span -> unit
(** Exit an explicit span. (** Exit an explicit span.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)

View file

@ -27,25 +27,32 @@ let[@inline] with_span ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name f =
with_span_collector_ collector ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name with_span_collector_ collector ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data name
f f
let enter_explicit_span_collector_ (module C : Collector.S) ~surrounding let enter_explicit_span_collector_ (module C : Collector.S) ~parent
?__FUNCTION__ ~__FILE__ ~__LINE__ ?(data = fun () -> []) name : ?__FUNCTION__ ~__FILE__ ~__LINE__ ?(data = fun () -> []) name :
explicit_span = explicit_span =
let data = data () in let data = data () in
C.enter_explicit_span ~surrounding ?__FUNCTION__ ~__FILE__ ~__LINE__ ~data C.enter_manual_span ~parent ?__FUNCTION__ ~__FILE__ ~__LINE__ ~data name
name
let[@inline] enter_explicit_span ~surrounding ?__FUNCTION__ ~__FILE__ ~__LINE__ let[@inline] enter_manual_sub_span ~parent ?__FUNCTION__ ~__FILE__ ~__LINE__
?data name : explicit_span = ?data name : explicit_span =
match A.get collector with match A.get collector with
| None -> Collector.dummy_explicit_span | None -> Collector.dummy_explicit_span
| Some coll -> | Some coll ->
enter_explicit_span_collector_ coll ~surrounding ?__FUNCTION__ ~__FILE__ enter_explicit_span_collector_ coll ~parent:(Some parent) ?__FUNCTION__
~__FILE__ ~__LINE__ ?data name
let[@inline] enter_manual_toplevel_span ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data
name : explicit_span =
match A.get collector with
| None -> Collector.dummy_explicit_span
| Some coll ->
enter_explicit_span_collector_ coll ~parent:None ?__FUNCTION__ ~__FILE__
~__LINE__ ?data name ~__LINE__ ?data name
let[@inline] exit_explicit_span espan : unit = let[@inline] exit_manual_span espan : unit =
match A.get collector with match A.get collector with
| None -> () | None -> ()
| Some (module C) -> C.exit_explicit_span espan | Some (module C) -> C.exit_manual_span espan
let message_collector_ (module C : Collector.S) ?span ?(data = fun () -> []) msg let message_collector_ (module C : Collector.S) ?span ?(data = fun () -> []) msg
: unit = : unit =

View file

@ -31,11 +31,11 @@ val with_span :
work for synchronous, direct style code. Monadic concurrency, Effect-based work for synchronous, direct style code. Monadic concurrency, Effect-based
fibers, etc. might not play well with this style of spans on some fibers, etc. might not play well with this style of spans on some
or all backends. If you use cooperative concurrency, or all backends. If you use cooperative concurrency,
see {!enter_explicit_span}. see {!enter_manual_span}.
*) *)
val enter_explicit_span : val enter_manual_sub_span :
surrounding:explicit_span option -> parent:explicit_span ->
?__FUNCTION__:string -> ?__FUNCTION__:string ->
__FILE__:string -> __FILE__:string ->
__LINE__:int -> __LINE__:int ->
@ -43,14 +43,28 @@ val enter_explicit_span :
string -> string ->
explicit_span explicit_span
(** Like {!with_span} but the caller is responsible for (** Like {!with_span} but the caller is responsible for
providing the [surrounding] context, and carry the resulting providing the [parent] context, and carry the resulting
{!explicit_span} to the matching {!exit_explicit_span}. {!explicit_span} to the matching {!exit_manual_span}.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
val exit_explicit_span : explicit_span -> unit val enter_manual_toplevel_span :
?__FUNCTION__:string ->
__FILE__:string ->
__LINE__:int ->
?data:(unit -> (string * user_data) list) ->
string ->
explicit_span
(** Like {!with_span} but the caller is responsible for carrying this
[explicit_span] around until it's exited with {!exit_manual_span}.
The span can be used as a parent in {!enter_manual_sub_span}.
@since NEXT_RELEASE *)
val exit_manual_span : explicit_span -> unit
(** Exit an explicit span. This can be on another thread, in a (** Exit an explicit span. This can be on another thread, in a
fiber or lightweight thread, etc. and will be supported by backends fiber or lightweight thread, etc. and will be supported by backends
nonetheless. nonetheless.
The span can be obtained via {!enter_manual_sub_span} or
{!enter_manual_toplevel_span}.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
val message : val message :

View file

@ -342,11 +342,11 @@ let collector ~out () : collector =
Fun.protect ~finally (fun () -> f span) Fun.protect ~finally (fun () -> f span)
let enter_explicit_span ~(surrounding : explicit_span option) let enter_manual_span ~(parent : explicit_span option) ?__FUNCTION__:_
?__FUNCTION__:_ ~__FILE__:_ ~__LINE__:_ ~data name : explicit_span = ~__FILE__:_ ~__LINE__:_ ~data name : explicit_span =
(* get the id, or make a new one *) (* get the id, or make a new one *)
let id = let id =
match surrounding with match parent with
| Some m -> Meta_map.find_exn key_async_id m.meta | Some m -> Meta_map.find_exn key_async_id m.meta
| None -> A.fetch_and_add span_id_gen_ 1 | None -> A.fetch_and_add span_id_gen_ 1
in in
@ -359,7 +359,7 @@ let collector ~out () : collector =
Meta_map.(empty |> add key_async_id id |> add key_async_name name); Meta_map.(empty |> add key_async_id id |> add key_async_name name);
} }
let exit_explicit_span (es : explicit_span) : unit = let exit_manual_span (es : explicit_span) : unit =
let id = Meta_map.find_exn key_async_id es.meta in let id = Meta_map.find_exn key_async_id es.meta in
let name = Meta_map.find_exn key_async_name es.meta in let name = Meta_map.find_exn key_async_name es.meta in
let time_us = now_us () in let time_us = now_us () in

View file

@ -7,8 +7,7 @@ let run () =
for _i = 1 to 50 do for _i = 1 to 50 do
Trace.with_span ~__FILE__ ~__LINE__ "outer.loop" @@ fun _sp -> Trace.with_span ~__FILE__ ~__LINE__ "outer.loop" @@ fun _sp ->
let pseudo_async_sp = let pseudo_async_sp =
Trace.enter_explicit_span ~surrounding:None ~__FILE__ ~__LINE__ Trace.enter_manual_toplevel_span ~__FILE__ ~__LINE__ "fake_sleep"
"fake_sleep"
in in
for _j = 2 to 5 do for _j = 2 to 5 do
@ -21,14 +20,14 @@ let run () =
if _j = 2 then ( if _j = 2 then (
(* fake micro sleep *) (* fake micro sleep *)
let _sp = let _sp =
Trace.enter_explicit_span ~surrounding:(Some pseudo_async_sp) Trace.enter_manual_sub_span ~parent:pseudo_async_sp ~__FILE__
~__FILE__ ~__LINE__ "sub-sleep" ~__LINE__ "sub-sleep"
in in
Thread.delay 0.005; Thread.delay 0.005;
Trace.exit_explicit_span _sp Trace.exit_manual_span _sp
) else if _j = 3 then ) else if _j = 3 then
(* pretend some task finished. Note that this is not well scoped wrt other spans. *) (* pretend some task finished. Note that this is not well scoped wrt other spans. *)
Trace.exit_explicit_span pseudo_async_sp Trace.exit_manual_span pseudo_async_sp
done done
done done