Skip to content

Commit f80d971

Browse files
author
leruaa
committed
fix: more granular metrics
1 parent 425c948 commit f80d971

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

host/bin/server.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use sp1_sdk::network::tee::SP1_TEE_VERSION;
1111
use sp1_tee_common::{EnclaveRequest, EnclaveResponse};
1212
use sp1_tee_host::{
1313
api::GetAddressResponse,
14-
metrics::HostMetrics,
1514
server::{Server, ServerArgs, ServerError},
1615
};
1716
use sp1_tee_host::{
@@ -209,8 +208,10 @@ async fn execute(
209208
}
210209

211210
let response = execute_inner(server.clone(), request);
212-
let response =
213-
stream::once(response).map(|response| Ok(sp1_tee_host::api::result_to_event(response)));
211+
let response = stream::once(response).map(|response| {
212+
sp1_tee_host::metrics::emit_response_metric(&response);
213+
Ok(sp1_tee_host::api::result_to_event(response))
214+
});
214215

215216
Ok(Sse::new(response))
216217
}
@@ -280,8 +281,6 @@ async fn execute_inner(
280281
signature,
281282
recovery_id,
282283
} => {
283-
HostMetrics::TeeExecutionCounter.increment();
284-
285284
Ok(TEEResponse {
286285
vkey,
287286
public_values,
@@ -294,8 +293,6 @@ async fn execute_inner(
294293
// This error type is expected, it can happen if the execution fails.
295294
tracing::error!("Error during execution from enclave: {:?}", error);
296295

297-
HostMetrics::TeeExecutionErrorCounter.increment();
298-
299296
Err(ServerError::EnclaveError(error))
300297
}
301298
_ => {

host/src/metrics.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,65 @@
1-
use metrics::{counter, describe_gauge, gauge};
1+
use metrics::{counter, describe_counter};
2+
use sp1_sdk::network::tee::api::TEEResponse;
23
use strum::{Display, EnumIter, EnumMessage, IntoEnumIterator};
34

5+
use crate::server::ServerError;
6+
47
#[derive(Debug, Display, EnumIter, EnumMessage)]
5-
pub enum HostMetrics {
8+
pub enum HostMetric {
9+
#[strum(serialize = "executions_count", message = "Total number of executions")]
10+
Execution,
11+
612
#[strum(
7-
serialize = "sp1_tee_executions",
8-
message = "Total number of executions"
13+
serialize = "execution_errors_count",
14+
message = "Total number of execution errors"
915
)]
10-
TeeExecutionCounter,
16+
ExecutionError,
17+
1118
#[strum(
12-
serialize = "sp1_tee_running_enclaves",
13-
message = "Total number of running enclaves"
19+
serialize = "unexpected_errors_count",
20+
message = "Total number of unexpected response errors"
1421
)]
15-
RunningEnclaveGauge,
22+
UnexpectedResponse,
23+
1624
#[strum(
17-
serialize = "sp1_tee_execution_errors",
18-
message = "Total number of execution errors"
25+
serialize = "stdin_too_large",
26+
message = "Total number of stdin too large errors"
1927
)]
20-
TeeExecutionErrorCounter,
28+
StdinTooLarge,
29+
30+
#[strum(
31+
serialize = "program_too_large",
32+
message = "Total number of program too large errors"
33+
)]
34+
ProgramTooLarge,
2135
}
2236

23-
impl HostMetrics {
37+
impl HostMetric {
2438
pub fn register() {
2539
for metric in Self::iter() {
2640
if let Some(message) = metric.get_message() {
27-
describe_gauge!(metric.to_string(), message);
41+
describe_counter!(metric.to_string(), message);
2842
}
2943
}
3044
}
3145

3246
pub fn increment(&self) {
33-
match self {
34-
HostMetrics::TeeExecutionCounter => counter!(self.to_string()).increment(1),
35-
HostMetrics::RunningEnclaveGauge => gauge!(self.to_string()).increment(1),
36-
HostMetrics::TeeExecutionErrorCounter => counter!(self.to_string()).increment(1),
47+
counter!(self.to_string()).increment(1)
48+
}
49+
}
50+
51+
pub fn emit_response_metric(response: &Result<TEEResponse, ServerError>) {
52+
if let Err(error) = response {
53+
match error {
54+
ServerError::UnexpectedResponseFromEnclave => {
55+
HostMetric::UnexpectedResponse.increment()
56+
}
57+
ServerError::EnclaveError(_) => HostMetric::ExecutionError.increment(),
58+
ServerError::StdinTooLarge(_) => HostMetric::StdinTooLarge.increment(),
59+
ServerError::ProgramTooLarge(_) => HostMetric::ProgramTooLarge.increment(),
60+
_ => (),
3761
}
62+
} else {
63+
HostMetric::Execution.increment();
3864
}
3965
}

host/src/server.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414
time::Duration,
1515
};
1616

17-
use crate::metrics::HostMetrics;
17+
use crate::metrics::HostMetric;
1818

1919
pub mod stream;
2020

@@ -59,8 +59,6 @@ impl Server {
5959
// Spawn a thread to collect metrics.
6060
spawn_metrics_thread(args.metrics_port, args.metrics_prefix.clone());
6161

62-
HostMetrics::RunningEnclaveGauge.increment();
63-
6462
Arc::new(Self {
6563
execution_mutex: tokio::sync::Mutex::new(()),
6664
cid: args.enclave_cid,
@@ -341,7 +339,7 @@ pub fn spawn_attestation_task(cid: u32, port: u16, interval: Duration) {
341339
}
342340

343341
pub fn spawn_metrics_thread(port: u16, prefix: Option<String>) {
344-
HostMetrics::register();
342+
HostMetric::register();
345343

346344
let builder = PrometheusBuilder::new().with_http_listener(SocketAddr::new(
347345
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),

0 commit comments

Comments
 (0)