我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用raven.Client()。
def set_sentry_client(sentry_dsn, basedir): """Sets a Sentry client using a given sentry_dsn To clear the client, pass in something falsey like ``''`` or ``None``. """ global _sentry_client if sentry_dsn: version_info = get_version_info(basedir) commit = version_info.get('commit')[:8] _sentry_client = Client( dsn=sentry_dsn, include_paths=['antenna'], tags={'commit': commit} ) logger.info('Set up sentry client') else: _sentry_client = None logger.info('Removed sentry client')
def prepare(): global country_count,countries,file_type,questions,poll_id,write_questions,write_results,results,position,national,worldwide print "Preparing ..." if production: client = Client(sentry_url) handler = SentryHandler(client) setup_logging(handler) logger = logging.getLogger(__name__) mysql_connect() if len(sys.argv) == 1: manual_run() elif len(sys.argv) >= 2: file_type = sys.argv[1] if file_type == "q": automatic_questions() elif file_type == "r": automatic_results() elif file_type == "v": automatic_votes() mysql_close() make_language_table()
def test_will_alert(self, node, rule, testapp): match = RuleMatch( rule=rule, node=node.to_dict(), result={ 'name': 'foo', 'action': 'added', 'timestamp': dt.datetime.utcnow(), 'columns': {'boo': 'baz', 'kung': 'bloo'}, } ) with mock.patch.object(raven.Client, 'captureMessage', return_value=None) as pmock: alerter = SentryAlerter(self.config) alerter.handle_alert(node.to_dict(), match) assert pmock.called _, kwargs = pmock.call_args assert kwargs['message'] == rule.template.safe_substitute( match.result['columns'], **node.to_dict() ).rstrip()
def main(loop, inventory): """ Trigger to populate kinto with the last inventories. """ server_url = os.getenv('SERVER_URL', 'http://localhost:8888/v1') bucket = os.getenv('BUCKET', 'build-hub') collection = os.getenv('COLLECTION', 'releases') kinto_auth = tuple(os.getenv('AUTH', 'user:pass').split(':')) kinto_client = kinto_http.Client(server_url=server_url, auth=kinto_auth, bucket=bucket, collection=collection, retry=NB_RETRY_REQUEST) # Create bucket/collection and schemas. if INITIALIZE_SERVER: await initialize_kinto(loop, kinto_client, bucket, collection) # Download CSVs, deduce records and push to Kinto. session = aiobotocore.get_session(loop=loop) boto_config = botocore.config.Config(signature_version=botocore.UNSIGNED) async with session.create_client('s3', region_name=REGION_NAME, config=boto_config) as client: keys_stream = list_manifest_entries(loop, client, inventory) csv_stream = download_csv(loop, client, keys_stream) records_stream = csv_to_records(loop, csv_stream, skip_incomplete=True) await to_kinto(loop, records_stream, kinto_client, skip_existing=True)
def _install_sentry(): if "AVALON_SENTRY" not in Session: return try: from raven import Client from raven.handlers.logging import SentryHandler from raven.conf import setup_logging except ImportError: # Note: There was a Sentry address in this Session return log.warning("Sentry disabled, raven not installed") client = Client(Session["AVALON_SENTRY"]) # Transmit log messages to Sentry handler = SentryHandler(client) handler.setLevel(logging.WARNING) setup_logging(handler) self._sentry_client = client self._sentry_logging_handler = handler log.info("Connected to Sentry @ %s" % Session["AVALON_SENTRY"])
def __init__(self, server, sock): """ Initialize a Client object :param server: server object :param sock: socket connection object :return: """ self.__timestamp = time.time() self.__readbuffer = "" self.__writebuffer = "" self.__sentPing = False self.__handleCommand = self.passHandler self.server = server self.socket = sock (self.ip, self.port) = sock.getpeername() self.IRCUsername = "" self.banchoUsername = "" self.supposedUsername = "" self.supposedUserID = 0 self.joinedChannels = []
def disconnect(self, quitmsg = "Client quit", callLogout = True): """ Disconnects this client from the IRC server :param quitmsg: IRC quit message. Default: 'Client quit' :param callLogout: if True, call logoutEvent on bancho :return: """ # Send error to client and close socket self.message("ERROR :{}".format(quitmsg)) self.socket.close() log.info("[IRC] Disconnected connection from {}:{} ({})".format(self.ip, self.port, quitmsg)) # Remove socket from server self.server.removeClient(self, quitmsg) # Bancho logout if callLogout and self.banchoUsername != "": chat.IRCDisconnect(self.IRCUsername)
def setup(dsn=None, name=None, level=logging.WARNING, **kwargs): global client dsn = dsn or os.getenv("SENTRY_DSN") if not dsn: raise ValueError("you must give SENTRY_DSN, or set it in env") client = raven.Client( dsn, name=name, processors=raven.conf.defaults.PROCESSORS + ( AdditionalInfoProcessor.__module__ + "." + AdditionalInfoProcessor.__name__,), **kwargs ) handler = raven.handlers.logging.SentryHandler(client) handler.setLevel(level) raven.conf.setup_logging(handler) return client
def ready(self): # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') installed_apps = [app_config.name for app_config in apps.get_app_configs()] app.autodiscover_tasks(lambda: installed_apps, force=True) if hasattr(settings, 'RAVEN_CONFIG'): # Celery signal registration from raven import Client as RavenClient from raven.contrib.celery import register_signal as raven_register_signal from raven.contrib.celery import register_logger_signal as raven_register_logger_signal raven_client = RavenClient(dsn=settings.RAVEN_CONFIG['DSN']) raven_register_logger_signal(raven_client) raven_register_signal(raven_client)
def ready(self): # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') installed_apps = [app_config.name for app_config in apps.get_app_configs()] app.autodiscover_tasks(lambda: installed_apps, force=True) {% if cookiecutter.use_sentry_for_error_reporting == 'y' -%} if hasattr(settings, 'RAVEN_CONFIG'): # Celery signal registration from raven import Client as RavenClient from raven.contrib.celery import register_signal as raven_register_signal from raven.contrib.celery import register_logger_signal as raven_register_logger_signal raven_client = RavenClient(dsn=settings.RAVEN_CONFIG['DSN']) raven_register_logger_signal(raven_client) raven_register_signal(raven_client) {%- endif %}
def __init__(self, bot): self.bot = bot self.settings = dataIO.load_json('data/sentry/settings.json') if self.settings['dsn'] is None: log.warning('Sentry: DSN key is not set. Not sending logs!') else: if self.settings['ssl'] is False: self.raven = Client(self.settings['dsn'] + '?verify_ssl=0') else: self.raven = Client(self.settings['dsn']) self.handler = SentryHandler(self.raven) self.handler.setLevel(self.settings['level']) self.logger = logging.getLogger("red").addHandler(self.handler) setup_logging(self.handler) # --- Raven settings self.raven.tags = self.settings['tags'] if self.settings['name'] is not None: self.raven.name = self.settings['name'] if self.settings['environment'] is not None: self.raven.environment = self.settings['environment'] if self.settings.get('ignore'): self.raven.ignore = self.settings['ignore']
def run_twitter_fetcher(): sentry = Client(SENTRY_DSN) while True: try: l.info('starting streamer with {} emojis...'.format(len(EMOJIS))) sentry.captureMessage('starting `emoji-prediction`') streamer = TwitterEmojiStreamer(TwitterAuth.CONSUMER_KEY, TwitterAuth.CONSUMER_SECRET, TwitterAuth.ACCESS_TOKEN, TwitterAuth.ACCESS_TOKEN_SECRET) streamer.statuses.filter(track=EMOJIS, language=LANGUAGE) # requests.exceptions.ConnectionError except ChunkedEncodingError: l.debug('chunked_encoding_error happened') pass except ConnectionError: l.debug('connection_error happened') pass except UnknownTwitterEmojiException as e: l.error('unknown exception ocurred') l.error(e) sentry.captureException()
def setup(app, version:str='undefined'): environment = getattr(app.settings, 'ROLE', None) or os.environ.get('ROLE') dsn = getattr(app.settings, 'SENTRY_DSN', None) tags = getattr(app.settings, 'SENTRY_TAGS', None) client = Client(dsn=dsn, transport=AioHttpTransport, version=version, environment=environment, tags=tags) handler = DjaioSentryHandler(client=client) handler.setLevel(logging.ERROR) setup_logging(handler) app.raven = client
def configure_sentry(in_debug=False): """ Configure the Raven client, or create a dummy one if `in_debug` is `True`. """ key = 'eaee971c463b49678f6f352dfec497a9' # the public DSN URL is not available on the Python client # so we're exposing the secret and will be revoking it on abuse # https://github.com/getsentry/raven-python/issues/569 secret = '4f37fdbde03a4753b78abb84d11f45ab' project_id = '191660' dsn = 'https://{key}:{secret}@sentry.io/{project_id}'.format( key=key, secret=secret, project_id=project_id) if in_debug: client = DebugRavenClient() else: client = Client(dsn=dsn, release=__version__) # adds context for Android devices if platform == 'android': from jnius import autoclass Build = autoclass("android.os.Build") VERSION = autoclass('android.os.Build$VERSION') android_os_build = { 'model': Build.MODEL, 'brand': Build.BRAND, 'device': Build.DEVICE, 'manufacturer': Build.MANUFACTURER, 'version_release': VERSION.RELEASE, } client.user_context({'android_os_build': android_os_build}) # Logger.error() to Sentry # https://docs.sentry.io/clients/python/integrations/logging/ handler = SentryHandler(client) handler.setLevel(LOG_LEVELS.get('error')) setup_logging(handler) return client
def _setup_logging(logger, options): formatter = logging.Formatter('%(asctime)s %(process)s %(levelname)s ' '%(message)s') logger.setLevel(logging.DEBUG) if options["log_file"]: filename = options["log_file"] file_handler = logging.handlers.WatchedFileHandler(filename) file_handler.setFormatter(formatter) if options["debug"]: file_handler.setLevel(logging.DEBUG) else: file_handler.setLevel(logging.INFO) logger.addHandler(file_handler) stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.CRITICAL) stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) if options["sentry_dsn"] and _has_raven: client = raven.Client(options["sentry_dsn"], enable_breadcrumbs=False, transport=raven.transport.HTTPTransport) # Add Sentry handle to application logger. sentry_handler = SentryHandler(client) sentry_handler.setLevel(logging.WARNING) logger.addHandler(sentry_handler) null_loggers = [ logging.getLogger("sentry.errors"), logging.getLogger("sentry.errors.uncaught") ] for null_logger in null_loggers: null_logger.handlers = [logging.NullHandler()] if options["debug"]: stream_handler.setLevel(logging.DEBUG) elif options["info"]: stream_handler.setLevel(logging.INFO)
def _configure_sentry(self, raven_config): import raven from raven.contrib.celery import (register_signal, register_logger_signal) client = raven.Client(**raven_config) # register a custom filter to filter out duplicate logs register_logger_signal(client) # hook into the Celery error handler register_signal(client)
def start_loop(cfg: Config, noop=False): handlers = [] handlers.append(StreamHandler(sys.stdout, level=cfg.log_level)) logger = Logger("Heart") logger.info("Initializing Oshino v{0}".format(get_version())) logger.info("Running forever in {0} seconds interval. Press Ctrl+C to exit" .format(cfg.interval)) if cfg.sentry_dsn: try: client = SentryClient(cfg.sentry_dsn) handlers.append(SentryHandler(client, level=logbook.ERROR, bubble=True)) except InvalidDsn: logger.warn("Invalid Sentry DSN '{0}' providen. Skipping" .format(cfg.sentry_dsn)) setup = NestedSetup(handlers) setup.push_application() loop = create_loop() try: loop.run_until_complete(main_loop(cfg, logger, cfg.riemann.transport(noop), forever, loop=loop)) finally: loop.close()
def ready(self): # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, force=True) if hasattr(settings, 'RAVEN_CONFIG'): # Celery signal registration from raven import Client as RavenClient from raven.contrib.celery import register_signal as raven_register_signal from raven.contrib.celery import register_logger_signal as raven_register_logger_signal raven_client = RavenClient(dsn=settings.RAVEN_CONFIG['DSN']) raven_register_logger_signal(raven_client) raven_register_signal(raven_client)
def create_raven(sentry_dsn=SENTRY_DSN, transport='sync', _raven=None): if _raven is not None: return _raven klass = DebugRavenClient if TESTING else RavenClient client = klass( dsn=sentry_dsn, transport=RAVEN_TRANSPORTS[transport], processors=( 'raven.processors.RemoveStackLocalsProcessor', 'miracle.log.SecretsProcessor', ), release=VERSION) return client
def create_client(conf, app_version='__UNKNOWN__', ignore_common_http=True): """Creates a sentry client. :param app_version: (string): App version sent to sentry for making events more rich :param conf['SENTRY_DSN']: (string, required): DSN of sentry server :param conf['SENTRY_SITE']: (string): The site description of the deployment. :returns: An initialized ``raven.Client`` instance. """ ignore_exceptions = [] if ignore_common_http: ignore_exceptions = [ 'werkzeug.exceptions.BadRequest', # 400 'werkzeug.exceptions.Unauthorized', # 401 'werkzeug.exceptions.Forbidden', # 403 'werkzeug.exceptions.NotFound', # 404 'werkzeug.exceptions.MethodNotAllowed', # 405 'marshmallow.exceptions.ValidationError', # Marshmallow Validation Error. 'webargs.core.ValidationError', # Webargs Validation Error ] client = Client( conf['SENTRY_DSN'], site=conf.get('SENTRY_SITE'), release=app_version, ignore_exceptions=ignore_exceptions ) return client
def on_configure(self): client = raven.Client(settings.SENTRY_DSN) # register a custom filter to filter out duplicate logs register_logger_signal(client) # hook into the Celery error handler register_signal(client)
def report_exceptions(fn): def wrapped(*args, **kwargs): try: fn(*args, **kwargs) except Exception: client = Sentry( os.environ["SENTRY_DSN"], transport=SentryHTTPTransport, environment=os.environ["ENV"], ) client.captureException() raise return wrapped
def sentry_report_exception(context=None): sentry = raven.Client(SENTRY_URL) if context is not None: with sentry.context: sentry.context.merge(context) try: sentry.captureException() finally: sentry.context.clear()
def load_sentry(self): if self.clientid == "": return self.raven = Client(self.clientid, transport=AioHttpTransport) self.handler = SentryHandler(self.raven) setup_logging(self.handler) log.debug("Sentry handler activated.")
def sentryio(self): """ Sentry.io Client stuff """
def make_celery(app, celery): """ From http://flask.pocoo.org/docs/0.10/patterns/celery/ """ # Register our custom serializer type before updating the configuration. from kombu.serialization import register from doorman.celery_serializer import djson_dumps, djson_loads register( 'djson', djson_dumps, djson_loads, content_type='application/x-djson', content_encoding='utf-8' ) # Actually update the config celery.config_from_object(app.config) # Register Sentry client if 'SENTRY_DSN' in app.config and app.config['SENTRY_DSN']: client = Client(app.config['SENTRY_DSN']) # register a custom filter to filter out duplicate logs register_logger_signal(client) # hook into the Celery error handler register_signal(client) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery
def __init__(self, config): self.client = Client( config['dsn'], auto_log_stacks=False, enable_breadcrumbs=False, ) self.config = config
def ready(self): # pragma: no cover # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, force=True) if hasattr(settings, 'RAVEN_CONFIG'): # Celery signal registration from raven import Client as RavenClient from raven.contrib.celery import register_signal as raven_register_signal from raven.contrib.celery import register_logger_signal as raven_register_logger_signal raven_client = RavenClient(dsn=settings.RAVEN_CONFIG['DSN']) raven_register_logger_signal(raven_client) raven_register_signal(raven_client)
def __init__(self, bot): super().__init__(bot) self.sentry = raven.Client(bot.cfg['monitoring']['raven_client_url'])
def on_configure(self): if 'SENTRY_DSN' in os.environ: client = raven.Client(os.environ.get('SENTRY_DSN')) register_logger_signal(client) register_signal(client)
def on_configure(self): if hasattr(settings, 'RAVEN_CONFIG'): from raven import Client from raven.contrib.celery import register_signal from raven.contrib.celery import register_logger_signal client = Client(settings.RAVEN_CONFIG.get('dsn')) register_logger_signal(client) register_signal(client)
def on_configure(self): client = Client(settings.RAVEN_CONFIG["dsn"]) # register a custom filter to filter out duplicate logs register_logger_signal(client) # hook into the Celery error handler register_signal(client)
def configure_logging(config, log_name='transcribersofreddit.log'): logging.basicConfig( level=logging.INFO, format='%(levelname)s | %(funcName)s | %(message)s', datefmt='%Y-%m-%dT%H:%M:%S', ) # will intercept anything error level or above if config.bugsnag_api_key: bs_handler = BugsnagHandler() bs_handler.setLevel(logging.ERROR) logging.getLogger('').addHandler(bs_handler) logging.info('Bugsnag enabled!') else: logging.info('Not running with Bugsnag!') if config.sentry_api_url: sentry_handler = SentryHandler(Client(config.sentry_api_url)) sentry_handler.setLevel(logging.ERROR) # I don't know what this line does but it seems required by raven setup_logging(sentry_handler) logging.getLogger('').addHandler(sentry_handler) logging.info('Sentry enabled!') else: logging.info('Not running with Sentry!') log_header('Starting!')
def ready(self): # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') installed_apps = [app_config.name for app_config in apps.get_app_configs()] app.autodiscover_tasks(lambda: installed_apps, force=True) if hasattr(settings, 'RAVEN_CONFIG'): # Celery signal registration from raven import Client as RavenClient from raven.contrib.celery import register_signal as raven_register_signal from raven.contrib.celery import register_logger_signal as raven_register_logger_signal raven_client = RavenClient(dsn=settings.RAVEN_CONFIG['DSN']) raven_register_logger_signal(raven_client) raven_register_signal(raven_client) if hasattr(settings, 'OPBEAT'): from opbeat.contrib.django.models import client as opbeat_client from opbeat.contrib.django.models import logger as opbeat_logger from opbeat.contrib.django.models import register_handlers as opbeat_register_handlers from opbeat.contrib.celery import register_signal as opbeat_register_signal try: opbeat_register_signal(opbeat_client) except Exception as e: opbeat_logger.exception('Failed installing celery hook: %s', e) if 'opbeat.contrib.django' in settings.INSTALLED_APPS: opbeat_register_handlers()
def _get_mock_raven_client(dsn, django_client): response_list = [] raven_client = Client( dsn=dsn, transport=lambda: MockTransport(django_client=django_client, response_list=response_list), ) raven_client.response_list = response_list raven_client.raise_send_errors = True return raven_client
def lambda_handler(event, context=None): # Periodic if event.get('detail-type') == 'Scheduled Event': debug(event, context) return app.on_timer(event) # SNS / Dynamodb / Kinesis elif event.get('Records'): records = event['Records'] if records and records[0]['EventSource'] == 'aws:sns': return app.on_config_message(records) else: return debug(event, context) elif not event.get('path'): return debug(event, context) # API Gateway if app.config.get('sentry-dsn'): from raven import Client from raven.contrib.bottle import Sentry client = Client(app.config['sentry-dsn']) app.app.catchall = False wrapped_app = Sentry(app.app, client) else: wrapped_app = app.app return wsgigw.invoke(wrapped_app, event)
def init_logging(gn_env: GNEnvironment) -> None: if len(gn_env.config) == 0 or gn_env.config.get(ConfigKeys.TESTING, False): # assume we're testing return logging_type = gn_env.config.get(ConfigKeys.TYPE, domain=ConfigKeys.LOGGING, default='logger') if logging_type is None or len(logging_type.strip()) == 0 or logging_type in ['logger', 'default', 'mock']: return if logging_type != 'sentry': raise RuntimeError('unknown logging type %s' % logging_type) dsn = gn_env.config.get(ConfigKeys.DSN, domain=ConfigKeys.LOGGING, default='') if dsn is None or len(dsn.strip()) == 0: logger.warning('sentry logging selected but no DSN supplied, not configuring senty') return import raven import socket from git.cmd import Git home_dir = os.environ.get('DINO_HOME', default=None) if home_dir is None: home_dir = '.' tag_name = Git(home_dir).describe() gn_env.sentry = raven.Client( dsn=dsn, environment=os.getenv(ENV_KEY_ENVIRONMENT), name=socket.gethostname(), release=tag_name ) def capture_exception(e_info) -> None: try: gn_env.sentry.captureException(e_info) except Exception as e2: logger.exception(e_info) logger.error('could not capture exception with sentry: %s' % str(e2)) gn_env.capture_exception = capture_exception
def init_app(self, app): dsn = app.config.get('SENTRY_DSN') if dsn: client = raven.Client(dsn) handler = SentryHandler(client) handler.setLevel(logging.ERROR) setup_logging(handler) try: from raven.contrib.celery import ( register_signal, register_logger_signal) register_logger_signal(client) register_signal(client) except ImportError: pass
def on_init(self, function): self.client = Client(dsn=os.environ['SENTRY_IO'], transport=RequestsHTTPTransport, name=os.environ.get('AWS_LAMBDA_FUNCTION_NAME')) self.handler = SentryHandler(client=self.client, level=logging.ERROR) setup_logging(self.handler) BaseLogger.on_init(self, function)
def main(): """This is the main entry point. Here we launch the threads that stream submissions and comments to the database """ logging.basicConfig(level=logging.INFO, format='%(asctime)s @ %(threadName)s -- %(module)s.' '%(funcName)s -- [%(levelname)s] %(message)s') # parse arguments parser = _get_parser() args = vars(parser.parse_args()) # get configuration variables config = _get_config(args['config']) sentry = config.get('sentry', None) mongo_uri = _get_env_or_config(config, 'mongo') subs = config.get('subreddits') credentials = config.get('credentials') # initialize sentry client if sentry is not None: raven.Client(sentry) logging.info('Sentry client initialized.') # initialize database client = pymongo.MongoClient(mongo_uri) database = client.get_default_database() hoottit.util.initialize_indexes(database) # fire streaming processes pool = multiprocessing.dummy.Pool(2) pool.map(hoottit.util.execute, ( hoottit.util.pipe( getattr(hoottit.producers, p)(subs, credentials.get(p)), hoottit.consumers.mongo_upsert(database[p], 'reddit_id', args['log_threshold']) ) for p in ['comments', 'submissions'] if args[p]))
def get_sentry(): try: from raven import Client try: os.environ['SENTRY_DSN'] except KeyError: raise ImportError client = Client(release=__version__) client.user_context({'username': getpass.getuser()}) client.tags_context({ 'os_version': platform.platform(), 'nuke_version': nuke.NUKE_VERSION_STRING}) return client except ImportError: return None
def includeme(config): environment = os.environ.get('ENV', 'dev') config.registry["raven.client"] = raven.Client(environment=environment, release=__version__) config.add_request_method( get_raven_client, name="raven", reify=True)