mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-08 03:47:59 -04:00
add clock
This commit is contained in:
parent
a954deb46d
commit
a3e1fcc362
1 changed files with 26 additions and 0 deletions
26
src/core/clock.ml
Normal file
26
src/core/clock.ml
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
open Opentelemetry_atomic
|
||||
|
||||
type t = { now: unit -> Timestamp_ns.t } [@@unboxed]
|
||||
(** A clock: can get the current timestamp, with nanoseconds precision *)
|
||||
|
||||
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)) }
|
||||
|
||||
module Main = struct
|
||||
open struct
|
||||
let main : t Atomic.t = Atomic.make unix
|
||||
end
|
||||
|
||||
let[@inline] get () = Atomic.get main
|
||||
|
||||
let set t : unit = Util_atomic.update_cas main (fun _ -> (), t)
|
||||
|
||||
(** Clock that always defers to the current main clock *)
|
||||
let dynamic_main : t = { now = (fun () -> now (get ())) }
|
||||
end
|
||||
|
||||
(** Timestamp using the main clock *)
|
||||
let[@inline] now_main () = now (Main.get ())
|
||||
Loading…
Add table
Reference in a new issue