@@ -22,22 +22,27 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, Ret
2222use hyperlight_host:: sandbox:: uninitialized:: UninitializedSandbox ;
2323use hyperlight_host:: sandbox_state:: sandbox:: EvolvableSandbox ;
2424use hyperlight_host:: sandbox_state:: transition:: Noop ;
25- use hyperlight_host:: { set_metrics_registry , GuestBinary , MultiUseSandbox , Result } ;
25+ use hyperlight_host:: { GuestBinary , MultiUseSandbox , Result } ;
2626use hyperlight_testing:: simple_guest_as_string;
27- use lazy_static:: lazy_static;
28- use prometheus:: Registry ;
2927
30- lazy_static ! {
31- static ref HOST_REGISTRY : Registry = Registry :: new( ) ;
32- }
33- fn fn_writer ( _msg : String ) -> Result < i32 > {
34- Ok ( 0 )
28+ fn main ( ) {
29+ // Install prometheus metrics exporter.
30+ // We only install the metrics recorder here, but you can also use the
31+ // `metrics_exporter_prometheus::PrometheusBuilder::new().install()` method
32+ // to install a HTTP listener that serves the metrics.
33+ let prometheus_handle = metrics_exporter_prometheus:: PrometheusBuilder :: new ( )
34+ . install_recorder ( )
35+ . expect ( "Failed to install Prometheus exporter" ) ;
36+
37+ // Do some hyperlight stuff to generate metrics.
38+ do_hyperlight_stuff ( ) ;
39+
40+ // Get the metrics and print them in prometheus exposition format.
41+ let payload = prometheus_handle. render ( ) ;
42+ println ! ( "Prometheus metrics:\n {}" , payload) ;
3543}
3644
37- fn main ( ) -> Result < ( ) > {
38- // If this is not called then the default registry `prometheus::default_registry` will be used.
39- set_metrics_registry ( & HOST_REGISTRY ) ?;
40-
45+ fn do_hyperlight_stuff ( ) {
4146 // Get the path to a simple guest binary.
4247 let hyperlight_guest_path =
4348 simple_guest_as_string ( ) . expect ( "Cannot find the guest binary at the expected location." ) ;
@@ -60,7 +65,7 @@ fn main() -> Result<()> {
6065
6166 let no_op = Noop :: < UninitializedSandbox , MultiUseSandbox > :: default ( ) ;
6267
63- let mut multiuse_sandbox = usandbox. evolve ( no_op) ? ;
68+ let mut multiuse_sandbox = usandbox. evolve ( no_op) . expect ( "Failed to evolve sandbox" ) ;
6469
6570 // Call a guest function 5 times to generate some metrics.
6671 for _ in 0 ..5 {
@@ -97,13 +102,14 @@ fn main() -> Result<()> {
97102 None ,
98103 None ,
99104 None ,
100- ) ?;
105+ )
106+ . expect ( "Failed to create UninitializedSandbox" ) ;
101107
102108 // Initialize the sandbox.
103109
104110 let no_op = Noop :: < UninitializedSandbox , MultiUseSandbox > :: default ( ) ;
105111
106- let mut multiuse_sandbox = usandbox. evolve ( no_op) ? ;
112+ let mut multiuse_sandbox = usandbox. evolve ( no_op) . expect ( "Failed to evolve sandbox" ) ;
107113
108114 // Call a function that gets cancelled by the host function 5 times to generate some metrics.
109115
@@ -121,73 +127,8 @@ fn main() -> Result<()> {
121127 let result = join_handle. join ( ) ;
122128 assert ! ( result. is_ok( ) ) ;
123129 }
124-
125- get_metrics ( ) ;
126-
127- Ok ( ( ) )
128130}
129131
130- fn get_metrics ( ) {
131- // Get the metrics from the registry.
132-
133- let metrics = HOST_REGISTRY . gather ( ) ;
134-
135- // Print the metrics.
136-
137- print ! ( "\n METRICS:\n " ) ;
138-
139- for metric in metrics. iter ( ) {
140- match metric. get_field_type ( ) {
141- prometheus:: proto:: MetricType :: COUNTER => {
142- println ! ( "Counter: {:?}" , metric. get_help( ) ) ;
143- metric. get_metric ( ) . iter ( ) . for_each ( |metric| {
144- let pair = metric. get_label ( ) ;
145- for pair in pair. iter ( ) {
146- println ! ( "Label: {:?} Name: {:?}" , pair. get_name( ) , pair. get_value( ) ) ;
147- }
148- println ! ( "Value: {:?}" , metric. get_counter( ) . get_value( ) ) ;
149- } ) ;
150- }
151- prometheus:: proto:: MetricType :: GAUGE => {
152- println ! ( "Gauge: {:?}" , metric. get_help( ) ) ;
153- metric. get_metric ( ) . iter ( ) . for_each ( |metric| {
154- let pair = metric. get_label ( ) ;
155- for pair in pair. iter ( ) {
156- println ! ( "Label: {:?} Name: {:?}" , pair. get_name( ) , pair. get_value( ) ) ;
157- }
158- println ! ( "Value: {:?}" , metric. get_gauge( ) . get_value( ) ) ;
159- } ) ;
160- }
161- prometheus:: proto:: MetricType :: UNTYPED => {
162- println ! ( "Metric: {:?}" , metric. get_help( ) ) ;
163- }
164- prometheus:: proto:: MetricType :: HISTOGRAM => {
165- println ! ( "Histogram: {:?}" , metric. get_help( ) ) ;
166- for metric in metric. get_metric ( ) {
167- let pair = metric. get_label ( ) ;
168- for pair in pair. iter ( ) {
169- println ! ( "Label: {:?} Name: {:?}" , pair. get_name( ) , pair. get_value( ) ) ;
170- }
171- let count = metric. get_histogram ( ) . get_sample_count ( ) ;
172- println ! ( "Number of observations: {:?}" , count) ;
173- let sm = metric. get_histogram ( ) . get_sample_sum ( ) ;
174- println ! ( "Sum of observations: {:?}" , sm) ;
175- metric
176- . get_histogram ( )
177- . get_bucket ( )
178- . iter ( )
179- . for_each ( |bucket| {
180- println ! (
181- "Bucket: {:?} Count: {:?}" ,
182- bucket. get_upper_bound( ) ,
183- bucket. get_cumulative_count( )
184- )
185- } ) ;
186- }
187- }
188- prometheus:: proto:: MetricType :: SUMMARY => {
189- println ! ( "Summary: {:?}" , metric. get_help( ) ) ;
190- }
191- }
192- }
132+ fn fn_writer ( _msg : String ) -> Result < i32 > {
133+ Ok ( 0 )
193134}
0 commit comments