Skip to content

Commit

Permalink
rpc: use grpc-status code for 'status' label on metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Dec 11, 2024
1 parent 8c594ec commit b4bf711
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion crates/sui-rpc-api/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,28 @@ pub struct RpcMetricsCallbackHandler {

impl ResponseHandler for RpcMetricsCallbackHandler {
fn on_response(&mut self, response: &http::response::Parts) {
const GRPC_STATUS: http::HeaderName = http::HeaderName::from_static("grpc-status");

let status = if response
.headers
.get(&http::header::CONTENT_TYPE)
.is_some_and(|header| header == tonic::metadata::GRPC_CONTENT_TYPE)
{
let code = response
.headers
.get(&GRPC_STATUS)
.map(http::HeaderValue::as_bytes)
.map(tonic::Code::from_bytes)
.unwrap_or(tonic::Code::Ok);

code_as_str(code)
} else {
response.status.as_str()
};

self.metrics
.num_requests
.with_label_values(&[self.path.as_ref(), response.status.as_str()])
.with_label_values(&[self.path.as_ref(), status])
.inc();

self.counted_response = true;
Expand Down Expand Up @@ -145,3 +164,25 @@ impl Drop for RpcMetricsCallbackHandler {
}
}
}

fn code_as_str(code: tonic::Code) -> &'static str {
match code {
tonic::Code::Ok => "ok",
tonic::Code::Cancelled => "canceled",
tonic::Code::Unknown => "unknown",
tonic::Code::InvalidArgument => "invalid-argument",
tonic::Code::DeadlineExceeded => "deadline-exceeded",
tonic::Code::NotFound => "not-found",
tonic::Code::AlreadyExists => "already-exists",
tonic::Code::PermissionDenied => "permission-denied",
tonic::Code::ResourceExhausted => "resource-exhausted",
tonic::Code::FailedPrecondition => "failed-precondition",
tonic::Code::Aborted => "aborted",
tonic::Code::OutOfRange => "out-of-range",
tonic::Code::Unimplemented => "unimplemented",
tonic::Code::Internal => "internal",
tonic::Code::Unavailable => "unavailable",
tonic::Code::DataLoss => "data-loss",
tonic::Code::Unauthenticated => "unauthenticated",
}
}

0 comments on commit b4bf711

Please sign in to comment.