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(monitoring): add non-rpc providers cache latency Grafana panel #783

Merged
merged 2 commits into from
Sep 23, 2024
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
5 changes: 5 additions & 0 deletions src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ mod test {
("RPC_PROXY_ANALYTICS_S3_ENDPOINT", "s3://127.0.0.1"),
("RPC_PROXY_ANALYTICS_EXPORT_BUCKET", "EXPORT_BUCKET"),
// Providers config
(
"RPC_PROXY_PROVIDER_CACHE_REDIS_ADDR",
"redis://127.0.0.1/providers_cache",
),
("RPC_PROXY_PROVIDER_INFURA_PROJECT_ID", "INFURA_PROJECT_ID"),
("RPC_PROXY_PROVIDER_POKT_PROJECT_ID", "POKT_PROJECT_ID"),
("RPC_PROXY_PROVIDER_ZERION_API_KEY", "ZERION_API_KEY"),
Expand Down Expand Up @@ -255,6 +259,7 @@ mod test {
providers: ProvidersConfig {
prometheus_query_url: Some("PROMETHEUS_QUERY_URL".to_owned()),
prometheus_workspace_header: Some("PROMETHEUS_WORKSPACE_HEADER".to_owned()),
cache_redis_addr: Some("redis://127.0.0.1/providers_cache".to_owned()),
infura_project_id: "INFURA_PROJECT_ID".to_string(),
pokt_project_id: "POKT_PROJECT_ID".to_string(),
quicknode_api_tokens: "QUICKNODE_API_TOKENS".to_string(),
Expand Down
19 changes: 19 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub struct Metrics {
pub history_lookup_success_counter: Counter<u64>,
pub history_lookup_latency_tracker: Histogram<f64>,

// Generic Non-RPC providers caching
pub non_rpc_providers_cache_latency_tracker: Histogram<f64>,

// System metrics
pub cpu_usage: Histogram<f64>,
pub memory_total: Histogram<f64>,
Expand Down Expand Up @@ -252,6 +255,10 @@ impl Metrics {
.u64_counter("rate_limited_responses_counter")
.with_description("Rate limiting responses counter")
.init();
let non_rpc_providers_cache_latency_tracker = meter
.f64_histogram("non_rpc_providers_cache_latency_tracker")
.with_description("The latency of non-RPC providers cache lookups")
.init();

Metrics {
rpc_call_counter,
Expand Down Expand Up @@ -282,6 +289,7 @@ impl Metrics {
history_lookup_counter,
history_lookup_success_counter,
history_lookup_latency_tracker,
non_rpc_providers_cache_latency_tracker,
cpu_usage,
memory_total,
memory_used,
Expand Down Expand Up @@ -592,6 +600,17 @@ impl Metrics {
);
}

pub fn add_non_rpc_providers_cache_latency(&self, start: SystemTime) {
self.non_rpc_providers_cache_latency_tracker.record(
&otel::Context::new(),
start
.elapsed()
.unwrap_or(Duration::from_secs(0))
.as_secs_f64(),
&[],
);
}

pub fn add_rate_limited_response(&self) {
self.rate_limited_responses_counter
.add(&otel::Context::new(), 1, &[]);
Expand Down
35 changes: 30 additions & 5 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use {
hash::Hash,
sync::Arc,
},
tracing::{debug, log::warn},
tracing::{debug, error, log::warn},
wc::metrics::TaskMetrics,
};

Expand Down Expand Up @@ -87,6 +87,9 @@ pub struct ProvidersConfig {
pub prometheus_query_url: Option<String>,
pub prometheus_workspace_header: Option<String>,

/// Redis address for provider's responses caching
pub cache_redis_addr: Option<String>,

pub infura_project_id: String,
pub pokt_project_id: String,
pub quicknode_api_tokens: String,
Expand Down Expand Up @@ -151,6 +154,27 @@ impl ProviderRepository {
.clone()
.unwrap_or("localhost:9090".into());

// Redis pool for providers responses caching where needed
let mut redis_pool = None;
if let Some(redis_addr) = &config.cache_redis_addr {
let redis_builder = deadpool_redis::Config::from_url(redis_addr)
.builder()
.map_err(|e| {
error!(
"Failed to create redis pool builder for provider's responses caching: {:?}",
e
);
})
.expect("Failed to create redis pool builder for provider's responses caching, builder is None");

redis_pool = Some(Arc::new(
redis_builder
.runtime(deadpool_redis::Runtime::Tokio1)
.build()
.expect("Failed to create redis pool"),
));
};

// Don't crash the application if the ZERION_API_KEY is not set
// TODO: find a better way to handle this
let zerion_api_key = config
Expand Down Expand Up @@ -189,6 +213,7 @@ impl ProviderRepository {
let solscan_provider = Arc::new(SolScanProvider::new(
config.solscan_api_v1_token.clone(),
config.solscan_api_v2_token.clone(),
redis_pool.clone(),
));

let mut balance_providers: HashMap<CaipNamespaces, Arc<dyn BalanceProvider>> =
Expand Down Expand Up @@ -605,7 +630,7 @@ pub trait RateLimited {
}

#[async_trait]
pub trait HistoryProvider: Send + Sync + Debug {
pub trait HistoryProvider: Send + Sync {
async fn get_transactions(
&self,
address: String,
Expand Down Expand Up @@ -640,7 +665,7 @@ pub trait OnRampProvider: Send + Sync + Debug {
}

#[async_trait]
pub trait BalanceProvider: Send + Sync + Debug {
pub trait BalanceProvider: Send + Sync {
async fn get_balance(
&self,
address: String,
Expand All @@ -650,7 +675,7 @@ pub trait BalanceProvider: Send + Sync + Debug {
}

#[async_trait]
pub trait FungiblePriceProvider: Send + Sync + Debug {
pub trait FungiblePriceProvider: Send + Sync {
async fn get_price(
&self,
chain_id: &str,
Expand All @@ -661,7 +686,7 @@ pub trait FungiblePriceProvider: Send + Sync + Debug {
}

#[async_trait]
pub trait ConversionProvider: Send + Sync + Debug {
pub trait ConversionProvider: Send + Sync {
async fn get_tokens_list(
&self,
params: TokensListQueryParams,
Expand Down
Loading
Loading