bothan_lib/metrics/
utils.rs

1//! Utility functions for metrics modules.
2//!
3//! This module provides helper functions for working with gRPC codes and other metrics-related utilities.
4
5use tonic::Code;
6
7/// Converts a gRPC [`Code`] to a human-readable string for labeling metrics.
8///
9/// # Arguments
10///
11/// * `code` - The gRPC status code to convert.
12///
13/// # Returns
14///
15/// A string representation of the gRPC code suitable for use as a metric label.
16pub fn code_to_str(code: Code) -> String {
17    match code {
18        Code::Ok => "ok".to_string(),
19        Code::Cancelled => "cancelled".to_string(),
20        Code::Unknown => "unknown".to_string(),
21        Code::InvalidArgument => "invalid_argument".to_string(),
22        Code::DeadlineExceeded => "deadline_exceeded".to_string(),
23        Code::NotFound => "not_found".to_string(),
24        Code::AlreadyExists => "already_exists".to_string(),
25        Code::PermissionDenied => "permission_denied".to_string(),
26        Code::ResourceExhausted => "resource_exhausted".to_string(),
27        Code::FailedPrecondition => "failed_precondition".to_string(),
28        Code::Aborted => "aborted".to_string(),
29        Code::OutOfRange => "out_of_range".to_string(),
30        Code::Unimplemented => "unimplemented".to_string(),
31        Code::Internal => "internal".to_string(),
32        Code::Unavailable => "unavailable".to_string(),
33        Code::DataLoss => "data_loss".to_string(),
34        Code::Unauthenticated => "unauthenticated".to_string(),
35    }
36}