1919)
2020from graphql import DocumentNode
2121from sentry_sdk import capture_exception
22- from sentry_sdk import metrics as sentry_metrics
23- from shared .metrics import Counter , Histogram
22+ from shared .metrics import Counter , Histogram , inc_counter
2423
2524from codecov .commands .exceptions import BaseException
2625from codecov .commands .executor import get_executor_from_request
5150 buckets = [0.05 , 0.1 , 0.25 , 0.5 , 0.75 , 1 , 2 , 5 , 10 , 30 ],
5251)
5352
53+ GQL_REQUEST_MADE_COUNTER = Counter (
54+ "api_gql_requests_made" ,
55+ "Total API GQL requests made" ,
56+ ["path" ],
57+ )
58+
59+ GQL_ERROR_TYPE_COUNTER = Counter (
60+ "api_gql_errors" ,
61+ "Number of times API GQL endpoint failed with an exception by type" ,
62+ ["error_type" , "path" ],
63+ )
5464
5565# covers named and 3 unnamed operations (see graphql_api/types/query/query.py)
5666GQL_TYPE_AND_NAME_PATTERN = r"^(query|mutation|subscription)(?:\(\$input:|) (\w+)(?:\(| \(|{| {|!)|^(?:{) (me|owner|config)(?:\(| |{)"
@@ -109,9 +119,13 @@ def request_started(self, context):
109119 """
110120 self .set_type_and_name (query = context ["clean_query" ])
111121 self .start_timestamp = time .perf_counter ()
112- GQL_HIT_COUNTER .labels (
113- operation_type = self .operation_type , operation_name = self .operation_name
114- ).inc ()
122+ inc_counter (
123+ GQL_HIT_COUNTER ,
124+ labels = dict (
125+ operation_type = self .operation_type ,
126+ operation_name = self .operation_name ,
127+ ),
128+ )
115129
116130 def request_finished (self , context ):
117131 """
@@ -226,10 +240,12 @@ async def post(self, request, *args, **kwargs):
226240 "user" : request .user ,
227241 }
228242 log .info ("GraphQL Request" , extra = log_data )
229- sentry_metrics .incr ("graphql.info.request_made" , tags = {"path" : req_path })
230-
243+ inc_counter (GQL_REQUEST_MADE_COUNTER , labels = dict (path = req_path ))
231244 if self ._check_ratelimit (request = request ):
232- sentry_metrics .incr ("graphql.error.rate_limit" , tags = {"path" : req_path })
245+ inc_counter (
246+ GQL_ERROR_TYPE_COUNTER ,
247+ labels = dict (error_type = "rate_limit" , path = req_path ),
248+ )
233249 return JsonResponse (
234250 data = {
235251 "status" : 429 ,
@@ -245,7 +261,10 @@ async def post(self, request, *args, **kwargs):
245261 data = json .loads (content )
246262
247263 if "errors" in data :
248- sentry_metrics .incr ("graphql.error.all" , tags = {"path" : req_path })
264+ inc_counter (
265+ GQL_ERROR_TYPE_COUNTER ,
266+ labels = dict (error_type = "all" , path = req_path ),
267+ )
249268 try :
250269 if data ["errors" ][0 ]["extensions" ]["cost" ]:
251270 costs = data ["errors" ][0 ]["extensions" ]["cost" ]
@@ -257,9 +276,12 @@ async def post(self, request, *args, **kwargs):
257276 request_body = req_body ,
258277 ),
259278 )
260- sentry_metrics .incr (
261- "graphql.error.query_cost_exceeded" ,
262- tags = {"path" : req_path },
279+ inc_counter (
280+ GQL_ERROR_TYPE_COUNTER ,
281+ labels = dict (
282+ error_type = "query_cost_exceeded" ,
283+ path = req_path ,
284+ ),
263285 )
264286 return HttpResponseBadRequest (
265287 JsonResponse ("Your query is too costly." )
0 commit comments