From 6e5511a3a90a47b91104592d029e432a3d72c95d Mon Sep 17 00:00:00 2001 From: trent-codecov Date: Wed, 15 May 2024 10:59:22 -0400 Subject: [PATCH] Adding metrics for BA and adding tests --- upload/tests/views/test_bundle_analysis.py | 15 +++++++++++++ upload/tests/views/test_commits.py | 14 ++++++++++++- upload/tests/views/test_empty_upload.py | 16 +++++++++++--- upload/tests/views/test_reports.py | 16 +++++++++++++- upload/tests/views/test_test_results.py | 13 ++++++++++++ upload/tests/views/test_upload_completion.py | 19 +++++++++++++---- upload/tests/views/test_uploads.py | 2 ++ upload/views/bundle_analysis.py | 4 ++-- upload/views/empty_upload.py | 22 ++++++++++---------- 9 files changed, 99 insertions(+), 22 deletions(-) diff --git a/upload/tests/views/test_bundle_analysis.py b/upload/tests/views/test_bundle_analysis.py index 82679bfc1f..90de3c1147 100644 --- a/upload/tests/views/test_bundle_analysis.py +++ b/upload/tests/views/test_bundle_analysis.py @@ -14,6 +14,9 @@ def test_upload_bundle_analysis(db, client, mocker, mock_redis): upload = mocker.patch.object(TaskService, "upload") + mock_sentry_metrics = mocker.patch( + "upload.views.bundle_analysis.sentry_metrics.incr" + ) create_presigned_put = mocker.patch( "services.archive.StorageService.create_presigned_put", return_value="test-presigned-put", @@ -36,6 +39,7 @@ def test_upload_bundle_analysis(db, client, mocker, mock_redis): "service": "test-service", }, format="json", + headers={"User-Agent": "codecov-cli/0.4.7"}, ) assert res.status_code == 201 @@ -79,6 +83,17 @@ def test_upload_bundle_analysis(db, client, mocker, mock_redis): report_code=None, report_type="bundle_analysis", ) + mock_sentry_metrics.assert_called_with( + "upload", + tags={ + "agent": "cli", + "version": "0.4.7", + "action": "bundle_analysis", + "endpoint": "bundle_analysis", + "repo_visibility": "private", + "is_using_shelter": "no", + }, + ) def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis): diff --git a/upload/tests/views/test_commits.py b/upload/tests/views/test_commits.py index 3992d504e3..c574f8ed9b 100644 --- a/upload/tests/views/test_commits.py +++ b/upload/tests/views/test_commits.py @@ -295,6 +295,7 @@ def test_commit_github_oidc_auth(mock_jwks_client, mock_jwt_decode, db, mocker): private=False, author__username="codecov", name="the_repo" ) mocked_call = mocker.patch.object(TaskService, "update_commit") + mock_sentry_metrics = mocker.patch("upload.views.commits.sentry_metrics.incr") mock_jwt_decode.return_value = { "repository": f"url/{repository.name}", "repository_owner": repository.author.username, @@ -315,7 +316,7 @@ def test_commit_github_oidc_auth(mock_jwks_client, mock_jwt_decode, db, mocker): "pullid": "4", }, format="json", - headers={"Authorization": f"token {token}"}, + headers={"Authorization": f"token {token}", "User-Agent": "codecov-cli/0.4.7"}, ) assert response.status_code == 201 response_json = response.json() @@ -340,3 +341,14 @@ def test_commit_github_oidc_auth(mock_jwks_client, mock_jwt_decode, db, mocker): } assert expected_response == response_json mocked_call.assert_called_with(commitid="commit_sha", repoid=repository.repoid) + mock_sentry_metrics.assert_called_with( + "upload", + tags={ + "agent": "cli", + "version": "0.4.7", + "action": "coverage", + "endpoint": "create_commit", + "repo_visibility": "public", + "is_using_shelter": "no", + }, + ) diff --git a/upload/tests/views/test_empty_upload.py b/upload/tests/views/test_empty_upload.py index b0b6581a3c..f7bfadcc44 100644 --- a/upload/tests/views/test_empty_upload.py +++ b/upload/tests/views/test_empty_upload.py @@ -48,6 +48,7 @@ def test_empty_upload_with_yaml_ignored_files( mocker.patch.object( CanDoCoverageUploadsPermission, "has_permission", return_value=True ) + mock_sentry_metrics = mocker.patch("upload.views.empty_upload.sentry_metrics.incr") mock_final_yaml.return_value = UserYaml( { "ignore": [ @@ -79,9 +80,7 @@ def test_empty_upload_with_yaml_ignored_files( commit.commitid, ], ) - response = client.post( - url, - ) + response = client.post(url, headers={"User-Agent": "codecov-cli/0.4.7"}) response_json = response.json() assert response.status_code == 200 assert ( @@ -91,6 +90,17 @@ def test_empty_upload_with_yaml_ignored_files( notify_mock.assert_called_once_with( repoid=repository.repoid, commitid=commit.commitid, empty_upload="pass" ) + mock_sentry_metrics.assert_called_with( + "upload", + tags={ + "agent": "cli", + "version": "0.4.7", + "action": "coverage", + "endpoint": "empty_upload", + "repo_visibility": "private", + "is_using_shelter": "no", + }, + ) @patch("services.task.TaskService.notify") diff --git a/upload/tests/views/test_reports.py b/upload/tests/views/test_reports.py index 91a47be1e9..6c648fa546 100644 --- a/upload/tests/views/test_reports.py +++ b/upload/tests/views/test_reports.py @@ -24,6 +24,7 @@ def test_reports_get_not_allowed(client, mocker): def test_reports_post(client, db, mocker): mocked_call = mocker.patch.object(TaskService, "preprocess_upload") + mock_sentry_metrics = mocker.patch("upload.views.reports.sentry_metrics.incr") repository = RepositoryFactory( name="the_repo", author__username="codecov", author__service="github" ) @@ -35,7 +36,9 @@ def test_reports_post(client, db, mocker): "new_upload.reports", args=["github", "codecov::::the_repo", commit.commitid], ) - response = client.post(url, data={"code": "code1"}) + response = client.post( + url, data={"code": "code1"}, headers={"User-Agent": "codecov-cli/0.4.7"} + ) assert ( url == f"/upload/github/codecov::::the_repo/commits/{commit.commitid}/reports" @@ -45,6 +48,17 @@ def test_reports_post(client, db, mocker): commit_id=commit.id, code="code1", report_type=CommitReport.ReportType.COVERAGE ).exists() mocked_call.assert_called_with(repository.repoid, commit.commitid, "code1") + mock_sentry_metrics.assert_called_with( + "upload", + tags={ + "agent": "cli", + "version": "0.4.7", + "action": "coverage", + "endpoint": "create_report", + "repo_visibility": "private", + "is_using_shelter": "no", + }, + ) @patch("upload.helpers.jwt.decode") diff --git a/upload/tests/views/test_test_results.py b/upload/tests/views/test_test_results.py index bbac051a7b..5c0e76b384 100644 --- a/upload/tests/views/test_test_results.py +++ b/upload/tests/views/test_test_results.py @@ -14,6 +14,7 @@ def test_upload_test_results(db, client, mocker, mock_redis): upload = mocker.patch.object(TaskService, "upload") + mock_sentry_metrics = mocker.patch("upload.views.test_results.metrics.incr") create_presigned_put = mocker.patch( "services.archive.StorageService.create_presigned_put", return_value="test-presigned-put", @@ -37,6 +38,7 @@ def test_upload_test_results(db, client, mocker, mock_redis): "service": "test-service", }, format="json", + headers={"User-Agent": "codecov-cli/0.4.7"}, ) assert res.status_code == 201 @@ -89,6 +91,17 @@ def test_upload_test_results(db, client, mocker, mock_redis): report_code=None, report_type="test_results", ) + mock_sentry_metrics.assert_called_with( + "upload", + tags={ + "agent": "cli", + "version": "0.4.7", + "action": "test_results", + "endpoint": "test_results", + "repo_visibility": "private", + "is_using_shelter": "no", + }, + ) def test_test_results_org_token(db, client, mocker, mock_redis): diff --git a/upload/tests/views/test_upload_completion.py b/upload/tests/views/test_upload_completion.py index 0df6bea9fc..59ba798d49 100644 --- a/upload/tests/views/test_upload_completion.py +++ b/upload/tests/views/test_upload_completion.py @@ -49,7 +49,9 @@ def test_upload_completion_view_processed_uploads(mocked_manual_trigger, db, moc mocker.patch.object( CanDoCoverageUploadsPermission, "has_permission", return_value=True ) - + mock_sentry_metrics = mocker.patch( + "upload.views.upload_completion.sentry_metrics.incr" + ) repository = RepositoryFactory( name="the_repo", author__username="codecov", author__service="github" ) @@ -74,9 +76,7 @@ def test_upload_completion_view_processed_uploads(mocked_manual_trigger, db, moc commit.commitid, ], ) - response = client.post( - url, - ) + response = client.post(url, headers={"User-Agent": "codecov-cli/0.4.7"}) response_json = response.json() assert response.status_code == 200 assert response_json == { @@ -86,6 +86,17 @@ def test_upload_completion_view_processed_uploads(mocked_manual_trigger, db, moc "uploads_error": 0, } mocked_manual_trigger.assert_called_once_with(repository.repoid, commit.commitid) + mock_sentry_metrics.assert_called_with( + "upload", + tags={ + "agent": "cli", + "version": "0.4.7", + "action": "coverage", + "endpoint": "upload_complete", + "repo_visibility": "private", + "is_using_shelter": "no", + }, + ) @patch("services.task.TaskService.manual_upload_completion_trigger") diff --git a/upload/tests/views/test_uploads.py b/upload/tests/views/test_uploads.py index 41b999878d..4a69112008 100644 --- a/upload/tests/views/test_uploads.py +++ b/upload/tests/views/test_uploads.py @@ -655,6 +655,7 @@ def test_uploads_post_shelter(db, mocker, mock_redis): "agent": "cli", "version": "0.4.7", "action": "coverage", + "endpoint": "create_upload", "repo_visibility": "private", "is_using_shelter": "yes", }, @@ -667,6 +668,7 @@ def test_uploads_post_shelter(db, mocker, mock_redis): "agent": "cli", "version": "0.4.7", "action": "coverage", + "endpoint": "create_upload", "repo_visibility": "private", "is_using_shelter": "yes", }, diff --git a/upload/views/bundle_analysis.py b/upload/views/bundle_analysis.py index 30016a516a..41e995c9e3 100644 --- a/upload/views/bundle_analysis.py +++ b/upload/views/bundle_analysis.py @@ -134,8 +134,8 @@ def post(self, request): sentry_metrics.incr( "upload", tags=generate_upload_sentry_metrics_tags( - action="coverage", - endpoint="create_report", + action="bundle_analysis", + endpoint="bundle_analysis", request=self.request, repository=repo, is_shelter_request=self.is_shelter_request(), diff --git a/upload/views/empty_upload.py b/upload/views/empty_upload.py index 054090bdf0..ef77308447 100644 --- a/upload/views/empty_upload.py +++ b/upload/views/empty_upload.py @@ -132,7 +132,16 @@ def post(self, request, *args, **kwargs): ) ) ] - + sentry_metrics.incr( + "upload", + tags=generate_upload_sentry_metrics_tags( + action="coverage", + endpoint="empty_upload", + request=self.request, + repository=repo, + is_shelter_request=self.is_shelter_request(), + ), + ) if set(changed_files) == set(ignored_changed_files): TaskService().notify( repoid=repo.repoid, commitid=commit.commitid, empty_upload="pass" @@ -149,16 +158,7 @@ def post(self, request, *args, **kwargs): TaskService().notify( repoid=repo.repoid, commitid=commit.commitid, empty_upload="fail" ) - sentry_metrics.incr( - "upload", - tags=generate_upload_sentry_metrics_tags( - action="coverage", - endpoint="empty_upload", - request=self.request, - repository=repo, - is_shelter_request=self.is_shelter_request(), - ), - ) + return Response( data={ "result": "Some files cannot be ignored. Triggering failing notifications.",