diff --git a/src/client-ocurl/config.ml b/src/client-ocurl/config.ml index 3dfea160..0abca7e1 100644 --- a/src/client-ocurl/config.ml +++ b/src/client-ocurl/config.ml @@ -6,18 +6,21 @@ type t = { headers: (string * string) list; batch_timeout_ms: int; bg_threads: int; + ticker_thread: bool; } let pp out self = let pp_header ppf (a, b) = Format.fprintf ppf "@[%s: @,%s@]@." a b in let ppheaders = Format.pp_print_list pp_header in - let { debug; url; headers; batch_timeout_ms; bg_threads } = self in + let { debug; url; headers; batch_timeout_ms; bg_threads; ticker_thread } = + self + in Format.fprintf out - "{@[ debug=%B;@ url=%S;@ headers=%a;@ batch_timeout_ms=%d; \ - bg_threads=%d;@]}" - debug url ppheaders headers batch_timeout_ms bg_threads + "{@[ debug=%B;@ url=%S;@ headers=%a;@ batch_timeout_ms=%d; bg_threads=%d;@ \ + ticker_thread=%B @]}" + debug url ppheaders headers batch_timeout_ms bg_threads ticker_thread let make ?(debug = !debug_) ?(url = get_url ()) ?(headers = get_headers ()) - ?(batch_timeout_ms = 500) ?(bg_threads = 4) () : t = + ?(batch_timeout_ms = 500) ?(bg_threads = 4) ?(ticker_thread = true) () : t = let bg_threads = max 2 (min bg_threads 32) in - { debug; url; headers; batch_timeout_ms; bg_threads } + { debug; url; headers; batch_timeout_ms; bg_threads; ticker_thread } diff --git a/src/client-ocurl/config.mli b/src/client-ocurl/config.mli index 79b0e4cf..17a7711e 100644 --- a/src/client-ocurl/config.mli +++ b/src/client-ocurl/config.mli @@ -15,6 +15,9 @@ type t = private { only checked when a new event occurs. Default 500. *) bg_threads: int; (** Are there background threads, and how many? Default [4] *) + ticker_thread: bool; + (** If true, start a thread that regularly checks if signals should + be sent to the collector. Default [true] *) } (** Configuration. @@ -27,6 +30,7 @@ val make : ?headers:(string * string) list -> ?batch_timeout_ms:int -> ?bg_threads:int -> + ?ticker_thread:bool -> unit -> t (** Make a configuration. diff --git a/src/client-ocurl/opentelemetry_client_ocurl.ml b/src/client-ocurl/opentelemetry_client_ocurl.ml index 39de6250..e0d6a596 100644 --- a/src/client-ocurl/opentelemetry_client_ocurl.ml +++ b/src/client-ocurl/opentelemetry_client_ocurl.ml @@ -415,9 +415,26 @@ let mk_backend ~stop ~config () : (module Collector.BACKEND) = end in (module M) +(** thread that calls [tick()] regularly, to help enforce timeouts *) +let setup_ticker_thread ~stop ~sleep_ms (module B : Collector.BACKEND) () = + let sleep_s = float sleep_ms /. 1000. in + let tick_loop () = + while not @@ Atomic.get stop do + Thread.delay sleep_s; + B.tick () + done + in + start_bg_thread tick_loop + let setup_ ?(stop = Atomic.make false) ~(config : Config.t) () = let ((module B) as backend) = mk_backend ~stop ~config () in Opentelemetry.Collector.set_backend backend; + + if config.ticker_thread then ( + let sleep_ms = min 5_000 (max 2 config.batch_timeout_ms) in + ignore (setup_ticker_thread ~stop ~sleep_ms backend () : Thread.t) + ); + B.cleanup let setup ?stop ?(config = Config.make ()) ?(enable = true) () =