88from rest_framework .negotiation import DefaultContentNegotiation
99from rest_framework .permissions import AllowAny
1010from rest_framework .views import APIView
11+ from shared .metrics import Counter , inc_counter
1112
1213from api .shared .mixins import RepoPropertyMixin
1314from core .models import Branch , Pull
1920
2021log = logging .getLogger (__name__ )
2122
23+ FLARE_USE_COUNTER = Counter (
24+ "graph_activity" ,
25+ "How are graphs and flare being used?" ,
26+ [
27+ "position" ,
28+ ],
29+ )
30+ FLARE_SUCCESS_COUNTER = Counter (
31+ "graph_activity" ,
32+ "How often are graphs successfully generated?" ,
33+ [
34+ "graph_type" ,
35+ ],
36+ )
37+
2238
2339class IgnoreClientContentNegotiation (DefaultContentNegotiation ):
2440 def select_parser (self , request , parsers ):
@@ -151,7 +167,16 @@ def get_object(self, request, *args, **kwargs):
151167 options = dict ()
152168 graph = self .kwargs .get ("graph" )
153169
170+ # a flare graph has been requested
171+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 0 ))
172+ log .info (
173+ msg = "flare graph activity" ,
174+ extra = dict (position = "start" , graph_type = graph , kwargs = self .kwargs ),
175+ )
176+
154177 flare = self .get_flare ()
178+ # flare success, will generate and return graph
179+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 20 ))
155180
156181 if graph == "tree" :
157182 options ["width" ] = int (
@@ -164,6 +189,11 @@ def get_object(self, request, *args, **kwargs):
164189 "height" , settings ["sunburst" ]["options" ]["height" ] or 100
165190 )
166191 )
192+ inc_counter (FLARE_SUCCESS_COUNTER , labels = dict (graph_type = graph ))
193+ log .info (
194+ msg = "flare graph activity" ,
195+ extra = dict (position = "success" , graph_type = graph , kwargs = self .kwargs ),
196+ )
167197 return tree (flare , None , None , ** options )
168198 elif graph == "icicle" :
169199 options ["width" ] = int (
@@ -176,6 +206,11 @@ def get_object(self, request, *args, **kwargs):
176206 "height" , settings ["icicle" ]["options" ]["height" ] or 100
177207 )
178208 )
209+ inc_counter (FLARE_SUCCESS_COUNTER , labels = dict (graph_type = graph ))
210+ log .info (
211+ msg = "flare graph activity" ,
212+ extra = dict (position = "success" , graph_type = graph , kwargs = self .kwargs ),
213+ )
179214 return icicle (flare , ** options )
180215 elif graph == "sunburst" :
181216 options ["width" ] = int (
@@ -188,16 +223,27 @@ def get_object(self, request, *args, **kwargs):
188223 "height" , settings ["sunburst" ]["options" ]["height" ] or 100
189224 )
190225 )
226+ inc_counter (FLARE_SUCCESS_COUNTER , labels = dict (graph_type = graph ))
227+ log .info (
228+ msg = "flare graph activity" ,
229+ extra = dict (position = "success" , graph_type = graph , kwargs = self .kwargs ),
230+ )
191231 return sunburst (flare , ** options )
192232
193233 def get_flare (self ):
194234 pullid = self .kwargs .get ("pullid" )
195235
196236 if not pullid :
237+ # pullid not in kwargs, try to generate flare from commit
238+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 12 ))
197239 return self .get_commit_flare ()
198240 else :
241+ # pullid was included in the request
242+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 1 ))
199243 pull_flare = self .get_pull_flare (pullid )
200244 if pull_flare is None :
245+ # failed to get flare from pull OR commit - graph request failed
246+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 15 ))
201247 raise NotFound (
202248 "Not found. Note: private repositories require ?token arguments"
203249 )
@@ -207,47 +253,73 @@ def get_commit_flare(self):
207253 commit = self .get_commit ()
208254
209255 if commit is None :
256+ # could not find a commit - graph request failed
257+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 13 ))
210258 raise NotFound (
211259 "Not found. Note: private repositories require ?token arguments"
212260 )
213261
262+ # will attempt to build a report from a commit
263+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 10 ))
214264 report = report_service .build_report_from_commit (commit )
215265
216266 if report is None :
267+ # report generation failed
268+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 14 ))
217269 raise NotFound ("Not found. Note: file for chunks not found in storage" )
218270
271+ # report successfully generated
272+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 11 ))
219273 return report .flare (None , [70 , 100 ])
220274
221275 def get_pull_flare (self , pullid ):
222276 try :
223277 repo = self .repo
278+ # repo was included
279+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 2 ))
224280 except Http404 :
225281 return None
226282 pull = Pull .objects .filter (pullid = pullid , repository_id = repo .repoid ).first ()
227283 if pull is not None :
284+ # pull found
285+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 3 ))
228286 if pull ._flare is not None or pull ._flare_storage_path is not None :
287+ # pull has flare
288+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 4 ))
229289 return pull .flare
290+ # pull not found or pull does not have flare, try to generate flare
291+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 5 ))
230292 return self .get_commit_flare ()
231293
232294 def get_commit (self ):
233295 try :
234296 repo = self .repo
297+ # repo included in request
298+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 6 ))
235299 except Http404 :
236300 return None
237301 if repo .private and repo .image_token != self .request .query_params .get ("token" ):
302+ # failed auth
303+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 7 ))
238304 return None
239305
240306 commitid = self .kwargs .get ("commit" )
241307 if commitid :
308+ # commitid included on request
309+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 8 ))
242310 commit = repo .commits .filter (commitid = commitid ).first ()
243311 else :
244312 branch_name = self .kwargs .get ("branch" ) or repo .branch
245313 branch = Branch .objects .filter (
246314 name = branch_name , repository_id = repo .repoid
247315 ).first ()
248316 if branch is None :
317+ # failed to get a commit
318+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 16 ))
249319 return None
250320
321+ # found a commit by finding a branch
322+ inc_counter (FLARE_USE_COUNTER , labels = dict (position = 9 ))
251323 commit = repo .commits .filter (commitid = branch .head ).first ()
252324
253325 return commit
0 commit comments