Skip to content

Commit fd1a225

Browse files
committed
Add function name to metrics
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 8694509 commit fd1a225

File tree

7 files changed

+98
-207
lines changed

7 files changed

+98
-207
lines changed

docs/hyperlight-metrics-logs-and-traces.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@ Hyperlight provides the following observability features:
1111
Metrics are provided using the [metrics](https://docs.rs/metrics/latest/metrics/index.html) crate, which is a lightweight metrics facade. When an executable installs a recorder, Hyperlight will emit its metrics to that record, which allows library authors to seamless emit their own metrics without knowing or caring which exporter implementation is chosen, or even if one is installed. In case no recorder is installed, the metrics will be emitted to the default recorder, which is a no-op implementation with minimal overhead.
1212

1313
There are many different implementations of recorders. One example is the [prometheus exporter](https://docs.rs/metrics-exporter-prometheus/latest/metrics_exporter_prometheus/) which can be used to export metrics to a Prometheus server.
14-
Hyperlight provides metrics using Prometheus.
1514

1615
The following metrics are provided and are enabled by default:
1716

18-
* `NUM_GUEST_ERRORS` - Counter that tracks the number of guest errors by code and message.
19-
* `NUM_GUEST_CANCELLATIONS` - Counter that tracks the number of guest executions that have been cancelled because the execution time exceeded the time allowed.
17+
* `guest_errors_total` - Counter that tracks the number of guest errors by error code.
18+
* `guest_cancellations_total` - Counter that tracks the number of guest executions that have been cancelled because the execution time exceeded the time allowed.
2019

21-
The following metrics are provided but are disabled by default and require the feature `function_call_metrics` to be enabled:
20+
The following metrics are provided but are disabled by default:
2221

23-
* `GUEST_CALL_DURATION` - Histogram that tracks the execution time of guest functions in seconds by function name. The histogram also tracks the number of calls to each function.
24-
* `HOST_CALL_DURATION` - Histogram that tracks the execution time of host functions in seconds by function name. The histogram also tracks the number of calls to each function.
22+
* `guest_call_duration_seconds` - Histogram that tracks the execution time of guest functions in seconds by function name. The histogram also tracks the number of calls to each function.
23+
* `host_call_duration_seconds` - Histogram that tracks the execution time of host functions in seconds by function name. The histogram also tracks the number of calls to each function.
2524

2625
The rationale for disabling the function call metrics by default is that:
27-
2826
* A Hyperlight host may wish to provide its own metrics for function calls.
2927
* Enabling a trace subscriber will cause the function call metrics to be emitted as trace events, which may be sufficient for some use cases.
28+
* These 2 metrics require string clones for the function names, which may be too expensive for some use cases.
3029

3130
There is an example of how to gather metrics in the [examples/metrics](../src/hyperlight_host/examples/metrics) directory.
3231
## Logs

src/hyperlight_host/src/func/guest_err.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) fn check_for_guest_error(mgr: &MemMgrWrapper<HostSharedMemory>) -> Re
2929
ErrorCode::NoError => Ok(()),
3030
ErrorCode::OutbError => match mgr.as_ref().get_host_error()? {
3131
Some(host_err) => {
32-
CounterMetric::guest_error(guest_err.code.into(), guest_err.message.clone()).emit();
32+
CounterMetric::guest_error(guest_err.code.into()).emit();
3333

3434
log_then_return!(OutBHandlingError(
3535
host_err.source.clone(),
@@ -40,11 +40,11 @@ pub(crate) fn check_for_guest_error(mgr: &MemMgrWrapper<HostSharedMemory>) -> Re
4040
None => Ok(()),
4141
},
4242
ErrorCode::StackOverflow => {
43-
CounterMetric::guest_error(guest_err.code.into(), guest_err.message.clone()).emit();
43+
CounterMetric::guest_error(guest_err.code.into()).emit();
4444
log_then_return!(StackOverflow());
4545
}
4646
_ => {
47-
CounterMetric::guest_error(guest_err.code.into(), guest_err.message.clone()).emit();
47+
CounterMetric::guest_error(guest_err.code.into()).emit();
4848
log_then_return!(GuestError(guest_err.code, guest_err.message.clone()));
4949
}
5050
}

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use crate::mem::mgr::SandboxMemoryManager;
4848
use crate::mem::ptr::{GuestPtr, RawPtr};
4949
use crate::mem::ptr_offset::Offset;
5050
use crate::mem::shared_mem::{GuestSharedMemory, HostSharedMemory, SharedMemory};
51-
use crate::metrics::Metric;
51+
use crate::metrics::HistogramMetric;
5252
#[cfg(gdb)]
5353
use crate::sandbox::config::DebugInfo;
5454
use crate::sandbox::hypervisor::{get_available_hypervisor, HypervisorType};
@@ -432,37 +432,18 @@ impl HypervisorHandler {
432432
})?
433433
.shared_mem
434434
.lock
435-
.try_read();
435+
.try_read();
436436

437-
let res = {
438-
#[cfg(feature = "function_call_metrics")]
439-
{
440-
use crate::metrics::HistogramMetric;
441-
442-
let start = std::time::Instant::now();
443-
let result = hv.dispatch_call_from_host(
444-
dispatch_function_addr,
445-
configuration.outb_handler.clone(),
446-
configuration.mem_access_handler.clone(),
447-
Some(hv_handler_clone.clone()),
448-
#[cfg(gdb)]
449-
configuration.dbg_mem_access_handler.clone(),
450-
);
451-
let elapsed = start.elapsed();
452-
HistogramMetric::guest_call(elapsed).emit();
453-
result
454-
}
455-
456-
#[cfg(not(feature = "function_call_metrics"))]
457-
hv.dispatch_call_from_host(
437+
let res = HistogramMetric::time_and_emit_guest_call(
438+
function_name.clone(), || hv.dispatch_call_from_host(
458439
dispatch_function_addr,
459440
configuration.outb_handler.clone(),
460441
configuration.mem_access_handler.clone(),
461442
Some(hv_handler_clone.clone()),
462443
#[cfg(gdb)]
463444
configuration.dbg_mem_access_handler.clone(),
464-
)
465-
};
445+
));
446+
466447
drop(mem_lock_guard);
467448
drop(evar_lock_guard);
468449

src/hyperlight_host/src/metrics/int_gauge_vec.rs

Lines changed: 0 additions & 121 deletions
This file was deleted.

src/hyperlight_host/src/metrics/metrics_macro.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ pub(crate) trait NamedMetric {
77
#[macro_export]
88
macro_rules! define_metrics {
99
(
10-
$( $metric_type:ident {
10+
$( $metric_type:ident $( < $( $gen:tt ),+ > )? {
1111
$(
1212
$( #[cfg($feature:meta)] )?
13-
$variant:ident $( { $($field_name:ident : $field_ty:ty),* } )? => $name:expr
13+
$variant:ident $( { $($field_name:ident : $field_ty:ty),* $(,)? } )? => $name:expr
1414
),* $(,)?
1515
} )*
1616
) => {
1717
$(
1818
#[derive(Debug, Clone)]
19-
pub(crate) enum $metric_type {
19+
pub(crate) enum $metric_type $( < $( $gen ),+ > )? {
2020
$(
2121
$( #[cfg($feature)] )?
2222
$variant $( { $($field_name : $field_ty),* } )?
2323
),*
2424
}
2525

26-
impl $crate::metrics::metrics_macro::NamedMetric for $metric_type {
26+
impl $( < $( $gen ),+ > )? $crate::metrics::metrics_macro::NamedMetric for $metric_type $( < $( $gen ),+ > )? {
2727
fn name(&self) -> &'static str {
2828
match self {
2929
$(
3030
$( #[cfg($feature)] )?
31-
Self::$variant{..} => $name,
31+
Self::$variant { .. } => $name,
3232
)*
3333
}
3434
}

0 commit comments

Comments
 (0)