rename add_data_to_current_span; give it span explicitly

This commit is contained in:
Simon Cruanes 2023-09-13 14:43:16 -04:00
parent e0fe99f500
commit 431811c995
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
5 changed files with 15 additions and 31 deletions

View file

@ -53,8 +53,8 @@ module type S = sig
(** Exit an explicit span. (** Exit an explicit span.
@since 0.3 *) @since 0.3 *)
val add_data_to_current_span : (string * user_data) list -> unit val add_data_to_span : span -> (string * user_data) list -> unit
(** @since Adds data to the current, implicit span. (** @since Adds data to the current span.
NEXT_RELEASE *) NEXT_RELEASE *)
val add_data_to_manual_span : val add_data_to_manual_span :

View file

@ -55,11 +55,11 @@ let[@inline] exit_manual_span espan : unit =
| None -> () | None -> ()
| Some (module C) -> C.exit_manual_span espan | Some (module C) -> C.exit_manual_span espan
let[@inline] add_data_to_current_span data : unit = let[@inline] add_data_to_span sp data : unit =
if data <> [] then ( if data <> [] then (
match A.get collector with match A.get collector with
| None -> () | None -> ()
| Some (module C) -> C.add_data_to_current_span data | Some (module C) -> C.add_data_to_span sp data
) )
let[@inline] add_data_to_manual_span esp data : unit = let[@inline] add_data_to_manual_span esp data : unit =

View file

@ -34,9 +34,9 @@ val with_span :
see {!enter_manual_span}. see {!enter_manual_span}.
*) *)
val add_data_to_current_span : (string * user_data) list -> unit val add_data_to_span : span -> (string * user_data) list -> unit
(** Add structured data to the current, implicit span (see {!with_span}). (** Add structured data to the given active span (see {!with_span}).
Behavior is not specified if there is no current span. Behavior is not specified if the span has been exited.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
val enter_manual_sub_span : val enter_manual_sub_span :

View file

@ -55,7 +55,7 @@ type event =
time_us: float; time_us: float;
} }
| E_add_data of { | E_add_data of {
tid: int; id: span;
data: (string * user_data) list; data: (string * user_data) list;
} }
| E_enter_manual_span of { | E_enter_manual_span of {
@ -93,13 +93,6 @@ module Span_tbl = Hashtbl.Make (struct
let hash : t -> int = Hashtbl.hash let hash : t -> int = Hashtbl.hash
end) end)
module Int_tbl = Hashtbl.Make (struct
type t = int
let equal : t -> t -> bool = ( = )
let hash : t -> int = Hashtbl.hash
end)
type span_info = { type span_info = {
tid: int; tid: int;
name: string; name: string;
@ -280,7 +273,6 @@ let bg_thread ~out (events : event B_queue.t) : unit =
Writer.with_ ~out @@ fun writer -> Writer.with_ ~out @@ fun writer ->
(* local state, to keep track of span information and implicit stack context *) (* local state, to keep track of span information and implicit stack context *)
let spans : span_info Span_tbl.t = Span_tbl.create 32 in let spans : span_info Span_tbl.t = Span_tbl.create 32 in
let ambient_span : span_info Int_tbl.t = Int_tbl.create 16 in
let local_q = Queue.create () in let local_q = Queue.create () in
(* add function name, if provided, to the metadata *) (* add function name, if provided, to the metadata *)
@ -299,8 +291,6 @@ let bg_thread ~out (events : event B_queue.t) : unit =
| E_define_span { tid; name; id; time_us; fun_name; data } -> | E_define_span { tid; name; id; time_us; fun_name; data } ->
let data = add_fun_name_ fun_name data in let data = add_fun_name_ fun_name data in
let info = { tid; name; start_us = time_us; data } in let info = { tid; name; start_us = time_us; data } in
(* make this span the "ambient" one for the given thread *)
Int_tbl.add ambient_span tid info;
(* save the span so we find it at exit *) (* save the span so we find it at exit *)
Span_tbl.add spans id info Span_tbl.add spans id info
| E_exit_span { id; time_us = stop_us } -> | E_exit_span { id; time_us = stop_us } ->
@ -308,14 +298,11 @@ let bg_thread ~out (events : event B_queue.t) : unit =
| None -> !on_tracing_error (Printf.sprintf "cannot find span %Ld" id) | None -> !on_tracing_error (Printf.sprintf "cannot find span %Ld" id)
| Some { tid; name; start_us; data } -> | Some { tid; name; start_us; data } ->
Span_tbl.remove spans id; Span_tbl.remove spans id;
Int_tbl.remove ambient_span tid;
Writer.emit_duration_event ~tid ~name ~start:start_us ~end_:stop_us Writer.emit_duration_event ~tid ~name ~start:start_us ~end_:stop_us
~args:data writer) ~args:data writer)
| E_add_data { tid; data } -> | E_add_data { id; data } ->
(match Int_tbl.find_opt ambient_span tid with (match Span_tbl.find_opt spans id with
| None -> | None -> !on_tracing_error (Printf.sprintf "cannot find span %Ld" id)
!on_tracing_error
(Printf.sprintf "cannot find ambient span for thread %d" tid)
| Some info -> info.data <- List.rev_append data info.data) | Some info -> info.data <- List.rev_append data info.data)
| E_enter_manual_span { tid; time_us; name; id; data; fun_name; flavor } -> | E_enter_manual_span { tid; time_us; name; id; data; fun_name; flavor } ->
let data = add_fun_name_ fun_name data in let data = add_fun_name_ fun_name data in
@ -410,11 +397,8 @@ let collector ~out () : collector =
Fun.protect ~finally (fun () -> f span) Fun.protect ~finally (fun () -> f span)
let add_data_to_current_span data = let add_data_to_span span data =
if data <> [] then ( if data <> [] then B_queue.push events (E_add_data { id = span; data })
let tid = get_tid_ () in
B_queue.push events (E_add_data { tid; data })
)
let enter_manual_span ~(parent : explicit_span option) ~flavor let enter_manual_span ~(parent : explicit_span option) ~flavor
~__FUNCTION__:fun_name ~__FILE__:_ ~__LINE__:_ ~data name : ~__FUNCTION__:fun_name ~__FILE__:_ ~__LINE__:_ ~data name :

View file

@ -17,10 +17,10 @@ let run () =
Trace.message "world"; Trace.message "world";
Trace.counter_int "n" !n; Trace.counter_int "n" !n;
Trace.add_data_to_current_span [ "i", `Int _i ]; Trace.add_data_to_span _sp [ "i", `Int _i ];
if _j = 2 then ( if _j = 2 then (
Trace.add_data_to_current_span [ "j", `Int _j ]; Trace.add_data_to_span _sp [ "j", `Int _j ];
let _sp = let _sp =
Trace.enter_manual_sub_span ~parent:pseudo_async_sp Trace.enter_manual_sub_span ~parent:pseudo_async_sp
~flavor: ~flavor: