From 08088ccaf0d44329201d44bc81a1c7e014126a0b Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Thu, 10 Nov 2022 08:54:11 -0800 Subject: [PATCH 1/2] Add extra context to change application error reporting. --- contentcuration/contentcuration/utils/sentry.py | 13 +++++++++++-- contentcuration/contentcuration/viewsets/base.py | 10 +++++----- contentcuration/contentcuration/viewsets/channel.py | 4 ++-- .../contentcuration/viewsets/sync/base.py | 2 +- .../contentcuration/viewsets/sync/utils.py | 13 +++++++++++-- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/contentcuration/contentcuration/utils/sentry.py b/contentcuration/contentcuration/utils/sentry.py index aaa3dc10a3..c9566de78a 100644 --- a/contentcuration/contentcuration/utils/sentry.py +++ b/contentcuration/contentcuration/utils/sentry.py @@ -1,8 +1,17 @@ from django.conf import settings -def report_exception(exception=None): +def report_exception(exception=None, user=None, contexts=None): if getattr(settings, "SENTRY_ACTIVE", False): from sentry_sdk import capture_exception - capture_exception(exception) + scope_args = { + "contexts": contexts + } + + if user and not user.is_anonymous: + scope_args["user"] = { + "email": user.email, + } + + capture_exception(exception, **scope_args) diff --git a/contentcuration/contentcuration/viewsets/base.py b/contentcuration/contentcuration/viewsets/base.py index afa3eeaa98..ef430b0fee 100644 --- a/contentcuration/contentcuration/viewsets/base.py +++ b/contentcuration/contentcuration/viewsets/base.py @@ -650,7 +650,7 @@ def create_from_changes(self, changes): change.update({"errors": serializer.errors}) errors.append(change) except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=self.request.user, change=change) change["errors"] = [str(e)] errors.append(change) @@ -693,7 +693,7 @@ def delete_from_changes(self, changes): # job done! pass except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=self.request.user, change=change) change["errors"] = [str(e)] errors.append(change) return errors @@ -731,7 +731,7 @@ def update_from_changes(self, changes): change.update({"errors": ValidationError("Not found").detail}) errors.append(change) except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=self.request.user, change=change) change["errors"] = [str(e)] errors.append(change) return errors @@ -768,7 +768,7 @@ def create_from_changes(self, changes): try: self.perform_bulk_create(serializer) except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=self.request.user, changes=changes) for change in changes: change["errors"] = [str(e)] errors.extend(changes) @@ -807,7 +807,7 @@ def update_from_changes(self, changes): try: self.perform_bulk_update(serializer) except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=self.request.user, changes=changes) for change in changes: change["errors"] = [str(e)] errors.extend(changes) diff --git a/contentcuration/contentcuration/viewsets/channel.py b/contentcuration/contentcuration/viewsets/channel.py index 53fc93580f..4ab57c4595 100644 --- a/contentcuration/contentcuration/viewsets/channel.py +++ b/contentcuration/contentcuration/viewsets/channel.py @@ -459,7 +459,7 @@ def publish_from_changes(self, changes): publish["key"], version_notes=publish.get("version_notes"), language=publish.get("language") ) except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=self.request.user, change=publish) publish["errors"] = [str(e)] errors.append(publish) return errors @@ -518,7 +518,7 @@ def sync_from_changes(self, changes): assessment_items=sync.get("assessment_items") ) except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=self.request.user, change=sync) sync["errors"] = [str(e)] errors.append(sync) return errors diff --git a/contentcuration/contentcuration/viewsets/sync/base.py b/contentcuration/contentcuration/viewsets/sync/base.py index 77b4061f7b..455a97e4aa 100644 --- a/contentcuration/contentcuration/viewsets/sync/base.py +++ b/contentcuration/contentcuration/viewsets/sync/base.py @@ -121,7 +121,7 @@ def apply_changes(changes_queryset): change.applied = True changed_fields = ("applied",) except Exception as e: - log_sync_exception(e) + log_sync_exception(e, user=change.created_by, change=change.serialize_to_change_dict()) change.errored = True change.kwargs["errors"] = [str(e)] change.save(update_fields=changed_fields) diff --git a/contentcuration/contentcuration/viewsets/sync/utils.py b/contentcuration/contentcuration/viewsets/sync/utils.py index 6bfc072e5c..1d3718b5e2 100644 --- a/contentcuration/contentcuration/viewsets/sync/utils.py +++ b/contentcuration/contentcuration/viewsets/sync/utils.py @@ -74,10 +74,19 @@ def generate_publish_event( return event -def log_sync_exception(e): +def log_sync_exception(e, user=None, change=None, changes=None): # Capture exception and report, but allow sync # to complete properly. - report_exception(e) + + contexts = {} + + if change is not None: + contexts["change"] = change + + elif changes is not None: + contexts["changes"] = changes + + report_exception(e, user=user, contexts=contexts) # make sure we leave a record in the logs just in case. logging.error(e) From 372e8e79e0f72495000bca94d6700c2868c83c4a Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Thu, 10 Nov 2022 08:54:25 -0800 Subject: [PATCH 2/2] Upgrade sentry-vue to latest. --- yarn.lock | 72 ++++++++++++++++++++++++------------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/yarn.lock b/yarn.lock index 7572759256..a12cc9970d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1605,57 +1605,47 @@ estree-walker "^1.0.1" picomatch "^2.2.2" -"@sentry/browser@7.11.1": - version "7.11.1" - resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.11.1.tgz#377d417e833ef54c78a93ef720a742bda5022625" - integrity sha512-k2XHuzPfnm8VJPK5eWd1+Y5VCgN42sLveb8Qxc3prb5PSL416NWMLZaoB7RMIhy430fKrSFiosnm6QDk2M6pbA== - dependencies: - "@sentry/core" "7.11.1" - "@sentry/types" "7.11.1" - "@sentry/utils" "7.11.1" +"@sentry/browser@7.19.0": + version "7.19.0" + resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-7.19.0.tgz#ce21544b843d5c4d5dcb9fe9b7ee31c5c4e91f42" + integrity sha512-dWi5VjEwiLb4ofata0UCLdTbXLD1uDUebe9rNSBkHZ3fHF4eap4ZJlu3dYePKB0CKZhZrjzbydimMhaMUNdnug== + dependencies: + "@sentry/core" "7.19.0" + "@sentry/types" "7.19.0" + "@sentry/utils" "7.19.0" tslib "^1.9.3" -"@sentry/core@7.11.1": - version "7.11.1" - resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.11.1.tgz#d68e796f3b6428aefd6086a1db00118df7a9a9e4" - integrity sha512-kaDSZ6VNuO4ZZdqUOOX6XM6x+kjo2bMnDQ3IJG51FPvVjr8lXYhXj1Ccxcot3pBYAIWPPby2+vNDOXllmXqoBA== +"@sentry/core@7.19.0": + version "7.19.0" + resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.19.0.tgz#74e0eaf4b9f42bb0290f4b3f3b0ea3e272dd693e" + integrity sha512-YF9cTBcAnO4R44092BJi5Wa2/EO02xn2ziCtmNgAVTN2LD31a/YVGxGBt/FDr4Y6yeuVehaqijVVvtpSmXrGJw== dependencies: - "@sentry/hub" "7.11.1" - "@sentry/types" "7.11.1" - "@sentry/utils" "7.11.1" + "@sentry/types" "7.19.0" + "@sentry/utils" "7.19.0" tslib "^1.9.3" -"@sentry/hub@7.11.1": - version "7.11.1" - resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-7.11.1.tgz#1749b2b102ea1892ff388d65d66d3b402b393958" - integrity sha512-M6ClgdXdptS0lUBKB5KpXXe2qMQhsoiEN2pEGRI6+auqhfHCUQB1ZXsfjiOYexKC9fwx7TyFyZ9Jcaf2DTxEhw== - dependencies: - "@sentry/types" "7.11.1" - "@sentry/utils" "7.11.1" - tslib "^1.9.3" - -"@sentry/types@7.11.1": - version "7.11.1" - resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.11.1.tgz#06e2827f6ba37159c33644208a0453b86d25e232" - integrity sha512-gIEhOPxC2cjrxQ0+K2SFJ1P6e/an5osSxVc9OOtekN28eHtVsXFCLB8XVWeNQnS7N2VkrVrkqORMBz1kvIcvVQ== +"@sentry/types@7.19.0": + version "7.19.0" + resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.19.0.tgz#3ebb96670399b637a945fa499fa7436f7b930147" + integrity sha512-oGRAT6lfzoKrxO1mvxiSj0XHxWPd6Gd1wpPGuu6iJo03xgWDS+MIlD1h2unqL4N5fAzLjzmbC2D2lUw50Kn2pA== -"@sentry/utils@7.11.1": - version "7.11.1" - resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.11.1.tgz#1635c5b223369d9428bc83c9b8908c9c3287ee10" - integrity sha512-tRVXNT5O9ilkV31pyHeTqA1PcPQfMV/2OR6yUYM4ah+QVISovC0f0ybhByuH5nYg6x/Gsnx1o7pc8L1GE3+O7A== +"@sentry/utils@7.19.0": + version "7.19.0" + resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.19.0.tgz#0e039fe57056074c3a5e47bd50d9cb4ac9a6e909" + integrity sha512-2L6lq+c9Ol2uiRxQDdcgoapmHJp24MhMN0gIkn2alSfMJ+ls6bGXzQHx6JAIdoOiwFQXRZHKL9ecfAc8O+vItA== dependencies: - "@sentry/types" "7.11.1" + "@sentry/types" "7.19.0" tslib "^1.9.3" "@sentry/vue@^7.11.1": - version "7.11.1" - resolved "https://registry.yarnpkg.com/@sentry/vue/-/vue-7.11.1.tgz#dc3a1d4868a3400222053465dac154a9e30aedce" - integrity sha512-4RiaMbvGITpKpnzJBixCR874HjYCmK11yt9YRt/rwXE185TKCVyq54h+57l3680WgNz4MU9oI+PF6C/1E/LfXg== - dependencies: - "@sentry/browser" "7.11.1" - "@sentry/core" "7.11.1" - "@sentry/types" "7.11.1" - "@sentry/utils" "7.11.1" + version "7.19.0" + resolved "https://registry.yarnpkg.com/@sentry/vue/-/vue-7.19.0.tgz#3352eb1dac349045c71c522bf12770be310422c0" + integrity sha512-d8voF9B+Dqnn9YiTQaiaNUC7p0dq+UxMWjtVatC9YVR7efgQPyTbgTmw6N4yRmWtlD2Itaspwz7UNZdeCGQbHw== + dependencies: + "@sentry/browser" "7.19.0" + "@sentry/core" "7.19.0" + "@sentry/types" "7.19.0" + "@sentry/utils" "7.19.0" tslib "^1.9.3" "@sinclair/typebox@^0.24.1":