diff --git a/src/client/exporter_stdout.ml b/src/client/exporter_stdout.ml index 551a5f4f..b65482e6 100644 --- a/src/client/exporter_stdout.ml +++ b/src/client/exporter_stdout.ml @@ -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 diff --git a/src/client/generic_consumer_exporter.ml b/src/client/generic_consumer_exporter.ml index cbbae6eb..790462b0 100644 --- a/src/client/generic_consumer_exporter.ml +++ b/src/client/generic_consumer_exporter.ml @@ -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 diff --git a/src/client/bounded_queue_sync.ml b/src/client/sync/bounded_queue_sync.ml similarity index 100% rename from src/client/bounded_queue_sync.ml rename to src/client/sync/bounded_queue_sync.ml diff --git a/src/client/bounded_queue_sync.mli b/src/client/sync/bounded_queue_sync.mli similarity index 100% rename from src/client/bounded_queue_sync.mli rename to src/client/sync/bounded_queue_sync.mli diff --git a/src/client/sync/common_.ml b/src/client/sync/common_.ml new file mode 100644 index 00000000..6a337b5c --- /dev/null +++ b/src/client/sync/common_.ml @@ -0,0 +1 @@ +module OTEL = Opentelemetry diff --git a/src/client/sync/dune b/src/client/sync/dune new file mode 100644 index 00000000..d9bd0731 --- /dev/null +++ b/src/client/sync/dune @@ -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")) diff --git a/src/client/io_sync.ml b/src/client/sync/io_sync.ml similarity index 100% rename from src/client/io_sync.ml rename to src/client/sync/io_sync.ml diff --git a/src/client/io_sync.mli b/src/client/sync/io_sync.mli similarity index 100% rename from src/client/io_sync.mli rename to src/client/sync/io_sync.mli diff --git a/src/client/notifier_sync.ml b/src/client/sync/notifier_sync.ml similarity index 100% rename from src/client/notifier_sync.ml rename to src/client/sync/notifier_sync.ml diff --git a/src/client/notifier_sync.mli b/src/client/sync/notifier_sync.mli similarity index 100% rename from src/client/notifier_sync.mli rename to src/client/sync/notifier_sync.mli diff --git a/src/client/sync/shutdown_sync.ml b/src/client/sync/shutdown_sync.ml new file mode 100644 index 00000000..97ab5c2f --- /dev/null +++ b/src/client/sync/shutdown_sync.ml @@ -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 ()) diff --git a/src/client/sync_queue.ml b/src/client/sync/sync_queue.ml similarity index 100% rename from src/client/sync_queue.ml rename to src/client/sync/sync_queue.ml diff --git a/src/client/sync_queue.mli b/src/client/sync/sync_queue.mli similarity index 100% rename from src/client/sync_queue.mli rename to src/client/sync/sync_queue.mli diff --git a/src/client/util_thread.ml b/src/client/sync/util_thread.ml similarity index 100% rename from src/client/util_thread.ml rename to src/client/sync/util_thread.ml