Skip to content

Commit ddf2bc6

Browse files
committed
CP-50537: Propagate originator as a http request header
XenAPI.py now passes an additional originator header when making requests to xapi, if the "ORIGINATOR" env var is present. Sm_exec now passes an env var, "ORIGINATOR=SM". To classify the threads correctly, we first need to determine the requests originators. This commit makes it possibly to explicitly state the originators as headers. Signed-off-by: Gabriel Buica <[email protected]>
1 parent 6ebb6e6 commit ddf2bc6

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

ocaml/libs/http-lib/http.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ module Hdr = struct
132132

133133
let location = "location"
134134

135+
let originator = "originator"
136+
135137
let traceparent = "traceparent"
136138

137139
let hsts = "strict-transport-security"
@@ -688,6 +690,14 @@ module Request = struct
688690
let frame_header = if x.frame then make_frame_header headers else "" in
689691
frame_header ^ headers ^ body
690692

693+
let with_originator_of req f =
694+
Option.iter
695+
(fun req ->
696+
let originator = List.assoc_opt Hdr.originator req.additional_headers in
697+
f originator
698+
)
699+
req
700+
691701
let traceparent_of req =
692702
let open Tracing in
693703
let ( let* ) = Option.bind in

ocaml/libs/http-lib/http.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ module Request : sig
129129
val to_wire_string : t -> string
130130
(** [to_wire_string t] returns a string which could be sent to a server *)
131131

132+
val with_originator_of : t option -> (string option -> unit) -> unit
133+
132134
val traceparent_of : t -> Tracing.Span.t option
133135

134136
val with_tracing :

ocaml/xapi/sm_exec.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ let with_dbg ~name ~dbg f =
3838
(*********************************************************************************************)
3939
(* Random utility functions *)
4040

41+
let env_vars =
42+
Some
43+
(Array.concat
44+
[
45+
Forkhelpers.default_path_env_pair
46+
; Env_record.to_string_array [Env_record.pair ("ORIGINATOR", "SM")]
47+
]
48+
)
49+
4150
type call = {
4251
(* All calls are performed by a specific Host with a special Session and device_config *)
4352
host_ref: API.ref_host
@@ -355,7 +364,7 @@ let exec_xmlrpc ~dbg ?context:_ ?(needs_session = true) (driver : string)
355364
let env, exe, args =
356365
match Xapi_observer_components.is_smapi_enabled () with
357366
| false ->
358-
(None, exe, args)
367+
(env_vars, exe, args)
359368
| true ->
360369
Xapi_observer_components.env_exe_args_of
361370
~component:Xapi_observer_components.SMApi ~exe ~args

ocaml/xapi/xapi_observer_components.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ let dir_name_of_component component =
103103

104104
let env_exe_args_of ~component ~exe ~args =
105105
let dir_name_value = Filename.quote (dir_name_of_component component) in
106+
let originator = match component with SMApi -> "SM" | _ -> "none" in
106107
let env_vars =
107108
Array.concat
108109
[
109110
Forkhelpers.default_path_env_pair
110111
; Env_record.to_string_array
111112
[
112-
Env_record.pair ("OBSERVER_CONFIG_DIR", dir_name_value)
113+
Env_record.pair ("ORIGINATOR", originator)
114+
; Env_record.pair ("OBSERVER_CONFIG_DIR", dir_name_value)
113115
; Env_record.pair ("PYTHONPATH", Filename.dirname exe)
114116
]
115117
]

python3/examples/XenAPI/XenAPI.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,18 @@ def with_tracecontext(self):
119119
for k, v in headers.items():
120120
self.add_extra_header(k, v)
121121

122+
def with_originator(self):
123+
originator_k = "ORIGINATOR"
124+
originator_v = os.getenv(originator_k, None)
125+
if originator_v:
126+
self.add_extra_header(originator_k.lower(), originator_v)
127+
122128
def make_connection(self, host):
123129
# clear the extra headers when making a new connection. This makes sure
124130
# headers such as "traceparent" do not get duplicated.
125131
self._extra_headers = []
126132
self.with_tracecontext()
133+
self.with_originator()
127134

128135
# compatibility with parent xmlrpclib.Transport HTTP/1.1 support
129136
if self._connection and host == self._connection[0]:

0 commit comments

Comments
 (0)