From 718306073e930aba49f8a716bdbb99284109c83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 29 Mar 2019 14:12:23 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20error=20tracking:=20Restrict=20what?= =?UTF-8?q?=20gets=20tracked?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We are only interested in * Exceptions that are * logged either in octoprint.* or tornado.* and not from third party plugins or unhandled Implements #3094 --- .../plugins/errortracking/__init__.py | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/octoprint/plugins/errortracking/__init__.py b/src/octoprint/plugins/errortracking/__init__.py index 9203ad6d2..8da28779f 100644 --- a/src/octoprint/plugins/errortracking/__init__.py +++ b/src/octoprint/plugins/errortracking/__init__.py @@ -94,13 +94,43 @@ def _enable_errortracking(): if _is_enabled(enabled, enabled_unreleased): import sentry_sdk + from octoprint.plugin import plugin_manager + + def _before_send(event, hint): + if not "exc_info" in hint: + # we only want exceptions + return None + + handled = True + if event.get("exception") and event["exception"].get("values"): + handled = not any(map(lambda x: x.get("mechanism") and x["mechanism"].get("handled", True) == False, + event["exception"]["values"])) + + if handled: + # error is handled, restrict further based on logger + logger = event.get("logger", "") + + if logger != "" and not (logger.startswith("octoprint.") or logger.startswith("tornado.")): + # we only want unhandled errors or errors logged by loggers octoprint.* or tornado.* + return None + + if logger.startswith("octoprint.plugins."): + plugin_id = logger.split(".")[2] + plugin_info = plugin_manager().get_plugin_info(plugin_id) + if plugin_info is None or not plugin_info.bundled: + # we only want our active bundled plugins + return None + + return event + sentry_sdk.init(url_server, - release=version) + release=version, + before_send=_before_send) with sentry_sdk.configure_scope() as scope: scope.user = dict(id=unique_id) - logging.getLogger("octoprint.plugin.errortracking").info("Initialized error tracking") + logging.getLogger("octoprint.plugins.errortracking").info("Initialized error tracking") _enabled = True