1
- use std:: collections:: HashMap ;
2
1
use std:: sync:: Arc ;
3
2
use std:: time:: Instant ;
4
3
4
+ use crate :: metrics:: GraphQLMetrics ;
5
5
use crate :: prelude:: { QueryExecutionOptions , StoreResolver , SubscriptionExecutionOptions } ;
6
6
use crate :: query:: execute_query;
7
7
use crate :: subscription:: execute_prepared_subscription;
8
8
use graph:: prelude:: MetricsRegistry ;
9
- use graph:: prometheus:: { Gauge , Histogram } ;
10
9
use graph:: {
11
10
components:: store:: SubscriptionManager ,
12
11
prelude:: {
13
- async_trait, o, CheapClone , DeploymentState , GraphQlRunner as GraphQlRunnerTrait , Logger ,
14
- Query , QueryExecutionError , Subscription , SubscriptionError , SubscriptionResult , ENV_VARS ,
12
+ async_trait, o, CheapClone , DeploymentState , GraphQLMetrics as GraphQLMetricsTrait ,
13
+ GraphQlRunner as GraphQlRunnerTrait , Logger , Query , QueryExecutionError , Subscription ,
14
+ SubscriptionError , SubscriptionResult , ENV_VARS ,
15
15
} ,
16
16
} ;
17
17
use graph:: { data:: graphql:: effort:: LoadManager , prelude:: QueryStoreManager } ;
@@ -20,59 +20,13 @@ use graph::{
20
20
prelude:: QueryStore ,
21
21
} ;
22
22
23
- pub struct ResultSizeMetrics {
24
- histogram : Box < Histogram > ,
25
- max_gauge : Box < Gauge > ,
26
- }
27
-
28
- impl ResultSizeMetrics {
29
- fn new ( registry : Arc < dyn MetricsRegistry > ) -> Self {
30
- // Divide the Histogram into exponentially sized buckets between 1k and 4G
31
- let bins = ( 10 ..32 ) . map ( |n| 2u64 . pow ( n) as f64 ) . collect :: < Vec < _ > > ( ) ;
32
- let histogram = registry
33
- . new_histogram (
34
- "query_result_size" ,
35
- "the size of the result of successful GraphQL queries (in CacheWeight)" ,
36
- bins,
37
- )
38
- . unwrap ( ) ;
39
-
40
- let max_gauge = registry
41
- . new_gauge (
42
- "query_result_max" ,
43
- "the maximum size of a query result (in CacheWeight)" ,
44
- HashMap :: new ( ) ,
45
- )
46
- . unwrap ( ) ;
47
-
48
- Self {
49
- histogram,
50
- max_gauge,
51
- }
52
- }
53
-
54
- // Tests need to construct one of these, but normal code doesn't
55
- #[ cfg( debug_assertions) ]
56
- pub fn make ( registry : Arc < dyn MetricsRegistry > ) -> Self {
57
- Self :: new ( registry)
58
- }
59
-
60
- pub fn observe ( & self , size : usize ) {
61
- let size = size as f64 ;
62
- self . histogram . observe ( size) ;
63
- if self . max_gauge . get ( ) < size {
64
- self . max_gauge . set ( size) ;
65
- }
66
- }
67
- }
68
-
69
23
/// GraphQL runner implementation for The Graph.
70
24
pub struct GraphQlRunner < S , SM > {
71
25
logger : Logger ,
72
26
store : Arc < S > ,
73
27
subscription_manager : Arc < SM > ,
74
28
load_manager : Arc < LoadManager > ,
75
- result_size : Arc < ResultSizeMetrics > ,
29
+ graphql_metrics : Arc < GraphQLMetrics > ,
76
30
}
77
31
78
32
#[ cfg( debug_assertions) ]
@@ -95,13 +49,13 @@ where
95
49
registry : Arc < dyn MetricsRegistry > ,
96
50
) -> Self {
97
51
let logger = logger. new ( o ! ( "component" => "GraphQlRunner" ) ) ;
98
- let result_size = Arc :: new ( ResultSizeMetrics :: new ( registry) ) ;
52
+ let graphql_metrics = Arc :: new ( GraphQLMetrics :: new ( registry) ) ;
99
53
GraphQlRunner {
100
54
logger,
101
55
store,
102
56
subscription_manager,
103
57
load_manager,
104
- result_size ,
58
+ graphql_metrics ,
105
59
}
106
60
}
107
61
143
97
max_depth : Option < u8 > ,
144
98
max_first : Option < u32 > ,
145
99
max_skip : Option < u32 > ,
146
- result_size : Arc < ResultSizeMetrics > ,
100
+ metrics : Arc < GraphQLMetrics > ,
147
101
) -> Result < QueryResults , QueryResults > {
148
102
// We need to use the same `QueryStore` for the entire query to ensure
149
103
// we have a consistent view if the world, even when replicas, which
@@ -175,6 +129,7 @@ where
175
129
query,
176
130
max_complexity,
177
131
max_depth,
132
+ metrics. cheap_clone ( ) ,
178
133
) ?;
179
134
self . load_manager
180
135
. decide (
@@ -197,7 +152,7 @@ where
197
152
bc,
198
153
error_policy,
199
154
query. schema . id ( ) . clone ( ) ,
200
- result_size . cheap_clone ( ) ,
155
+ metrics . cheap_clone ( ) ,
201
156
)
202
157
. await ?;
203
158
max_block = max_block. max ( resolver. block_number ( ) ) ;
@@ -259,7 +214,7 @@ where
259
214
max_depth,
260
215
max_first,
261
216
max_skip,
262
- self . result_size . cheap_clone ( ) ,
217
+ self . graphql_metrics . clone ( ) ,
263
218
)
264
219
. await
265
220
. unwrap_or_else ( |e| e)
@@ -281,6 +236,7 @@ where
281
236
subscription. query ,
282
237
ENV_VARS . graphql . max_complexity ,
283
238
ENV_VARS . graphql . max_depth ,
239
+ self . graphql_metrics . cheap_clone ( ) ,
284
240
) ?;
285
241
286
242
if let Err ( err) = self
@@ -306,12 +262,16 @@ where
306
262
max_depth : ENV_VARS . graphql . max_depth ,
307
263
max_first : ENV_VARS . graphql . max_first ,
308
264
max_skip : ENV_VARS . graphql . max_skip ,
309
- result_size : self . result_size . clone ( ) ,
265
+ graphql_metrics : self . graphql_metrics . clone ( ) ,
310
266
} ,
311
267
)
312
268
}
313
269
314
270
fn load_manager ( & self ) -> Arc < LoadManager > {
315
271
self . load_manager . clone ( )
316
272
}
273
+
274
+ fn metrics ( & self ) -> Arc < dyn GraphQLMetricsTrait > {
275
+ self . graphql_metrics . clone ( )
276
+ }
317
277
}
0 commit comments