diff --git a/src/core/clock.ml b/src/core/clock.ml index a9282522..473af440 100644 --- a/src/core/clock.ml +++ b/src/core/clock.ml @@ -5,13 +5,25 @@ type t = { now: unit -> Timestamp_ns.t } [@@unboxed] let[@inline] now (self : t) : Timestamp_ns.t = self.now () -(** Clock using {!Unix.gettimeofday} *) -let unix : t = - { now = (fun () -> Int64.of_float (Unix.gettimeofday () *. 1e9)) } +open struct + module TS = Timestamp_ns + + let ns_in_a_day = Int64.(mul 1_000_000_000L (of_int (24 * 3600))) + + (** Current unix timestamp in nanoseconds *) + let[@inline] now_ptime_ () : TS.t = + let d, ps = Ptime_clock.now_d_ps () in + let d = Int64.(mul (of_int d) ns_in_a_day) in + let ns = Int64.(div ps 1_000L) in + Int64.(add d ns) +end + +(** Clock that uses ptime. *) +let ptime_clock : t = { now = now_ptime_ } module Main = struct open struct - let main : t Atomic.t = Atomic.make unix + let main : t Atomic.t = Atomic.make ptime_clock end let[@inline] get () = Atomic.get main diff --git a/src/core/dune b/src/core/dune index 5b1001ea..1c0ae1ab 100644 --- a/src/core/dune +++ b/src/core/dune @@ -10,6 +10,7 @@ (re_export opentelemetry.emitter) pbrt threads - unix + ptime + ptime.clock.os mtime hmap)) diff --git a/src/ptime/dune b/src/ptime/dune deleted file mode 100644 index 8dfe6bd8..00000000 --- a/src/ptime/dune +++ /dev/null @@ -1,5 +0,0 @@ -(library - (name opentelemetry_ptime) - (public_name opentelemetry.ptime) - (synopsis "clock for opentelemetry using ptime") - (libraries opentelemetry.core ptime ptime.clock.os)) diff --git a/src/ptime/opentelemetry_ptime.ml b/src/ptime/opentelemetry_ptime.ml deleted file mode 100644 index 1862498f..00000000 --- a/src/ptime/opentelemetry_ptime.ml +++ /dev/null @@ -1,32 +0,0 @@ -open Opentelemetry_core - -open struct - module TS = Timestamp_ns - - let ns_in_a_day = Int64.(mul 1_000_000_000L (of_int (24 * 3600))) -end - -(** Current unix timestamp in nanoseconds *) -let[@inline] now_unix_ns () : TS.t = - let span = Ptime_clock.now () |> Ptime.to_span in - let d, ps = Ptime.Span.to_d_ps span in - let d = Int64.(mul (of_int d) ns_in_a_day) in - let ns = Int64.(div ps 1_000L) in - Int64.(add d ns) - -let clock : Clock.t = { now = now_unix_ns } - -(** Nicer pretty-printer *) -let pp_debug out (self : TS.t) = - let d = Int64.(to_int (div self ns_in_a_day)) in - let ns = Int64.(rem self ns_in_a_day) in - let ps = Int64.(mul ns 1_000L) in - match Ptime.Span.of_d_ps (d, ps) with - | None -> Format.fprintf out "ts: <%Ld ns>" self - | Some span -> - (match Ptime.add_span Ptime.epoch span with - | None -> Format.fprintf out "ts: <%Ld ns>" self - | Some ptime -> Ptime.pp_human () out ptime) - -(** Install as main clock. *) -let set_as_main () = Clock.Main.set clock diff --git a/src/util/dune b/src/util/dune index 70a6f758..dcb8951b 100644 --- a/src/util/dune +++ b/src/util/dune @@ -4,5 +4,6 @@ (flags :standard -open Opentelemetry_atomic) (libraries (re_export opentelemetry.atomic) + ptime opentelemetry.domain) (synopsis "Basic utilities for opentelemetry")) diff --git a/src/util/timestamp_ns.ml b/src/util/timestamp_ns.ml index 73e4f3ef..5a8eae63 100644 --- a/src/util/timestamp_ns.ml +++ b/src/util/timestamp_ns.ml @@ -5,4 +5,17 @@ type t = int64 -let pp_debug out (self : t) = Format.fprintf out "" self +open struct + let ns_in_a_day = Int64.(mul 1_000_000_000L (of_int (24 * 3600))) +end + +let pp_debug out (self : t) = + let d = Int64.(to_int (div self ns_in_a_day)) in + let ns = Int64.(rem self ns_in_a_day) in + let ps = Int64.(mul ns 1_000L) in + match Ptime.Span.of_d_ps (d, ps) with + | None -> Format.fprintf out "ts: <%Ld ns>" self + | Some span -> + (match Ptime.add_span Ptime.epoch span with + | None -> Format.fprintf out "ts: <%Ld ns>" self + | Some ptime -> Ptime.pp_human () out ptime)