|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::fmt; |
| 3 | +use std::sync::Arc; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +use graph::data::query::QueryResults; |
| 7 | +use graph::prelude::{DeploymentHash, GraphQLMetrics as GraphQLMetricsTrait, MetricsRegistry}; |
| 8 | +use graph::prometheus::{Gauge, Histogram, HistogramVec}; |
| 9 | + |
| 10 | +pub struct GraphQLMetrics { |
| 11 | + query_execution_time: Box<HistogramVec>, |
| 12 | + query_parsing_time: Box<HistogramVec>, |
| 13 | + query_validation_time: Box<HistogramVec>, |
| 14 | + query_result_size: Box<Histogram>, |
| 15 | + query_result_size_max: Box<Gauge>, |
| 16 | +} |
| 17 | + |
| 18 | +impl fmt::Debug for GraphQLMetrics { |
| 19 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 20 | + write!(f, "GraphQLMetrics {{ }}") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl GraphQLMetricsTrait for GraphQLMetrics { |
| 25 | + fn observe_query_execution(&self, duration: Duration, results: &QueryResults) { |
| 26 | + let id = results |
| 27 | + .deployment_hash() |
| 28 | + .map(|h| h.as_str()) |
| 29 | + .unwrap_or_else(|| { |
| 30 | + if results.not_found() { |
| 31 | + "notfound" |
| 32 | + } else { |
| 33 | + "unknown" |
| 34 | + } |
| 35 | + }); |
| 36 | + let status = if results.has_errors() { |
| 37 | + "failed" |
| 38 | + } else { |
| 39 | + "success" |
| 40 | + }; |
| 41 | + self.query_execution_time |
| 42 | + .with_label_values(&[id, status]) |
| 43 | + .observe(duration.as_secs_f64()); |
| 44 | + } |
| 45 | + |
| 46 | + fn observe_query_parsing(&self, duration: Duration, results: &QueryResults) { |
| 47 | + let id = results |
| 48 | + .deployment_hash() |
| 49 | + .map(|h| h.as_str()) |
| 50 | + .unwrap_or_else(|| { |
| 51 | + if results.not_found() { |
| 52 | + "notfound" |
| 53 | + } else { |
| 54 | + "unknown" |
| 55 | + } |
| 56 | + }); |
| 57 | + self.query_parsing_time |
| 58 | + .with_label_values(&[id]) |
| 59 | + .observe(duration.as_secs_f64()); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl GraphQLMetrics { |
| 64 | + pub fn new(registry: Arc<dyn MetricsRegistry>) -> Self { |
| 65 | + let query_execution_time = registry |
| 66 | + .new_histogram_vec( |
| 67 | + "query_execution_time", |
| 68 | + "Execution time for successful GraphQL queries", |
| 69 | + vec![String::from("deployment"), String::from("status")], |
| 70 | + vec![0.1, 0.5, 1.0, 10.0, 100.0], |
| 71 | + ) |
| 72 | + .expect("failed to create `query_execution_time` histogram"); |
| 73 | + let query_parsing_time = registry |
| 74 | + .new_histogram_vec( |
| 75 | + "query_parsing_time", |
| 76 | + "Parsing time for GraphQL queries", |
| 77 | + vec![String::from("deployment")], |
| 78 | + vec![0.1, 0.5, 1.0, 10.0, 100.0], |
| 79 | + ) |
| 80 | + .expect("failed to create `query_parsing_time` histogram"); |
| 81 | + |
| 82 | + let query_validation_time = registry |
| 83 | + .new_histogram_vec( |
| 84 | + "query_validation_time", |
| 85 | + "Validation time for GraphQL queries", |
| 86 | + vec![String::from("deployment")], |
| 87 | + vec![0.1, 0.5, 1.0, 10.0, 100.0], |
| 88 | + ) |
| 89 | + .expect("failed to create `query_validation_time` histogram"); |
| 90 | + |
| 91 | + let bins = (10..32).map(|n| 2u64.pow(n) as f64).collect::<Vec<_>>(); |
| 92 | + let query_result_size = registry |
| 93 | + .new_histogram( |
| 94 | + "query_result_size", |
| 95 | + "the size of the result of successful GraphQL queries (in CacheWeight)", |
| 96 | + bins, |
| 97 | + ) |
| 98 | + .unwrap(); |
| 99 | + |
| 100 | + let query_result_size_max = registry |
| 101 | + .new_gauge( |
| 102 | + "query_result_max", |
| 103 | + "the maximum size of a query result (in CacheWeight)", |
| 104 | + HashMap::new(), |
| 105 | + ) |
| 106 | + .unwrap(); |
| 107 | + |
| 108 | + Self { |
| 109 | + query_execution_time, |
| 110 | + query_parsing_time, |
| 111 | + query_validation_time, |
| 112 | + query_result_size, |
| 113 | + query_result_size_max, |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Tests need to construct one of these, but normal code doesn't |
| 118 | + #[cfg(debug_assertions)] |
| 119 | + pub fn make(registry: Arc<dyn MetricsRegistry>) -> Self { |
| 120 | + Self::new(registry) |
| 121 | + } |
| 122 | + |
| 123 | + pub fn observe_query_validation(&self, duration: Duration, id: &DeploymentHash) { |
| 124 | + self.query_validation_time |
| 125 | + .with_label_values(&[id.as_str()]) |
| 126 | + .observe(duration.as_secs_f64()); |
| 127 | + } |
| 128 | + |
| 129 | + pub fn observe_query_result_size(&self, size: usize) { |
| 130 | + let size = size as f64; |
| 131 | + self.query_result_size.observe(size); |
| 132 | + if self.query_result_size_max.get() < size { |
| 133 | + self.query_result_size_max.set(size); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments