update otel trace

This commit is contained in:
Simon Cruanes 2025-12-17 11:19:16 -05:00
parent dd2fe8fc52
commit ec584b4829
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -43,10 +43,11 @@ module Internal = struct
type state = { type state = {
tbl: span_begin Active_span_tbl.t; tbl: span_begin Active_span_tbl.t;
span_gen: int Atomic.t; span_gen: int Atomic.t;
clock: Opentelemetry_core.Clock.t;
} }
let create_state () : state = let create_state ~clock () : state =
{ tbl = Active_span_tbl.create (); span_gen = Atomic.make 0 } { tbl = Active_span_tbl.create (); span_gen = Atomic.make 0; clock }
(* sanity check: otrace meta-map must be the same as hmap *) (* sanity check: otrace meta-map must be the same as hmap *)
let () = ignore (fun (k : _ Hmap.key) : _ Otrace.Meta_map.key -> k) let () = ignore (fun (k : _ Hmap.key) : _ Otrace.Meta_map.key -> k)
@ -82,11 +83,11 @@ module Internal = struct
let open Opt_syntax in let open Opt_syntax in
match explicit_parent with match explicit_parent with
| Some p -> | Some p ->
let trace_id = Otrace.Meta_map.find k_trace_id p.meta in let trace_id = Otrace.Meta_map.find OTEL.Trace_id.k_trace_id p.meta in
let span_id = let span_id =
Otrace.Meta_map.find k_span_otrace p.meta >|= OTEL.Span.id Otrace.Meta_map.find k_span_otrace p.meta >|= OTEL.Span.id
in in
let span_ctx = Otrace.Meta_map.find k_span_ctx p.meta in let span_ctx = Otrace.Meta_map.find OTEL.Span_ctx.k_span_ctx p.meta in
( trace_id <?> (span_ctx >|= OTEL.Span_ctx.trace_id), ( trace_id <?> (span_ctx >|= OTEL.Span_ctx.trace_id),
span_id <?> (span_ctx >|= OTEL.Span_ctx.parent_id) ) span_id <?> (span_ctx >|= OTEL.Span_ctx.parent_id) )
| None -> None, None | None -> None, None
@ -118,7 +119,7 @@ module Internal = struct
:: data :: data
in in
let start_time = Timestamp_ns.now_unix_ns () in let start_time = Clock.now self.clock in
let span : OTEL.Span.t = let span : OTEL.Span.t =
OTEL.Span.make ?parent:parent_id ~trace_id ~id:otel_id ~attrs name OTEL.Span.make ?parent:parent_id ~trace_id ~id:otel_id ~attrs name
~start_time ~end_time:start_time ~start_time ~end_time:start_time
@ -144,17 +145,17 @@ module Internal = struct
Active_span_tbl.add self.tbl otrace_span sb; Active_span_tbl.add self.tbl otrace_span sb;
sb sb
let exit_span_ { span } : OTEL.Span.t = let exit_span_ self { span } : OTEL.Span.t =
let open OTEL in let open OTEL in
if Span.is_not_dummy span then ( if Span.is_not_dummy span then (
let end_time = Timestamp_ns.now_unix_ns () in let end_time = Clock.now self.clock in
Proto.Trace.span_set_end_time_unix_nano span end_time Proto.Trace.span_set_end_time_unix_nano span end_time
); );
span span
let exit_span' (self : state) otrace_id otel_span_begin = let exit_span' (self : state) otrace_id otel_span_begin =
Active_span_tbl.remove self.tbl otrace_id; Active_span_tbl.remove self.tbl otrace_id;
exit_span_ otel_span_begin exit_span_ self otel_span_begin
(** Find the OTEL span corresponding to this Trace span *) (** Find the OTEL span corresponding to this Trace span *)
let exit_span_from_id (self : state) otrace_id = let exit_span_from_id (self : state) otrace_id =
@ -162,7 +163,7 @@ module Internal = struct
| exception Not_found -> None | exception Not_found -> None
| otel_span_begin -> | otel_span_begin ->
Active_span_tbl.remove self.tbl otrace_id; Active_span_tbl.remove self.tbl otrace_id;
Some (exit_span_ otel_span_begin) Some (exit_span_ self otel_span_begin)
end end
module type COLLECTOR_ARG = sig module type COLLECTOR_ARG = sig
@ -174,7 +175,7 @@ module Make_collector (A : COLLECTOR_ARG) = struct
let exporter = A.exporter let exporter = A.exporter
let state = create_state () let state = create_state ~clock:exporter.clock ()
(* NOTE: perf: it would be interesting to keep the "current (OTEL) span" in (* NOTE: perf: it would be interesting to keep the "current (OTEL) span" in
local storage/ambient-context, to accelerate most span-modifying local storage/ambient-context, to accelerate most span-modifying
@ -301,7 +302,10 @@ module Make_collector (A : COLLECTOR_ARG) = struct
in in
let span_id = Opt_syntax.(span_id_from_parent <?> span_id_from_ambient) in let span_id = Opt_syntax.(span_id_from_parent <?> span_id_from_ambient) in
let log = OTEL.Log_record.make_str ?trace_id ?span_id msg in let log =
let observed_time_unix_nano = OTEL.Clock.now exporter.clock in
OTEL.Log_record.make_str ~observed_time_unix_nano ?trace_id ?span_id msg
in
OTEL.Exporter.send_logs exporter [ log ] OTEL.Exporter.send_logs exporter [ log ]
let shutdown () = () let shutdown () = ()