Skip to content

Commit 8eb47f7

Browse files
authored
CP-53843: tracing -- link internal subtasks traces (#6991)
`exec_with_new_*` function create orphaned traces for subtasks with parents not in the database. This is meant to address this and link these spans to their correct parent.
2 parents d2e5f32 + e43bdb0 commit 8eb47f7

5 files changed

Lines changed: 29 additions & 12 deletions

File tree

ocaml/xapi-idl/lib/debug_info.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ let with_dbg ?attributes ?(with_thread = false) ?(module_name = "") ~name ~dbg f
9393
| false ->
9494
f_with_trace ()
9595

96+
let span_of di = di.tracing
97+
9698
let traceparent_of_dbg dbg =
9799
match String.split_on_char separator dbg with
98100
| [_; trace_context] -> (

ocaml/xapi-idl/lib/debug_info.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ val with_dbg :
3131
-> (t -> 'a)
3232
-> 'a
3333

34+
val span_of : t -> Tracing.Span.t option
35+
3436
val traceparent_of_dbg : string -> string option

ocaml/xapi/context.ml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ module D = Debug.Make (struct let name = "dummytaskhelper" end)
1818
(** Every operation has an origin: either the HTTP connection it came from or
1919
an internal subsystem (eg synchroniser thread / event handler
2020
thread) *)
21-
type origin = Http of Http.Request.t * Unix.file_descr | Internal
21+
type origin =
22+
| Http of Http.Request.t * Unix.file_descr
23+
| Internal
24+
| Internal_Traced of Tracing.Span.t option
2225

2326
let string_of_origin = function
2427
| Http (req, fd) ->
@@ -32,7 +35,7 @@ let string_of_origin = function
3235
(* unfortunately all connections come from stunnel on localhost *)
3336
Printf.sprintf "HTTP request from %s with User-Agent: %s" peer
3437
(Option.value ~default:"unknown" req.Http.Request.user_agent)
35-
| Internal ->
38+
| Internal | Internal_Traced _ ->
3639
"Internal"
3740

3841
(** A Context is used to represent every API invocation. It may be extended
@@ -105,7 +108,7 @@ let default_database () =
105108

106109
let preauth ~__context =
107110
match __context.origin with
108-
| Internal ->
111+
| Internal | Internal_Traced _ ->
109112
None
110113
| Http (_, s) -> (
111114
match Unix.getsockname s with
@@ -203,7 +206,7 @@ let trackid ?(with_brackets = false) ?(prefix = "") __context =
203206
trackid_of_session ~with_brackets ~prefix __context.session_id
204207

205208
let _client_of_origin = function
206-
| Internal ->
209+
| Internal | Internal_Traced _ ->
207210
None
208211
| Http (req, fd) ->
209212
Http_svr.client_of_req_and_fd req fd
@@ -233,7 +236,9 @@ let parent_of_origin (origin : origin) span_name =
233236
let* span_context = SpanContext.of_trace_context context in
234237
let span = Tracer.span_of_span_context span_context span_name in
235238
Some span
236-
| _ ->
239+
| Internal_Traced span ->
240+
span
241+
| Internal ->
237242
None
238243

239244
let attribute_helper_fn f v = Option.fold ~none:[] ~some:f v
@@ -312,7 +317,7 @@ let make_attributes ?task_name ?task_id ?task_uuid ?session_id ?origin () =
312317
; attribute_helper_fn
313318
(fun origin ->
314319
match origin with
315-
| Internal ->
320+
| Internal | Internal_Traced _ ->
316321
[("xs.xapi.task.origin", "internal")]
317322
| Http (req, s) ->
318323
[attr_of_req req; attr_of_fd s] |> List.concat
@@ -518,7 +523,11 @@ let get_client_ip context =
518523
context.client |> Option.map (fun (_, ip) -> Ipaddr.to_string ip)
519524

520525
let get_user_agent context =
521-
match context.origin with Internal -> None | Http (rq, _) -> rq.user_agent
526+
match context.origin with
527+
| Internal | Internal_Traced _ ->
528+
None
529+
| Http (rq, _) ->
530+
rq.user_agent
522531

523532
let finally_destroy_context ~__context f =
524533
let tracing = __context.tracing in

ocaml/xapi/context.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
to include extra data without changing all the autogenerated signatures *)
1717
type t
1818

19-
type origin = Http of Http.Request.t * Unix.file_descr | Internal
19+
type origin =
20+
| Http of Http.Request.t * Unix.file_descr
21+
| Internal
22+
| Internal_Traced of Tracing.Span.t option
2023

2124
(** {6 Constructors} *)
2225

ocaml/xapi/sm_exec.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,9 @@ let methodResponse xml =
320320
(****************************************************************************************)
321321
(* Functions that actually execute the python backends *)
322322

323-
let with_session sr f =
324-
Server_helpers.exec_with_new_task "sm_exec" (fun __context ->
323+
let with_session ~traceparent sr f =
324+
Server_helpers.exec_with_new_task "sm_exec"
325+
~origin:(Internal_Traced traceparent) (fun __context ->
325326
let create_session () =
326327
let host = !Xapi_globs.localhost_ref in
327328
let session =
@@ -466,8 +467,8 @@ let exec_xmlrpc ~dbg ?context:_ ?(needs_session = true) (driver : string)
466467
)
467468
in
468469
if needs_session then
469-
with_session call.sr_ref (fun session_id ->
470-
do_call {call with session_ref= Some session_id}
470+
with_session ~traceparent:(Debug_info.span_of di) call.sr_ref
471+
(fun session_id -> do_call {call with session_ref= Some session_id}
471472
)
472473
else
473474
do_call call

0 commit comments

Comments
 (0)