Skip to content

Commit

Permalink
Merge pull request xapi-project#5435 from snwoods/private/stevenwo/CP…
Browse files Browse the repository at this point in the history
…-46155

CP-46155: Call SM scripts via observer.py when SM observer is enabled
  • Loading branch information
mg12 committed Feb 8, 2024
2 parents 1a4145d + 155509f commit f92edf0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion ocaml/forkexecd/lib/forkhelpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ let safe_close_and_exec ?env stdin stdout stderr
List.fold_left maybe_add_id_to_fd_map dest_named_fds predefined_fds
in

let env = match env with Some e -> e | None -> default_path_env_pair in
let env = Option.value ~default:default_path_env_pair env in
let syslog_stdout =
match syslog_stdout with
| NoSyslogging ->
Expand Down
11 changes: 5 additions & 6 deletions ocaml/xapi-idl/lib/debuginfo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ let with_dbg ?(with_thread = false) ~module_name ~name ~dbg f =
| false ->
f_with_trace ()

let span_context_of_di di =
Option.map (fun span -> Tracing.Span.get_context span) di.tracing

let traceparent_of_dbg dbg =
of_string dbg
|> span_context_of_di
|> Option.map Tracing.SpanContext.trace_id_of_span_context
match String.split_on_char separator dbg with
| [_; traceparent] ->
Some traceparent
| _ ->
None
15 changes: 7 additions & 8 deletions ocaml/xapi/sm_exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,20 +336,19 @@ let exec_xmlrpc ~dbg ?context:_ ?(needs_session = true) (driver : string)
(* Logging call.cmd is safe, but call.args could contain a password. *)
try
E.debug "smapiv2=>smapiv1 [label=\"%s\"];" call.cmd ;
let args = [Xml.to_string xml] in
let output, stderr =
let env =
let env, exe, args =
match Xapi_observer_components.is_smapi_enabled () with
| false ->
None
(None, exe, args)
| true ->
let traceparent = Debuginfo.traceparent_of_dbg dbg in
Some
(Xapi_observer_components.env_vars_of_component
~component:Xapi_observer_components.SMApi ~traceparent
)
Xapi_observer_components.env_exe_args_of
~component:Xapi_observer_components.SMApi ~traceparent
~exe ~args
in
Forkhelpers.execute_command_get_output ?env exe
[Xml.to_string xml]
Forkhelpers.execute_command_get_output ?env exe args
in
try (Xml.parse_string output, stderr)
with e ->
Expand Down
2 changes: 2 additions & 0 deletions ocaml/xapi/xapi_globs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ let observer_endpoint_http_enabled = ref false

let observer_endpoint_https_enabled = ref false

let python3_path = ref "/usr/bin/python3"

let xapi_globs_spec =
[
( "master_connection_reset_timeout"
Expand Down
36 changes: 22 additions & 14 deletions ocaml/xapi/xapi_observer_components.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,26 @@ let ( // ) = Filename.concat
let dir_name_of_component component =
Xapi_globs.observer_config_dir // to_string component // "enabled"

let env_vars_of_component ~component ~traceparent =
let env_exe_args_of ~component ~traceparent ~exe ~args =
let dir_name_value = Filename.quote (dir_name_of_component component) in
Array.concat
[
Forkhelpers.default_path_env_pair
; Env_record.to_string_array
([Env_record.pair ("OBSERVER_CONFIG_DIR", dir_name_value)]
@
match traceparent with
| None ->
[]
| Some traceparent ->
[Env_record.pair ("TRACEPARENT", traceparent)]
)
]
let env_vars =
Array.concat
[
Forkhelpers.default_path_env_pair
; Env_record.to_string_array
([
Env_record.pair ("OBSERVER_CONFIG_DIR", dir_name_value)
; Env_record.pair ("PYTHONPATH", Filename.dirname exe)
]
@
match traceparent with
| None ->
[]
| Some traceparent ->
[Env_record.pair ("TRACEPARENT", traceparent)]
)
]
in
let args = "-m" :: "observer" :: exe :: args in
let new_exe = !Xapi_globs.python3_path in
(Some env_vars, new_exe, args)
22 changes: 13 additions & 9 deletions ocaml/xapi/xapi_observer_components.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* GNU Lesser General Public License for more details.
*)

(** Xapi_observer_components module contains generic operations on components
* instrumented with tracing. These operations act on components independently
(** Xapi_observer_components module contains generic operations on components
* instrumented with tracing. These operations act on components independently
* of observers.
*)

Expand All @@ -39,13 +39,13 @@ val startup_components : unit -> t list
*)

val assert_valid_components : string list -> unit
(** Checks if a given list is made of only string equivalents of supported
(** Checks if a given list is made of only string equivalents of supported
* components.
* Raises Server_error if at least one of the elements is invalid.
*)

val observed_components_of : t list -> t list
(** Transforms a list of components to a list of components that are expected
(** Transforms a list of components to a list of components that are expected
* to be observed. If the list is empty we expect all startup components.
*)

Expand All @@ -58,12 +58,16 @@ val is_smapi_enabled : unit -> bool
*)

val dir_name_of_component : t -> string
(** Returns the directory path that python scripts check to see if a component
(** Returns the directory path that python scripts check to see if a component
* is enabled.
*)

val env_vars_of_component :
component:t -> traceparent:string option -> string array
(** Returns an array of environment viariables used by python scripts to
* configure the python observers.
val env_exe_args_of :
component:t
-> traceparent:string option
-> exe:string
-> args:string list
-> string array option * string * string list
(** Returns an array option of environment variables and the modified exe and
* args used by python scripts to configure the python observers.
*)

0 comments on commit f92edf0

Please sign in to comment.