merge back ptime clock into Clock, make it default

we already depend on ptime in the same package and it's not a big cost.
This commit is contained in:
Simon Cruanes 2025-12-17 11:54:30 -05:00
parent e2c4a4e680
commit 43cd3aa230
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
6 changed files with 33 additions and 43 deletions

View file

@ -5,13 +5,25 @@ type t = { now: unit -> Timestamp_ns.t } [@@unboxed]
let[@inline] now (self : t) : Timestamp_ns.t = self.now () let[@inline] now (self : t) : Timestamp_ns.t = self.now ()
(** Clock using {!Unix.gettimeofday} *) open struct
let unix : t = module TS = Timestamp_ns
{ now = (fun () -> Int64.of_float (Unix.gettimeofday () *. 1e9)) }
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 module Main = struct
open struct open struct
let main : t Atomic.t = Atomic.make unix let main : t Atomic.t = Atomic.make ptime_clock
end end
let[@inline] get () = Atomic.get main let[@inline] get () = Atomic.get main

View file

@ -10,6 +10,7 @@
(re_export opentelemetry.emitter) (re_export opentelemetry.emitter)
pbrt pbrt
threads threads
unix ptime
ptime.clock.os
mtime mtime
hmap)) hmap))

View file

@ -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))

View file

@ -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

View file

@ -4,5 +4,6 @@
(flags :standard -open Opentelemetry_atomic) (flags :standard -open Opentelemetry_atomic)
(libraries (libraries
(re_export opentelemetry.atomic) (re_export opentelemetry.atomic)
ptime
opentelemetry.domain) opentelemetry.domain)
(synopsis "Basic utilities for opentelemetry")) (synopsis "Basic utilities for opentelemetry"))

View file

@ -5,4 +5,17 @@
type t = int64 type t = int64
let pp_debug out (self : t) = Format.fprintf out "<timestamp: %Ld ns>" 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)