From 0f8f1ec8e0ac65799c323f7a615319d13ff63321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 19 Feb 2019 13:11:25 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Finalize=20first=20version=20of=20e?= =?UTF-8?q?rrortracking=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also add docs for that & the Anonymous Usage Tracking plugin. --- docs/bundledplugins/errortracking.rst | 40 +++++++ docs/bundledplugins/index.rst | 2 + docs/bundledplugins/tracking.rst | 45 ++++++++ .../plugins/errortracking/__init__.py | 48 +++++++- .../errortracking/static/js/errortracking.js | 103 ++++++++++++++++++ .../errortracking_javascripts.jinja2 | 25 +++-- .../templates/errortracking_settings.jinja2 | 42 +++++++ src/octoprint/server/views.py | 2 +- src/octoprint/settings.py | 3 +- 9 files changed, 292 insertions(+), 18 deletions(-) create mode 100644 docs/bundledplugins/errortracking.rst create mode 100644 docs/bundledplugins/tracking.rst create mode 100644 src/octoprint/plugins/errortracking/static/js/errortracking.js create mode 100644 src/octoprint/plugins/errortracking/templates/errortracking_settings.jinja2 diff --git a/docs/bundledplugins/errortracking.rst b/docs/bundledplugins/errortracking.rst new file mode 100644 index 000000000..f87e7e8b2 --- /dev/null +++ b/docs/bundledplugins/errortracking.rst @@ -0,0 +1,40 @@ +.. _sec-bundledplugins-errortracking: + +Error Tracking Plugin +===================== + +The Error Tracking plugin will cause any logged exceptions in the server and the browser interface to be sent to +OctoPrint's `Sentry instance `_. + +By enabling it you help to gather detailed information on the cause of bugs or other issues. This is especially +valuable on release candidates, which is why this plugin will also prompt you to enable error tracking if it detects +that you are subscribed to an RC release channel. + +By default, even when enabled it will only be active if you are running a released OctoPrint version, so either a stable +release or a release candidate. + +The Error Tracking plugin is using a Sentry instance kindly provided by `sentry.io `_. For information on their service +please refer to their `Security & Compliance documentation `_ +and their `Privacy Policy `_. + +The Error Tracking plugin has been bundled with OctoPrint since version 1.3.11. + +.. _sec-bundledplugins-errortracking-configuration: + +Configuring the plugin +---------------------- + +The plugin supports the following configuration keys: + + * ``enabled``: Whether to enable error tracking. Defaults to ``false``. + * ``enabled_unreleased``: Whether to also enable tracking on unreleased OctoPrint versions (anything not stable releases + or release candidates). Defaults to ``false``. + * ``unique_id``: Unique instance identifier, auto generated on first activation + +.. _sec-bundledplugins-errortracking-sourcecode: + +Source Code +----------- + +The source of the Error Tracking plugin is bundled with OctoPrint and can be +found in its source repository under ``src/octoprint/plugins/errortracking``. diff --git a/docs/bundledplugins/index.rst b/docs/bundledplugins/index.rst index 8bf1d5d35..c5ae589cc 100644 --- a/docs/bundledplugins/index.rst +++ b/docs/bundledplugins/index.rst @@ -9,10 +9,12 @@ Bundled Plugins action_command_prompt.rst announcements.rst + tracking.rst appkeys.rst backup.rst cura.rst discovery.rst + errortracking.rst forcelogin.rst logging.rst pluginmanager.rst diff --git a/docs/bundledplugins/tracking.rst b/docs/bundledplugins/tracking.rst new file mode 100644 index 000000000..4864deb6f --- /dev/null +++ b/docs/bundledplugins/tracking.rst @@ -0,0 +1,45 @@ +.. _sec-bundledplugins-tracking: + +Anonymous Usage Tracking Plugin +=============================== + +The Anonymous Usage Tracking plugin provides valuable insights into how many instances running what versions of +OctoPrint are out there, whether they are successfully completing print jobs and various other metrics. + +By enabling it you help to identify problems with new releases and release candidates early on, and to better tailor +OctoPrint's future development to actual use. + +For details on what gets tracked, please refer to `tracking.octoprint.org `_ +and also the `Privacy Policy at tracking.octoprint.org `_. + +The Anonymous Usage Tracking plugin has been bundled with OctoPrint since version 1.3.10. + +.. _sec-bundledplugins-tracking-configuration: + +Configuring the plugin +---------------------- + +The plugin supports the following configuration keys: + + * ``enabled``: Whether to enable usage tracking. Defaults to ``false``. + * ``unique_id``: Unique instance identifier, auto generated on first activation + * ``server``: The tracking server to track against. Defaults to a tracking endpoint on ``https://tracking.octoprint.org``. + * ``ping``: How often to send a ``ping`` tracking event, in seconds. Defaults to a 15min interval. + * ``events``: Granular configuration of enabled tracking events. All default to ``true``. + + * ``startup``: Whether to track startup/shutdown events + * ``printjob``: Whether to track print job related events (start, completion, cancel, ...) + * ``commerror``: Whether to track communication errors with the printer + * ``plugin``: Whether to track plugin related events (install, uninstall, ...) + * ``update``: Whether to track update related events (update successful or not, ...) + * ``printer``: Whether to track printer related events (connected, firmware, ...) + * ``printer_safety_check``: Whether to track warnings of the :ref:`Printer Safety Check ` + * ``throttled``: Whether to track throttle events detected on the underlying system + +.. _sec-bundledplugins-tracking-sourcecode: + +Source Code +----------- + +The source of the Anonymous Usage Tracking plugin is bundled with OctoPrint and can be +found in its source repository under ``src/octoprint/plugins/tracking``. diff --git a/src/octoprint/plugins/errortracking/__init__.py b/src/octoprint/plugins/errortracking/__init__.py index e9f19107f..9203ad6d2 100644 --- a/src/octoprint/plugins/errortracking/__init__.py +++ b/src/octoprint/plugins/errortracking/__init__.py @@ -9,6 +9,9 @@ import logging from octoprint.util.version import get_octoprint_version_string, is_released_octoprint_version +from flask import jsonify +from flask_babel import gettext + SENTRY_URL_SERVER = "https://827d1f11ccda4b31b924f29aaacab493@sentry.io/1373987" SENTRY_URL_COREUI = "https://30bdc39d2248444c8bc01484f38c9444@sentry.io/1374096" @@ -21,10 +24,15 @@ SETTINGS_DEFAULTS = dict(enabled=False, class ErrorTrackingPlugin(octoprint.plugin.SettingsPlugin, octoprint.plugin.AssetPlugin, - octoprint.plugin.TemplatePlugin): + octoprint.plugin.TemplatePlugin, + octoprint.plugin.SimpleApiPlugin): def get_template_configs(self): return [ + dict(type="settings", + name=gettext("Error Tracking"), + template="errortracking_settings.jinja2", + custom_bindings=False), dict(type="generic", template="errortracking_javascripts.jinja2") ] @@ -32,22 +40,42 @@ class ErrorTrackingPlugin(octoprint.plugin.SettingsPlugin, enabled = self._settings.get_boolean(["enabled"]) enabled_unreleased = self._settings.get_boolean(["enabled_unreleased"]) - return dict(enabled=enabled and (enabled_unreleased or is_released_octoprint_version()), + return dict(enabled=_is_enabled(enabled, enabled_unreleased), unique_id=self._settings.get(["unique_id"]), url_coreui=self._settings.get(["url_coreui"])) def get_assets(self): - return dict(js=["js/sentry.min.js",]) + return dict(js=["js/sentry.min.js", "js/errortracking.js"]) def get_settings_defaults(self): return SETTINGS_DEFAULTS + def on_settings_save(self, data): + old_enabled = _is_enabled(self._settings.get_boolean(["enabled"]), + self._settings.get_boolean(["enabled_unreleased"])) -def __plugin_enable__(): + octoprint.plugin.SettingsPlugin.on_settings_save(self, data) + + enabled = _is_enabled(self._settings.get_boolean(["enabled"]), + self._settings.get_boolean(["enabled_unreleased"])) + + if old_enabled != enabled: + _enable_errortracking() + + def on_api_get(self, request): + return jsonify(**self.get_template_vars()) + + +_enabled = False +def _enable_errortracking(): # this is a bit hackish, but we want to enable error tracking as early in the platform lifecycle as possible # and hence can't wait until our implementation is initialized and injected with settings from octoprint.settings import settings + global _enabled + + if _enabled: + return version = get_octoprint_version_string() @@ -64,7 +92,7 @@ def __plugin_enable__(): s.set(["plugins", "errortracking", "unique_id"], unique_id, defaults=plugin_defaults) s.save() - if enabled and (enabled_unreleased or is_released_octoprint_version()): + if _is_enabled(enabled, enabled_unreleased): import sentry_sdk sentry_sdk.init(url_server, release=version) @@ -73,6 +101,16 @@ def __plugin_enable__(): scope.user = dict(id=unique_id) logging.getLogger("octoprint.plugin.errortracking").info("Initialized error tracking") + _enabled = True + + +def _is_enabled(enabled, enabled_unreleased): + return enabled and (enabled_unreleased or is_released_octoprint_version()) + + +def __plugin_enable__(): + _enable_errortracking() + __plugin_name__ = "Error Tracking" __plugin_author__ = "Gina Häußge" diff --git a/src/octoprint/plugins/errortracking/static/js/errortracking.js b/src/octoprint/plugins/errortracking/static/js/errortracking.js new file mode 100644 index 000000000..32ed05e0b --- /dev/null +++ b/src/octoprint/plugins/errortracking/static/js/errortracking.js @@ -0,0 +1,103 @@ +$(function() { + function ErrorTrackingViewModel(parameters) { + var self = this; + + self.settings = parameters[0]; + self.loginState = parameters[1]; + + var notification = undefined; + var performCheck = function() { + if (!self.loginState.isAdmin()) return; + + // already enabled? + if (self.settings.settings.plugins.errortracking.enabled()) return; + + // RC release channel? + var releaseChannel = self.settings.settings.plugins.softwareupdate.octoprint_release_channel(); + if (releaseChannel === "rc/maintenance" || releaseChannel === "rc/devel") { + if (notification !== undefined) return; + + // ignored? + try { + var ignoredString = localStorage["plugin.errortracking.notification_ignored"]; + var ignored = false; + + if (ignoredString) { + ignored = JSON.parse(ignoredString); + } + + if (ignored) return; + } catch (ex) { + log.error("Error while reading plugin.errortracking.notification_ignored from local storage"); + } + + // show notification + notification = new PNotify({ + title: gettext("Enable error reporting?"), + text: gettext("

It looks like you are tracking an OctoPrint RC release channel. It " + + "would be great if you would enable error reporting so that any kind of errors that occur " + + "with an RC can be looked into more easily. Thank you!

" + + "

You can find more information on error reporting " + + "under Settings > Error Tracking

"), + hide: false, + confirm: { + confirm: true, + buttons: [{ + text: gettext("Ignore"), + click: function() { + notification.remove(); + notification = undefined; + new PNotify({ + text: gettext("You can still enable error tracking via \"Settings\" > \"Error Tracking\"") + }); + + if (Modernizr.localstorage) { + localStorage["plugin.errortracking.notification_ignored"] = JSON.stringify(true); + } + } + }, { + text: gettext("Enable"), + addClass: "btn-primary", + click: function() { + self.settings.saveData({ + plugins: { + errortracking: { + enabled: true + } + } + }).done(function() { + notification.remove(); + notification = undefined; + location.reload(true); + }); + } + }] + }, + buttons: { + closer: false, + sticker: false + } + }); + + // not an RC release channel, close notification + } else if (notification !== undefined) { + notification.remove(); + notification = undefined; + } + }; + + var subbed = false; + self.onStartup = self.onUserLoggedIn = self.onUserLoggedOut = function() { + performCheck(); + if (self.settings && self.settings.settings && self.settings.settings.plugins && self.settings.settings.plugins.softwareupdate && !subbed) { + subbed = true; + self.settings.settings.plugins.softwareupdate.octoprint_release_channel.subscribe(performCheck); + } + }; + } + + OCTOPRINT_VIEWMODELS.push({ + construct: ErrorTrackingViewModel, + dependencies: ["settingsViewModel", "loginStateViewModel"] + }); +}); diff --git a/src/octoprint/plugins/errortracking/templates/errortracking_javascripts.jinja2 b/src/octoprint/plugins/errortracking/templates/errortracking_javascripts.jinja2 index e11b8f9bb..e953144dc 100644 --- a/src/octoprint/plugins/errortracking/templates/errortracking_javascripts.jinja2 +++ b/src/octoprint/plugins/errortracking/templates/errortracking_javascripts.jinja2 @@ -1,21 +1,24 @@ diff --git a/src/octoprint/plugins/errortracking/templates/errortracking_settings.jinja2 b/src/octoprint/plugins/errortracking/templates/errortracking_settings.jinja2 new file mode 100644 index 000000000..e5adefa92 --- /dev/null +++ b/src/octoprint/plugins/errortracking/templates/errortracking_settings.jinja2 @@ -0,0 +1,42 @@ +

{{ _('Error Tracking') }}

+ +{% trans %} +

+ Error tracking will cause any logged exceptions in the server and the browser interface to be sent + to OctoPrint's Sentry instance. +

+

+ By enabling it you help to gather detailed information on the cause of bugs or other issues. This is especially valuable + on release candidates, which is why this plugin will also prompt you to enable error tracking if it detects that + you are subscribed to an RC release channel. +

+

+ Please enable this if you are running a release candidate, or are prompted to do so while debugging an issue that you reported. +

+{% endtrans %} + +
+
+
+ + {{ _('Please reload the page after enabling/disabling & saving') }} +
+
+
+ +
+ +
+
+
+ +{% trans %} +

+ The Error Tracking plugin is using a Sentry instance kindly provided by + sentry.io. For information on their service + please refer to their Security & Compliance documentation + and their Privacy Policy. +

+{% endtrans %} diff --git a/src/octoprint/server/views.py b/src/octoprint/server/views.py index 8006ad5b6..8cc5f7174 100644 --- a/src/octoprint/server/views.py +++ b/src/octoprint/server/views.py @@ -196,7 +196,7 @@ def index(): default_additional_etag = [enable_accesscontrol, enable_gcodeviewer, - enable_timelapse] + enable_timelapse] + sorted(["{}:{}".format(k, v) for k, v in _plugin_vars.items()]) def get_preemptively_cached_view(key, view, data=None, additional_request_data=None, additional_unless=None): if (data is None and additional_request_data is None) or g.locale is None: diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index d97c6d171..f9e644974 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -298,7 +298,8 @@ default_settings = { "section_printer", "serial", "printerprofiles", "temperatures", "terminalfilters", "gcodescripts", "section_features", "features", "webcam", "accesscontrol", "gcodevisualizer", "api", "plugin_appkeys", "section_octoprint", "server", "folders", "appearance", "plugin_logging", "plugin_pluginmanager", - "plugin_softwareupdate", "plugin_announcements", "plugin_backup", "plugin_tracking", "plugin_pi_support" + "plugin_softwareupdate", "plugin_announcements", "plugin_backup", "plugin_tracking", "plugin_errortracking", + "plugin_pi_support" ], "usersettings": ["access", "interface"], "wizard": ["access"],