Skip to content

Commit

Permalink
CP-52744: Update trace_context when starting a context span
Browse files Browse the repository at this point in the history
Refresh the trace_context with the correct traceparent when creating a
span with `start_tracing_helper` in `context.ml`.

This ensures the tracing of the context has the correct parent.
  • Loading branch information
GabrielBuica committed Jan 31, 2025
1 parent 4abfe62 commit 421fabf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ocaml/libs/tracing/tracing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ module Span = struct
{span with status= {status_code; _description}}
| _ ->
span

let with_trace_context span trace_context =
let span_context =
span |> get_context |> SpanContext.with_trace_context trace_context
in
{span with context= span_context}
end

module TraceMap = Map.Make (Trace_id)
Expand Down
2 changes: 2 additions & 0 deletions ocaml/libs/tracing/tracing.mli
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ module Span : sig
val get_end_time : t -> float option

val get_attributes : t -> (string * string) list

val with_trace_context : t -> TraceContext.t -> t
end

module TraceMap : module type of Map.Make (Trace_id)
Expand Down
15 changes: 14 additions & 1 deletion ocaml/xapi/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,20 @@ let start_tracing_helper ?(span_attributes = []) parent_fn task_name =
~parent ()
with
| Ok x ->
x
Option.map
(fun span ->
let traceparent =
span |> Span.get_context |> SpanContext.to_traceparent
in
let trace_context =
span
|> Span.get_context
|> SpanContext.context_of_span_context
|> TraceContext.with_traceparent (Some traceparent)
in
Span.with_trace_context span trace_context
)
x
| Error e ->
R.warn "Failed to start tracing: %s" (Printexc.to_string e) ;
None
Expand Down

0 comments on commit 421fabf

Please sign in to comment.