hyperlight_host/metrics/
mod.rs

1/*
2Copyright 2025  The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Counter metric that counter number of times a guest error occurred
18pub(crate) static METRIC_GUEST_ERROR: &str = "guest_errors_total";
19pub(crate) static METRIC_GUEST_ERROR_LABEL_CODE: &str = "code";
20
21// Counter metric that counts the number of times a guest function was called due to timing out
22pub(crate) static METRIC_GUEST_CANCELLATION: &str = "guest_cancellations_total";
23
24// Histogram metric that measures the duration of guest function calls
25#[cfg(feature = "function_call_metrics")]
26pub(crate) static METRIC_GUEST_FUNC_DURATION: &str = "guest_call_duration_seconds";
27
28// Histogram metric that measures the duration of host function calls
29#[cfg(feature = "function_call_metrics")]
30pub(crate) static METRIC_HOST_FUNC_DURATION: &str = "host_call_duration_seconds";
31
32/// If the the `function_call_metrics` feature is enabled, this function measures
33/// the time it takes to execute the given closure, and will then emit a guest call metric
34/// with the given function name.
35///
36/// If the feature is not enabled, the given closure is executed without any additional metrics being emitted,
37/// and the result of the closure is returned directly.
38pub(crate) fn maybe_time_and_emit_guest_call<T, F: FnOnce() -> T>(
39    #[allow(unused_variables)] name: &str,
40    f: F,
41) -> T {
42    cfg_if::cfg_if! {
43        if #[cfg(feature = "function_call_metrics")] {
44            use std::time::Instant;
45
46            let start = Instant::now();
47            let result = f();
48            let duration = start.elapsed();
49
50            static LABEL_GUEST_FUNC_NAME: &str = "function_name";
51            metrics::histogram!(METRIC_GUEST_FUNC_DURATION, LABEL_GUEST_FUNC_NAME => name.to_string()).record(duration);
52            result
53        } else {
54            f()
55        }
56    }
57}
58
59/// If the the `function_call_metrics` feature is enabled, this function measures
60/// the time it takes to execute the given closure, and will then emit a host call metric
61/// with the given function name.
62///
63/// If the feature is not enabled, the given closure is executed without any additional metrics being emitted,
64/// and the result of the closure is returned directly.
65pub(crate) fn maybe_time_and_emit_host_call<T, F: FnOnce() -> T>(
66    #[allow(unused_variables)] name: &str,
67    f: F,
68) -> T {
69    cfg_if::cfg_if! {
70        if #[cfg(feature = "function_call_metrics")] {
71            use std::time::Instant;
72
73            let start = Instant::now();
74            let result = f();
75            let duration = start.elapsed();
76
77            static LABEL_HOST_FUNC_NAME: &str = "function_name";
78            metrics::histogram!(METRIC_HOST_FUNC_DURATION, LABEL_HOST_FUNC_NAME => name.to_string()).record(duration);
79            result
80        } else {
81            f()
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use std::thread;
89    use std::time::Duration;
90
91    use hyperlight_testing::simple_guest_as_string;
92    use metrics::{Key, with_local_recorder};
93    use metrics_util::CompositeKey;
94
95    use super::*;
96    use crate::{GuestBinary, UninitializedSandbox};
97
98    #[test]
99    fn test_metrics_are_emitted() {
100        let recorder = metrics_util::debugging::DebuggingRecorder::new();
101        let snapshotter = recorder.snapshotter();
102        let snapshot = with_local_recorder(&recorder, || {
103            let uninit = UninitializedSandbox::new(
104                GuestBinary::FilePath(simple_guest_as_string().unwrap()),
105                None,
106            )
107            .unwrap();
108
109            let mut multi = uninit.evolve().unwrap();
110            let interrupt_handle = multi.interrupt_handle();
111
112            // interrupt the guest function call to "Spin" after 1 second
113            let thread = thread::spawn(move || {
114                thread::sleep(Duration::from_secs(1));
115                assert!(interrupt_handle.kill());
116            });
117
118            multi
119                .call::<i32>("PrintOutput", "Hello".to_string())
120                .unwrap();
121
122            multi.call::<i32>("Spin", ()).unwrap_err();
123            thread.join().unwrap();
124
125            snapshotter.snapshot()
126        });
127
128        // Convert snapshot into a hashmap for easier lookup
129        #[expect(clippy::mutable_key_type)]
130        let snapshot = snapshot.into_hashmap();
131
132        cfg_if::cfg_if! {
133            if #[cfg(feature = "function_call_metrics")] {
134                use metrics::Label;
135
136                let expected_num_metrics = 4;
137
138                // Verify that the histogram metrics are recorded correctly
139                assert_eq!(snapshot.len(), expected_num_metrics);
140
141                // 1. Guest call duration
142                let histogram_key = CompositeKey::new(
143                    metrics_util::MetricKind::Histogram,
144                    Key::from_parts(
145                        METRIC_GUEST_FUNC_DURATION,
146                        vec![Label::new("function_name", "PrintOutput")],
147                    ),
148                );
149                let histogram_value = &snapshot.get(&histogram_key).unwrap().2;
150                assert!(
151                    matches!(
152                        histogram_value,
153                        metrics_util::debugging::DebugValue::Histogram(histogram) if histogram.len() == 1
154                    ),
155                    "Histogram metric does not match expected value"
156                );
157
158                // 2. Guest cancellation
159                let counter_key = CompositeKey::new(
160                    metrics_util::MetricKind::Counter,
161                    Key::from_name(METRIC_GUEST_CANCELLATION),
162                );
163                assert_eq!(
164                    snapshot.get(&counter_key).unwrap().2,
165                    metrics_util::debugging::DebugValue::Counter(1)
166                );
167
168                // 3. Guest call duration
169                let histogram_key = CompositeKey::new(
170                    metrics_util::MetricKind::Histogram,
171                    Key::from_parts(
172                        METRIC_GUEST_FUNC_DURATION,
173                        vec![Label::new("function_name", "Spin")],
174                    ),
175                );
176                let histogram_value = &snapshot.get(&histogram_key).unwrap().2;
177                assert!(
178                    matches!(
179                        histogram_value,
180                        metrics_util::debugging::DebugValue::Histogram(histogram) if histogram.len() == 1
181                    ),
182                    "Histogram metric does not match expected value"
183                );
184            } else {
185                // Verify that the counter metrics are recorded correctly
186                assert_eq!(snapshot.len(), 1);
187
188                let counter_key = CompositeKey::new(
189                    metrics_util::MetricKind::Counter,
190                    Key::from_name(METRIC_GUEST_CANCELLATION),
191                );
192                assert_eq!(
193                    snapshot.get(&counter_key).unwrap().2,
194                    metrics_util::debugging::DebugValue::Counter(1)
195                );
196            }
197        }
198    }
199}