Skip to content

Commit 502fc45

Browse files
authored
CP-47063: Tracing instrumentation around message-switch send (#6503)
Instrument xenops vm non-atomic functions. Instruments: - `VM.add`, - `VM.stat`, - `VM.exists`, - `VM.list`. Instruments `switch_rpc` according to OpenTelemetry standard on instrumenting rpc calls. - `server.address` is the name of the message queue. Intruments sending the message on a queue according to OpenTelemetry standard on instrumenting messaging. - `destination` is the name of the message queue. `Tracing.with_tracing` now accepts an optional argument to set the Span Kind. ![image](https://github.com/user-attachments/assets/b58a3309-6e31-4e40-84a5-c053bc00d5c8)
2 parents af0769d + 5138375 commit 502fc45

File tree

12 files changed

+94
-39
lines changed

12 files changed

+94
-39
lines changed

ocaml/libs/tracing/tracing.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,14 @@ end
792792
let enable_span_garbage_collector ?(timeout = 86400.) () =
793793
Spans.GC.initialise_thread ~timeout
794794

795-
let with_tracing ?(attributes = []) ?(parent = None) ?trace_context ~name f =
795+
let with_tracing ?(attributes = []) ?(parent = None) ?span_kind ?trace_context
796+
~name f =
796797
let tracer = Tracer.get_tracer ~name in
797798
if tracer.enabled then (
798-
match Tracer.start ~tracer ?trace_context ~attributes ~name ~parent () with
799+
match
800+
Tracer.start ?span_kind ~tracer ?trace_context ~attributes ~name ~parent
801+
()
802+
with
799803
| Ok span -> (
800804
try
801805
let result = f span in

ocaml/libs/tracing/tracing.mli

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ module Tracer : sig
190190
-> (Span.t option, exn) result
191191

192192
val update_span_with_parent : Span.t -> Span.t option -> Span.t option
193-
(**[update_span_with_parent s p] returns [Some span] where [span] is an
193+
(**[update_span_with_parent s p] returns [Some span] where [span] is an
194194
updated verison of the span [s].
195-
If [p] is [Some parent], [span] is a child of [parent], otherwise it is the
195+
If [p] is [Some parent], [span] is a child of [parent], otherwise it is the
196196
original [s].
197-
198-
If the span [s] is finished or is no longer considered an on-going span,
197+
198+
If the span [s] is finished or is no longer considered an on-going span,
199199
returns [None].
200200
*)
201201

@@ -209,7 +209,7 @@ module Tracer : sig
209209
val finished_span_hashtbl_is_empty : unit -> bool
210210
end
211211

212-
(** [TracerProvider] module provides ways to intereact with the tracer providers.
212+
(** [TracerProvider] module provides ways to intereact with the tracer providers.
213213
*)
214214
module TracerProvider : sig
215215
(** Type that represents a tracer provider.*)
@@ -222,7 +222,7 @@ module TracerProvider : sig
222222
-> name_label:string
223223
-> uuid:string
224224
-> unit
225-
(** [create ~enabled ~attributes ~endpoints ~name_label ~uuid] initializes a
225+
(** [create ~enabled ~attributes ~endpoints ~name_label ~uuid] initializes a
226226
tracer provider based on the following parameters: [enabled], [attributes],
227227
[endpoints], [name_label], and [uuid]. *)
228228

@@ -234,17 +234,17 @@ module TracerProvider : sig
234234
-> unit
235235
-> unit
236236
(** [set ?enabled ?attributes ?endpoints ~uuid ()] updates the tracer provider
237-
identified by the given [uuid] with the new configuration paremeters:
238-
[enabled], [attributes], and [endpoints].
239-
237+
identified by the given [uuid] with the new configuration paremeters:
238+
[enabled], [attributes], and [endpoints].
239+
240240
If any of the configuration parameters are
241241
missing, the old ones are kept.
242-
242+
243243
Raises [Failure] if there are no tracer provider with the given [uuid].
244244
*)
245245

246246
val destroy : uuid:string -> unit
247-
(** [destroy ~uuid] destroys the tracer provider with the given [uuid].
247+
(** [destroy ~uuid] destroys the tracer provider with the given [uuid].
248248
If there are no tracer provider with the given [uuid], it does nothing.
249249
*)
250250

@@ -269,6 +269,7 @@ val enable_span_garbage_collector : ?timeout:float -> unit -> unit
269269
val with_tracing :
270270
?attributes:(string * string) list
271271
-> ?parent:Span.t option
272+
-> ?span_kind:SpanKind.t
272273
-> ?trace_context:TraceContext.t
273274
-> name:string
274275
-> (Span.t option -> 'a)
@@ -288,24 +289,24 @@ val get_observe : unit -> bool
288289

289290
val validate_attribute : string * string -> bool
290291

291-
(** [EnvHelpers] module is a helper module for the tracing library to easily
292-
transition back and forth between a string list of environment variables to
293-
a traceparent.
292+
(** [EnvHelpers] module is a helper module for the tracing library to easily
293+
transition back and forth between a string list of environment variables to
294+
a traceparent.
294295
*)
295296
module EnvHelpers : sig
296297
val traceparent_key : string
297298
(** [traceparent_key] is a constant the represents the key of the traceparent
298-
environment variable.
299+
environment variable.
299300
*)
300301

301302
val of_traceparent : string option -> string list
302303
(** [of_traceparent traceparent_opt] returns a singleton list consisting of a
303-
envirentment variable with the key [traceparent_key] and value [v] if
304+
envirentment variable with the key [traceparent_key] and value [v] if
304305
[traceparent_opt] is [Some v]. Otherwise, returns an empty list. *)
305306

306307
val to_traceparent : string list -> string option
307-
(** [to_traceparent env_var_lst] returns [Some v] where v is the value of the
308-
environmental variable coresponding to the key [traceparent_key] from a
308+
(** [to_traceparent env_var_lst] returns [Some v] where v is the value of the
309+
environmental variable coresponding to the key [traceparent_key] from a
309310
string list of environmental variables [env_var_lst]. If there is no such
310311
evironmental variable in the list, it returns [None].
311312
*)
@@ -314,7 +315,7 @@ module EnvHelpers : sig
314315
(** [of_span span] returns a singleton list consisting of a
315316
envirentment variable with the key [traceparent_key] and value [v], where
316317
[v] is traceparent representation of span [s] (if [span] is [Some s]).
317-
318+
318319
If [span] is [None], it returns an empty list.
319320
*)
320321
end

ocaml/libs/tracing/tracing_export.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ module Destination = struct
278278
]
279279
in
280280
let@ _ =
281-
with_tracing ~trace_context:TraceContext.empty ~parent ~attributes
282-
~name
281+
with_tracing ~span_kind:Server ~trace_context:TraceContext.empty
282+
~parent ~attributes ~name
283283
in
284284
all_spans
285285
|> Content.Json.ZipkinV2.content_of
@@ -293,8 +293,8 @@ module Destination = struct
293293
let ((_span_list, span_count) as span_info) = Spans.since () in
294294
let attributes = [("export.traces.count", string_of_int span_count)] in
295295
let@ parent =
296-
with_tracing ~trace_context:TraceContext.empty ~parent:None ~attributes
297-
~name:"Tracing.flush_spans"
296+
with_tracing ~span_kind:Server ~trace_context:TraceContext.empty
297+
~parent:None ~attributes ~name:"Tracing.flush_spans"
298298
in
299299
TracerProvider.get_tracer_providers ()
300300
|> List.filter TracerProvider.get_enabled

ocaml/message-switch/core/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
sexplib
1010
sexplib0
1111
threads.posix
12+
tracing
1213
uri
1314
xapi-log
1415
xapi-stdext-threads

ocaml/message-switch/core/make.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ functor
229229
in
230230
return (Ok t)
231231

232-
let rpc ~t ~queue ?timeout ~body:x () =
232+
let rpc ?_span_parent ~t ~queue ?timeout ~body:x () =
233233
let ivar = M.Ivar.create () in
234234
let timer =
235235
Option.map

ocaml/message-switch/core/s.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ module type CLIENT = sig
144144
(** [disconnect] closes the connection *)
145145

146146
val rpc :
147-
t:t
147+
?_span_parent:Tracing.Span.t
148+
-> t:t
148149
-> queue:string
149150
-> ?timeout:int
150151
-> body:string

ocaml/message-switch/unix/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
rpclib.core
1212
rpclib.json
1313
threads.posix
14+
tracing
1415
xapi-stdext-threads
1516
xapi-stdext-unix
1617
)

ocaml/message-switch/unix/protocol_unix.ml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ module Client = struct
347347
Ok c'
348348
)
349349

350-
let rpc ~t:c ~queue:dest_queue_name ?timeout ~body:x () =
350+
let rpc ?_span_parent ~t:c ~queue:dest_queue_name ?timeout ~body:x () =
351351
let t = Ivar.create () in
352352
let timer =
353353
Option.map
@@ -364,9 +364,23 @@ module Client = struct
364364
do_rpc c.requests_conn (In.CreatePersistent dest_queue_name)
365365
>>|= fun (_ : string) ->
366366
let msg =
367-
In.Send
368-
( dest_queue_name
369-
, {Message.payload= x; kind= Message.Request c.reply_queue_name}
367+
Tracing.with_tracing
368+
~attributes:
369+
[
370+
("messaging.operation.name", "send")
371+
; ("messaging.system", "message-switch")
372+
; ("messaging.destination.name", dest_queue_name)
373+
]
374+
~span_kind:Producer ~parent:_span_parent
375+
~name:("send" ^ " " ^ dest_queue_name)
376+
(fun _ ->
377+
In.Send
378+
( dest_queue_name
379+
, {
380+
Message.payload= x
381+
; kind= Message.Request c.reply_queue_name
382+
}
383+
)
370384
)
371385
in
372386
do_rpc c.requests_conn msg >>|= fun (id : string) ->

ocaml/xapi-idl/lib/debug_info.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let to_log_string t = t.log
7676

7777
(* Sets the logging context based on `dbg`.
7878
Also adds a new tracing span, linked to the parent span from `dbg`, if available. *)
79-
let with_dbg ?(with_thread = false) ~module_name ~name ~dbg f =
79+
let with_dbg ?(with_thread = false) ?(module_name = "") ~name ~dbg f =
8080
let di = of_string dbg in
8181
let f_with_trace () =
8282
let name =

ocaml/xapi-idl/lib/debug_info.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ val to_log_string : t -> string
2424

2525
val with_dbg :
2626
?with_thread:bool
27-
-> module_name:string
27+
-> ?module_name:string
2828
-> name:string
2929
-> dbg:string
3030
-> (t -> 'a)

ocaml/xapi-idl/lib/xcp_client.ml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,35 @@ let switch_rpc ?timeout queue_name string_of_call response_of_string =
3838
get_ok
3939
(Message_switch_unix.Protocol_unix.Client.connect ~switch:!switch_path ())
4040
in
41-
fun call ->
41+
fun (call : Rpc.call) ->
42+
let _span_parent =
43+
call.params
44+
|> List.find_map (function Rpc.Dict kv_list -> Some kv_list | _ -> None)
45+
|> Fun.flip Option.bind
46+
(List.find_map (function
47+
| "debug_info", Rpc.String debug_info ->
48+
let di = debug_info |> Debug_info.of_string in
49+
di.tracing
50+
| _ ->
51+
None
52+
)
53+
)
54+
in
55+
let rpc_service = "message_switch" in
56+
Tracing.with_tracing
57+
~attributes:
58+
[
59+
("rpc.system", "ocaml-rpc")
60+
; ("rpc.service", rpc_service)
61+
; ("server.address", queue_name)
62+
; ("rpc.method", call.name)
63+
]
64+
~parent:_span_parent
65+
~name:(rpc_service ^ "/" ^ call.name)
66+
@@ fun _span_parent ->
4267
response_of_string
4368
(get_ok
44-
(Message_switch_unix.Protocol_unix.Client.rpc ~t ?timeout
69+
(Message_switch_unix.Protocol_unix.Client.rpc ?_span_parent ~t ?timeout
4570
~queue:queue_name ~body:(string_of_call call) ()
4671
)
4772
)

ocaml/xenopsd/lib/xenops_server.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3682,7 +3682,9 @@ end
36823682
module VM = struct
36833683
module DB = VM_DB
36843684

3685-
let add _ dbg x = Debug.with_thread_associated dbg (fun () -> DB.add' x) ()
3685+
let add _ dbg x =
3686+
Debug_info.with_dbg ~with_thread:true ~name:__FUNCTION__ ~dbg @@ fun _ ->
3687+
DB.add' x
36863688

36873689
let rename _ dbg id1 id2 when' =
36883690
queue_operation dbg id1 (Atomic (VM_rename (id1, id2, when')))
@@ -3719,11 +3721,17 @@ module VM = struct
37193721
in
37203722
(vm_t, state)
37213723

3722-
let stat _ dbg id = Debug.with_thread_associated dbg (fun () -> stat' id) ()
3724+
let stat _ dbg id =
3725+
Debug_info.with_dbg ~with_thread:true ~name:__FUNCTION__ ~dbg @@ fun _ ->
3726+
stat' id
37233727

3724-
let exists _ _dbg id = match DB.read id with Some _ -> true | None -> false
3728+
let exists _ dbg id =
3729+
Debug_info.with_dbg ~name:__FUNCTION__ ~dbg @@ fun _ ->
3730+
match DB.read id with Some _ -> true | None -> false
37253731

3726-
let list _ dbg () = Debug.with_thread_associated dbg (fun () -> DB.list ()) ()
3732+
let list _ dbg () =
3733+
Debug_info.with_dbg ~with_thread:true ~name:__FUNCTION__ ~dbg @@ fun _ ->
3734+
DB.list ()
37273735

37283736
let create _ dbg id =
37293737
let no_sharept = false in

0 commit comments

Comments
 (0)