def __init__(self, code, message, status_code=400, file_name="", task_id="", task_name="", tag="", task_data=None, error_type="", error_line="", line_number=0, offset=0): if task_data is None: task_data = {} self.status_code = status_code self.code = code # a short consistent string describing the error. self.message = message # A detailed message that provides more information. self.task_id = task_id or "" # OPTIONAL: The id of the task in the BPMN Diagram. self.task_name = task_name or "" # OPTIONAL: The name of the task in the BPMN Diagram. self.file_name = file_name or "" # OPTIONAL: The file that caused the error. self.tag = tag or "" # OPTIONAL: The XML Tag that caused the issue. self.task_data = task_data or "" # OPTIONAL: A snapshot of data connected to the task when error occurred. self.line_number = line_number self.offset = offset self.error_type = error_type self.error_line = error_line if hasattr(g, 'user'): user = g.user.uid else: user = '******' self.task_user = user # This is for sentry logging into Slack sentry_sdk.set_context("User", {'user': user}) Exception.__init__(self, self.message)
def __call__(self, update: Update, context: CallbackContext) -> None: user: users.User = users.get_user(update.effective_user.id) if not user: user: users.User = users.save_user(update.effective_user) if user: sentry_sdk.set_context('user', { 'username': user.username, 'chat_id': user.chat_id, }) if user.language in settings.LANGUAGES: gettext.translation( 'messages', 'locale', languages=[user.language], ).install() try: ok = self._execute(user, update, context) except Exception as e: logging.exception(str(e)) update.message.reply_text( _( 'Произошла ошибка. ' 'Бот находится в разработке, так что это неудивительно', )) return text = [self.FAIL_TEXT, self.OK_TEXT][ok] if text: update.effective_chat.send_message(_(text))
def readLog(setting): path = getLogPath() if path is None: setting.isLorRunning = False else: setting.isLorRunning = True try: with open(path, 'r', encoding='utf-8') as lorLog: for line in lorLog.readlines(): line = line.strip() if '[TrySetShardDnsLive] setting dns data by affinity' in line: setting.riotServer = str(line).split().pop() if 'Server opened successfully at port: ' in line: setting.port = str(line).split().pop() if 'Using user-preferred language CultureInfo of ' in line: setting.language = str(line).split().pop() if '[CheckingForUpdates] StartCheckingForUpdates for user ' in line: playerId = str(line).split( "[CheckingForUpdates] StartCheckingForUpdates for user ", 1)[1] if playerId != setting.playerId: setting.playerId = playerId sentry_sdk.set_user({ "id": playerId, "username": playerId + ' ' + setting.riotServer, "ip_address": "{{auto}}" }) sentry_sdk.set_context( "info", { "version": c.VERSION_NUM, "riotLanguage": setting.language, "sysLanguage": sysLanguage }) sentry_sdk.capture_message(playerId + ' ' + setting.riotServer) try: data = {} data['riot_id'] = setting.playerId data['server'] = setting.riotServer data['riot_language'] = setting.language data['sys_language'] = sysLanguage url = "https://lmttest.herokuapp.com/login" headers = { 'Content-type': 'application/json', 'Accept': 'text/plain' } response = requests.post(url, data=json.dumps(data), headers=headers) print(response.text) except requests.exceptions.HTTPError as e: print('post error', e.response.text) except IOError: print('log file not accessible: ', path) except Exception as e: print('readLog error', e)
def init_sentry(self, sentry_dsn, balena_app_name, balena_device_uuid, variant, firmware_version): sentry_sdk.init( sentry_dsn, environment=balena_app_name, release=f"hm-config@{firmware_version}" ) sentry_sdk.set_user({"id": balena_device_uuid}) sentry_sdk.set_context("variant", {variant})
def verify_hawk_response(response, sender): if "server-authorization" not in response.headers: sentry_sdk.set_context("response", {"content": response.content}) raise RuntimeError("Missing server_authorization header. Probable API HAWK auth failure") sender.accept_response( response.headers["server-authorization"], content=response.content, content_type=response.headers["Content-Type"], )
def log_request(request): logger.info(f"Path: {request.path}") logger.info(f"User-agent: {request.headers.get('USER-AGENT')}") try: data = request.data except: pass else: logger.info(data.raw) sentry_sdk.set_context("data", data.raw)
def capture_error(error, data=None, email=None, **tags): tags.setdefault('environment', EVIRONMENT) with configure_scope() as scope: for tag, value in tags.items(): scope.set_tag(tag, value) if data: sentry_sdk.set_context('DATA', data) capture_exception(error, scope=scope) if email: send_email(error, str(data), email=email)
def before(self, state): request = state.request sentry_sdk.set_context( "request", { "host": request.host, "method": request.method, "path": request.path_qs, # To ensure we don't submit an entire job "body": request.body[:1024], }, )
async def handler(context): try: try: parameter = context.pattern_match.group(1).split(' ') if parameter == ['']: parameter = [] context.parameter = parameter context.arguments = context.pattern_match.group(1) except BaseException: context.parameter = None context.arguments = None await function(context) except StopPropagation: raise StopPropagation except MessageTooLongError: await context.edit(lang('too_long')) except BaseException as e: exc_info = sys.exc_info()[1] exc_format = format_exc() try: await context.edit(lang('run_error')) except BaseException: pass if not diagnostics: return if strtobool(config['error_report']): report = f"# Generated: {strftime('%H:%M %d/%m/%Y', gmtime())}. \n" \ f"# ChatID: {str(context.chat_id)}. \n" \ f"# UserID: {str(context.sender_id)}. \n" \ f"# Message: \n-----BEGIN TARGET MESSAGE-----\n" \ f"{context.text}\n-----END TARGET MESSAGE-----\n" \ f"# Traceback: \n-----BEGIN TRACEBACK-----\n" \ f"{str(exc_format)}\n-----END TRACEBACK-----\n" \ f"# Error: \"{str(exc_info)}\". \n" await attach_report(report, f"exception.{time()}.pagermaid", None, "Error report generated.") try: sentry_sdk.set_context( "Target", { "ChatID": str(context.chat_id), "UserID": str(context.sender_id), "Msg": context.text }) sentry_sdk.set_tag( 'com', re.findall("\w+", str.lower(context.text.split()[0]))[0]) sentry_sdk.capture_exception(e) except: logs.info(lang('report_error'))
def start_sentry(self): sentry_sdk.init( SENTRY_DSN, traces_sample_rate=0, release=config.VERSION ) sentry_sdk.set_context("machine", { "system": platform.system(), "release": platform.release() }) sentry_sdk.set_context("api", { "key": self.f1laps_api_key, "telemetry_enabled": self.telemetry_enabled })
def capture(settings: 'SkyTempleSettingsStore', exc_info: Optional['ExceptionInfo'], **error_context_in: 'Capturable'): from skytemple_files.common.util import capture_capturable error_context: Dict[str, Union[str, int]] = { k: capture_capturable(v) for k, v in error_context_in.items() } # type: ignore try_ignore_err(collect_device_context, lambda c: sentry_sdk.set_context("device", c)) try_ignore_err(collect_os_context, lambda c: sentry_sdk.set_context("os", c)) try_ignore_err(collect_runtime_context, lambda c: sentry_sdk.set_context("runtime", c)) try_ignore_err(collect_app_context, lambda c: sentry_sdk.set_context("app", c)) try_ignore_err(collect_state_context, lambda c: sentry_sdk.set_context("skytemple_state", c)) try_ignore_err(partial(collect_config_context, settings), lambda c: sentry_sdk.set_context("config", c)) sentry_sdk.set_context("error", error_context) if exc_info: sentry_sdk.capture_exception(exc_info) else: if 'message' in error_context: sentry_sdk.capture_message( f"Error without exception: {error_context['message']}") else: sentry_sdk.capture_message("Unknown event. See context.")
def handle(self, *args, **options): failures = 0 non_mp4_entries = get_non_mp4_ad_entries() for non_mp4_entry in non_mp4_entries: try: sentry_sdk.set_context( 'django_management_command', { 'command': 'replace non-mp4 videos', 'existing_filepath': non_mp4_entry.ad_filepath, 'adfile_id': non_mp4_entry.id, }) # 1. Download new video print("old adFile") print(non_mp4_entry) video_id = extract_yt_id(non_mp4_entry.ad_filepath) print(f"video_id: {video_id}") download_dir = os.environ["AD_ARCHIVE_FILESTORE_DIR"] replacement_file_path = tasks.video_download_with_collection( video_id, base_download_dir=download_dir, collection_type=non_mp4_entry.collection_type) print(f"replacement path: {replacement_file_path.as_posix()}") # 2. Verify it exists on LSS assert Path(download_dir).joinpath( replacement_file_path).exists() # 3. Update filepath in DB # store old filepath for later old_filepath = Path(download_dir).joinpath( non_mp4_entry.ad_filepath) non_mp4_entry.ad_filepath = replacement_file_path.as_posix() print("updated ad_filepath obj") print(non_mp4_entry) non_mp4_entry.save() # 4. Delete video at old filepath from LSS old_filepath.unlink(missing_ok=True) print("deleted old filepath", old_filepath.as_posix()) except Exception as err: failures += 1 sentry_sdk.capture_exception(err) continue if failures > 0: self.stderr.write( self.style.FAILED( f"({failures}) videos failed to be replaced with mp4 versions" )) else: self.stdout.write( self.style.SUCCESS("Successfully replaced non-mp4 files"))
def wrap(update, callback): logger.info("User {user} sent {message}".format( user=update.message.from_user.username, message=update.message.text)) try: return f(update, callback) except Exception as e: sentry_sdk.init(os.getenv("SENTRY_DSN"), traces_sample_rate=1.0) sentry_sdk.set_user( {"username": update.message.from_user.username}) sentry_sdk.set_context("user_message", {"text": update.message.text}) sentry_sdk.capture_exception() logger.error(str(e)) update.message.reply_text(text="An error has occurred")
def log_in_with_session(self, session: LastfmSession) -> None: set_user({ 'username': session.username }) # TODO: Follow guidelines for OS context set_context('system_profile', { **helpers.generate_system_profile() }) set_context('app', { 'app_version': 'Private Beta 2' }) self.username = session.username self.__session_key = session.session_key
async def connect(self, websocket: websockets.WebSocketServerProtocol) -> None: global active_sessions active_sessions[self.connection_id] = self self.websocket = websocket self.connection_state = "HELLO" print(self.connection_id, "Connected") ice_config = get_turn_credentials() print(self.connection_id, "Obtained ICE") sentry_sdk.set_context("session", {"session_id": self.connection_id}) await websocket.send( json.dumps({ "kind": "HELLO", "connectionId": self.connection_id, "iceServers": ice_config })) try: async for msg in websocket: data = json.loads(msg) if data["kind"] == "OFFER": await self.process_ice(data) elif data["kind"] == "TIME": time = datetime.now().time() await websocket.send( json.dumps({ "kind": "TIME", "time": str(time) })) else: print(self.connection_id, "Unknown kind {}".format(data["kind"])) await websocket.send( json.dumps({ "kind": "ERROR", "error": "unknown_kind" })) except websockets.exceptions.ConnectionClosed: print(self.connection_id, "Ending due to dead WebSocket") await self.end()
async def submit_error_message(exc: BaseException, doing: str, bot: MyBot, ctx: Optional[MyContext] = None): if bot is None: return # unknown bug! error_channel = bot.get_channel(796093696374079519) if ctx: desc = f"Guild details:\n" \ f"ID: `{ctx.guild.id}`\n" \ f"Name: `{ctx.guild.name}`\n\n" \ f"Channel details:\n" \ f"ID: `{ctx.channel.id}`\n" \ f"Name: `{ctx.channel.name}`\n\n" \ f"Invoking message details:\n" \ f"ID: `{ctx.message.id}`\n\n" \ f"Author details:\n" \ f"ID: `{ctx.author.id}`\n" \ f"Name: `{str(ctx.author)}`" sentry_sdk.set_context("user", {"repr": repr(ctx.author), "id": ctx.author.id, "name": str(ctx.author)}) sentry_sdk.set_context("channel", {"repr": repr(ctx.channel), "id": ctx.channel.id}) sentry_sdk.set_context("guild", {"repr": repr(ctx.guild), "id": ctx.guild.id}) sentry_sdk.set_context("message", {"repr": repr(ctx.message), "id": ctx.message.id}) sentry_sdk.set_context("command", {"repr": repr(ctx.command)}) else: desc = "No context was passed, possibly a slash command?" error_embed = discord.Embed(title=f"Fatal error while working on {doing}!", description=desc, color=discord.Color.dark_red()) tb = f"```py\n{''.join(traceback.format_tb(exc.__traceback__))}\n```" error_embed.add_field(name="Exception Name", value=str(exc.__class__)) error_embed.add_field(name="Exception Reason", value=str(exc), inline=False) kwargs = {} if len(tb) < 1024: error_embed.add_field(name="Exception Traceback", value=tb) else: error_embed.add_field(name="Exception Traceback", value="Attached File") kwargs["file"] = discord.File(StringIO(''.join(traceback.format_tb(exc.__traceback__))), filename="traceback.txt") kwargs["embed"] = error_embed try: sentry_sdk.capture_exception(exc) except Exception as e: bot.logger.exception("Error while sending exception to Sentry!", exc_info=e) await error_channel.send(**kwargs)
def setup() -> None: # get local environment operating_system = get_operating_system() version = settings.VERSION environment = settings.ENV dsn = settings.SENTRY_DSN # init the Sentry SDK sentry_sdk.init( dsn=dsn, environment=environment, attach_stacktrace=True, sample_rate=1.0, ) # add context to Sentry reports sentry_sdk.set_context( "app", {"version": version, "operating_system": operating_system, "env": environment}, ) log.info("Error reporting setup!")
def init_sentry(): if mlconfig.MLCHAIN_SENTRY_DSN is None or mlconfig.MLCHAIN_SENTRY_DSN == 'None': return None logger.debug( "Initializing Sentry to {0} and traces_sample_rate: {1} and sample_rate: {2} and drop_modules: {3}" .format(mlconfig.MLCHAIN_SENTRY_DSN, mlconfig.MLCHAIN_SENTRY_TRACES_SAMPLE_RATE, mlconfig.MLCHAIN_SENTRY_SAMPLE_RATE, mlconfig.MLCHAIN_SENTRY_DROP_MODULES)) try: sentry_sdk.init( dsn=mlconfig.MLCHAIN_SENTRY_DSN, integrations=[FlaskIntegration()], sample_rate=mlconfig.MLCHAIN_SENTRY_SAMPLE_RATE, traces_sample_rate=mlconfig.MLCHAIN_SENTRY_TRACES_SAMPLE_RATE, server_name=mlconfig.MLCHAIN_SERVER_NAME, environment=mlconfig.MLCHAIN_DEFAULT_MODE, before_send=before_send) sentry_sdk.set_context(key="app", value={ "app_start_time": datetime.datetime.now(), "app_name": str(mlconfig.MLCHAIN_SERVER_NAME), "app_version": str(mlconfig.MLCHAIN_SERVER_VERSION), }) logger.info( "Initialized Sentry to {0} and traces_sample_rate: {1} and sample_rate: {2} and drop_modules: {3}" .format(mlconfig.MLCHAIN_SENTRY_DSN, mlconfig.MLCHAIN_SENTRY_TRACES_SAMPLE_RATE, mlconfig.MLCHAIN_SENTRY_SAMPLE_RATE, mlconfig.MLCHAIN_SENTRY_DROP_MODULES)) except sentry_sdk.utils.BadDsn: if 'http' in mlconfig.MLCHAIN_SENTRY_DSN: raise SystemExit("Sentry DSN configuration is invalid")
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8') print('utf8 string test: ', '卡尼能布恩', '째남모') sentry_sdk.init( "https://[email protected]/5907306", integrations=[FlaskIntegration()], traces_sample_rate=1.0, send_default_pii=True, debug=isDebug, release=constants.VERSION_NUM ) sentry_sdk.set_context("info", { "version": constants.VERSION_NUM }) cacheModel = Cache() settingTrack = Setting() localTrack = Local(settingTrack, cacheModel) class FlaskApp(Flask): def __init__(self, *args, **kwargs): super(FlaskApp, self).__init__(*args, **kwargs) self.processWork() def processWork(self): def run_work(): while True: readLog(settingTrack) time.sleep(3)
def run_comparison( fn_name: str, metrics_fn: Callable[..., Any], should_compare: bool, rollup: Optional[int], organization: Optional[Organization], schema: Optional[Schema], function_args: Tuple[Any], sessions_result: Any, sessions_time: datetime, **kwargs, ) -> None: if rollup is None: rollup = 0 # force exact date comparison if not specified tags = {"method": fn_name, "rollup": str(rollup)} set_tag("releasehealth.duplex.rollup", str(rollup)) set_tag("releasehealth.duplex.method", fn_name) set_tag("releasehealth.duplex.org_id", str(getattr(organization, "id"))) set_context( "release-health-duplex-sessions", { "sessions": sessions_result, }, ) try: delay = (datetime.now(pytz.utc) - sessions_time).total_seconds() set_extra("delay", delay) timing("releasehealth.metrics.delay", delay) # We read from the metrics source even if there is no need to compare. with timer("releasehealth.metrics.duration", tags=tags, sample_rate=1.0): metrics_val = metrics_fn(*function_args) incr( "releasehealth.metrics.check_should_compare", tags={"should_compare": str(should_compare), **tags}, sample_rate=1.0, ) if not should_compare: return copy = deepcopy(sessions_result) set_context("release-health-duplex-metrics", {"metrics": metrics_val}) with timer("releasehealth.results-diff.duration", tags=tags, sample_rate=1.0): errors = compare_results(copy, metrics_val, rollup, None, schema) set_context("release-health-duplex-errors", {"errors": [str(error) for error in errors]}) should_report = features.has( "organizations:release-health-check-metrics-report", organization ) incr( "releasehealth.metrics.compare", tags={"has_errors": str(bool(errors)), "reported": str(should_report), **tags}, sample_rate=1.0, ) if errors and should_report: tag_delta(errors, tags) # We heavily rely on Sentry's message sanitization to properly deduplicate this capture_message(f"{fn_name} - Release health metrics mismatch: {errors[0]}") except Exception: capture_exception() should_compare = False incr( "releasehealth.metrics.crashed", tags=tags, sample_rate=1.0, )
def dispatch(self, request, *args, **kwargs): t = self.tournament if not getattr(settings, 'DISABLE_SENTRY', False): from sentry_sdk import set_context set_context( "Tabbycat debug info", { "Tab director email": getattr(settings, 'TAB_DIRECTOR_EMAIL', "not provided"), "Tournament preferences": self.tournament.preferences.all(), }) # Lack of current_round caused by creating a tournament without rounds if t.current_round is None: if hasattr(self.request, 'user') and self.request.user.is_superuser: messages.warning( request, _("You've been redirected to this " "page because tournament %(tournament)s has no rounds. " "Please create some before returning to the admin site.") % {'tournament': t.name}) admin_url = reverse('admin:tournaments_round_changelist') return redirect(admin_url) else: logger.warning( "Current round wasn't set, redirecting to site index") messages.warning( request, _("There's a problem with the data " "for the tournament %(tournament)s. Please contact a " "tab director and ask them to investigate.") % {'tournament': t.name}) url = add_query_string_parameter(reverse('tabbycat-index'), 'redirect', 'false') return redirect(url) try: return super().dispatch(request, *args, **kwargs) except (MultipleDebateTeamsError, NoDebateTeamFoundError): if hasattr(self.request, 'user') and self.request.user.is_superuser: logger.warning( "Debate team side assignment error, redirecting " "to tournament-fix-debate-teams") messages.warning( request, _("You've been redirected to this " "page because of a problem with how teams are assigned to " "sides in a debate.")) return redirect_tournament('tournament-fix-debate-teams', t) else: logger.warning( "Debate team side assignment error, redirecting " "to tournament-public-index") messages.warning( request, _("There's a problem with how teams " "are assigned to sides in a debate. The tab director will " "need to resolve this issue.")) return redirect_tournament('tournament-public-index', t)
def collect_versions(force=False): """ Collect versions for all modules :return: dict with sub-dicts of version descriptions """ import label_studio # prevent excess checks by time intervals current_time = time.time() need_check = current_time - settings.VERSIONS_CHECK_TIME > 300 settings.VERSIONS_CHECK_TIME = current_time if settings.VERSIONS and not force and not need_check: return settings.VERSIONS # main pypi package result = { 'release': label_studio.__version__, 'label-studio-os-package': { 'version': label_studio.__version__, 'short_version': '.'.join(label_studio.__version__.split('.')[:2]), 'latest_version_from_pypi': label_studio.__latest_version__, 'latest_version_upload_time': label_studio.__latest_version_upload_time__, 'current_version_is_outdated': label_studio.__current_version_is_outdated__ }, # backend full git info 'label-studio-os-backend': version.get_git_commit_info() } # label studio frontend try: with open(os.path.join(settings.EDITOR_ROOT, 'version.json')) as f: lsf = json.load(f) result['label-studio-frontend'] = lsf except: pass # data manager try: with open(os.path.join(settings.DM_ROOT, 'version.json')) as f: dm = json.load(f) result['dm2'] = dm except: pass # converter try: import label_studio_converter result['label-studio-converter'] = { 'version': label_studio_converter.__version__ } except Exception as e: pass # ml try: import label_studio_ml result['label-studio-ml'] = {'version': label_studio_ml.__version__} except Exception as e: pass result.update(settings.COLLECT_VERSIONS(result=result)) for key in result: if 'message' in result[key] and len(result[key]['message']) > 70: result[key]['message'] = result[key]['message'][0:70] + ' ...' if settings.SENTRY_DSN: import sentry_sdk sentry_sdk.set_context("versions", copy.deepcopy(result)) for package in result: if 'version' in result[package]: sentry_sdk.set_tag('version-' + package, result[package]['version']) if 'commit' in result[package]: sentry_sdk.set_tag('commit-' + package, result[package]['commit']) settings.VERSIONS = result return result
def submit_report(self, dataset, job, tool, **kwargs): """Submit the error report to sentry """ extra = { 'info': job.info, 'id': job.id, 'command_line': job.command_line, 'destination_id': job.destination_id, 'stderr': job.stderr, 'traceback': job.traceback, 'exit_code': job.exit_code, 'stdout': job.stdout, 'handler': job.handler, 'tool_id': job.tool_id, 'tool_version': job.tool_version, 'tool_xml': tool.config_file if tool else None } if self.redact_user_details_in_bugreport: extra['email'] = 'redacted' else: if 'email' in kwargs: extra['email'] = kwargs['email'] # User submitted message extra['message'] = kwargs.get('message', '') # Construct the error message to send to sentry. The first line # will be the issue title, everything after that becomes the # "message" error_message = ERROR_TEMPLATE.format(**extra) # Update context with user information in a sentry-specific manner context = {} # Getting the url allows us to link to the dataset info page in case # anything is missing from this report. try: url = web.url_for(controller="dataset", action="show_params", dataset_id=self.app.security.encode_id( dataset.id), qualified=True) except AttributeError: # The above does not work when handlers are separate from the web handlers url = None user = job.get_user() if self.redact_user_details_in_bugreport: if user: # Opaque identifier context['user'] = {'id': user.id} else: if user: # User information here also places email links + allows seeing # a list of affected users in the tags/filtering. context['user'] = { 'name': user.username, 'email': user.email, } context['request'] = {'url': url} for key, value in context.items(): sentry_sdk.set_context(key, value) sentry_sdk.set_context('job', extra) sentry_sdk.set_tag('tool_id', job.tool_id) sentry_sdk.set_tag('tool_version', job.tool_version) # Send the message, using message because response = sentry_sdk.capture_message(error_message) return ( f'Submitted bug report to Sentry. Your guru meditation number is {response}', 'success')
def register_plugins(app: Flask): """ Register FlexMeasures plugins as Blueprints. This is configured by the config setting FLEXMEASURES_PLUGINS. Assumptions: - a setting EITHER points to a plugin folder containing an __init__.py file OR it is the name of an installed module, which can be imported. - each plugin defines at least one Blueprint object. These will be registered with the Flask app, so their functionality (e.g. routes) becomes available. If you load a plugin via a file path, we'll refer to the plugin with the name of your plugin folder (last part of the path). """ plugins = app.config.get("FLEXMEASURES_PLUGINS", []) if not plugins: # this is deprecated behaviour which we should remove in version 1.0 app.logger.debug( "No plugins configured. Attempting deprecated setting FLEXMEASURES_PLUGIN_PATHS ..." ) plugins = app.config.get("FLEXMEASURES_PLUGIN_PATHS", []) if not isinstance(plugins, list): app.logger.error( f"The value of FLEXMEASURES_PLUGINS is not a list: {plugins}. Cannot install plugins ..." ) return app.config["LOADED_PLUGINS"] = {} for plugin in plugins: plugin_name = plugin.split("/")[-1] app.logger.info(f"Importing plugin {plugin_name} ...") module = None if not os.path.exists(plugin): # assume plugin is a package pkg_name = os.path.split(plugin)[ -1 ] # rule out attempts for relative package imports app.logger.debug( f"Attempting to import {pkg_name} as an installed package ..." ) try: module = importlib.import_module(pkg_name) except ModuleNotFoundError: app.logger.error( f"Attempted to import module {pkg_name} (as it is not a valid file path), but it is not installed." ) continue else: # assume plugin is a file path if not os.path.exists(os.path.join(plugin, "__init__.py")): app.logger.error( f"Plugin {plugin_name} is a valid file path, but does not contain an '__init__.py' file. Cannot load plugin {plugin_name}." ) continue spec = importlib.util.spec_from_file_location( plugin_name, os.path.join(plugin, "__init__.py") ) if spec is None: app.logger.error( f"Could not load specs for plugin {plugin_name} at {plugin}." ) continue module = importlib.util.module_from_spec(spec) sys.modules[plugin_name] = module assert isinstance(spec.loader, Loader) spec.loader.exec_module(module) if module is None: app.logger.error(f"Plugin {plugin} could not be loaded.") continue plugin_version = getattr(module, "__version__", "0.1") plugin_settings = getattr(module, "__settings__", {}) check_config_settings(app, plugin_settings) # Look for blueprints in the plugin's main __init__ module and register them plugin_blueprints = [ getattr(module, a) for a in dir(module) if isinstance(getattr(module, a), Blueprint) ] if not plugin_blueprints: app.logger.warning( f"No blueprints found for plugin {plugin_name} at {plugin}." ) continue for plugin_blueprint in plugin_blueprints: app.logger.debug(f"Registering {plugin_blueprint} ...") app.register_blueprint(plugin_blueprint) app.config["LOADED_PLUGINS"][plugin_name] = plugin_version app.logger.info(f"Loaded plugins: {app.config['LOADED_PLUGINS']}") sentry_sdk.set_context("plugins", app.config.get("LOADED_PLUGINS", {}))
async def on_command_error(self, ctx: Context, error: commands.CommandError): if hasattr(ctx.command, 'on_error'): # Don't try to handle the error if the command has a local handler return arguments_error = [ commands.MissingRequiredArgument, commands.BadArgument, commands.TooManyArguments, ] if any([isinstance(error, arg_error) for arg_error in arguments_error]): embed = discord.Embed( title=f"Argumentos do comando '{ctx.command}':", description="", color=discord.Colour.red()) for param, param_type in ctx.command.clean_params.items(): try: default_name = param_type.default.__name__ except AttributeError: default_name = param_type.default default = f"(Opcional, Padrão: {default_name})" if default_name != '_empty' else '(Obrigatório)' p_type = param_type.annotation.__name__ if p_type == 'str': p_type = 'Texto' elif p_type == 'bool': p_type = '[True, False]' elif p_type == 'Member': p_type = 'Membro' elif p_type == 'int': p_type = 'Número' embed.add_field(name=param, value=f"**Tipo:** *{p_type}*\n*{default}*", inline=False) try: await ctx.send(embed=embed) except discord.errors.Forbidden: await ctx.send( "Erro. Permissões insuficientes para enviar um Embed.") elif isinstance(error, commands.CommandNotFound): pass elif isinstance(error, commands.DisabledCommand): pass # await ctx.send("Esse comando está desabilitado.") elif isinstance(error, commands.NoPrivateMessage): await ctx.send( "Esse comando não pode ser usado em mensagens privadas.") elif isinstance(error, commands.PrivateMessageOnly): await ctx.send( f"Esse comando só pode ser usado em Mensagens Privadas.\n" f"Fale comigo aqui: {self.bot.user.mention}") elif isinstance(error, commands.NotOwner): await ctx.send("Você não pode usar isso.") elif isinstance(error, commands.MissingPermissions): permissions = [ f"***{perm.title().replace('_', ' ')}***" for perm in error.missing_perms ] await ctx.send( f"Você precisa das seguintes permissões para fazer isso: {', '.join(permissions)}" ) elif isinstance(error, commands.CommandOnCooldown): await ctx.send( f"Ei! Você já usou este comando recentemente. " f"Espere mais {error.retry_after:.1f}s para usar novamente") elif isinstance(error, commands.BotMissingPermissions): permissions = [ f"***{perm.title().replace('_', ' ')}***" for perm in error.missing_perms ] await ctx.send( f"Eu preciso das seguintes permissões para fazer isso: {', '.join(permissions)}" ) elif isinstance(error, commands.errors.CheckFailure): pass elif isinstance(error, commands.errors.CommandInvokeError) and isinstance( error.original, TimeoutError): await ctx.send('Ação cancelada. Tempo esgotado.') else: await ctx.send( "Erro inesperado. Os logs desse erro foram enviados para um Dev e em breve será arrumado." ) sentry_sdk.set_user({ 'id': ctx.author and ctx.author.id, 'username': str(ctx.author) if ctx.author else None, }) sentry_sdk.set_context( 'discord', { 'guild': ctx.guild, 'channel': ctx.channel and (hasattr(ctx.channel, 'name') or None) and ctx.channel, 'message': ctx.message and ctx.message.content, 'message_id': ctx.message and ctx.message.id, 'cog': ctx.cog and ctx.cog.qualified_name, 'command': ctx.command and ctx.command.name }) sentry_sdk.capture_exception(error)
def _dispatch_call_inner( self, fn_name: str, should_compare: Union[bool, Callable[[Any], bool]], rollup: Optional[int], organization: Optional[Organization], schema: Optional[Schema], *args: Any, ) -> ReleaseHealthResult: if rollup is None: rollup = 0 # force exact date comparison if not specified sessions_fn = getattr(self.sessions, fn_name) set_tag("releasehealth.duplex.rollup", str(rollup)) set_tag("releasehealth.duplex.method", fn_name) set_tag("releasehealth.duplex.org_id", str(getattr(organization, "id"))) tags = {"method": fn_name, "rollup": str(rollup)} with timer("releasehealth.sessions.duration", tags=tags, sample_rate=1.0): ret_val = sessions_fn(*args) if organization is None or not features.has( "organizations:release-health-check-metrics", organization): return ret_val # cannot check feature without organization set_context( "release-health-duplex-sessions", { "sessions": ret_val, }, ) try: # We read from the metrics source even if there is no need to compare. metrics_fn = getattr(self.metrics, fn_name) with timer("releasehealth.metrics.duration", tags=tags, sample_rate=1.0): metrics_val = metrics_fn(*args) if not isinstance(should_compare, bool): # should compare depends on the session result # evaluate it now should_compare = should_compare(ret_val) incr( "releasehealth.metrics.check_should_compare", tags={ "should_compare": str(should_compare), **tags }, sample_rate=1.0, ) if not should_compare: return ret_val copy = deepcopy(ret_val) set_context("release-health-duplex-metrics", {"metrics": metrics_val}) with timer("releasehealth.results-diff.duration", tags=tags, sample_rate=1.0): errors = compare_results(copy, metrics_val, rollup, None, schema) set_context("release-health-duplex-errors", {"errors": [str(error) for error in errors]}) should_report = features.has( "organizations:release-health-check-metrics-report", organization) incr( "releasehealth.metrics.compare", tags={ "has_errors": str(bool(errors)), "reported": str(should_report), **tags }, sample_rate=1.0, ) if errors and should_report: tag_delta(errors, tags) # We heavily rely on Sentry's message sanitization to properly deduplicate this capture_message( f"{fn_name} - Release health metrics mismatch: {errors[0]}" ) except Exception: capture_exception() should_compare = False incr( "releasehealth.metrics.crashed", tags=tags, sample_rate=1.0, ) return ret_val
logger.info("Detected PyInstaller's FrozenImporter class.") break else: logger.warning("Failed to detect PyInstaller's FrozenImporter class.") importer = None if importer is not None: FrozenImporter.get_source = get_source_internet logger.info('Overrided FrozenImporter get_source function') sys_info = { 'name': platform.system(), 'version': platform.version(), 'theme': darkdetect.theme() } sentry_sdk.set_context('os', sys_info) sentry_sdk.set_context('app', {'app_version': __version__}) def before_breadcrumbs(crumb, hint): if crumb['message'] == 'The following error occured:': return None return crumb sentry_sdk.init( # Testing URL: # 'https://[email protected]/5867522', # Production URL:
def main(): '''Runs the Wahoo! Results scoreboard''' global FILE_WATCHER # pylint: disable=global-statement global IC # pylint: disable=global-statement # Determine if running as a PyInstaller exe bundle dsn = None execution_environment = "source" if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): execution_environment = "executable" dsn = SENTRY_DSN # only report if in executable mode # pylint: disable=abstract-class-instantiated # https://github.com/getsentry/sentry-python/issues/1081 # Initialize Sentry crash reporting sentry_sdk.init( dsn=dsn, sample_rate=1.0, traces_sample_rate=1.0, environment=execution_environment, release=f"wahoo-results@{WAHOO_RESULTS_VERSION}", with_locals=True, integrations=[ThreadingIntegration(propagate_hub=True)], debug=False, ) uname = platform.uname() sentry_sdk.set_tag("os_system", uname.system) sentry_sdk.set_tag("os_release", uname.release) sentry_sdk.set_tag("os_version", uname.version) sentry_sdk.set_tag("os_machine", uname.machine) config = WahooConfig() sentry_sdk.set_user({ "id": config.get_str("client_id"), "ip_address": "{{auto}}", }) hub = sentry_sdk.Hub.current hub.start_session(session_mode="application") bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__))) root = Tk() screen_size = f"{root.winfo_screenwidth()}x{root.winfo_screenheight()}" sentry_sdk.set_context("display", { "size": screen_size, }) root.title("Wahoo! Results") icon_file = os.path.abspath(os.path.join(bundle_dir, 'wahoo-results.ico')) root.iconbitmap(icon_file) root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) FILE_WATCHER = watchdog.observers.Observer() FILE_WATCHER.start() IC = ImageCast(8011) settings_window(root, config) # Intercept the close button so we can save the config before destroying # the main window and exiting the event loop in case we need to display an # error dialog. def close_cb(): try: config.save() except PermissionError as err: messagebox.showerror( title="Error saving configuration", message= f'Unable to write configuration file "{err.filename}". {err.strerror}', detail="Please ensure the working directory is writable.") root.destroy() root.protocol("WM_DELETE_WINDOW", close_cb) root.mainloop() FILE_WATCHER.stop() FILE_WATCHER.join() IC.stop()
def __init__(self, iface): self.iface = iface # Initialize plugin directory self.plugin_dir = os.path.dirname(__file__) # Initialize locale locale = QSettings().value("locale/userLocale")[0:2] locale_path = os.path.join( self.plugin_dir, "i18n", "{0}Plugin_{1}.qm".format(PE, locale) ) if os.path.exists(locale_path): self.translator = QTranslator() self.translator.load(locale_path) QCoreApplication.installTranslator(self.translator) # Declare instance attributes self.actions = [] self.menu = self.tr("&{0}".format(P_E)) self.toolbar = None # noinspection PyTypeChecker self.explorer_dock_widget = None self._terms_browser = None if is_segments_write_key_valid(): analytics.write_key = segments_write_key() if is_sentry_dsn_valid(): try: sentry_sdk.init(sentry_dsn(), release=plugin_version(True)) sentry_sdk.set_context( "qgis", { "type": "runtime", "name": Qgis.QGIS_RELEASE_NAME, "version": Qgis.QGIS_VERSION, }, ) system = platform.system() if system == "Darwin": sentry_sdk.set_context( "mac", { "type": "os", "name": "macOS", "version": platform.mac_ver()[0], "kernel_version": platform.uname().release, }, ) if system == "Linux": sentry_sdk.set_context( "linux", { "type": "os", "name": "Linux", "version": platform.release(), "build": platform.version(), }, ) if system == "Windows": sentry_sdk.set_context( "windows", { "type": "os", "name": "Windows", "version": platform.version(), }, ) except Exception: QMessageBox.warning( self.iface.mainWindow(), "Error", "Error initializing Planet Explorer.\n" "Please restart QGIS to load updated libraries.", ) self.qgis_hook = sys.excepthook def plugin_hook(t, value, tb): trace = "".join(traceback.format_exception(t, value, tb)) if PLUGIN_NAMESPACE in trace.lower(): s = "" if issubclass(t, exceptions.Timeout): s = "Connection to Planet server timed out." elif issubclass(t, exceptions.ConnectionError): s = ( "Connection error.\n Verify that your computer is correctly" " connected to the Internet" ) elif issubclass(t, (exceptions.ProxyError, exceptions.InvalidProxyURL)): s = ( "ProxyError.\n Verify that your proxy is correctly configured" " in the QGIS settings" ) elif issubclass(t, planet.api.exceptions.ServerError): s = "Server Error.\n Please, try again later" elif issubclass(t, urllib3.exceptions.ProxySchemeUnknown): s = ( "Proxy Error\n Proxy URL must start with 'http://' or" " 'https://'" ) if s: QMessageBox.warning(self.iface.mainWindow(), "Error", s) else: try: sentry_sdk.capture_exception(value) except Exception: pass # we swallow all exceptions here, to avoid entering an endless loop self.qgis_hook(t, value, tb) else: self.qgis_hook(t, value, tb) sys.excepthook = plugin_hook
import diagnostics_pb2 import wifi_connect_pb2 import wifi_remove_pb2 import wifi_services_pb2 from gpiozero import Button, LED # ET Phone Home variant = os.getenv('VARIANT') sentry_key = os.getenv('SENTRY_CONFIG') balena_id = os.getenv('BALENA_DEVICE_UUID') balena_app = os.getenv('BALENA_APP_NAME') uuids.FIRMWARE_VERSION = os.getenv('FIRMWARE_VERSION') sentry_sdk.init(sentry_key, environment=balena_app) sentry_sdk.set_user({"id": balena_id}) sentry_sdk.set_context("variant", {variant}) variantDetails = variant_definitions[variant] # Disable sudo for nmcli nmcli.disable_use_sudo() GATT_CHRC_IFACE = "org.bluez.GattCharacteristic1" NOTIFY_TIMEOUT = 5000 # Public Onboarding Keys while True: try: public_keys_file = open("/var/data/public_keys").readline().split('"') break