Skip to content

Commit 6281e1e

Browse files
committed
all: Report moving average of total query time to Prometheus
1 parent 7ee08cc commit 6281e1e

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

graph/src/data/graphql/effort.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::str::FromStr;
99
use std::sync::{Arc, RwLock};
1010
use std::time::{Duration, Instant};
1111

12+
use crate::components::metrics::{Gauge, MetricsRegistry};
1213
use crate::components::store::PoolWaitStats;
1314
use crate::data::graphql::shape_hash::shape_hash;
1415
use crate::prelude::{info, o, warn, Logger};
@@ -78,9 +79,10 @@ impl QueryEffort {
7879
}
7980
}
8081

81-
pub fn add(&self, shape_hash: u64, duration: Duration) {
82+
pub fn add(&self, shape_hash: u64, duration: Duration, gauge: &Box<Gauge>) {
8283
let mut inner = self.inner.write().unwrap();
8384
inner.add(shape_hash, duration);
85+
gauge.set(inner.total.average().unwrap_or(*ZERO_DURATION).as_millis() as f64);
8486
}
8587

8688
/// Return what we know right now about the effort for the query
@@ -195,13 +197,15 @@ pub struct LoadManager {
195197
/// restarting the process
196198
jailed_queries: RwLock<HashSet<u64>>,
197199
kill_state: RwLock<KillState>,
200+
effort_gauge: Box<Gauge>,
198201
}
199202

200203
impl LoadManager {
201204
pub fn new(
202205
logger: &Logger,
203206
store_wait_stats: PoolWaitStats,
204207
blocked_queries: Vec<Arc<q::Document>>,
208+
registry: Arc<dyn MetricsRegistry>,
205209
) -> Self {
206210
let logger = logger.new(o!("component" => "LoadManager"));
207211
let blocked_queries = blocked_queries
@@ -212,19 +216,27 @@ impl LoadManager {
212216
logger,
213217
"Creating LoadManager with disabled={}", *LOAD_MANAGEMENT_DISABLED,
214218
);
219+
let effort_gauge = registry
220+
.new_gauge(
221+
String::from("query_effort_ms"),
222+
String::from("Moving average of time spent running queries"),
223+
HashMap::new(),
224+
)
225+
.expect("failed to create `query_effort_ms` counter");
215226
Self {
216227
logger,
217228
effort: QueryEffort::default(),
218229
store_wait_stats,
219230
blocked_queries,
220231
jailed_queries: RwLock::new(HashSet::new()),
221232
kill_state: RwLock::new(KillState::new()),
233+
effort_gauge,
222234
}
223235
}
224236

225237
pub fn add_query(&self, shape_hash: u64, duration: Duration) {
226238
if !*LOAD_MANAGEMENT_DISABLED {
227-
self.effort.add(shape_hash, duration);
239+
self.effort.add(shape_hash, duration, &self.effort_gauge);
228240
}
229241
}
230242

graphql/tests/query.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ use graphql_parser::{query as q, Pos};
55
use lazy_static::lazy_static;
66
use std::collections::HashMap;
77
use std::iter::FromIterator;
8-
use std::sync::{Arc, RwLock};
8+
use std::sync::Arc;
99
use std::time::{Duration, Instant};
1010

11-
use graph::data::graphql::effort::LoadManager;
1211
use graph::prelude::{
1312
futures03::stream::StreamExt, futures03::FutureExt, futures03::TryFutureExt, o, slog, tokio,
14-
Entity, EntityKey, EntityOperation, EthereumBlockPointer, FutureExtension, Logger, MovingStats,
15-
Query, QueryError, QueryExecutionError, QueryResult, QueryVariables, Schema, Store,
13+
Entity, EntityKey, EntityOperation, EthereumBlockPointer, FutureExtension, Logger, Query,
14+
QueryError, QueryExecutionError, QueryResult, QueryVariables, Schema, Store,
1615
SubgraphDeploymentEntity, SubgraphDeploymentId, SubgraphDeploymentStore, SubgraphManifest,
1716
Subscription, SubscriptionError, Value,
1817
};
1918
use graph_graphql::prelude::*;
2019
use test_store::{
2120
execute_subgraph_query_with_complexity, execute_subgraph_query_with_deadline, return_err,
22-
transact_entity_operations, BLOCK_ONE, GENESIS_PTR, LOGGER, STORE,
21+
transact_entity_operations, BLOCK_ONE, GENESIS_PTR, LOAD_MANAGER, LOGGER, STORE,
2322
};
2423

2524
lazy_static! {
@@ -231,9 +230,7 @@ fn execute_query_document_with_variables(
231230
query: q::Document,
232231
variables: Option<QueryVariables>,
233232
) -> QueryResult {
234-
let stats = Arc::new(RwLock::new(MovingStats::default()));
235-
let load_manager = Arc::new(LoadManager::new(&*LOGGER, stats, vec![]));
236-
let runner = GraphQlRunner::new(&*LOGGER, STORE.clone(), load_manager);
233+
let runner = GraphQlRunner::new(&*LOGGER, STORE.clone(), LOAD_MANAGER.clone());
237234
let query = Query::new(Arc::new(api_test_schema()), query, variables);
238235

239236
return_err!(runner.execute(query, None, None, None))

node/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ async fn main() {
516516
let connection_pool_registry = metrics_registry.clone();
517517
let stores_metrics_registry = metrics_registry.clone();
518518
let graphql_metrics_registry = metrics_registry.clone();
519+
519520
let stores_logger = logger.clone();
520521
let stores_error_logger = logger.clone();
521522
let stores_eth_adapters = eth_adapters.clone();
@@ -593,6 +594,7 @@ async fn main() {
593594
&logger,
594595
wait_stats.clone(),
595596
expensive_queries,
597+
metrics_registry.clone(),
596598
));
597599
let graphql_runner = Arc::new(GraphQlRunner::new(
598600
&logger,

store/test-store/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ lazy_static! {
3939

4040
pub static ref POOL_WAIT_STATS: PoolWaitStats = Arc::new(RwLock::new(MovingStats::default()));
4141

42-
pub static ref LOAD_MANAGER: Arc<LoadManager> = Arc::new(LoadManager::new(&*LOGGER, POOL_WAIT_STATS.clone(), Vec::new()));
42+
pub static ref LOAD_MANAGER: Arc<LoadManager> = Arc::new(
43+
LoadManager::new(&*LOGGER,
44+
POOL_WAIT_STATS.clone(),
45+
Vec::new(),
46+
Arc::new(MockMetricsRegistry::new())));
4347

4448
// Create Store instance once for use with each of the tests.
4549
pub static ref STORE: Arc<Store> = {

0 commit comments

Comments
 (0)