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 = if cfg!(all(feature = "seccomp", target_os = "linux")) {
137                    3 // if seccomp enabled, the host call duration metric is emitted on a separate thread which this local recorder doesn't capture
138                } else {
139                    4
140                };
141
142                // Verify that the histogram metrics are recorded correctly
143                assert_eq!(snapshot.len(), expected_num_metrics);
144
145                // 1. Guest call duration
146                let histogram_key = CompositeKey::new(
147                    metrics_util::MetricKind::Histogram,
148                    Key::from_parts(
149                        METRIC_GUEST_FUNC_DURATION,
150                        vec![Label::new("function_name", "PrintOutput")],
151                    ),
152                );
153                let histogram_value = &snapshot.get(&histogram_key).unwrap().2;
154                assert!(
155                    matches!(
156                        histogram_value,
157                        metrics_util::debugging::DebugValue::Histogram(histogram) if histogram.len() == 1
158                    ),
159                    "Histogram metric does not match expected value"
160                );
161
162                // 2. Guest cancellation
163                let counter_key = CompositeKey::new(
164                    metrics_util::MetricKind::Counter,
165                    Key::from_name(METRIC_GUEST_CANCELLATION),
166                );
167                assert_eq!(
168                    snapshot.get(&counter_key).unwrap().2,
169                    metrics_util::debugging::DebugValue::Counter(1)
170                );
171
172                // 3. Guest call duration
173                let histogram_key = CompositeKey::new(
174                    metrics_util::MetricKind::Histogram,
175                    Key::from_parts(
176                        METRIC_GUEST_FUNC_DURATION,
177                        vec![Label::new("function_name", "Spin")],
178                    ),
179                );
180                let histogram_value = &snapshot.get(&histogram_key).unwrap().2;
181                assert!(
182                    matches!(
183                        histogram_value,
184                        metrics_util::debugging::DebugValue::Histogram(histogram) if histogram.len() == 1
185                    ),
186                    "Histogram metric does not match expected value"
187                );
188
189                if !cfg!(all(feature = "seccomp", target_os = "linux")) {
190                    // 4. Host call duration
191                    let histogram_key = CompositeKey::new(
192                        metrics_util::MetricKind::Histogram,
193                        Key::from_parts(
194                            METRIC_HOST_FUNC_DURATION,
195                            vec![Label::new("function_name", "HostPrint")],
196                        ),
197                    );
198                    let histogram_value = &snapshot.get(&histogram_key).unwrap().2;
199                    assert!(
200                        matches!(
201                            histogram_value,
202                            metrics_util::debugging::DebugValue::Histogram(histogram) if histogram.len() == 1
203                        ),
204                        "Histogram metric does not match expected value"
205                    );
206                }
207            } else {
208                // Verify that the counter metrics are recorded correctly
209                assert_eq!(snapshot.len(), 1);
210
211                let counter_key = CompositeKey::new(
212                    metrics_util::MetricKind::Counter,
213                    Key::from_name(METRIC_GUEST_CANCELLATION),
214                );
215                assert_eq!(
216                    snapshot.get(&counter_key).unwrap().2,
217                    metrics_util::debugging::DebugValue::Counter(1)
218                );
219            }
220        }
221    }
222}