Skip to content

Commit

Permalink
split query metrics by request type (#9466)
Browse files Browse the repository at this point in the history
* split query metrics by request type

* return metrics name from process_request_internal
  • Loading branch information
telezhnaya authored Sep 5, 2023
1 parent 57f3ac2 commit 4a5f100
Showing 1 changed file with 53 additions and 21 deletions.
74 changes: 53 additions & 21 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use near_jsonrpc_primitives::errors::RpcError;
use near_jsonrpc_primitives::message::{Message, Request};
use near_jsonrpc_primitives::types::config::RpcProtocolConfigResponse;
use near_jsonrpc_primitives::types::entity_debug::{EntityDebugHandler, EntityQuery};
use near_jsonrpc_primitives::types::query::RpcQueryRequest;
use near_jsonrpc_primitives::types::split_storage::RpcSplitStorageInfoResponse;
use near_network::tcp;
use near_network::PeerManagerActor;
Expand All @@ -31,7 +32,7 @@ use near_o11y::{WithSpanContext, WithSpanContextExt};
use near_primitives::hash::CryptoHash;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::{AccountId, BlockHeight};
use near_primitives::views::FinalExecutionOutcomeViewEnum;
use near_primitives::views::{FinalExecutionOutcomeViewEnum, QueryRequest};
use serde_json::{json, Value};
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -241,37 +242,73 @@ impl JsonRpcHandler {
// `process_request_internal`.
async fn process_request(&self, request: Request) -> Result<Value, RpcError> {
let timer = Instant::now();
let (metrics_name, response) = self.process_request_internal(request).await;

let request_method = request.method.clone();
let response = self.process_request_internal(request).await;

let request_method = match &response {
Err(err) if err.code == -32_601 => "UNSUPPORTED_METHOD",
_ => &request_method,
};

metrics::HTTP_RPC_REQUEST_COUNT.with_label_values(&[request_method]).inc();
metrics::HTTP_RPC_REQUEST_COUNT.with_label_values(&[&metrics_name]).inc();
metrics::RPC_PROCESSING_TIME
.with_label_values(&[request_method])
.with_label_values(&[&metrics_name])
.observe(timer.elapsed().as_secs_f64());

if let Err(err) = &response {
metrics::RPC_ERROR_COUNT
.with_label_values(&[request_method, &err.code.to_string()])
.with_label_values(&[&metrics_name, &err.code.to_string()])
.inc();
}

response
}

/// Processes the request without updating any metrics.
async fn process_request_internal(&self, request: Request) -> Result<Value, RpcError> {
/// Returns metrics name (method name with optional details as a suffix)
/// and the result of the execution.
async fn process_request_internal(
&self,
request: Request,
) -> (String, Result<Value, RpcError>) {
let method_name = request.method.to_string();
let request = match self.process_adversarial_request_internal(request).await {
Ok(response) => return response,
Ok(response) => return (method_name, response),
Err(request) => request,
};

let request = match self.process_basic_requests_internal(request).await {
Ok(response) => return (method_name, response),
Err(request) => request,
};

match request.method.as_ref() {
"query" => {
let params: RpcQueryRequest = match RpcRequest::parse(request.params) {
Ok(params) => params,
Err(err) => return (method_name, Err(RpcError::from(err))),
};
let metrics_name = match params.request {
QueryRequest::ViewAccount { .. } => "query_view_account",
QueryRequest::ViewCode { .. } => "query_view_code",
QueryRequest::ViewState { include_proof, .. } => {
if include_proof {
"query_view_state_with_proof"
} else {
"query_view_state"
}
}
QueryRequest::ViewAccessKey { .. } => "query_view_access_key",
QueryRequest::ViewAccessKeyList { .. } => "query_view_access_key_list",
QueryRequest::CallFunction { .. } => "query_call_function",
};
(metrics_name.to_string(), process_query_response(self.query(params).await))
}
_ => {
("UNSUPPORTED_METHOD".to_string(), Err(RpcError::method_not_found(request.method)))
}
}
}

async fn process_basic_requests_internal(
&self,
request: Request,
) -> Result<Result<Value, RpcError>, Request> {
Ok(match request.method.as_ref() {
// Handlers ordered alphabetically
"block" => process_method_call(request, |params| self.block(params)).await,
"broadcast_tx_async" => {
Expand All @@ -297,11 +334,6 @@ impl JsonRpcHandler {
process_method_call(request, |params| self.next_light_client_block(params)).await
}
"network_info" => process_method_call(request, |_params: ()| self.network_info()).await,
"query" => {
let params = RpcRequest::parse(request.params)?;
let query_response = self.query(params).await;
process_query_response(query_response)
}
"status" => process_method_call(request, |_params: ()| self.status()).await,
"tx" => {
process_method_call(request, |params| self.tx_status_common(params, false)).await
Expand Down Expand Up @@ -360,8 +392,8 @@ impl JsonRpcHandler {
"sandbox_fast_forward" => {
process_method_call(request, |params| self.sandbox_fast_forward(params)).await
}
_ => Err(RpcError::method_not_found(request.method)),
}
_ => return Err(request),
})
}

/// Handles adversarial requests if they are enabled.
Expand Down

0 comments on commit 4a5f100

Please sign in to comment.