mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-07 18:37:56 -05:00
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:
parent
e2c4a4e680
commit
43cd3aa230
6 changed files with 33 additions and 43 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
(re_export opentelemetry.emitter)
|
||||
pbrt
|
||||
threads
|
||||
unix
|
||||
ptime
|
||||
ptime.clock.os
|
||||
mtime
|
||||
hmap))
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
@ -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
|
||||
|
|
@ -4,5 +4,6 @@
|
|||
(flags :standard -open Opentelemetry_atomic)
|
||||
(libraries
|
||||
(re_export opentelemetry.atomic)
|
||||
ptime
|
||||
opentelemetry.domain)
|
||||
(synopsis "Basic utilities for opentelemetry"))
|
||||
|
|
|
|||
|
|
@ -5,4 +5,17 @@
|
|||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue