mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-07 18:37:56 -05:00
add to_hex/of_hex for span id/trace id
This commit is contained in:
parent
d982dafa60
commit
8c1d658ab9
1 changed files with 40 additions and 0 deletions
|
|
@ -120,6 +120,38 @@ module Collector = struct
|
||||||
| Some (module B) -> B.rand_bytes_8()
|
| Some (module B) -> B.rand_bytes_8()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Util_ = struct
|
||||||
|
let bytes_to_hex (b:bytes) : string =
|
||||||
|
let i_to_hex (i:int) =
|
||||||
|
if i < 10 then Char.chr (i + Char.code '0')
|
||||||
|
else Char.chr (i - 10 + Char.code 'a')
|
||||||
|
in
|
||||||
|
|
||||||
|
let res = Bytes.create (2 * Bytes.length b) in
|
||||||
|
for i = 0 to Bytes.length b-1 do
|
||||||
|
let n = Char.code (Bytes.get b i) in
|
||||||
|
Bytes.set res (2 * i) (i_to_hex ((n land 0xf0) lsr 4));
|
||||||
|
Bytes.set res (2 * i + 1) (i_to_hex (n land 0x0f));
|
||||||
|
done;
|
||||||
|
Bytes.unsafe_to_string res
|
||||||
|
|
||||||
|
let bytes_of_hex (s:string) : bytes =
|
||||||
|
let n_of_c = function
|
||||||
|
| '0' .. '9' as c -> Char.code c - Char.code '0'
|
||||||
|
| 'a' .. 'f' as c -> 10 + Char.code c - Char.code 'a'
|
||||||
|
| _ -> failwith "invalid hex char"
|
||||||
|
in
|
||||||
|
assert (String.length s mod 2 = 0);
|
||||||
|
let res = Bytes.make (String.length s / 2) '\x00' in
|
||||||
|
for i=0 to String.length s/2-1 do
|
||||||
|
let n1 = n_of_c (String.get s (2*i)) in
|
||||||
|
let n2 = n_of_c (String.get s (2*i+1)) in
|
||||||
|
let n = (n1 lsl 4) lor n2 in
|
||||||
|
Bytes.set res i (Char.chr n)
|
||||||
|
done;
|
||||||
|
res
|
||||||
|
end
|
||||||
|
|
||||||
(** Trace ID.
|
(** Trace ID.
|
||||||
|
|
||||||
This 16 bytes identifier is shared by all spans in one trace. *)
|
This 16 bytes identifier is shared by all spans in one trace. *)
|
||||||
|
|
@ -128,12 +160,16 @@ module Trace_id : sig
|
||||||
val create : unit -> t
|
val create : unit -> t
|
||||||
val to_bytes : t -> bytes
|
val to_bytes : t -> bytes
|
||||||
val of_bytes : bytes -> t
|
val of_bytes : bytes -> t
|
||||||
|
val to_hex : t -> string
|
||||||
|
val of_hex : string -> t
|
||||||
end = struct
|
end = struct
|
||||||
open Proto.Trace
|
open Proto.Trace
|
||||||
type t = bytes
|
type t = bytes
|
||||||
let to_bytes self = self
|
let to_bytes self = self
|
||||||
let create () : t = Collector.rand_bytes_16()
|
let create () : t = Collector.rand_bytes_16()
|
||||||
let of_bytes b = assert(Bytes.length b=16); b
|
let of_bytes b = assert(Bytes.length b=16); b
|
||||||
|
let to_hex self = Util_.bytes_to_hex self
|
||||||
|
let of_hex s = of_bytes (Util_.bytes_of_hex s)
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Unique ID of a span. *)
|
(** Unique ID of a span. *)
|
||||||
|
|
@ -142,12 +178,16 @@ module Span_id : sig
|
||||||
val create : unit -> t
|
val create : unit -> t
|
||||||
val to_bytes : t -> bytes
|
val to_bytes : t -> bytes
|
||||||
val of_bytes : bytes -> t
|
val of_bytes : bytes -> t
|
||||||
|
val to_hex : t -> string
|
||||||
|
val of_hex : string -> t
|
||||||
end = struct
|
end = struct
|
||||||
open Proto.Trace
|
open Proto.Trace
|
||||||
type t = bytes
|
type t = bytes
|
||||||
let to_bytes self = self
|
let to_bytes self = self
|
||||||
let create () : t = Collector.rand_bytes_8()
|
let create () : t = Collector.rand_bytes_8()
|
||||||
let of_bytes b = assert(Bytes.length b=8); b
|
let of_bytes b = assert(Bytes.length b=8); b
|
||||||
|
let to_hex self = Util_.bytes_to_hex self
|
||||||
|
let of_hex s = of_bytes (Util_.bytes_of_hex s)
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Process-wide metadata, environment variables, etc. *)
|
(** Process-wide metadata, environment variables, etc. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue