@@ -22,6 +22,8 @@ use kvm_bindings::{kvm_debugregs, kvm_fpu, kvm_regs, kvm_sregs, kvm_userspace_me
2222use kvm_ioctls:: Cap :: UserMemory ;
2323use kvm_ioctls:: { Kvm , VcpuExit , VcpuFd , VmFd } ;
2424use tracing:: { Span , instrument} ;
25+ #[ cfg( feature = "trace_guest" ) ]
26+ use tracing_opentelemetry:: OpenTelemetrySpanExt ;
2527
2628#[ cfg( gdb) ]
2729use crate :: hypervisor:: gdb:: { DebugError , DebuggableVm } ;
@@ -33,6 +35,8 @@ use crate::hypervisor::virtual_machine::{
3335 VmExit ,
3436} ;
3537use crate :: mem:: memory_region:: MemoryRegion ;
38+ #[ cfg( feature = "trace_guest" ) ]
39+ use crate :: sandbox:: trace:: TraceContext as SandboxTraceContext ;
3640
3741/// Return `true` if the KVM API is available, version 12, and has UserMemory capability, or `false` otherwise
3842#[ instrument( skip_all, parent = Span :: current( ) , level = "Trace" ) ]
@@ -134,7 +138,14 @@ impl VirtualMachine for KvmVm {
134138 . map_err ( |e| UnmapMemoryError :: Hypervisor ( e. into ( ) ) )
135139 }
136140
137- fn run_vcpu ( & mut self ) -> std:: result:: Result < VmExit , RunVcpuError > {
141+ fn run_vcpu (
142+ & mut self ,
143+ #[ cfg( feature = "trace_guest" ) ] tc : & mut SandboxTraceContext ,
144+ ) -> std:: result:: Result < VmExit , RunVcpuError > {
145+ // setup_trace_guest must be called right before vcpu_run.run() call, because
146+ // it sets the guest span, no other traces or spans must be setup in between these calls.
147+ #[ cfg( feature = "trace_guest" ) ]
148+ tc. setup_guest_trace ( Span :: current ( ) . context ( ) ) ;
138149 match self . vcpu_fd . run ( ) {
139150 Ok ( VcpuExit :: Hlt ) => Ok ( VmExit :: Halt ( ) ) ,
140151 Ok ( VcpuExit :: IoOut ( port, data) ) => Ok ( VmExit :: IoOut ( port, data. to_vec ( ) ) ) ,
0 commit comments