Skip to content

Commit

Permalink
CA-385278: Add interface for flush spans and exit the export thread
Browse files Browse the repository at this point in the history
Currently there is no way to force flush the spans in memory, this patch
adds an interface to allow such flush. This is useful when, for example,
clusterd is about to exit.

Signed-off-by: Vincent Liu <[email protected]>
  • Loading branch information
Vincent-lau committed Dec 19, 2023
1 parent ee31290 commit ae881de
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions ocaml/libs/tracing/tracing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*)
module D = Debug.Make (struct let name = "tracing" end)

module Delay = Xapi_stdext_threads.Threadext.Delay
open D

type endpoint = Bugtool | Url of Uri.t
Expand Down Expand Up @@ -820,14 +821,23 @@ module Export = struct
|> List.concat_map (fun x -> TracerProvider.get_endpoints x)
|> List.iter (export_to_endpoint parent span_list)

let delay = Delay.make ()

(* Note this signal will flush the spans and terminate the exporter thread *)
let signal () = Delay.signal delay

let create_exporter () =
enable_span_garbage_collector () ;
Thread.create
(fun () ->
while true do
let signaled = ref false in
while not !signaled do
debug "Tracing: Waiting %d seconds before exporting spans"
(int_of_float !export_interval) ;
Thread.delay !export_interval ;
if not (Delay.wait delay !export_interval) then (
debug "Tracing: we are signaled, export spans now and exit" ;
signaled := true
) ;
flush_spans ()
done
)
Expand All @@ -850,6 +860,6 @@ module Export = struct
end
end

let main = Export.Destination.main
let flush_and_exit = Export.Destination.signal

let flush_spans = Export.Destination.flush_spans
let main = Export.Destination.main
2 changes: 1 addition & 1 deletion ocaml/libs/tracing/tracing.mli
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ val set_observe : bool -> unit

val validate_attribute : string * string -> bool

val flush_spans : unit -> unit
val flush_and_exit : unit -> unit

val main : unit -> Thread.t

0 comments on commit ae881de

Please sign in to comment.