Skip to content

Commit 82064d3

Browse files
authored
CP-53161: Baggage threaded through smapi and back into xapi (#6278)
It continues the path of threading the `trace_context` across different components. Specifically, from `xapi` to `smapi` and back to `xapi`. Additional improvement: the tracecontext is passed even when `smapi` component is experimental and it does not create spans. Therefore, it is not creating orphaned spans from http requests made to xapi. BVT+BST: 213297
2 parents e5c2612 + 87a4a5a commit 82064d3

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

ocaml/libs/tracing/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(library
22
(name tracing)
33
(modules tracing)
4-
(libraries re uri yojson xapi-log xapi-stdext-threads threads.posix)
4+
(libraries astring re uri yojson xapi-log xapi-stdext-threads threads.posix)
55
(preprocess
66
(pps ppx_deriving_yojson))
77
(public_name xapi-tracing))
@@ -33,7 +33,7 @@
3333
(library
3434
(name tracing_propagator)
3535
(modules propagator)
36-
(libraries astring http-lib tracing))
36+
(libraries http-lib tracing))
3737

3838
(test
3939
(name test_tracing)

ocaml/libs/tracing/propagator.ml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,11 @@ module Http = struct
5454
| xs ->
5555
Some xs
5656

57-
let parse input =
58-
let open Astring.String in
59-
let trim_pair (key, value) = (trim key, trim value) in
60-
input
61-
|> cuts ~sep:";"
62-
|> List.map (cut ~sep:"=" >> Option.map trim_pair)
63-
|> List.filter_map Fun.id
64-
6557
let inject_into ctx req =
6658
let open Tracing in
6759
let traceparent = (hdr_traceparent, TraceContext.traceparent_of ctx) in
6860
let baggage =
69-
let encoded =
70-
let encode =
71-
List.map (fun (k, v) -> Printf.sprintf "%s=%s" k v)
72-
>> String.concat ";"
73-
in
74-
TraceContext.baggage_of ctx |> Option.map encode
75-
in
61+
let encoded = TraceContext.encode_baggage ctx in
7662
(hdr_baggage, encoded)
7763
in
7864
let entries = [traceparent; baggage] in
@@ -102,7 +88,7 @@ module Http = struct
10288
let traceparent = List.assoc_opt hdr_traceparent headers in
10389
let baggage =
10490
let* all = alloc_assoc hdr_baggage headers in
105-
Some (List.concat_map parse all)
91+
Some (List.concat_map TraceContext.parse all)
10692
in
10793
let open TraceContext in
10894
empty |> maybe with_traceparent traceparent |> maybe with_baggage baggage

ocaml/libs/tracing/tracing.ml

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ open D
1818

1919
let fail fmt = Printf.ksprintf failwith fmt
2020

21+
let ( >> ) f g x = g (f x)
22+
2123
let failures = Atomic.make 0
2224

2325
let not_throttled () =
@@ -228,6 +230,20 @@ module TraceContext = struct
228230

229231
let baggage_of ctx = ctx.baggage
230232

233+
let parse input =
234+
let open Astring.String in
235+
let trim_pair (key, value) = (trim key, trim value) in
236+
input
237+
|> cuts ~sep:";"
238+
|> List.map (cut ~sep:"=" >> Option.map trim_pair)
239+
|> List.filter_map Fun.id
240+
241+
let encode_baggage ctx =
242+
let encode =
243+
List.map (fun (k, v) -> Printf.sprintf "%s=%s" k v) >> String.concat ";"
244+
in
245+
ctx |> baggage_of |> Option.map encode
246+
231247
let to_json_string t = Yojson.Safe.to_string (to_yojson t)
232248

233249
let of_json_string s = of_yojson (Yojson.Safe.from_string s)
@@ -810,6 +826,8 @@ let with_child_trace ?attributes ?trace_context parent ~name f =
810826
module EnvHelpers = struct
811827
let traceparent_key = "TRACEPARENT"
812828

829+
let baggage_key = "BAGGAGE"
830+
813831
let of_traceparent traceparent =
814832
match traceparent with
815833
| None ->
@@ -834,8 +852,23 @@ module EnvHelpers = struct
834852
| None ->
835853
[]
836854
| Some span ->
837-
Some (span |> Span.get_context |> SpanContext.to_traceparent)
838-
|> of_traceparent
855+
let baggage_env =
856+
span
857+
|> Span.get_context
858+
|> SpanContext.context_of_span_context
859+
|> TraceContext.encode_baggage
860+
|> Option.fold ~none:[] ~some:(fun baggage ->
861+
[String.concat "=" [baggage_key; baggage]]
862+
)
863+
in
864+
let traceparent_env =
865+
span
866+
|> Span.get_context
867+
|> SpanContext.to_traceparent
868+
|> Option.some
869+
|> of_traceparent
870+
in
871+
List.concat [baggage_env; traceparent_env]
839872
end
840873

841874
module Propagator = struct

ocaml/libs/tracing/tracing.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ module TraceContext : sig
9595

9696
val baggage_of : t -> baggage option
9797

98+
val parse : string -> baggage
99+
100+
val encode_baggage : t -> string option
101+
98102
val to_json_string : t -> string
99103

100104
val of_json_string : string -> (t, string) result

python3/examples/XenAPI/XenAPI.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ def with_tracecontext(self):
118118

119119
for k, v in headers.items():
120120
self.add_extra_header(k, v)
121+
else:
122+
headers ={}
123+
traceparent = os.getenv("TRACEPARENT", None)
124+
if traceparent:
125+
self.add_extra_header("traceparent", traceparent)
126+
127+
baggage = os.getenv("BAGGAGE", None)
128+
if baggage:
129+
self.add_extra_header("baggage", baggage)
121130

122131
def with_originator(self):
123132
originator_k = "ORIGINATOR"

python3/packages/observer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ def create_tracer_from_config(path):
210210
if "=" in item
211211
)
212212

213+
baggage = os.getenv("BAGGAGE")
214+
if baggage:
215+
update = dict(
216+
item.split("=", 1) for item in baggage.split(";") if "=" in item
217+
)
218+
otel_resource_attrs.update(update)
219+
213220
service_name = config.get(
214221
"otel_service_name", otel_resource_attrs.get("service.name", "unknown")
215222
)

0 commit comments

Comments
 (0)