Skip to content

Commit

Permalink
Metrics(rpc-server): Extend metrics (#263)
Browse files Browse the repository at this point in the history
* extend metrics to collect block category

* fmt

* code improvement according git comments
  • Loading branch information
kobayurii authored May 30, 2024
1 parent d5e1510 commit 8a2307a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 41 deletions.
50 changes: 50 additions & 0 deletions rpc-server/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,56 @@ lazy_static! {

}

/// Help method to increment block category metrics
/// Main idea is to have a single place to increment metrics
/// It should help to analyze the most popular requests
/// And build s better caching strategy
pub async fn increase_request_category_metrics(
data: &jsonrpc_v2::Data<crate::config::ServerContext>,
block_reference: &near_primitives::types::BlockReference,
block_height: Option<u64>,
) {
match block_reference {
near_primitives::types::BlockReference::BlockId(_) => {
let final_block = data.blocks_info_by_finality.final_cache_block().await;
let expected_earliest_available_block =
final_block.block_height - 5 * data.genesis_info.genesis_config.epoch_length;
if block_height.unwrap_or_default() > expected_earliest_available_block {
// This is request to regular nodes which includes 5 last epochs
REQUESTS_COUNTER.with_label_values(&["regular"]).inc();
} else {
// This is a request to archival nodes which include blocks from genesis (later than 5 epochs ago)
REQUESTS_COUNTER.with_label_values(&["historical"]).inc();
}
}
near_primitives::types::BlockReference::Finality(finality) => {
match finality {
// Increase the REQUESTS_COUNTER `final` metric
// if the request has final finality
near_primitives::types::Finality::DoomSlug
| near_primitives::types::Finality::Final => {
REQUESTS_COUNTER.with_label_values(&["final"]).inc();
}
// Increase the REQUESTS_COUNTER `optimistic` metric
// if the request has optimistic finality
near_primitives::types::Finality::None => {
REQUESTS_COUNTER.with_label_values(&["optimistic"]).inc();
// Increase the REQUESTS_COUNTER `proxy_optimistic` metric
// if the optimistic is not updating and proxy to native JSON-RPC
if crate::metrics::OPTIMISTIC_UPDATING.is_not_working() {
REQUESTS_COUNTER
.with_label_values(&["proxy_optimistic"])
.inc();
}
}
}
}
near_primitives::types::BlockReference::SyncCheckpoint(_) => {
REQUESTS_COUNTER.with_label_values(&["historical"]).inc();
}
}
}

/// Exposes prometheus metrics
#[get("/metrics")]
pub(crate) async fn get_metrics() -> impl Responder {
Expand Down
86 changes: 55 additions & 31 deletions rpc-server/src/modules/blocks/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ pub async fn block(
near_primitives::types::Finality::None,
) = &block_request.block_reference
{
// Increase the OPTIMISTIC_REQUESTS_TOTAL metric
// if the request has optimistic finality
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["optimistic"])
.inc();
if crate::metrics::OPTIMISTIC_UPDATING.is_not_working() {
// Increase the PROXY_OPTIMISTIC_REQUESTS_TOTAL metric
// if optimistic not updating and proxy to near-rpc
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["proxy_optimistic"])
.inc();
// increase metrics before proxy request
crate::metrics::increase_request_category_metrics(
&data,
&block_request.block_reference,
None,
)
.await;
// Proxy if the optimistic updating is not working
let block_view = data.near_rpc_client.call(block_request).await?;
return Ok(near_jsonrpc::primitives::types::blocks::RpcBlockResponse { block_view });
}
Expand Down Expand Up @@ -82,17 +80,15 @@ pub async fn changes_in_block_by_type(
near_primitives::types::Finality::None,
) = &changes_in_block_request.block_reference
{
// Increase the OPTIMISTIC_REQUESTS_TOTAL metric
// if the request has optimistic finality
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["optimistic"])
.inc();
if crate::metrics::OPTIMISTIC_UPDATING.is_not_working() {
// Increase the PROXY_OPTIMISTIC_REQUESTS_TOTAL metric
// if optimistic not updating and proxy to near-rpc
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["proxy_optimistic"])
.inc();
// increase metrics before proxy request
crate::metrics::increase_request_category_metrics(
&data,
&changes_in_block_request.block_reference,
None,
)
.await;
// Proxy if the optimistic updating is not working
return Ok(data.near_rpc_client.call(changes_in_block_request).await?);
}
};
Expand All @@ -117,17 +113,15 @@ pub async fn changes_in_block(
near_primitives::types::Finality::None,
) = &changes_in_block_request.block_reference
{
// Increase the OPTIMISTIC_REQUESTS_TOTAL metric
// if the request has optimistic finality
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["optimistic"])
.inc();
if crate::metrics::OPTIMISTIC_UPDATING.is_not_working() {
// Increase the PROXY_OPTIMISTIC_REQUESTS_TOTAL metric
// if optimistic not updating and proxy to near-rpc
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["proxy_optimistic"])
.inc();
// increase metrics before proxy request
crate::metrics::increase_request_category_metrics(
&data,
&changes_in_block_request.block_reference,
None,
)
.await;
// Proxy if the optimistic updating is not working
return Ok(data.near_rpc_client.call(changes_in_block_request).await?);
}
};
Expand All @@ -143,7 +137,19 @@ async fn block_call(
mut block_request: near_jsonrpc::primitives::types::blocks::RpcBlockRequest,
) -> Result<near_jsonrpc::primitives::types::blocks::RpcBlockResponse, RPCError> {
tracing::debug!("`block` called with parameters: {:?}", block_request);
let result = fetch_block(&data, &block_request.block_reference, "block").await;
let result = match fetch_block(&data, &block_request.block_reference, "block").await {
Ok(block) => {
// increase block category metrics
crate::metrics::increase_request_category_metrics(
&data,
&block_request.block_reference,
Some(block.block_view.header.height),
)
.await;
Ok(block)
}
Err(err) => Err(err),
};

#[cfg(feature = "shadow_data_consistency")]
{
Expand Down Expand Up @@ -185,6 +191,15 @@ async fn changes_in_block_call(
)
.await
.map_err(near_jsonrpc::primitives::errors::RpcError::from)?;

// increase block category metrics
crate::metrics::increase_request_category_metrics(
&data,
&params.block_reference,
Some(cache_block.block_height),
)
.await;

let result = fetch_changes_in_block(&data, cache_block, &params.block_reference).await;
#[cfg(feature = "shadow_data_consistency")]
{
Expand Down Expand Up @@ -217,6 +232,15 @@ async fn changes_in_block_by_type_call(
fetch_block_from_cache_or_get(&data, &params.block_reference, "EXPERIMENTAL_changes")
.await
.map_err(near_jsonrpc::primitives::errors::RpcError::from)?;

// increase block category metrics
crate::metrics::increase_request_category_metrics(
&data,
&params.block_reference,
Some(cache_block.block_height),
)
.await;

let result = fetch_changes_in_block_by_type(
&data,
cache_block,
Expand Down
26 changes: 16 additions & 10 deletions rpc-server/src/modules/queries/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ pub async fn query(
near_primitives::types::Finality::None,
) = &query_request.block_reference
{
// Increase the OPTIMISTIC_REQUESTS_TOTAL metric
// if the request has optimistic finality
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["optimistic"])
.inc();
return if crate::metrics::OPTIMISTIC_UPDATING.is_not_working() {
// Increase the PROXY_OPTIMISTIC_REQUESTS_TOTAL metric
// if optimistic not updating and proxy to near-rpc
crate::metrics::REQUESTS_COUNTER
.with_label_values(&["proxy_optimistic"])
.inc();
// increase metrics before proxy request
crate::metrics::increase_request_category_metrics(
&data,
&query_request.block_reference,
None,
)
.await;
// Proxy if the optimistic updating is not working
Ok(data.near_rpc_client.call(query_request).await?)
} else {
// query_call with optimistic block
Expand All @@ -59,6 +57,14 @@ async fn query_call(
let block = fetch_block_from_cache_or_get(data, &query_request.block_reference, "query")
.await
.map_err(near_jsonrpc::primitives::errors::RpcError::from)?;

// increase block category metrics
crate::metrics::increase_request_category_metrics(
data,
&query_request.block_reference,
Some(block.block_height),
)
.await;
let result = match &query_request.request {
near_primitives::views::QueryRequest::ViewAccount { account_id } => {
view_account(data, block, account_id, is_optimistic).await
Expand Down

0 comments on commit 8a2307a

Please sign in to comment.