Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit ea5980f

Browse files
authored
Bundle Analysis: Add metric label for upload result (#1255)
1 parent f6fcffa commit ea5980f

File tree

2 files changed

+87
-31
lines changed

2 files changed

+87
-31
lines changed

upload/tests/views/test_bundle_analysis.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
104104
"endpoint": "bundle_analysis",
105105
"is_using_shelter": "no",
106106
"position": "end",
107+
"result": "success",
107108
},
108109
)
109110

@@ -207,6 +208,7 @@ def test_upload_bundle_analysis_success_shelter(db, client, mocker, mock_redis):
207208
"endpoint": "bundle_analysis",
208209
"is_using_shelter": "no",
209210
"position": "end",
211+
"result": "success",
210212
},
211213
)
212214

@@ -245,6 +247,7 @@ def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
245247
"endpoint": "bundle_analysis",
246248
"is_using_shelter": "no",
247249
"position": "end",
250+
"result": "success",
248251
},
249252
)
250253

@@ -292,6 +295,7 @@ def test_upload_bundle_analysis_existing_commit(db, client, mocker, mock_redis):
292295
"endpoint": "bundle_analysis",
293296
"is_using_shelter": "no",
294297
"position": "end",
298+
"result": "success",
295299
},
296300
)
297301

@@ -340,7 +344,8 @@ def test_upload_bundle_analysis_missing_args(db, client, mocker, mock_redis):
340344
"action": "bundle_analysis",
341345
"endpoint": "bundle_analysis",
342346
"is_using_shelter": "no",
343-
"position": "start",
347+
"position": "end",
348+
"result": "bad_request",
344349
},
345350
)
346351

@@ -412,6 +417,7 @@ def test_upload_bundle_analysis_github_oidc_auth(
412417
"endpoint": "bundle_analysis",
413418
"is_using_shelter": "no",
414419
"position": "end",
420+
"result": "success",
415421
},
416422
)
417423

@@ -472,6 +478,7 @@ def test_upload_bundle_analysis_measurement_datasets_created(
472478
"endpoint": "bundle_analysis",
473479
"is_using_shelter": "no",
474480
"position": "end",
481+
"result": "success",
475482
},
476483
)
477484

@@ -533,6 +540,7 @@ def test_upload_bundle_analysis_measurement_timeseries_disabled(
533540
"endpoint": "bundle_analysis",
534541
"is_using_shelter": "no",
535542
"position": "end",
543+
"result": "success",
536544
},
537545
)
538546

@@ -574,7 +582,8 @@ def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
574582
"action": "bundle_analysis",
575583
"endpoint": "bundle_analysis",
576584
"is_using_shelter": "no",
577-
"position": "start",
585+
"position": "end",
586+
"result": "error",
578587
},
579588
)
580589

@@ -629,6 +638,7 @@ def test_upload_bundle_analysis_tokenless_success(db, client, mocker, mock_redis
629638
"endpoint": "bundle_analysis",
630639
"is_using_shelter": "no",
631640
"position": "end",
641+
"result": "success",
632642
},
633643
)
634644

@@ -810,3 +820,34 @@ def test_upload_bundle_analysis_tokenless_mismatched_branch(
810820
assert res.status_code == 401
811821
assert res.json() == {"detail": "Not valid tokenless upload"}
812822
assert not upload.called
823+
824+
825+
def test_upload_bundle_analysis_view_exception_handling(db, client, mocker, mock_redis):
826+
try:
827+
with patch(
828+
"upload.views.bundle_analysis.BundleAnalysisView._handle_upload",
829+
side_effect=Exception("Test Exception"),
830+
):
831+
client = APIClient()
832+
repository = RepositoryFactory.create()
833+
client.credentials(HTTP_AUTHORIZATION=f"token {repository.upload_token}")
834+
835+
mock_inc_counter = mocker.patch("upload.views.bundle_analysis.inc_counter")
836+
837+
client.post(
838+
reverse("upload-bundle-analysis"),
839+
{
840+
"commit": "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef",
841+
"slug": f"{repository.author.username}::::{repository.name}",
842+
},
843+
format="json",
844+
)
845+
except Exception as e:
846+
# Check that the Test Exception was raised and the inc_counter went up
847+
assert str(e) == "Test Exception"
848+
mock_inc_counter.assert_any_call(
849+
ANY,
850+
labels=mocker.ANY,
851+
)
852+
labels = mock_inc_counter.call_args[1]["labels"]
853+
assert labels["result"] == "error"

upload/views/bundle_analysis.py

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import uuid
3-
from typing import Any, Callable
3+
from typing import Any, Callable, Tuple
44

55
from django.conf import settings
66
from django.http import HttpRequest
@@ -48,6 +48,7 @@
4848
"endpoint",
4949
"is_using_shelter",
5050
"position",
51+
"result",
5152
],
5253
)
5354

@@ -85,23 +86,13 @@ class BundleAnalysisView(APIView, ShelterMixin):
8586
def get_exception_handler(self) -> Callable:
8687
return repo_auth_custom_exception_handler
8788

88-
def post(self, request: HttpRequest) -> Response:
89-
labels = generate_upload_prometheus_metrics_labels(
90-
action="bundle_analysis",
91-
endpoint="bundle_analysis",
92-
request=self.request,
93-
is_shelter_request=self.is_shelter_request(),
94-
position="start",
95-
include_empty_labels=False,
96-
)
97-
inc_counter(
98-
BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER,
99-
labels=labels,
100-
)
101-
89+
def _handle_upload(self, request: HttpRequest) -> Tuple[str, Response]:
10290
serializer = UploadSerializer(data=request.data)
10391
if not serializer.is_valid():
104-
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
92+
return (
93+
"bad_request",
94+
Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST),
95+
)
10596
data = serializer.validated_data
10697

10798
if isinstance(request.user, Owner):
@@ -191,18 +182,6 @@ def post(self, request: HttpRequest) -> Response:
191182
task_arguments=task_arguments,
192183
),
193184
)
194-
labels = generate_upload_prometheus_metrics_labels(
195-
action="bundle_analysis",
196-
endpoint="bundle_analysis",
197-
request=self.request,
198-
is_shelter_request=self.is_shelter_request(),
199-
position="end",
200-
include_empty_labels=False,
201-
)
202-
inc_counter(
203-
BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER,
204-
labels=labels,
205-
)
206185

207186
dispatch_upload_task(
208187
task_arguments,
@@ -235,4 +214,40 @@ def post(self, request: HttpRequest) -> Response:
235214
),
236215
)
237216

238-
return Response({"url": url}, status=201)
217+
return ("success", Response({"url": url}, status=201))
218+
219+
def post(self, request: HttpRequest) -> Response:
220+
labels = generate_upload_prometheus_metrics_labels(
221+
action="bundle_analysis",
222+
endpoint="bundle_analysis",
223+
request=self.request,
224+
is_shelter_request=self.is_shelter_request(),
225+
position="start",
226+
include_empty_labels=False,
227+
)
228+
labels["result"] = "pending"
229+
inc_counter(
230+
BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER,
231+
labels=labels,
232+
)
233+
234+
try:
235+
upload_result, response = self._handle_upload(request)
236+
return response
237+
except Exception as e:
238+
log.error(
239+
"Error handling bundle analysis upload",
240+
extra=dict(
241+
error=e,
242+
),
243+
exc_info=True,
244+
)
245+
upload_result = "error"
246+
raise
247+
finally:
248+
labels["position"] = "end"
249+
labels["result"] = upload_result
250+
inc_counter(
251+
BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER,
252+
labels=labels,
253+
)

0 commit comments

Comments
 (0)