Skip to content

Commit

Permalink
graph, server: Measure query timings for successful and failed queries
Browse files Browse the repository at this point in the history
Before, we weren't recording failed queries at all
  • Loading branch information
lutter committed Apr 28, 2022
1 parent 475b65b commit cadda88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
11 changes: 11 additions & 0 deletions graph/src/data/query/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ impl QueryResults {
pub fn first(&self) -> Option<&Arc<QueryResult>> {
self.results.first()
}

pub fn has_errors(&self) -> bool {
self.results.iter().any(|result| result.has_errors())
}

pub fn deployment_hash(&self) -> Option<&DeploymentHash> {
self.results
.iter()
.filter_map(|result| result.deployment.as_ref())
.next()
}
}

impl Serialize for QueryResults {
Expand Down
40 changes: 15 additions & 25 deletions server/http/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::task::Context;
use std::task::Poll;
use std::time::Instant;

use graph::data::query::QueryResults;
use graph::prelude::*;
use graph::{components::server::query::GraphQLServerError, data::query::QueryTarget};
use http::header;
Expand All @@ -19,7 +20,6 @@ use crate::request::parse_graphql_request;

pub struct GraphQLServiceMetrics {
query_execution_time: Box<HistogramVec>,
failed_query_execution_time: Box<HistogramVec>,
}

impl fmt::Debug for GraphQLServiceMetrics {
Expand All @@ -34,36 +34,29 @@ impl GraphQLServiceMetrics {
.new_histogram_vec(
"query_execution_time",
"Execution time for successful GraphQL queries",
vec![String::from("deployment")],
vec![String::from("deployment"), String::from("status")],
vec![0.1, 0.5, 1.0, 10.0, 100.0],
)
.expect("failed to create `query_execution_time` histogram");

let failed_query_execution_time = registry
.new_histogram_vec(
"query_failed_execution_time",
"Execution time for failed GraphQL queries",
vec![String::from("deployment")],
vec![0.1, 0.5, 1.0, 10.0, 100.0],
)
.expect("failed to create `query_failed_execution_time` histogram");

Self {
query_execution_time,
failed_query_execution_time,
}
}

pub fn observe_query_execution_time(&self, duration: f64, deployment_id: String) {
pub fn observe_query(&self, duration: Duration, results: &QueryResults) {
let id = results
.deployment_hash()
.map(|h| h.as_str())
.unwrap_or("unknown");
let status = if results.has_errors() {
"failed"
} else {
"success"
};
self.query_execution_time
.with_label_values(vec![deployment_id.as_ref()].as_slice())
.observe(duration);
}

pub fn observe_failed_query_execution_time(&self, duration: f64, deployment_id: String) {
self.failed_query_execution_time
.with_label_values(vec![deployment_id.as_ref()].as_slice())
.observe(duration);
.with_label_values(&[id, status])
.observe(duration.as_secs_f64());
}
}

Expand Down Expand Up @@ -197,10 +190,7 @@ where
Err(e) => return Err(e),
};

if let Some(id) = result.first().and_then(|res| res.deployment.clone()) {
service_metrics
.observe_query_execution_time(start.elapsed().as_secs_f64(), id.to_string());
}
service_metrics.observe_query(start.elapsed(), &result);

Ok(result.as_http_response())
}
Expand Down

0 comments on commit cadda88

Please sign in to comment.