ocurl: add an ?after_shutdown callback

This commit is contained in:
Simon Cruanes 2025-12-09 22:26:08 -05:00
parent 239d9d5aec
commit bfde7700e8
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 14 additions and 5 deletions

View file

@ -97,13 +97,16 @@ let create_exporter ?(config = Config.make ()) () : OTEL.Exporter.t =
let create_backend = create_exporter let create_backend = create_exporter
let shutdown_and_wait (self : OTEL.Exporter.t) : unit = let shutdown_and_wait ?(after_shutdown = ignore) (self : OTEL.Exporter.t) : unit
=
let open Opentelemetry_client in let open Opentelemetry_client in
let sq = Sync_queue.create () in let sq = Sync_queue.create () in
OTEL.Aswitch.on_turn_off (OTEL.Exporter.active self) (fun () -> OTEL.Aswitch.on_turn_off (OTEL.Exporter.active self) (fun () ->
Sync_queue.push sq ()); Sync_queue.push sq ());
OTEL.Exporter.shutdown self; OTEL.Exporter.shutdown self;
Sync_queue.pop sq Sync_queue.pop sq;
after_shutdown self;
()
let setup_ ?(config : Config.t = Config.make ()) () : OTEL.Exporter.t = let setup_ ?(config : Config.t = Config.make ()) () : OTEL.Exporter.t =
let exporter = create_exporter ~config () in let exporter = create_exporter ~config () in
@ -133,10 +136,10 @@ let remove_backend = remove_exporter
let setup ?config ?(enable = true) () = let setup ?config ?(enable = true) () =
if enable then ignore (setup_ ?config () : OTEL.Exporter.t) if enable then ignore (setup_ ?config () : OTEL.Exporter.t)
let with_setup ?config ?(enable = true) () f = let with_setup ?after_shutdown ?config ?(enable = true) () f =
if enable then ( if enable then (
let exp = setup_ ?config () in let exp = setup_ ?config () in
Fun.protect f ~finally:(fun () -> shutdown_and_wait exp) Fun.protect f ~finally:(fun () -> shutdown_and_wait ?after_shutdown exp)
) else ) else
f () f ()

View file

@ -40,6 +40,12 @@ val remove_backend : unit -> unit
[@@deprecated "use remove_exporter"] [@@deprecated "use remove_exporter"]
(** @since 0.12 *) (** @since 0.12 *)
val with_setup : ?config:Config.t -> ?enable:bool -> unit -> (unit -> 'a) -> 'a val with_setup :
?after_shutdown:(Opentelemetry.Exporter.t -> unit) ->
?config:Config.t ->
?enable:bool ->
unit ->
(unit -> 'a) ->
'a
(** [with_setup () f] is like [setup(); f()] but takes care of cleaning up after (** [with_setup () f] is like [setup(); f()] but takes care of cleaning up after
[f()] returns See {!setup} for more details. *) [f()] returns See {!setup} for more details. *)