From dd6fa4b19cef77de10c404ece094f048d6809514 Mon Sep 17 00:00:00 2001 From: Steven Woods Date: Mon, 22 Jan 2024 17:24:58 +0000 Subject: [PATCH 1/2] CP-46155: Call smapi scripts via observer.py when smapi observer is enabled Signed-off-by: Steven Woods --- ocaml/xapi/sm_exec.ml | 15 +++++------ ocaml/xapi/xapi_globs.ml | 2 ++ ocaml/xapi/xapi_observer_components.ml | 36 +++++++++++++++---------- ocaml/xapi/xapi_observer_components.mli | 22 ++++++++------- 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/ocaml/xapi/sm_exec.ml b/ocaml/xapi/sm_exec.ml index 0cec2e88523..a6d0d231ee2 100644 --- a/ocaml/xapi/sm_exec.ml +++ b/ocaml/xapi/sm_exec.ml @@ -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 -> diff --git a/ocaml/xapi/xapi_globs.ml b/ocaml/xapi/xapi_globs.ml index d7f48ea081a..f722fbe83cd 100644 --- a/ocaml/xapi/xapi_globs.ml +++ b/ocaml/xapi/xapi_globs.ml @@ -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" diff --git a/ocaml/xapi/xapi_observer_components.ml b/ocaml/xapi/xapi_observer_components.ml index cdd278403a8..33a6570d008 100644 --- a/ocaml/xapi/xapi_observer_components.ml +++ b/ocaml/xapi/xapi_observer_components.ml @@ -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) diff --git a/ocaml/xapi/xapi_observer_components.mli b/ocaml/xapi/xapi_observer_components.mli index 23b2fea8066..9ee531bfe36 100644 --- a/ocaml/xapi/xapi_observer_components.mli +++ b/ocaml/xapi/xapi_observer_components.mli @@ -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. *) @@ -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. *) @@ -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. *) From 155509fea2cb90199a44c472890aeca68d2f98aa Mon Sep 17 00:00:00 2001 From: Steven Woods Date: Mon, 5 Feb 2024 13:10:24 +0000 Subject: [PATCH 2/2] Actually get the traceparent from debuginfo instead of trace_id Signed-off-by: Steven Woods --- ocaml/forkexecd/lib/forkhelpers.ml | 2 +- ocaml/xapi-idl/lib/debuginfo.ml | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/ocaml/forkexecd/lib/forkhelpers.ml b/ocaml/forkexecd/lib/forkhelpers.ml index 0477a93c98d..d55901c3c68 100644 --- a/ocaml/forkexecd/lib/forkhelpers.ml +++ b/ocaml/forkexecd/lib/forkhelpers.ml @@ -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 -> diff --git a/ocaml/xapi-idl/lib/debuginfo.ml b/ocaml/xapi-idl/lib/debuginfo.ml index c29a74c14e0..599537ff5b1 100644 --- a/ocaml/xapi-idl/lib/debuginfo.ml +++ b/ocaml/xapi-idl/lib/debuginfo.ml @@ -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