restore ticker_thread feature

This commit is contained in:
Simon Cruanes 2023-06-21 14:23:04 -04:00
parent 64bd211ac7
commit c016f00a27
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 30 additions and 6 deletions

View file

@ -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 }

View file

@ -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.

View file

@ -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) () =