From 431811c99584bb5f4dc053847f498b69bc3d9ef5 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 13 Sep 2023 14:43:16 -0400 Subject: [PATCH] rename `add_data_to_current_span`; give it span explicitly --- src/core/collector.ml | 4 ++-- src/core/trace_core.ml | 4 ++-- src/core/trace_core.mli | 6 +++--- src/tef/trace_tef.ml | 28 ++++++---------------------- test/t1.ml | 4 ++-- 5 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/core/collector.ml b/src/core/collector.ml index 93f8faa..6b637fa 100644 --- a/src/core/collector.ml +++ b/src/core/collector.ml @@ -53,8 +53,8 @@ module type S = sig (** Exit an explicit span. @since 0.3 *) - val add_data_to_current_span : (string * user_data) list -> unit - (** @since Adds data to the current, implicit span. + val add_data_to_span : span -> (string * user_data) list -> unit + (** @since Adds data to the current span. NEXT_RELEASE *) val add_data_to_manual_span : diff --git a/src/core/trace_core.ml b/src/core/trace_core.ml index 2c4a072..84e9b90 100644 --- a/src/core/trace_core.ml +++ b/src/core/trace_core.ml @@ -55,11 +55,11 @@ let[@inline] exit_manual_span espan : unit = | None -> () | 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 ( match A.get collector with | 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 = diff --git a/src/core/trace_core.mli b/src/core/trace_core.mli index a1f335e..edb3c70 100644 --- a/src/core/trace_core.mli +++ b/src/core/trace_core.mli @@ -34,9 +34,9 @@ val with_span : see {!enter_manual_span}. *) -val add_data_to_current_span : (string * user_data) list -> unit -(** Add structured data to the current, implicit span (see {!with_span}). - Behavior is not specified if there is no current span. +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. @since NEXT_RELEASE *) val enter_manual_sub_span : diff --git a/src/tef/trace_tef.ml b/src/tef/trace_tef.ml index 2020c9e..2f685c9 100644 --- a/src/tef/trace_tef.ml +++ b/src/tef/trace_tef.ml @@ -55,7 +55,7 @@ type event = time_us: float; } | E_add_data of { - tid: int; + id: span; data: (string * user_data) list; } | E_enter_manual_span of { @@ -93,13 +93,6 @@ module Span_tbl = Hashtbl.Make (struct let hash : t -> int = Hashtbl.hash 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 = { tid: int; name: string; @@ -280,7 +273,6 @@ let bg_thread ~out (events : event B_queue.t) : unit = Writer.with_ ~out @@ fun writer -> (* 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 ambient_span : span_info Int_tbl.t = Int_tbl.create 16 in let local_q = Queue.create () in (* 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 } -> let data = add_fun_name_ fun_name 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 *) Span_tbl.add spans id info | 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) | Some { tid; name; start_us; data } -> Span_tbl.remove spans id; - Int_tbl.remove ambient_span tid; Writer.emit_duration_event ~tid ~name ~start:start_us ~end_:stop_us ~args:data writer) - | E_add_data { tid; data } -> - (match Int_tbl.find_opt ambient_span tid with - | None -> - !on_tracing_error - (Printf.sprintf "cannot find ambient span for thread %d" tid) + | E_add_data { id; data } -> + (match Span_tbl.find_opt spans id with + | None -> !on_tracing_error (Printf.sprintf "cannot find span %Ld" id) | Some info -> info.data <- List.rev_append data info.data) | E_enter_manual_span { tid; time_us; name; id; data; fun_name; flavor } -> let data = add_fun_name_ fun_name data in @@ -410,11 +397,8 @@ let collector ~out () : collector = Fun.protect ~finally (fun () -> f span) - let add_data_to_current_span data = - if data <> [] then ( - let tid = get_tid_ () in - B_queue.push events (E_add_data { tid; data }) - ) + let add_data_to_span span data = + if data <> [] then B_queue.push events (E_add_data { id = span; data }) let enter_manual_span ~(parent : explicit_span option) ~flavor ~__FUNCTION__:fun_name ~__FILE__:_ ~__LINE__:_ ~data name : diff --git a/test/t1.ml b/test/t1.ml index b4db240..87e4792 100644 --- a/test/t1.ml +++ b/test/t1.ml @@ -17,10 +17,10 @@ let run () = Trace.message "world"; 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 ( - Trace.add_data_to_current_span [ "j", `Int _j ]; + Trace.add_data_to_span _sp [ "j", `Int _j ]; let _sp = Trace.enter_manual_sub_span ~parent:pseudo_async_sp ~flavor: