client: split opentelemetry-client.sync off of main client library

that's the place for the synchronous primitives.
This commit is contained in:
Simon Cruanes 2025-12-17 15:06:21 -05:00
parent 62cd8c0cd2
commit e4177c2843
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
14 changed files with 49 additions and 5 deletions

View file

@ -7,13 +7,17 @@ open struct
let pp_span out (sp : OTEL.Span.t) =
let open OTEL in
Format.fprintf out
"@[<2>SPAN {@ trace_id: %a@ span_id: %a@ name: %S@ start: %a@ end: %a@]}"
"@[<2>SPAN {@ trace_id: %a@ span_id: %a@ name: %S@ start: %a@ end: %a@ \
dur: %.6fs@]}"
Trace_id.pp
(Trace_id.of_bytes sp.trace_id)
Span_id.pp
(Span_id.of_bytes sp.span_id)
sp.name Timestamp_ns.pp_debug sp.start_time_unix_nano
Timestamp_ns.pp_debug sp.end_time_unix_nano
((Int64.to_float sp.end_time_unix_nano
-. Int64.to_float sp.start_time_unix_nano)
/. 1e9)
let pp_log out l =
Format.fprintf out "@[<2>LOG %a@]" Proto.Logs.pp_log_record l
@ -34,7 +38,7 @@ open struct
)
end
let stdout ?(clock = OTEL.Clock.Main.dynamic_main) () : OTEL.Exporter.t =
let stdout ?(clock = OTEL.Clock.ptime_clock) () : OTEL.Exporter.t =
let open Opentelemetry_util in
let out = Format.std_formatter in
let mutex = Mutex.create () in

View file

@ -52,10 +52,12 @@ end = struct
let tick (self : state) = Notifier.trigger self.notify
(** Shutdown one worker, when the queue is closed *)
(** Shutdown worker *)
let shutdown_worker (self : state) : unit =
(* we were the last worker *)
(* Printf.eprintf "worker %d: last one!\n%!" tid; *)
(* only one worker, so, turn off exporter *)
OTEL.Exporter.shutdown self.exp;
(* and we are shut down! *)
Atomic.set self.status Stopped;
Aswitch.turn_off self.active_trigger

View file

@ -0,0 +1 @@
module OTEL = Opentelemetry

23
src/client/sync/dune Normal file
View file

@ -0,0 +1,23 @@
(library
(name opentelemetry_client_sync)
(public_name opentelemetry-client.sync)
(flags
:standard
-open
Opentelemetry_util
-open
Opentelemetry_client
-open
Opentelemetry_atomic)
(libraries
opentelemetry.util
opentelemetry.atomic
opentelemetry.emitter
(re_export opentelemetry.core)
(re_export opentelemetry)
(re_export opentelemetry-client)
(re_export threads)
mtime
mtime.clock.os
unix)
(synopsis "Synchronous/threading-related helpers for opentelemetry-client"))

View file

@ -0,0 +1,14 @@
open Common_
(** Shutdown this exporter and block the thread until it's done.
{b NOTE}: this might deadlock if the exporter runs entirely in the current
thread! *)
let shutdown (exp : OTEL.Exporter.t) : unit =
let q = Sync_queue.create () in
OTEL.Exporter.on_stop exp (Sync_queue.push q);
OTEL.Exporter.shutdown exp;
Sync_queue.pop q
(** Shutdown main exporter and wait *)
let shutdown_main () : unit = Option.iter shutdown (OTEL.Main_exporter.get ())