Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to remove backend #73

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,8 @@ 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;
B.cleanup
OT.Collector.remove_backend

let setup ?stop ?config ?(enable = true) () =
if enable then (
Expand Down
5 changes: 2 additions & 3 deletions src/client-ocurl/opentelemetry_client_ocurl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -508,8 +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)
);

B.cleanup
OT.Collector.remove_backend

let setup ?stop ?config ?(enable = true) () =
if enable then (
Expand Down
26 changes: 18 additions & 8 deletions src/core/opentelemetry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open struct
let spf = Printf.sprintf

module Atomic = Opentelemetry_atomic.Atomic
module Ambient_context = Opentelemetry_ambient_context
end

Expand Down Expand Up @@ -163,33 +164,42 @@ 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 =
match Atomic.exchange backend None with
| None -> ()
| Some (module B) ->
B.tick ();
B.cleanup ()

(** 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

Expand All @@ -202,7 +212,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 ()

Expand Down
Loading