From 0ef97a002ab78733669053fa9aec76301b880429 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 16 Oct 2024 13:22:19 -0400 Subject: [PATCH 1/5] feat: allow to remove backend close #70 --- src/core/opentelemetry.ml | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/core/opentelemetry.ml b/src/core/opentelemetry.ml index b08516e..1e250f2 100644 --- a/src/core/opentelemetry.ml +++ b/src/core/opentelemetry.ml @@ -3,6 +3,7 @@ open struct let spf = Printf.sprintf + module Atomic = Opentelemetry_atomic.Atomic module Ambient_context = Opentelemetry_ambient_context end @@ -163,33 +164,37 @@ module Collector = struct open struct let on_tick_cbs_ = AList.make () - let backend : backend option ref = ref None + let backend : backend option Atomic.t = Atomic.make None end (** Set collector backend *) let set_backend (b : backend) : unit = let (module B) = b in B.set_on_tick_callbacks on_tick_cbs_; - backend := Some b + Atomic.set backend (Some b) + + (** Remove current backend, if any. + @since NEXT_RELEASE *) + let remove_backend () : unit = Atomic.set backend None (** Is there a configured backend? *) - let[@inline] has_backend () : bool = !backend != None + let[@inline] has_backend () : bool = Atomic.get backend != None (** Current backend, if any *) - let[@inline] get_backend () : backend option = !backend + let[@inline] get_backend () : backend option = Atomic.get backend let send_trace (l : Trace.resource_spans list) ~ret = - match !backend with + match Atomic.get backend with | None -> ret () | Some (module B) -> B.send_trace.send l ~ret let send_metrics (l : Metrics.resource_metrics list) ~ret = - match !backend with + match Atomic.get backend with | None -> ret () | Some (module B) -> B.send_metrics.send l ~ret let send_logs (l : Logs.resource_logs list) ~ret = - match !backend with + match Atomic.get backend with | None -> ret () | Some (module B) -> B.send_logs.send l ~ret @@ -202,7 +207,7 @@ module Collector = struct (** Do background work. Call this regularly if the collector doesn't already have a ticker thread or internal timer. *) let tick () = - match !backend with + match Atomic.get backend with | None -> () | Some (module B) -> B.tick () From 1924ff788baf43b6f6783140a786bcaf701db62b Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 16 Oct 2024 13:28:47 -0400 Subject: [PATCH 2/5] in `remove_backend`, call tick() then cleanup() on it --- src/core/opentelemetry.ml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/opentelemetry.ml b/src/core/opentelemetry.ml index 1e250f2..87b99d0 100644 --- a/src/core/opentelemetry.ml +++ b/src/core/opentelemetry.ml @@ -175,7 +175,12 @@ module Collector = struct (** Remove current backend, if any. @since NEXT_RELEASE *) - let remove_backend () : unit = Atomic.set backend None + let remove_backend () : unit = + match Atomic.exchange backend None with + | None -> () + | Some (module B) -> + B.tick (); + B.cleanup () (** Is there a configured backend? *) let[@inline] has_backend () : bool = Atomic.get backend != None From 62f876a0be704abb9063a039b5e7f54650753a70 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 16 Oct 2024 13:26:57 -0400 Subject: [PATCH 3/5] in backends, call tick() before cleaning up this helps flushing signals that are being batched. close #69 --- src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml | 6 +++++- src/client-ocurl/opentelemetry_client_ocurl.ml | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml index 05b1bab..48ad9c8 100644 --- a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml +++ b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml @@ -569,7 +569,11 @@ let setup_ ?stop ?config () = let backend = create_backend ?stop ?config () in let (module B : OT.Collector.BACKEND) = backend in OT.Collector.set_backend backend; - B.cleanup + let cleanup () = + B.tick (); + B.cleanup () + in + cleanup let setup ?stop ?config ?(enable = true) () = if enable then ( diff --git a/src/client-ocurl/opentelemetry_client_ocurl.ml b/src/client-ocurl/opentelemetry_client_ocurl.ml index 0cb32a3..8d3ea11 100644 --- a/src/client-ocurl/opentelemetry_client_ocurl.ml +++ b/src/client-ocurl/opentelemetry_client_ocurl.ml @@ -509,7 +509,11 @@ let setup_ ?(stop = Atomic.make false) ?(config : Config.t = Config.make ()) () ignore (setup_ticker_thread ~stop ~sleep_ms backend () : Thread.t) ); - B.cleanup + let cleanup () = + B.tick (); + B.cleanup () + in + cleanup let setup ?stop ?config ?(enable = true) () = if enable then ( From 6201c041dbb986ac886291ab8fed45027f58934b Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 16 Oct 2024 13:29:39 -0400 Subject: [PATCH 4/5] simplify --- src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml | 6 +----- src/client-ocurl/opentelemetry_client_ocurl.ml | 7 +------ 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml index 48ad9c8..cffe8a7 100644 --- a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml +++ b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml @@ -569,11 +569,7 @@ let setup_ ?stop ?config () = let backend = create_backend ?stop ?config () in let (module B : OT.Collector.BACKEND) = backend in OT.Collector.set_backend backend; - let cleanup () = - B.tick (); - B.cleanup () - in - cleanup + OT.Collector.remove_backend let setup ?stop ?config ?(enable = true) () = if enable then ( diff --git a/src/client-ocurl/opentelemetry_client_ocurl.ml b/src/client-ocurl/opentelemetry_client_ocurl.ml index 8d3ea11..b5412f2 100644 --- a/src/client-ocurl/opentelemetry_client_ocurl.ml +++ b/src/client-ocurl/opentelemetry_client_ocurl.ml @@ -508,12 +508,7 @@ let setup_ ?(stop = Atomic.make false) ?(config : Config.t = Config.make ()) () let sleep_ms = min 60_000 (max 2 config.ticker_interval_ms) in ignore (setup_ticker_thread ~stop ~sleep_ms backend () : Thread.t) ); - - let cleanup () = - B.tick (); - B.cleanup () - in - cleanup + OT.Collector.remove_backend let setup ?stop ?config ?(enable = true) () = if enable then ( From 0749e898d0b3091c071191819c37dd0bb3d0bac5 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 16 Oct 2024 13:30:20 -0400 Subject: [PATCH 5/5] cleanup --- src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml | 1 - src/client-ocurl/opentelemetry_client_ocurl.ml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml index cffe8a7..5894f8b 100644 --- a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml +++ b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml @@ -567,7 +567,6 @@ let create_backend ?(stop = Atomic.make false) ?(config = Config.make ()) () = let setup_ ?stop ?config () = let backend = create_backend ?stop ?config () in - let (module B : OT.Collector.BACKEND) = backend in OT.Collector.set_backend backend; OT.Collector.remove_backend diff --git a/src/client-ocurl/opentelemetry_client_ocurl.ml b/src/client-ocurl/opentelemetry_client_ocurl.ml index b5412f2..3da89ba 100644 --- a/src/client-ocurl/opentelemetry_client_ocurl.ml +++ b/src/client-ocurl/opentelemetry_client_ocurl.ml @@ -498,7 +498,7 @@ let setup_ticker_thread ~stop ~sleep_ms (module B : Collector.BACKEND) () = let setup_ ?(stop = Atomic.make false) ?(config : Config.t = Config.make ()) () = - let ((module B) as backend) = create_backend ~stop ~config () in + let backend = create_backend ~stop ~config () in Opentelemetry.Collector.set_backend backend; Atomic.set Self_trace.enabled config.self_trace;