Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prometheus metrics #201

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 69 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions mpc-recovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ hex = "0.4"
hyper = { version = "0.14", features = ["full"] }
hyper-rustls = { version = "=0.23", features = ["http2"] }
jsonwebtoken = "8.3.0"
lazy_static = "1.4.0"
oauth2 = "4.3.0"
prometheus = { version = "0.13.3", features = ["process"] }
rand = "0.7"
rand8 = { package = "rand", version = "0.8" }
reqwest = "0.11.16"
Expand Down
73 changes: 71 additions & 2 deletions mpc-recovery/src/leader_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::msg::{
MpcPkRequest, MpcPkResponse, NewAccountRequest, NewAccountResponse, SignNodeRequest,
SignRequest, SignResponse, UserCredentialsRequest, UserCredentialsResponse,
};
use crate::nar;
use crate::oauth::OAuthTokenVerifier;
use crate::relayer::error::RelayerError;
use crate::relayer::msg::RegisterAccountRequest;
Expand All @@ -13,15 +12,26 @@ use crate::transaction::{
get_create_account_delegate_action, get_local_signed_delegated_action, get_mpc_signature,
sign_payload_with_mpc, to_dalek_combined_public_key,
};
use crate::{metrics, nar};
use anyhow::Context;
use axum::extract::MatchedPath;
use axum::middleware::{self, Next};
use axum::response::IntoResponse;
use axum::routing::get;
use axum::{http::StatusCode, routing::post, Extension, Json, Router};
use axum::{
http::{Request, StatusCode},
routing::post,
Extension, Json, Router,
};
use curv::elliptic::curves::{Ed25519, Point};
use near_crypto::SecretKey;
use near_primitives::account::id::ParseAccountError;
use near_primitives::types::AccountId;
use near_primitives::views::FinalExecutionStatus;
use prometheus::{Encoder, TextEncoder};
use rand::{distributions::Alphanumeric, Rng};
use std::net::SocketAddr;
use std::time::Instant;

pub struct Config {
pub env: String,
Expand Down Expand Up @@ -118,6 +128,8 @@ pub async fn run<T: OAuthTokenVerifier + 'static>(config: Config) {
.route("/user_credentials", post(user_credentials::<T>))
.route("/new_account", post(new_account::<T>))
.route("/sign", post(sign::<T>))
.route("/metrics", get(metrics))
.route_layer(middleware::from_fn(track_metrics))
.layer(Extension(state))
.layer(cors_layer);

Expand All @@ -129,6 +141,63 @@ pub async fn run<T: OAuthTokenVerifier + 'static>(config: Config) {
.unwrap();
}

async fn track_metrics<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
let timer = Instant::now();
let path = if let Some(matched_path) = req.extensions().get::<MatchedPath>() {
matched_path.as_str().to_owned()
} else {
req.uri().path().to_owned()
};
let method = req.method().clone();

let response = next.run(req).await;
let processing_time = timer.elapsed().as_secs_f64();

metrics::HTTP_REQUEST_COUNT
.with_label_values(&[method.as_str(), &path])
.inc();
metrics::HTTP_PROCESSING_TIME
.with_label_values(&[method.as_str(), &path])
.observe(processing_time);

if response.status().is_client_error() {
metrics::HTTP_CLIENT_ERROR_COUNT
.with_label_values(&[method.as_str(), &path])
.inc();
}
if response.status().is_server_error() {
metrics::HTTP_SERVER_ERROR_COUNT
.with_label_values(&[method.as_str(), &path])
.inc();
}

response
}

async fn metrics() -> (StatusCode, String) {
let grab_metrics = || {
let encoder = TextEncoder::new();
let mut buffer = vec![];
encoder
.encode(&prometheus::gather(), &mut buffer)
.with_context(|| "failed to encode metrics")?;

let response = String::from_utf8(buffer.clone())
.with_context(|| "failed to convert bytes to string")?;
buffer.clear();

Ok::<String, anyhow::Error>(response)
};

match grab_metrics() {
Ok(response) => (StatusCode::OK, response),
Err(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
"failed to generate prometheus metrics".to_string(),
),
}
}

#[derive(Clone)]
struct LeaderState {
env: String,
Expand Down
1 change: 1 addition & 0 deletions mpc-recovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use multi_party_eddsa::protocols::ExpandedKeyPair;
pub mod gcp;
pub mod key_recovery;
pub mod leader_node;
pub mod metrics;
pub mod msg;
pub mod nar;
pub mod oauth;
Expand Down
43 changes: 43 additions & 0 deletions mpc-recovery/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use prometheus::{register_int_counter_vec, HistogramVec, IntCounterVec};

use lazy_static::lazy_static;
use prometheus::{opts, register_histogram_vec};

const EXPONENTIAL_SECONDS: &[f64] = &[
0.001, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256, 0.512, 1.024, 2.048, 4.096,
8.192, 16.384, 32.768,
];

lazy_static! {
pub static ref HTTP_REQUEST_COUNT: IntCounterVec = register_int_counter_vec!(
opts!(
"mpc_http_total_count",
"Total count of HTTP RPC requests received, by method and path"
),
&["method", "path"]
)
.expect("can't create a metric");
pub static ref HTTP_CLIENT_ERROR_COUNT: IntCounterVec = register_int_counter_vec!(
opts!(
"mpc_http_client_error_count",
"Total count of client errors (4xx) by method and path"
),
&["method", "path"]
)
.expect("can't create a metric");
pub static ref HTTP_SERVER_ERROR_COUNT: IntCounterVec = register_int_counter_vec!(
opts!(
"mpc_http_server_error_count",
"Total count of server errors (5xx) by method and path"
),
&["method", "path"]
)
.expect("can't create a metric");
pub static ref HTTP_PROCESSING_TIME: HistogramVec = register_histogram_vec!(
"mpc_http_processing_time",
"Time taken to process HTTP requests in seconds",
&["method", "path"],
EXPONENTIAL_SECONDS.to_vec(),
)
.expect("can't create a metric");
}