class Sentry(object): """Sentry wrapper class that allows this app to work without a sentry token. If no token is specified in the config, the messages used for logging are simply not called. """ initialized = False def __init__(self): """Construct new sentry wrapper.""" if config.SENTRY_TOKEN is not None: self.initialized = True self.sentry = Client(config.SENTRY_TOKEN) def captureMessage(self, *args, **kwargs): """Capture message with sentry.""" if self.initialized: if 'tags' not in kwargs: kwargs['tags'] = {} # Tag it as stickerfinder kwargs['tags']['bot'] = 'stickerfinder' self.sentry.captureMessage(*args, **kwargs) def captureException(self, *args, **kwargs): """Capture exception with sentry.""" if self.initialized: if 'tags' not in kwargs: kwargs['tags'] = {} # Tag it as stickerfinder kwargs['tags']['bot'] = 'stickerfinder' self.sentry.captureException(*args, **kwargs)
class SentryNotifier(object): # pylint: disable=unused-variable """Send messages to a Sentry server.""" command = "sentry" def __init__(self, args, extra): """Initialize Sentry notifier with client""" self.args = args self.extra = extra self.client = Client(self.args.sentry_dsn) @classmethod def parse_args(cls, parser): """Add command line argument parsing rules for Sentry.""" parser.add_argument( "--sentry-dsn", required=False, help="""Sentry DSN to be used for notifications. It can also be set with the evironment variable $SENTRY_DSN.""") def notify(self, msg=""): """Send a message to Sentry server. The message can be an exception or a simple string. In the first case, handle it with `captureException` method to add more context to the error. """ if issubclass(msg.__class__, Exception): self.client.captureException() else: self.client.captureMessage(msg)
def _submit_trace(exc_info): kwargs: Dict[str, str] = dict() if "+git" not in snapcraft_legacy.__version__: kwargs["release"] = snapcraft_legacy.__version__ client = RavenClient( "https://*****:*****@sentry.io/277754", transport=RequestsHTTPTransport, # Should Raven automatically log frame stacks (including locals) # for all calls as it would for exceptions. auto_log_stacks=False, # Set a name to not send the real hostname. name="snapcraft", # Removes all stacktrace context variables. This will cripple the # functionality of Sentry, as you’ll only get raw tracebacks, # but it will ensure no local scoped information is available to the # server. processors=( "raven.processors.RemoveStackLocalsProcessor", "raven.processors.SanitizePasswordsProcessor", ), **kwargs ) client.captureException(exc_info=exc_info)
class SentryPlugin(object): name = 'sentry' def __init__(self, dsn, **kwargs): self.client = None self.kwargs = kwargs self.dsn = dsn def setup(self,app): for other in app.plugins: if not isinstance(other, SentryPlugin): continue if self.client is None: self.client = Client(self.dsn, **self.kwargs) def apply(self,callback,context): def wrapper(*args,**kwargs): try: rv = callback(*args, **kwargs) except Exception, e: if not isinstance(e, HTTPError): self.client.captureException() raise return rv return wrapper
def handle(self, *args, **kwargs): if args: pk = args[0] feed = Feed.objects.get(pk=pk) feed.etag = '' return FeedUpdater(feed.url).update() # Making a list of unique URLs. Makes one call whatever the number of # subscribers is. urls = Feed.objects.filter(muted=False).values_list('url', flat=True) unique_urls = {} map(unique_urls.__setitem__, urls, []) urls = unique_urls.keys() for url in urls: subscriber_count = Feed.objects.filter(url=url, muted=False).count() plural = '' if subscriber_count > 1: plural = 's' agent_detail = ' (%s subscriber%s)' % (subscriber_count, plural) try: updater = FeedUpdater(url, agent=agent_detail) updater.update() except Exception: # We don't know what to expect, and anyway # we're reporting the exception if settings.DEBUG or not hasattr(settings, 'SENTRY_DSN'): raise else: client = Client(dsn=settings.SENTRY_DSN) client.captureException() connection.close()
def main(): client = Client( 'https://*****:*****@sentry.io/255534') try: database.create_tables() TOKEN = os.environ.get('TOKEN', None) PORT = int(os.environ.get('PORT', '5000')) # Create the Updater and pass it your bot's token. updater = Updater(TOKEN) updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) updater.bot.set_webhook("https://alusabot.herokuapp.com/" + TOKEN) # Get the dispatcher to register handlers dp = updater.dispatcher alusa = AlusaBot() # on different commands - answer in Telegram dp.add_handler(CommandHandler("start", alusa.start)) dp.add_handler(CommandHandler("help", alusa.help)) dp.add_handler(CommandHandler("nfl", alusa.nfl)) dp.add_handler(CommandHandler("porra", alusa.football)) dp.add_handler(MessageHandler(Filters.photo, alusa.upload_photo)) dp.add_handler(MessageHandler(Filters.text, alusa.echo)) dp.add_handler(CallbackQueryHandler(alusa.callback_nfl)) # log all errors dp.add_error_handler(alusa.error) # Start the Bot updater.start_polling() updater.idle() except: client.captureException()
class Sentry(object): """Sentry wrapper class that allows this app to work without a sentry token. If no token is specified in the config, the messages used for logging are simply not called. """ initialized = False def __init__(self, config): """Construct new sentry wrapper.""" if config['develop']['sentry_token'] is not None \ and config['develop'].getboolean('sentry_enabled'): self.initialized = True self.sentry = Client( config['develop']['sentry_token'], ignore_exceptions=[ KeyboardInterrupt, GitError, ], ) def captureMessage(self, *args, **kwargs): """Capture message with sentry.""" if self.initialized: self.sentry.captureMessage(*args, **kwargs) def captureException(self, *args, **kwargs): """Capture exception with sentry.""" if self.initialized: self.sentry.captureException(*args, **kwargs)
def __call__(instance, pk_str): try: flush_transaction() pk = int(pk_str) data = JobData.objects.get(pk=pk) except (TypeError, ValueError) as e: error_msg = "Invalid value for pk" error_extra = { "pk tried": pk_str } raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureMessage(error_msg, extra=error_extra, stack=True) except JobData.DoesNotExist: error_msg = "Unable to find beanstalk job data." error_extra = { "pk tried": pk } raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureMessage(error_msg, extra=error_extra, stack=True) else: try: val = instance.f(data) if self.cleanup: data.delete() return val except Exception as e: raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureException()
class Sentry(object): """Sentry wrapper class that allows this app to work without a sentry token. If no token is specified in the config, the methods used for logging are simply not called. """ initialized = False def __init__(self): """Construct new sentry wrapper.""" if config['logging']['sentry_enabled']: self.initialized = True self.sentry = Client(config['logging']['sentry_token']) def captureMessage(self, *args, **kwargs): """Capture message with sentry.""" if self.initialized: if 'tags' not in kwargs: kwargs['tags'] = {} # Tag it as hetznerbot kwargs['tags']['bot'] = 'archivebot' self.sentry.captureMessage(*args, **kwargs) def captureException(self, *args, **kwargs): """Capture exception with sentry.""" if self.initialized: if 'tags' not in kwargs: kwargs['tags'] = {} # Tag it as hetznerbot kwargs['tags']['bot'] = 'archivebot' self.sentry.captureException(*args, **kwargs)
class Sentry(object): """Sentry wrapper class that allows this app to work without a sentry token. If no token is specified in the config, the messages used for logging are simply not called. """ initialized = False def __init__(self): """Construct new sentry wrapper.""" if config["logging"]["sentry_enabled"]: self.initialized = True self.sentry = Client(config["logging"]["sentry_token"]) def captureMessage(self, *args, **kwargs): """Capture message with sentry.""" if self.initialized: if "tags" not in kwargs: kwargs["tags"] = {} # Tag it as stickerfinder kwargs["tags"]["bot"] = "stickerfinder" self.sentry.captureMessage(*args, **kwargs) def captureException(self, *args, **kwargs): """Capture exception with sentry.""" if self.initialized: if "tags" not in kwargs: kwargs["tags"] = {} # Tag it as stickerfinder kwargs["tags"]["bot"] = "stickerfinder" self.sentry.captureException(*args, **kwargs)
async def run(): global thx_queue food = ['迪迪', '晨晨', '蓝BUFF', '蓝BUFF', '蓝BUFF', '蓝BUFF', '蓝BUFF', '蓝BUFF', '红BUFF', '红BUFF', '红BUFF', '红BUFF', '红BUFF', '河蟹', '野猪', '猎豹', '小鸟', '暴君', '黑暗暴君', '暗影主宰', '刚出生的蜥蜴弟弟', '雄猎豹的女票', '两只魔种小熊', '妲己', '自己的老婆貂蝉QwQ', '叶子', '自己', '小兵'] eat = [random.choice(['还想吃一个', '偷偷咬了一口', '一口吞下了', '吃掉了峡谷里最后的'])+i for i in food] g = ['亿圆', '喵娘', '蓝白胖次', '爱心便当', '闪耀之星', '游戏机', '海带缠潜艇', '盛夏么么茶', '真香', '狂欢之椅', '咸鱼', '给大佬递茶', '炮车', '锄头', '460', '三级头', '鸡小萌', '情书', '辣条', '比心', '小花花', '干杯', '凉了', '冰阔落', 'flag', '金币', '???', '吃瓜', 'B坷垃', '喵娘', '打榜', '小金人', '中国队加油', '氪金键盘', '变欧喷雾', '节奏风暴', '666', '233', '友谊的小船', '冰淇淋', '给代打的礼物', '门把手', '你别哭啊', '小光头', '灯塔', '疯狂打call', '粉丝卡', '小电视飞船', '月色真美', '月饼', '南瓜车', '摩天大楼', '礼花'] want_gift = ['还想吃许多'+i for i in g] while(True): length = thx_queue.qsize() temp_list = [] filter_list = [] for i in range(length): temp_list.append(thx_queue.get()) for j in temp_list: if len(filter_list) == 0: filter_list.append(j) continue added = False for k in range(len(filter_list)): # 添加重复 ans = filter_list[k] if j.get('uname') == ans.get('uname') and j.get('giftName') == ans.get('giftName') and j.get('roomid') == ans.get('roomid') and j.get('coin_type') == ans.get('coin_type'): filter_list[k].update({ 't': time.time(), 'num': ans.get('num') + j.get('num'), }) added = True break if not added: filter_list.append(j) for _ in range(len(filter_list)): thx_dic = filter_list[_] if time.time() - thx_dic['t'] > 5: try: if 'lc4t' in thx_dic['uname']: msg = '感谢[吨吨]赠送的%d个%s mua~' % (thx_dic['num'], thx_dic['giftName']) else: msg = '感谢[%s]赠送的%d个%s~' % (thx_dic['uname'], thx_dic['num'], thx_dic['giftName']) if thx_dic['giftName'] == 'B坷垃': msg = '恭喜[%s]喜提叶叶勋章~' % (thx_dic['uname']) if thx_dic['coin_type'] == 'gold': end = random.choice(['说了句还想吃', '说了句没吃饱', '说了句不够吃', '说了句还是饿'] + eat + want_gift) msg = '叶叶吃掉了%s的%d个%s并且' % (thx_dic['uname'], thx_dic['num'], thx_dic['giftName']) msg += '.' * (30-len(msg)) msg += end except: traceback.print_exc() dsn = ConfigLoader().dic_user['other_control']['sentry_dsn'] client = Client(dsn) client.captureException() await thx_danmu(msg, thx_dic['roomid']) else: thx_queue.put(thx_dic) await asyncio.sleep(1)
def handle(self, *args, **kwargs): if args: pk = args[0] feed = UniqueFeed.objects.get(pk=pk) return update_feed(feed.url, use_etags=False) # This command is run every 5 minutes. Don't queue more than # 5/45 = a ninth of the total number of feeds. limit = UniqueFeed.objects.count() / 9 uniques = UniqueFeed.objects.filter( muted=False, ).order_by('last_update')[:limit] for unique in uniques: try: if unique.should_update(): enqueue(update_feed, unique.url, timeout=20) except Exception: # We don't know what to expect, and anyway # we're reporting the exception if settings.DEBUG or not hasattr(settings, 'SENTRY_DSN'): raise else: client = Client(dsn=settings.SENTRY_DSN) client.captureException() connection.close()
def process_cache(app, model_name, location_id, start, end): model = apps.apps.get_model(app, model_name) Location = apps.apps.get_model('locations', 'Location') location = Location.objects.get(id=location_id) start = arrow.get(start).datetime end = arrow.get(end).datetime app_config = apps.apps.get_app_config(app) if model_name not in app_config.model_caches: raise Exception("Model {0} not found".format(model_name)) return for cache in app_config.model_caches[model_name]: cache_model = apps.apps.get_model(*cache) try: cache_model.objects.update_cache(location, start, end) except Exception: if settings.DSN: client = Client(settings.DSN) client.captureException() try: model.StatusModel().objects.update_status(model, start, end, location) except Exception: if settings.DSN: client = Client(settings.DSN) client.captureException()
async def run(self): self.roomid = ConfigLoader().dic_user['other_control']['gift_monitor_roomid'] if not self.roomid: print('没写gift房间') return self.danmuji = GiftMonitorHandler(self.roomid, self.areaid) while True: print('# 正在启动直播监控弹幕姬') time_start = int(utils.CurrentTime()) connect_results = await self.danmuji.connectServer() # print(connect_results) if not connect_results: continue task_main = asyncio.ensure_future(self.danmuji.ReceiveMessageLoop()) task_heartbeat = asyncio.ensure_future(self.danmuji.HeartbeatLoop()) finished, pending = await asyncio.wait([task_main, task_heartbeat], return_when=asyncio.FIRST_COMPLETED) print('主弹幕姬异常或主动断开,正在处理剩余信息') time_end = int(utils.CurrentTime()) if not task_heartbeat.done(): task_heartbeat.cancel() task_terminate = asyncio.ensure_future(self.danmuji.close_connection()) await asyncio.wait(pending) await asyncio.wait([task_terminate]) printer.info(['主弹幕姬退出,剩余任务处理完毕'], True) if time_end - time_start < 5: dsn = ConfigLoader().dic_user['other_control']['sentry_dsn'] client = Client(dsn) try: raise Exception('网络不稳定,重试中') except: client.captureException() print('# 当前网络不稳定,为避免频繁不必要尝试,将自动在5秒后重试') await asyncio.sleep(5)
class RunnerThread(threading.Thread): def __init__(self, runnerClass): threading.Thread.__init__(self) self.runner = runnerClass() self.client = Client(sentry_dsn) def __str__(self): return str(self.runner) def run(self): while 1: try: self.runner.call() print("{0}: Sleeping after success".format(self.runner)) sleep(self.runner.default_sleep_time) except NoRecordsToProcessError: print("{0}: Nothing to do: sleeping for five minutes".format(self.runner)) sleep(60 * 5) except TemporaryChillError as e: print("{0}: Temporary chill for {1} seconds".format(self.runner, e.pause_time)) self.client.captureException() sleep(e.pause_time) except KeyboardInterrupt: return
class SentryClient(object): client = None def __init__(self, ignore_exceptions=None): self.client = Client(dsn=settings.SENTRY_DSN, ignore_exceptions=ignore_exceptions) def error(self, message): return self.client.captureMessage(message, level='error') def warning(self, message): return self.client.captureMessage(message, level='warning') def info(self, message): return self.client.captureMessage(message, level='info') def debug(self, message): return self.client.captureMessage(message, level='debug') def fatal(self, message): return self.client.captureMessage(message, level='fatal') def capture_exception(self): try: self.client.captureException() except: pass
def lights433(host, port, adapter, adapter_args, switches, sentry): sentry_client, sentry_url = None, None if sentry or os.path.exists(DEFAULT_SENTRY_CONF): sentry = sentry if sentry is not None else DEFAULT_SENTRY_CONF with open(sentry, 'r') as f: sentry_url = f.read().strip() if not sentry_url: log.error("No sentry URL specified in [%s]" % sentry) sys.exit(1) else: sentry_client = Client(sentry_url) log.info("Sentry client configured!") try: log.info("Loading switch configurations from [%s]" % switches) adapter_kwargs = dict( pair.split('=') for pair in adapter_args.split(',') ) adapter = get_adapter(adapter)(**adapter_kwargs) server = Lights433Server(host, port, adapter, switches) except: if sentry_client: sentry_client.captureException() raise if sentry_client: Sentry(dsn=sentry_url).init_app(server.app) server.run()
def send_exception(exinfo): if can_send() and errorreporting: client = Client( dsn='http://*****:*****@sentry.kartoza.com/17', release=roam.__version__ ) roam.utils.info("Sending error report.") client.captureException(exinfo)
def ravenify(*args, **kwargs): try: function(*args, **kwargs) except Exception: if not settings.DEBUG: client = Client(dsn=settings.SENTRY_DSN) client.captureException() raise
def handle(self, *args, **kwargs): try: self.handle_sentry(*args, **kwargs) except Exception: if settings.DEBUG or 'SENTRY_DSN' not in os.environ: raise client = Client() client.captureException()
class SentryPlugin(Plugin): def __init__(self, container, dsn=None, **kwargs): self.container = container self.client = Client(dsn) self.container.error_hook.install(self.on_error) def on_error(self, exc_info): self.client.captureException(exc_info)
def run_with_sentry(callable, *args, sentry_dsn, **kwargs): client = Client(sentry_dsn) release = os.environ.get('SENTRY_RELEASE', 'unknown') try: callable(*args, **kwargs) except: client.user_context({'release': release}) client.captureException()
def send_exception(exinfo): if can_send() and errorreporting: client = Client( dsn= 'http://*****:*****@sentry.kartoza.com/17', release=roam.__version__) roam.utils.info("Sending error report.") client.captureException(exinfo)
def serialize_exception(e): from raven.conf import setup_logging, EXCLUDE_LOGGER_DEFAULTS tmp = {} conf = get_config() client = Client(conf["SENTRY_CLIENT_DSN"]) if conf["ENABLE_LOGGING"]: # future enhancement: add exclude loggers option EXCLUDE_LOGGER_DEFAULTS += ('werkzeug', ) handler = ContextSentryHandler(client) setup_logging(handler, exclude=EXCLUDE_LOGGER_DEFAULTS) if conf["ALLOW_ORM_WARNING"]: openerp.addons.web.controllers.main._serialize_exception = serialize_exception openerp.addons.report.controllers.main._serialize_exception = serialize_exception # wrap the main wsgi app openerp.service.wsgi_server.application = Sentry( openerp.service.wsgi_server.application, client=client) if conf["INCLUDE_USER_CONTEXT"]: client.extra_context(get_user_context()) # fire the first message if isinstance(e, openerp.osv.osv.except_osv): tmp["exception_type"] = "except_osv" elif isinstance(e, openerp.exceptions.Warning): tmp["exception_type"] = "warning" elif isinstance(e, openerp.exceptions.AccessError): tmp["exception_type"] = "access_error" elif isinstance(e, openerp.exceptions.AccessDenied): tmp["exception_type"] = "access_denied" t = sys.exc_info() if "exception_type" not in tmp: client.captureException(t) debug = "Ошибка отправлена разработчикам, они занимаются устранением проблемы" else: debug = t tmp.update({ "name": type(e).__module__ + "." + type(e).__name__ if type(e).__module__ else type(e).__name__, "debug": debug, "message": ustr(e), "arguments": to_jsonable(e.args), }) return tmp
def clean_url(self): url = URLObject(self.cleaned_data["url"]) # URLObject doesn't handle ipv6 very well yet. In the meantime, ... if url.netloc.count(":") > 3: raise forms.ValidationError(_("Enter a valid URL.")) URLValidator()(url.without_auth()) if url.scheme not in ["http", "https"]: raise forms.ValidationError( _("Invalid URL scheme: '%s'. Only HTTP and HTTPS are " "supported.") % url.scheme ) if url.netloc.hostname in ["localhost", "127.0.0.1", "::1"]: raise forms.ValidationError(_("Enter a valid URL.")) try: validate_ipv46_address(url.netloc.hostname) except forms.ValidationError: pass else: raise forms.ValidationError(_("Enter a valid URL.")) existing = self.user.feeds.filter(url=url) if self.instance is not None: existing = existing.exclude(pk=self.instance.pk) if existing.exists(): raise forms.ValidationError(_("It seems you're already subscribed to this feed.")) auth = None if url.auth != (None, None): auth = url.auth # Check this is actually a feed with user_lock("feed_check", self.user.pk, timeout=30): headers = {"User-Agent": USER_AGENT % "checking feed", "Accept": feedparser.ACCEPT_HEADER} try: response = requests.get(six.text_type(url.without_auth()), headers=headers, timeout=10, auth=auth) except Exception: if "SENTRY_DSN" in os.environ: client = Client() client.captureException() raise forms.ValidationError(_("Error fetching the feed.")) if response.status_code != 200: raise forms.ValidationError(_("Invalid response code from URL: " "HTTP %s.") % response.status_code) try: parsed = feedparser.parse(response.content) except Exception: raise forms.ValidationError(_("Error parsing the feed.")) if not is_feed(parsed): raise forms.ValidationError(_("This URL doesn't seem to be a valid feed.")) self.cleaned_data["title"] = parsed.feed.title # Cache this in case update_favicon needs it and it's not in the # scheduler data yet. if hasattr(parsed.feed, "link"): cache.set(u"feed_link:{0}".format(url), parsed.feed.link, 600) return url
def ravenify(*args, **kwargs): try: function(*args, **kwargs) except Exception as e: if not settings.DEBUG and hasattr(settings, 'SENTRY_DSN'): if not isinstance(e, JobTimeoutException): client = Client(dsn=settings.SENTRY_DSN) client.captureException() raise
def test_raven(ctx): import settings from raven import Client client = Client(settings.RAVEN_DSN) try: 1 / 0 except ZeroDivisionError: client.captureException()
def report_err(sentry_url=SENTRY_URL, trace=None): if sentry_url: try: client = Client(sentry_url) client.captureException() except Exception: log.error(trace) elif trace: log.error(trace)
def cli(db_uri, es_uri, es_query, es_index, file_path, crawler_tag, log_level, log_formatter, es, file, cli, domain): # pragma: no cover # Setup a default formatter incase one isn't provided. formatter = ('%(asctime)s - %(name)s - %(levelname)s - %(message)s' if not log_formatter else log_formatter) # Log to stdout with defaults. logging.basicConfig(stream=sys.stderr, level=log_level.upper(), format=formatter) log.info('Launching CLI and validating configuration.') # Check that the config we've been provided works. if not es and not file and not cli: raise ConfigurationError('Invalid configuration, no input given.') if file and not file_path: raise ConfigurationError( 'Invalid configuration, file used but no path chosen.') if cli and not domain: raise ConfigurationError( 'Invalid configuration, cli chosen but no domain.') if es and not all([es_query, es_index, es_uri]): raise ConfigurationError( 'Invalid configuration, es used but some configuration is ' 'missing. query=%r, index=%r, uri=%r', es_query, es_index, es_uri) crawler = AdsTxtCrawler(es, file, db_uri, es_uri=es_uri, es_query=es_query, es_index=es_index, file_uri=file_path, crawler_id=crawler_tag) version_hash = os.environ.get('GIT_HASH') sentry = Client(release=version_hash) sentry_handler = SentryHandler(sentry, level=logging.WARNING) setup_logging(sentry_handler) if cli: from adstxt.fetch import fetch crawler._bootstrap_db() crawler._check_viability(domain) loop = asyncio.get_event_loop() fetchdata = loop.run_until_complete(fetch(domain, crawler_tag)) crawler.process_domain(fetchdata) log.info('Domain processed. Exiting.') return try: crawler.run() except Exception as e: sentry.captureException() raise e
def run_job(instance, job_data_instance): try: val = instance.f(job_data_instance.data_dict) if self.cleanup: job_data_instance.delete() return val except: raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureException()
class Sentry: def __init__(self, api: hug.API = None, client: Client = None, **kwargs): self.client = Client(**kwargs) if client is None else client if api: self.mount(api) def mount(self, api): self.api = api hug.exception(api=self.api)(self.handle) def unmount(self): self.api.http.exception_handlers().pop(Exception) def handle(self, request, response, exception): self.capture(request, response, exception) return self.respond(request, response, exception) def capture(self, request, response, exception): data = self.context(request) self.client.captureException(exc_info=True, data=data) def respond(self, request, response, exception): raise exception def context(self, request: Request): return { 'request': self.request_info(request), 'user': self.user_info(request), } def request_info(self, request: Request): return { 'url': request.url, 'method': request.method, 'query_string': request.query_string, 'cookies': request.cookies, 'headers': request.headers, 'env': dict(get_environ(request.env)), } def user_info(self, request: Request): return { 'id': None, 'email': None, 'username': request.context.get('user'), 'ip_address': self.guess_ip(request), } def guess_ip(self, request: Request): for ip in request.access_route: try: ip_address(ip) except ValueError: pass else: return ip
def wrapper(request): if url is None: return func(request) try: return func(request) except Exception as e: client = Client(url) client.captureException(extra={"request": request}) raise e
def getPubmedObject(pmid): try: pubmedUrl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=Pubmed&Retmode=xml&id='+str(pmid) request = urllib2.Request(pubmedUrl) u = urllib2.urlopen(request) pubmedObject = u.read() return ET.fromstring(pubmedObject) except Exception, err: client = Client('http://*****:*****@sentry.sulab.org/9') client.captureException()
class SentryClient(object): def __init__(self, config): DSN = config.get("Sentry_dsn",None) if DSN: self.client = Client(DSN) else: raise Exception("Sentry_dsn input error!!!") def check_commom_err(self, e): self.client.captureException(e)
def call(self, func, data_dict, *args, **kwargs): try: data = JobData() data.data_dict = data_dict data.job_name = func data.save() kwargs['arg'] = str(data.pk) super(DataBeanstalkClient, self).call(func, **kwargs) except Exception as e: raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureException()
def handle(self, *args, **kwargs): try: self.handle_sentry(*args, **kwargs) except Exception as e: if ( self.ignore_exceptions is not None and isinstance(e, self.ignore_exceptions) ) or settings.DEBUG or 'SENTRY_DSN' not in os.environ: raise client = Client() client.captureException()
def handle(self, *args, **kwargs): for feed in UniqueFeed.objects.filter(failed_attempts__gt=0, muted=True): try: feed.resurrect() except Exception: if settings.DEBUG or not hasattr(settings, 'SENTRY_DSN'): raise client = Client(dsn=settings.SENTRY_DSN) client.captureException() connection.close()
def getLatestDiseaseOntology(): try: namespaces = {'owl': 'http://www.w3.org/2002/07/owl#', 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#', 'oboInOwl': 'http://www.geneontology.org/formats/oboInOwl#', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'} dourl = 'http://purl.obolibrary.org/obo/doid.owl' request = urllib2.Request(dourl) u = urllib2.urlopen(request) do = u.read() return ET.fromstring(do) except Exception, err: client = Client('http://*****:*****@sentry.sulab.org/9') client.captureException()
class SentryErrorHandler(object): def __init__(self, config): self.config = config if self.config.USE_SENTRY: self.sentry = Client(self.config.SENTRY_DSN_URL) self.modules = get_modules() def handle_exception(self, typ, value, tb, extra={}): if self.config.USE_SENTRY: self.sentry.captureException((typ, value, tb), extra=extra, data={"modules": self.modules})
def handle(self, *args, **kwargs): links = Feed.objects.values_list('link', flat=True).distinct() for link in links: try: Favicon.objects.update_favicon(link, force_update=kwargs['all']) except Exception: if settings.DEBUG or not hasattr(settings, 'SENTRY_DSN'): raise else: client = Client(dsn=settings.SENTRY_DSN) client.captureException()
class Sentry: """A simple cog for bug reports.""" def __init__(self, liara): self.liara = liara self.settings = dataIO.load_json('sentry') if 'dsn' not in self.settings: self.settings['dsn'] = None self.client = SentryClient(site=liara.user.id) async def on_command_error(self, exception, context): if isinstance(exception, commands_errors.MissingRequiredArgument): return if isinstance(exception, commands_errors.CommandNotFound): return if self.settings['dsn'] is None: return try: self.client.set_dsn(self.settings['dsn']) except InvalidDsn: self.settings['dsn'] = None self.client.set_dsn(None) _exception = exception.original message = context.message self.client.user_context({'id': message.author.id}) # noinspection PyBroadException try: raise _exception except: self.client.captureException(data={'message': message.content}, extra={ 'server_id': message.server.id, 'channel_id': message.channel.id, 'message_id': message.id }) @commands.command() @checks.is_owner() async def set_sentry(self, dsn=None): """Sets the DSN for Sentry.""" try: self.client.set_dsn(dsn) except InvalidDsn: await self.liara.say('That DSN is invalid.') return self.settings['dsn'] = dsn if dsn is None: await self.liara.say('DSN cleared.') else: await self.liara.say( 'DSN successfully set! All your exceptions will now be logged to Sentry.' ) self.client.captureMessage('Hello, world!')
class Logger(object): def __init__(self): self.client = Client( 'https://*****:*****@sentry.io/300868' ) def send_log(self, message, resp): self.client.captureException() resp.status = falcon.HTTP_200 resp.body = json.dumps(message) def send_exception(self): self.client.captureException()
def simulate_error(dsn): if not dsn: raise RuntimeError( 'A DSN must be provided by either the --dsn argument or SENTRY_DSN environment ' 'variable.' ) client = Client(dsn) try: 1 / 0 except ZeroDivisionError: client.captureException() print('Error sent')
def common_except_log(): """ 通用错误输出 by: 范俊伟 at:2015-03-08 :return: """ client = Client(settings.SENTRY_CLIENT_KEY) client.user_context({ "error": traceback.format_exc(), }) client.captureException() log.error('\n**common_except_log**\n' + traceback.format_exc()) print '\n**common_except_log**\n' + traceback.format_exc()
def handle_exception(raven: RavenClient): """Handle an occurred exception""" try: from settings import Settings Settings().write_defaults() written = True except Exception: written = False raven.captureException() messagebox.showerror( "Error", "Window initialization failed. The error has been " "reported. {}".format( "Settings have been reset to defaults." if written is True else ""))
def getPubmedObject(pmid): try: pubmedUrl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=Pubmed&Retmode=xml&id=' + str( pmid) request = urllib2.Request(pubmedUrl) u = urllib2.urlopen(request) pubmedObject = u.read() return ET.fromstring(pubmedObject) except Exception, err: client = Client( 'http://*****:*****@sentry.sulab.org/9' ) client.captureException()
class SentryErrorHandler(object): def __init__(self, config): self.config = config if self.config.USE_SENTRY: self.sentry = Client(self.config.SENTRY_DSN_URL) self.modules = get_modules() def handle_exception(self, typ, value, tb, extra={}): if self.config.USE_SENTRY: self.sentry.captureException((typ, value, tb), extra=extra, data={'modules': self.modules})
def ravenify(*args, **kwargs): try: function(*args, **kwargs) except Exception: if settings.DEBUG: raise if 'SENTRY_DSN' in os.environ: client = Client() elif hasattr(settings, 'SENTRY_DSN'): client = Client(dsn=settings.SENTRY_DSN) else: raise client.captureException() raise
def __call__(instance, arg): try: data = json.loads(arg) attempt = int(data.pop(u'__attempt', 0)) except (TypeError, ValueError) as e: return instance.f(arg) try: return instance.f(data) except BeanstalkRetryError as e: try: job = settings.BEANSTALK_JOB_NAME % { u'app': instance.app, u'job': instance.__name__, } except AttributeError: job = u"{}.{}".format(instance.app, instance.__name__) if attempt < self.max_retries: if self.warn_after is not None and attempt == (self.warn_after - 1): msg = u"Approaching max retry attempts for {}.".format(job) warn_data = { 'extra': { 'Job': job, 'Attempt number': attempt, 'Warn after': self.warn_after, 'Max retries': self.max_retries, 'Job data': data, } } raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureMessage(msg, data=warn_data, stack=True, level=logging.WARN) data[u'__attempt'] = attempt + 1 beanstalk_client = BeanstalkClient() beanstalk_client.call(job, json.dumps(data), delay=(2 ** attempt), priority=self.priority, ttr=self.ttr) else: msg = u"Exceeded max retry attempts for {}.".format(job) error_data = e.data if e.data is not None else {} raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureMessage(msg, data=error_data, stack=True) if e.should_email: send_mail(e.email_subject, e.email_body, settings.DEFAULT_FROM_EMAIL, [e.email_address], fail_silently=False) except Exception as e: raven_client = RavenClient(dsn=settings.RAVEN_CONFIG[u'dsn']) raven_client.captureException()
def run_from_argv(self, argv): if len(argv) <= 2 or argv[2] in ['-h', '--help']: print self.usage(argv[1]) sys.exit(1) subcommand_class = self._get_subcommand_class(argv[2]) parser = self.create_parser(argv[0], argv[2], subcommand_class) if hasattr(self, 'use_argparse') and self.use_argparse: subcommand_class.add_arguments(parser) options = parser.parse_args(argv[3:]) cmd_options = vars(options) args = cmd_options.pop('args', ()) else: options, args = parser.parse_args(argv[3:]) handle_default_options(options) try: subcommand_class.execute(*args, **options.__dict__) except Exception as e: if not isinstance(e, CommandError): if hasattr(settings, 'SENTRY_DSN'): dsn = settings.SENTRY_DSN elif hasattr(settings, 'RAVEN_CONFIG'): dsn = settings.RAVEN_CONFIG.get('dsn') else: raise sentry = Client(dsn) # Force sync transport to avoid race condition with the process exiting for url in sentry.servers: parsed = urlparse.urlparse(url) transport = sentry._registry.get_transport(parsed) transport.async = False sentry.get_ident(sentry.captureException()) self._write_error_in_stderr(e)
def error_trigger(request): context = {} if request.method == 'POST': form = forms.TriggerErrorForm(request.POST) if form.is_valid(): if form.cleaned_data['capture_with_raven']: try: dsn = settings.RAVEN_CONFIG['dsn'] except AttributeError: messages.error( request, "No settings.RAVEN_CONFIG['dsn'] set up" ) return redirect('manage:error_trigger') client = Client(dsn) try: raise NameError(form.cleaned_data['message']) except NameError: messages.info( request, str(client.captureException()) ) return redirect('manage:error_trigger') raise NameError( 'MANUAL ERROR TRIGGER: %s' % form.cleaned_data['message'] ) else: form = forms.TriggerErrorForm() context['form'] = form return render(request, 'manage/error_trigger.html', context)
def run_from_argv(self, argv): if len(argv) <= 2 or argv[2] in ['-h', '--help']: stdout = OutputWrapper(sys.stdout) stdout.write(self.usage(argv[1])) sys.exit(1) subcommand_class = self._get_subcommand_class(argv[2]) parser = self.create_parser(argv[0], argv[2], subcommand_class) if hasattr(self, 'use_argparse') and self.use_argparse: subcommand_class.add_arguments(parser) options = parser.parse_args(argv[3:]) cmd_options = vars(options) args = cmd_options.pop('args', ()) else: options, args = parser.parse_args(argv[3:]) handle_default_options(options) try: subcommand_class.execute(*args, **options.__dict__) except Exception as e: if not isinstance(e, CommandError): if hasattr(settings, 'SENTRY_DSN'): dsn = settings.SENTRY_DSN elif hasattr(settings, 'RAVEN_CONFIG'): dsn = settings.RAVEN_CONFIG.get('dsn') else: raise sentry = Client(dsn) if not sentry.is_enabled(): raise sentry.get_ident(sentry.captureException()) self._write_error_in_stderr(e)
def exception_to_sentry(extra=None): import conf import traceback from raven import Client if conf.test or conf.region.lower() == "local" or not conf.sentry.backend: traceback.print_exc() logbook.exception("") return data = { "version": getattr(conf, "version", None) } data.update(extra or {}) client = Client(conf.sentry.backend) client.captureException(extra=data, tags=conf.logging.handlers.Sentry.tags)
def sentry_handler(job, *exc_info): if 'SENTRY_DSN' not in os.environ: # Use the next exception handler (send to failed queue) return True client = Client() client.captureException( exc_info=exc_info, extra={ 'job_id': job.id, 'func': job.func, 'args': job.args, 'kwargs': job.kwargs, 'description': job.description, }, ) return False
def check_response_status(response): raven_config = getattr(settings, 'RAVEN_CONFIG', '') client = None if raven_config: client = Client(raven_config.get('dns')) try: response.raise_for_status() return response.json() except requests.exceptions.HTTPError as err: if client: client.captureException() raise requests.exceptions.HTTPError(_(u'Error: {} {}').format(str(response.status_code), err)) except ValueError as err: if client: client.captureException() raise ValueError(_(u'Cannot decode json, got {}').format(response.text), err)