def main(global_config, **settings): dsn = settings.get("sentry.dsn", None) if dsn: LOGGER.info("Init sentry sdk for {}".format(dsn)) sentry_sdk.init( dsn=dsn, integrations=[ LoggingIntegration(level=None, event_level=None), PyramidIntegration() ], send_default_pii=True, request_bodies="always", environment=settings.get("sentry.environment", None), debug=settings.get("sentry.debug", False), ) config = Configurator( autocommit=True, settings=settings, authentication_policy=AuthenticationPolicy(settings["auth.file"], __name__), authorization_policy=AuthorizationPolicy(), route_prefix=ROUTE_PREFIX, ) config.include("pyramid_exclog") config.include("cornice") config.add_forbidden_view(forbidden) config.add_view(precondition, context=HTTPPreconditionFailed) config.add_request_method(request_params, "params", reify=True) config.add_request_method(authenticated_role, reify=True) config.add_request_method(check_accreditations) config.add_request_method(get_currency_rates, name="currency_rates", reify=True) config.add_renderer("json", JSON(serializer=simplejson.dumps)) config.add_renderer("prettyjson", JSON(indent=4, serializer=simplejson.dumps)) config.add_renderer( "jsonp", JSONP(param_name="opt_jsonp", serializer=simplejson.dumps)) config.add_renderer( "prettyjsonp", JSONP(indent=4, param_name="opt_jsonp", serializer=simplejson.dumps)) # search for plugins plugins = settings.get("plugins") and [ plugin.strip() for plugin in settings["plugins"].split(",") ] for entry_point in iter_entry_points("openprocurement.api.plugins"): if not plugins or entry_point.name in plugins: plugin = entry_point.load() plugin(config) # CouchDB connection aserver, server, db = set_api_security(settings) config.registry.couchdb_server = server if aserver: config.registry.admin_couchdb_server = aserver config.registry.db = db # CouchDB specific databases connections config.registry.databases = Databases( admin_connection=aserver or server, connection=server, migrations=settings.get("couchdb.migrations_db_name"), frameworks=settings.get("couchdb.frameworks_db_name"), submissions=settings.get("couchdb.submissions_db_name"), qualifications=settings.get("couchdb.qualifications_db_name"), agreements=settings.get("couchdb.agreements_db_name"), transfers=settings.get("couchdb.transfers_db_name"), plans=settings.get("couchdb.plans_db_name"), contracts=settings.get("couchdb.contracts_db_name"), ) # readjust couchdb json decoder couchdb_json_decode() # Document Service key config.registry.docservice_url = settings.get("docservice_url") config.registry.docservice_username = settings.get("docservice_username") config.registry.docservice_password = settings.get("docservice_password") config.registry.docservice_upload_url = settings.get( "docservice_upload_url") signing_key = settings.get('dockey', '') signer = SigningKey( signing_key, encoder=HexEncoder) if signing_key else SigningKey.generate() config.registry.docservice_key = signer verifier = signer.verify_key config.registry.keyring = { verifier.encode(encoder=HexEncoder)[:8].decode(): verifier } dockeys = settings.get('dockeys', '') for key in dockeys.split('\0'): if key: config.registry.keyring[key[:8]] = VerifyKey(key, encoder=HexEncoder) # migrate data if not os.environ.get("MIGRATION_SKIP"): for entry_point in iter_entry_points("openprocurement.api.migrations"): plugin = entry_point.load() plugin(config.registry) config.registry.server_id = settings.get("id", "") # search subscribers subscribers_keys = [k for k in settings if k.startswith("subscribers.")] for k in subscribers_keys: subscribers = settings[k].split(",") for subscriber in subscribers: for entry_point in iter_entry_points( "openprocurement.{}".format(k), subscriber): if entry_point: plugin = entry_point.load() plugin(config) config.registry.health_threshold = float( settings.get("health_threshold", 512)) config.registry.health_threshold_func = settings.get( "health_threshold_func", "all") config.registry.update_after = asbool(settings.get("update_after", True)) return config.make_wsgi_app()
"handlers": ["console"], "propagate": False }, "django.security.DisallowedHost": { "level": "ERROR", "handlers": ["console"], "propagate": False, }, }, } # Sentry # ------------------------------------------------------------------------------ SENTRY_DSN = env("SENTRY_DSN") SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO) sentry_logging = LoggingIntegration( level=SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs event_level=logging.ERROR, # Send errors as events ) integrations = [sentry_logging, DjangoIntegration()] sentry_sdk.init( dsn=SENTRY_DSN, integrations=integrations, environment=env("SENTRY_ENVIRONMENT", default="production"), traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0), ) logging.error("Indiependent.to Sentry Health Check") # Your stuff... # ------------------------------------------------------------------------------ GOOGLE_ANALYTICS = { 'google_analytics_id': "UA-164161672-1", }
from kodiak.queue import WebhookEvent sentry_sdk.init() logging.basicConfig( stream=sys.stdout, level=conf.LOGGING_LEVEL, format="%(levelname)s %(name)s:%(filename)s:%(lineno)d %(message)s", ) # disable sentry logging middleware as the structlog processor provides more # info via the extra data field # TODO(sbdchd): waiting on https://github.com/getsentry/sentry-python/pull/444 # to be merged & released to remove `# type: ignore` sentry_sdk.init( integrations=[LoggingIntegration(level=None, event_level=None) ] # type: ignore ) structlog.configure( processors=[ structlog.stdlib.filter_by_level, structlog.stdlib.PositionalArgumentsFormatter(), structlog.processors.StackInfoRenderer(), structlog.processors.format_exc_info, structlog.processors.UnicodeDecoder(), add_request_info_processor, SentryProcessor(level=logging.WARNING), structlog.processors.KeyValueRenderer(key_order=["event"], sort_keys=True), ],
assert event["_meta"]["request"]["data"]["file"] == { "": { "len": -1, "rem": [["!raw", "x", 0, -1]], } # bottle default content-length is -1 } assert not event["request"]["data"]["file"] @pytest.mark.parametrize( "integrations", [ [bottle_sentry.BottleIntegration()], [ bottle_sentry.BottleIntegration(), LoggingIntegration(event_level="ERROR") ], ], ) def test_errors_not_reported_twice(sentry_init, integrations, capture_events, app, get_client): sentry_init(integrations=integrations) app.catchall = False logger = logging.getLogger("bottle.app") @app.route("/") def index(): try: 1 / 0
CELERY_BROKER_URL = env('CELERY_BROKER_URL', default='') # Store task results in Redis CELERY_RESULT_BACKEND = env('CELERY_BROKER_URL', default='') # Task result life time until they will be deleted CELERY_RESULT_EXPIRES = int(dt.timedelta(days=1).total_seconds()) # Needed for worker monitoring CELERY_SEND_EVENTS = True CELERY_BEAT_SCHEDULE = {} # default to json serialization only CELERY_ACCEPT_CONTENT = ['json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' {% endif %} if env('SENTRY_DSN', default=''): sentry_logging = LoggingIntegration( level=logging.INFO, # Capture info and above as breadcrumbs event_level=logging.ERROR # Send error events from log messages ) sentry_sdk.init( dsn=env('SENTRY_DSN', default=''), integrations=[DjangoIntegration(), sentry_logging] )
def __init__(self, env): # Load settings self.config = Config(os.path.dirname(os.path.realpath(__file__))) self.config.from_object(f'settings.servers.{env}.Config') # Sentry (logging) if self.config.get('SENTRY_DSN'): sentry_logging = LoggingIntegration(level=logging.INFO, event_level=logging.WARNING) self.sentry = sentry_sdk.init(self.config.get('SENTRY_DSN'), integrations=[ sentry_logging, TornadoIntegration(), RedisIntegration() ]) # Initialize application super().__init__( [ # Assets (collection) (r'/assets', api.assets.CollectionHandler), (r'/assets/analyze', api.assets.AnalyzeManyHandler), (r'/assets/expire', api.assets.ExpireManyHandler), (r'/assets/persist', api.assets.PersistManyHandler), (r'/assets/transform', api.assets.variations.TransformManyHandler), # Assets (document) (r'/assets/(\w+)', api.assets.DocumentHandler), (r'/assets/(\w+)/analyze', api.assets.AnalyzeHandler), (r'/assets/(\w+)/download', api.assets.DownloadHandler), (r'/assets/(\w+)/expire', api.assets.ExpireHandler), (r'/assets/(\w+)/persist', api.assets.PersistHandler), # Assets > Variations (r'/assets/(\w+)/variations', api.assets.variations.CollectionHandler), (r'/assets/(\w+)/variations/(\w+)', api.assets.variations.DocumentHandler), (r'/assets/(\w+)/variations/(\w+)/download', api.assets.variations.DownloadHandler) ], debug=self.config.get('DEBUG'), default_handler_class=api.APIErrorHandler, default_handler_args={'status_code': 404}) loop = asyncio.get_event_loop() # Set up redis connections self._redis = loop.run_until_complete(self._get_redis(loop)) self._redis_sub = loop.run_until_complete(self._get_redis(loop)) # Set up the event listener loop.run_until_complete( self.listen_for_task_events(self._redis_sub, 'h51_events')) # Mongo database self.mongo = pymongo.MongoClient(self.config.get('MONGO_URI')) self.db = self.mongo.get_default_database() mongoframes.Frame._client = self.mongo if self.config.get('MONGO_PASSWORD'): self.db.authenticate(self.config.get('MONGO_USERNAME'), self.config.get('MONGO_PASSWORD'))
import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration import logging sentry_sdk.init( dsn="https://[email protected]/1332439") sentry_logging = LoggingIntegration(level=logging.INFO, event_level=logging.ERROR) sentry_sdk.init( dsn="https://[email protected]/1332439", integrations=[sentry_logging]) logging.debug("I am a breadcrumb") logging.error("I am an event", extra=dict(just_test="True")) # def before_breadcrumb(crumb, hint): # if 'log_record' in hint: # crumb['data']['thread'] = hint['log_record'].threadName # return crumb # # # sentry_sdk.init(dsn="https://[email protected]/1332439", # before_breadcrumb=before_breadcrumb) x = 0 y = 1 print(y // x)
def setup_bot(backend_name: str, logger, config, restore=None) -> ErrBot: # from here the environment is supposed to be set (daemon / non daemon, # config.py in the python path ) bot_config_defaults(config) if hasattr(config, 'BOT_LOG_FORMATTER'): format_logs(formatter=config.BOT_LOG_FORMATTER) else: format_logs(theme_color=config.TEXT_COLOR_THEME) if config.BOT_LOG_FILE: hdlr = logging.FileHandler(config.BOT_LOG_FILE) hdlr.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(name)-25s %(message)s")) logger.addHandler(hdlr) if hasattr(config, 'BOT_LOG_SENTRY') and config.BOT_LOG_SENTRY: sentry_integrations = [] try: import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration except ImportError: log.exception( "You have BOT_LOG_SENTRY enabled, but I couldn't import modules " "needed for Sentry integration. Did you install sentry-sdk? " "(See https://docs.sentry.io/platforms/python for installation instructions)" ) exit(-1) sentry_logging = LoggingIntegration( level=config.SENTRY_LOGLEVEL, event_level=config.SENTRY_EVENTLEVEL ) sentry_integrations.append(sentry_logging) if hasattr(config, 'BOT_LOG_SENTRY_FLASK') and config.BOT_LOG_SENTRY_FLASK: try: from sentry_sdk.integrations.flask import FlaskIntegration except ImportError: log.exception( "You have BOT_LOG_SENTRY enabled, but I couldn't import modules " "needed for Sentry integration. Did you install sentry-sdk[flask]? " "(See https://docs.sentry.io/platforms/python/flask for installation instructions)" ) exit(-1) sentry_integrations.append(FlaskIntegration()) try: if hasattr(config, 'SENTRY_TRANSPORT') and isinstance(config.SENTRY_TRANSPORT, tuple): mod = importlib.import_module(config.SENTRY_TRANSPORT[1]) transport = getattr(mod, config.SENTRY_TRANSPORT[0]) sentry_sdk.init( dsn=config.SENTRY_DSN, integrations=sentry_integrations, transport=transport ) else: sentry_sdk.init( dsn=config.SENTRY_DSN, integrations=sentry_integrations ) except ImportError: log.exception(f'Unable to import selected SENTRY_TRANSPORT - {config.SENTRY_TRANSPORT}') exit(-1) logger.setLevel(config.BOT_LOG_LEVEL) storage_plugin = get_storage_plugin(config) # init the botplugin manager botplugins_dir = path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR) if not path.exists(botplugins_dir): makedirs(botplugins_dir, mode=0o755) plugin_indexes = getattr(config, 'BOT_PLUGIN_INDEXES', (PLUGIN_DEFAULT_INDEX,)) if isinstance(plugin_indexes, str): plugin_indexes = (plugin_indexes, ) # Extra backend is expected to be a list type, convert string to list. extra_backend = getattr(config, 'BOT_EXTRA_BACKEND_DIR', []) if isinstance(extra_backend, str): extra_backend = [extra_backend] backendpm = BackendPluginManager(config, 'errbot.backends', backend_name, ErrBot, CORE_BACKENDS, extra_backend) log.info(f'Found Backend plugin: {backendpm.plugin_info.name}') repo_manager = BotRepoManager(storage_plugin, botplugins_dir, plugin_indexes) try: bot = backendpm.load_plugin() botpm = BotPluginManager(storage_plugin, config.BOT_EXTRA_PLUGIN_DIR, config.AUTOINSTALL_DEPS, getattr(config, 'CORE_PLUGINS', None), getattr(config, 'DISABLE_CORE_PLUGINS', None), lambda name, clazz: clazz(bot, name), getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, )), getattr(config, 'PLUGIN_CONFIG', None)) bot.attach_storage_plugin(storage_plugin) bot.attach_repo_manager(repo_manager) bot.attach_plugin_manager(botpm) bot.initialize_backend_storage() # restore the bot from the restore script if restore: # Prepare the context for the restore script if 'repos' in bot: log.fatal('You cannot restore onto a non empty bot.') sys.exit(-1) log.info(f'**** RESTORING the bot from {restore}') restore_bot_from_backup(restore, bot=bot, log=log) print('Restore complete. You can restart the bot normally') sys.exit(0) errors = bot.plugin_manager.update_plugin_places(repo_manager.get_all_repos_paths()) if errors: log.error('Some plugins failed to load:\n' + '\n'.join(errors.values())) bot._plugin_errors_during_startup = "\n".join(errors.values()) return bot except Exception: log.exception("Unable to load or configure the backend.") exit(-1)
from girderformindlogger.utility._cache import cache, requestCache, rateLimitBuffer _quiet = False _originalStdOut = sys.stdout _originalStdErr = sys.stderr auditLogger = logging.getLogger('girder_audit') auditLogger.setLevel(logging.INFO) logger = logging.getLogger('girderformindlogger') logger.setLevel( logging.DEBUG) # Pass everything; let filters handle level-based filtering config.loadConfig() # Populate the config info at import time # Initialize sentry logging env = os.environ.get('HTTP_HOST', 'localhost') sentry_logging = LoggingIntegration( level=logging.INFO, # Capture info and above as breadcrumbs event_level=logging.CRITICAL # Send errors as events ) if env is not 'localhost': sentry_sdk.init(dsn=config.getConfig()['sentry']['backend_dsn'], environment=env, integrations=[sentry_logging]) class LogLevelFilter(object): """ Filter log records based on whether they are between a min and max level. """ def __init__(self, min, max): self.minLevel = min self.maxLevel = max
import logging import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.logging import LoggingIntegration from .settings import * # noqa from .settings import RELEASE, os ALLOWED_HOSTS = [os.environ["SITE"]] SECRET_KEY = os.environ["SECRET_KEY"] AWS_ACCESS_KEY_ID = os.environ["SES_ID"] AWS_SECRET_ACCESS_KEY = os.environ["SES_KEY"] GH_APP_PEM = os.environ["GHAPP_PRIVKEY"] GH_APP_WEBHOOK_SECRET = os.environ["GHAPP_WEBHOOK_SECRET"] SOCIAL_AUTH_GITHUB_APP_SECRET = os.environ["GHAPP_CLIENT_SECRET"] sentry_logging = LoggingIntegration( level=logging.INFO, event_level=logging.WARNING, ) sentry_sdk.init( dsn=os.environ["RAVEN_DSN"], release=RELEASE, integrations=[ DjangoIntegration(), sentry_logging, ], )
from sentry_sdk.integrations.logging import LoggingIntegration from fastdrewdru import config from fastdrewdru.views import router as fastdrewdru_router from helloworld.views import router as helloworld_router from movies.views import router as movies_router BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) settings = config.get_settings() # region: Initialize logs logging.config.fileConfig("logging.conf", disable_existing_loggers=False) logger = logging.getLogger(__name__) sentry_logging = LoggingIntegration( level=logging.WARNING, # Capture info and above as breadcrumbs event_level=logging.ERROR, # Send errors as events ) sentry_sdk.init( settings.SENTRY_DNS, traces_sample_rate=settings.SENTRY_TRACES_SAMPLE_RATE, integrations=[sentry_logging], ) # endregion app = FastAPI( title="drewdru.com", description="REST API for drewdru.com", version=settings.version, ) # region: Initialize middlewares
'api', None): return current_app.login_manager.unauthorized() register_commands(app) return app IGNORED_EXCEPTION_TYPES = (HTTPException, ) if dsn := os.getenv('PYCROFT_SENTRY_DSN'): def before_send(event, hint): if 'exc_info' in hint: exc_type, exc_value, _tb = hint['exc_info'] if isinstance(exc_value, IGNORED_EXCEPTION_TYPES): return None return event logging_integration = LoggingIntegration( level=logging. INFO, # INFO / WARN create breadcrumbs, just as SQL queries event_level=logging.ERROR, # errors and above create breadcrumbs ) sentry_sdk.init(dsn=dsn, integrations=[FlaskIntegration(), logging_integration], traces_sample_rate=1.0, before_send=before_send)
def __init__(self): # We don't want sentry making noise if an error is caught when you don't have internet sentry_errors = logging.getLogger('sentry.errors') sentry_errors.disabled = True sentry_uncaught = logging.getLogger('sentry.errors.uncaught') sentry_uncaught.disabled = True if SENTRY_SDK_AVAILABLE: cacert = None if hasattr(sys, "frozen"): cacert_resource = get_resource("cacert.pem") if cacert_resource is not None and os.path.isfile( cacert_resource): cacert = cacert_resource else: log.error( "The SSL certificate bundle file '{}' could not be found" .format(cacert_resource)) # Don't send log records as events. sentry_logging = LoggingIntegration(level=logging.INFO, event_level=None) sentry_sdk.init(dsn=CrashReport.DSN, release=__version__, ca_certs=cacert, default_integrations=False, integrations=[sentry_logging]) tags = { "os:name": platform.system(), "os:release": platform.release(), "os:win_32": " ".join(platform.win32_ver()), "os:mac": "{} {}".format(platform.mac_ver()[0], platform.mac_ver()[2]), "os:linux": " ".join(distro.linux_distribution()), } with sentry_sdk.configure_scope() as scope: for key, value in tags.items(): scope.set_tag(key, value) extra_context = { "python:version": "{}.{}.{}".format(sys.version_info[0], sys.version_info[1], sys.version_info[2]), "python:bit": struct.calcsize("P") * 8, "python:encoding": sys.getdefaultencoding(), "python:frozen": "{}".format(hasattr(sys, "frozen")) } if sys.platform.startswith("linux") and not hasattr(sys, "frozen"): # add locale information try: language, encoding = locale.getlocale() extra_context["locale:language"] = language extra_context["locale:encoding"] = encoding except ValueError: pass # add GNS3 VM version if it exists home = os.path.expanduser("~") gns3vm_version = os.path.join(home, ".config", "GNS3", "gns3vm_version") if os.path.isfile(gns3vm_version): try: with open(gns3vm_version) as fd: extra_context["gns3vm:version"] = fd.readline( ).strip() except OSError: pass with sentry_sdk.configure_scope() as scope: for key, value in extra_context.items(): scope.set_extra(key, value)
'level': 'WARNING', 'propagate': True, }, '__main__': { 'handlers': ['console', 'file'], 'level': os.getenv('LOGGING_LEVEL', 'DEBUG'), 'propagate': True, } } } dictConfig(LOGGING) sentry_logging = LoggingIntegration( level=logging.DEBUG, # Capture debug and above as breadcrumbs event_level=logging.ERROR # Send errors as events ) # Switch to public integration if possible sentry_sdk.init( dsn="https://[email protected]/5220965", integrations=[DjangoIntegration(), sentry_logging], release=os.getenv("RELEASE_TAG", "dev"), environment=os.getenv('ENV', ENVIRONMENT), send_default_pii=True ) with configure_scope() as scope: scope.set_extra("instance", ENVIRONMENT) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
def __init__(self): # We don't want sentry making noise if an error is caught when we don't have internet sentry_errors = logging.getLogger('sentry.errors') sentry_errors.disabled = True sentry_uncaught = logging.getLogger('sentry.errors.uncaught') sentry_uncaught.disabled = True self._sentry_initialized = False if SENTRY_SDK_AVAILABLE: cacert = None if hasattr(sys, "frozen"): cacert_resource = get_resource("cacert.pem") if cacert_resource is not None and os.path.isfile( cacert_resource): cacert = cacert_resource else: log.error( "The SSL certificate bundle file '{}' could not be found" .format(cacert_resource)) # Don't send log records as events. sentry_logging = LoggingIntegration(level=logging.INFO, event_level=None) sentry_sdk.init(dsn=CrashReport.DSN, release=__version__, ca_certs=cacert, default_integrations=False, integrations=[sentry_logging]) tags = { "os:name": platform.system(), "os:release": platform.release(), "os:win_32": " ".join(platform.win32_ver()), "os:mac": "{} {}".format(platform.mac_ver()[0], platform.mac_ver()[2]), "os:linux": " ".join(distro.linux_distribution()), } self._add_qt_information(tags) with sentry_sdk.configure_scope() as scope: for key, value in tags.items(): scope.set_tag(key, value) extra_context = { "python:version": "{}.{}.{}".format(sys.version_info[0], sys.version_info[1], sys.version_info[2]), "python:bit": struct.calcsize("P") * 8, "python:encoding": sys.getdefaultencoding(), "python:frozen": "{}".format(hasattr(sys, "frozen")) } # extra controller and compute information from .controller import Controller from .compute_manager import ComputeManager extra_context["controller:version"] = Controller.instance( ).version() extra_context["controller:host"] = Controller.instance().host() extra_context["controller:connected"] = Controller.instance( ).connected() for index, compute in enumerate( ComputeManager.instance().computes()): extra_context["compute{}:id".format(index)] = compute.id() extra_context["compute{}:name".format(index)] = compute.name(), extra_context["compute{}:host".format(index)] = compute.host(), extra_context["compute{}:connected".format( index)] = compute.connected() extra_context["compute{}:platform".format( index)] = compute.capabilities().get("platform") extra_context["compute{}:version".format( index)] = compute.capabilities().get("version") with sentry_sdk.configure_scope() as scope: for key, value in extra_context.items(): scope.set_extra(key, value)
}, }, } MAPBOX_KEY = env('MAPBOX_KEY') DEPLOY_ENV = env('DEPLOY_ENV') SENTRY_DSN = env('SENTRY_DSN') GIT_VERSION = env('GIT_VERSION') if SENTRY_DSN: from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.logging import LoggingIntegration import logging integrations = [DjangoIntegration()] logger_level = logging.INFO sentry_logging = LoggingIntegration(level=logger_level, event_level=logger_level) integrations.append(sentry_logging) sentry_sdk.init( dsn=SENTRY_DSN, integrations=integrations, send_default_pii=True, environment=DEPLOY_ENV, release=GIT_VERSION, ) MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'app.middleware.RestAPICsrfMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
import os import sys from _socket import gethostname, gethostbyname SERVICE_NAME = "@= project_name =@" production = bool(os.getenv('PRODUCTION', False)) DEBUG = True if not production else False TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test' import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.logging import LoggingIntegration sentry_logging = LoggingIntegration( level=logging.DEBUG, event_level=logging.ERROR ) if not DEBUG: sentry_sdk.init( dsn="CHANGE_ME", environment=os.getenv('ENVIRONMENT', 'local'), integrations=[sentry_logging, DjangoIntegration()] ) LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '%(levelname)s|%(asctime)s|%(name)s>> %(message)s',
import requests import sentry_sdk # noqa: I001; pylint: disable=ungrouped-imports; conflicts with Flake8 from flask import Flask from legal_api.models import Business from legal_api.services.bootstrap import AccountService from sentry_sdk.integrations.logging import LoggingIntegration # noqa: I001 import config # pylint: disable=import-error; false positive in gha only from utils.logging import setup_logging # noqa: I001; pylint: disable=import-error; false positive in gha only # noqa: 1005 setup_logging( os.path.join(os.path.abspath(os.path.dirname(__file__)), 'logging.conf')) SENTRY_LOGGING = LoggingIntegration( event_level=logging.ERROR # send errors as events ) def create_app(run_mode=os.getenv('FLASK_ENV', 'production')): """Return a configured Flask App using the Factory method.""" app = Flask(__name__) app.config.from_object(config.CONFIGURATION[run_mode]) # Configure Sentry if app.config.get('SENTRY_DSN', None): sentry_sdk.init(dsn=app.config.get('SENTRY_DSN'), integrations=[SENTRY_LOGGING]) register_shellcontext(app) return app
from asyncio import get_event_loop import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.aiohttp import AioHttpIntegration from aidbox_python_sdk.main import create_app as _create_app # Don't remove these imports from app.sdk import sdk_settings, sdk import app.subscriptions import app.sdc_demo import app.sdc.operations coloredlogs.install(level="DEBUG", fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s") logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG) logging.getLogger("aidbox_sdk").setLevel(logging.INFO) logging.getLogger("urllib3").setLevel(logging.INFO) sentry_logging = LoggingIntegration( level=logging.DEBUG, # Capture info and above as breadcrumbs event_level=logging.WARNING, # Send warnings as events ) sentry_sdk.init(integrations=[AioHttpIntegration(), sentry_logging]) async def create_app(): return await _create_app(sdk_settings, sdk, debug=True)
from moa.toot import Toot from moa.toot_poster import TootPoster from moa.tweet import Tweet from moa.tweet_poster import TweetPoster start_time = time.time() moa_config = os.environ.get('MOA_CONFIG', 'DevelopmentConfig') c = getattr(importlib.import_module('config'), moa_config) if c.SENTRY_DSN: import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration sentry_logging = LoggingIntegration( level=logging.INFO, # Capture info and above as breadcrumbs event_level=logging.FATAL # Only send fatal errors as events ) sentry_sdk.init(dsn=c.SENTRY_DSN, integrations=[sentry_logging]) parser = argparse.ArgumentParser(description='Moa Worker') parser.add_argument('--worker', dest='worker', type=int, required=False, default=1) args = parser.parse_args() worker_stat = WorkerStat(worker=args.worker) worker_stat.time = 0 FORMAT = "%(asctime)-15s [%(process)d] [%(filename)s:%(lineno)s : %(funcName)s()] %(message)s"
{%- if cookiecutter.use_whitenoise == 'n' -%} # Collectfast # ------------------------------------------------------------------------------ # https://github.com/antonagestam/collectfast#installation INSTALLED_APPS = ['collectfast'] + INSTALLED_APPS # noqa F405 AWS_PRELOAD_METADATA = True {% endif %} {%- if cookiecutter.use_sentry == 'y' -%} # Sentry # ------------------------------------------------------------------------------ SENTRY_DSN = env('SENTRY_DSN') SENTRY_CELERY_LOGLEVEL = env.int('DJANGO_SENTRY_LOG_LEVEL', logging.INFO) sentry_logging = LoggingIntegration( level=DJANGO_SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs event_level=None # Send no events from log messages ) sentry_sdk.init( dsn="SENTRY_DSN", integrations=[sentry_logging, DjangoIntegration()] ) {%- else %} # LOGGING # ------------------------------------------------------------------------------ # See: https://docs.djangoproject.com/en/dev/ref/settings/#logging # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error when DEBUG=False. # See https://docs.djangoproject.com/en/dev/topics/logging for # more details on how to customize your logging configuration.
import logging import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration from bot.bot import bot from bot.constants import Client, STAFF_ROLES, WHITELISTED_CHANNELS from bot.utils.decorators import in_channel_check from bot.utils.extensions import walk_extensions sentry_logging = LoggingIntegration(level=logging.DEBUG, event_level=logging.WARNING) sentry_sdk.init(dsn=Client.sentry_dsn, integrations=[sentry_logging]) log = logging.getLogger(__name__) bot.add_check(in_channel_check(*WHITELISTED_CHANNELS, bypass_roles=STAFF_ROLES)) for ext in walk_extensions(): bot.load_extension(ext) log.info("Get the coconuts, because Sir Lancebot is ready to mount his horse!") bot.run(Client.token)
import logging import discord import sentry_sdk from discord.ext.commands import when_mentioned_or from sentry_sdk.integrations.logging import LoggingIntegration from bot import patches from bot.bot import Bot from bot.constants import Bot as BotConfig, DEBUG_MODE sentry_logging = LoggingIntegration(level=logging.TRACE, event_level=logging.WARNING) sentry_sdk.init(dsn=BotConfig.sentry_dsn, integrations=[sentry_logging]) bot = Bot( command_prefix=when_mentioned_or(BotConfig.prefix), activity=discord.Game(name="Commands: !help"), case_insensitive=True, max_messages=10_000, ) # Internal/debug bot.load_extension("bot.cogs.error_handler") bot.load_extension("bot.cogs.filtering") bot.load_extension("bot.cogs.logging") bot.load_extension("bot.cogs.security") # Commands, etc bot.load_extension("bot.cogs.antimalware")
def main(global_config, **settings): dsn = settings.get("sentry.dsn") or os.environ.get("SENTRY_DSN") if dsn: sentry_sdk.init( dsn=dsn, integrations=[ LoggingIntegration(), PyramidIntegration(), ], send_default_pii=True, request_bodies="always", environment=settings.get("sentry.environment"), ) config = Configurator( autocommit=True, settings=settings, authentication_policy=AuthenticationPolicy(settings['auth.file'], __name__), authorization_policy=AuthorizationPolicy(), route_prefix=ROUTE_PREFIX, ) config.include('pyramid_exclog') config.include("cornice") config.add_forbidden_view(forbidden) config.add_request_method(request_params, 'params', reify=True) config.add_request_method(authenticated_role, reify=True) config.add_request_method(check_accreditation) config.add_renderer('json', JSON(serializer=simplejson.dumps)) config.add_renderer('prettyjson', JSON(indent=4, serializer=simplejson.dumps)) config.add_renderer('jsonp', JSONP(param_name='opt_jsonp', serializer=simplejson.dumps)) config.add_renderer('prettyjsonp', JSONP(indent=4, param_name='opt_jsonp', serializer=simplejson.dumps)) # search for plugins plugins = settings.get('plugins') and settings['plugins'].split(',') for entry_point in iter_entry_points('openprocurement.audit.api.plugins'): if not plugins or entry_point.name in plugins: plugin = entry_point.load() plugin(config) pass # CouchDB connection aserver, server, db = set_api_security(settings) config.registry.couchdb_server = server if aserver: config.registry.admin_couchdb_server = aserver config.registry.db = db # readjust couchdb json decoder couchdb_json_decode() # Document Service key config.registry.docservice_url = settings.get('docservice_url') config.registry.docservice_username = settings.get('docservice_username') config.registry.docservice_password = settings.get('docservice_password') config.registry.docservice_upload_url = settings.get('docservice_upload_url') # config.registry.docservice_key = dockey = Signer(settings.get('dockey', '').decode('hex')) # config.registry.keyring = keyring = {} # dockeys = settings.get('dockeys') if 'dockeys' in settings else dockey.hex_vk() # for key in dockeys.split('\0'): # keyring[key[:8]] = Verifier(key) signing_key = settings.get('dockey', '') signer = SigningKey(signing_key, encoder=HexEncoder) if signing_key else SigningKey.generate() config.registry.docservice_key = signer verifier = signer.verify_key config.registry.keyring = { verifier.encode(encoder=HexEncoder)[:8].decode(): verifier } dockeys = settings.get('dockeys', '') for key in dockeys.split('\0'): if key: config.registry.keyring[key[:8]] = VerifyKey(key, encoder=HexEncoder) # migrate data if not os.environ.get('MIGRATION_SKIP'): for entry_point in iter_entry_points('openprocurement.audit.api.migrations'): plugin = entry_point.load() plugin(config.registry) config.registry.server_id = settings.get('id', '') # search subscribers subscribers_keys = [k for k in settings if k.startswith('subscribers.')] for k in subscribers_keys: subscribers = settings[k].split(',') for subscriber in subscribers: for entry_point in iter_entry_points('openprocurement.{}'.format(k), subscriber): if entry_point: plugin = entry_point.load() plugin(config) config.registry.health_threshold = float(settings.get('health_threshold', 512)) config.registry.health_threshold_func = settings.get('health_threshold_func', 'all') config.registry.update_after = asbool(settings.get('update_after', True)) config.registry.disable_opt_fields_filter = asbool(settings.get('disable_opt_fields_filter', False)) return config.make_wsgi_app()
def configure_sdk(): from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.celery import CeleryIntegration from sentry_sdk.integrations.redis import RedisIntegration assert sentry_sdk.Hub.main.client is None sdk_options = dict(settings.SENTRY_SDK_CONFIG) relay_dsn = sdk_options.pop("relay_dsn", None) internal_project_key = get_project_key() upstream_dsn = sdk_options.pop("dsn", None) sdk_options["traces_sampler"] = traces_sampler if upstream_dsn: upstream_transport = make_transport( get_options(dsn=upstream_dsn, **sdk_options)) else: upstream_transport = None if relay_dsn: relay_transport = make_transport( get_options(dsn=relay_dsn, **sdk_options)) elif internal_project_key and internal_project_key.dsn_private: relay_transport = make_transport( get_options(dsn=internal_project_key.dsn_private, **sdk_options)) else: relay_transport = None _override_on_full_queue(relay_transport, "internal.uncaptured.events.relay") _override_on_full_queue(upstream_transport, "internal.uncaptured.events.upstream") class MultiplexingTransport(sentry_sdk.transport.Transport): def capture_envelope(self, envelope): # Temporarily capture envelope counts to compare to ingested # transactions. metrics.incr("internal.captured.events.envelopes") # Assume only transactions get sent via envelopes if options.get( "transaction-events.force-disable-internal-project"): return self._capture_anything("capture_envelope", envelope) def capture_event(self, event): if event.get("type") == "transaction" and options.get( "transaction-events.force-disable-internal-project"): return self._capture_anything("capture_event", event) def _capture_anything(self, method_name, *args, **kwargs): # Upstream should get the event first because it is most isolated from # the this sentry installation. if upstream_transport: metrics.incr("internal.captured.events.upstream") # TODO(mattrobenolt): Bring this back safely. # from sentry import options # install_id = options.get('sentry:install-id') # if install_id: # event.setdefault('tags', {})['install-id'] = install_id getattr(upstream_transport, method_name)(*args, **kwargs) if relay_transport and options.get( "store.use-relay-dsn-sample-rate") == 1: if is_current_event_safe(): metrics.incr("internal.captured.events.relay") getattr(relay_transport, method_name)(*args, **kwargs) else: metrics.incr( "internal.uncaptured.events.relay", skip_internal=False, tags={"reason": "unsafe"}, ) sentry_sdk.init( transport=MultiplexingTransport(), integrations=[ DjangoIntegration(), CeleryIntegration(), LoggingIntegration(event_level=None), RustInfoIntegration(), RedisIntegration(), ], **sdk_options, )
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Sentry from a config entry.""" # Migrate environment from config entry data to config entry options if ( CONF_ENVIRONMENT not in entry.options and CONF_ENVIRONMENT in entry.data and entry.data[CONF_ENVIRONMENT] ): options = {**entry.options, CONF_ENVIRONMENT: entry.data[CONF_ENVIRONMENT]} data = entry.data.copy() data.pop(CONF_ENVIRONMENT) hass.config_entries.async_update_entry(entry, data=data, options=options) # https://docs.sentry.io/platforms/python/logging/ sentry_logging = LoggingIntegration( level=entry.options.get(CONF_LOGGING_LEVEL, DEFAULT_LOGGING_LEVEL), event_level=entry.options.get( CONF_LOGGING_EVENT_LEVEL, DEFAULT_LOGGING_EVENT_LEVEL ), ) # Additional/extra data collection channel = get_channel(current_version) huuid = await hass.helpers.instance_id.async_get() system_info = await hass.helpers.system_info.async_get_system_info() custom_components = await async_get_custom_components(hass) tracing = {} if entry.options.get(CONF_TRACING): tracing = { "traces_sample_rate": entry.options.get( CONF_TRACING_SAMPLE_RATE, DEFAULT_TRACING_SAMPLE_RATE ), } # pylint: disable=abstract-class-instantiated sentry_sdk.init( dsn=entry.data[CONF_DSN], environment=entry.options.get(CONF_ENVIRONMENT), integrations=[sentry_logging, AioHttpIntegration(), SqlalchemyIntegration()], release=current_version, before_send=lambda event, hint: process_before_send( hass, entry.options, channel, huuid, system_info, custom_components, event, hint, ), **tracing, ) async def update_system_info(now): nonlocal system_info system_info = await hass.helpers.system_info.async_get_system_info() # Update system info every hour hass.helpers.event.async_call_later(3600, update_system_info) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, update_system_info) return True
} }, } if config.has_option('sentry', 'dsn'): import sentry_sdk from sentry_sdk.integrations.celery import CeleryIntegration from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger from .sentry import PretixSentryIntegration, setup_custom_filters sentry_sdk.init( dsn=config.get('sentry', 'dsn'), integrations=[ PretixSentryIntegration(), CeleryIntegration(), LoggingIntegration(level=logging.INFO, event_level=logging.CRITICAL) ], environment=SITE_URL, release=__version__, send_default_pii=False, ) ignore_logger('pretix.base.tasks') ignore_logger('django.security.DisallowedHost') setup_custom_filters() CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TASK_DEFAULT_QUEUE = 'default' CELERY_TASK_QUEUES = ( Queue('default', routing_key='default.#'), Queue('checkout', routing_key='checkout.#'),
@pytest.mark.parametrize("logger", [logger, other_logger]) def test_logging_works_with_many_loggers(sentry_init, capture_events, logger): sentry_init(integrations=[LoggingIntegration(event_level="ERROR")]) events = capture_events() logger.info("bread") logger.critical("LOL") (event, ) = events assert event["level"] == "fatal" assert not event["logentry"]["params"] assert event["logentry"]["message"] == "LOL" assert any(crumb["message"] == "bread" for crumb in event["breadcrumbs"]["values"]) @pytest.mark.parametrize("integrations", [None, [], [LoggingIntegration()]]) @pytest.mark.parametrize("kwargs", [{ "exc_info": None }, {}, { "exc_info": 0 }, { "exc_info": False }]) def test_logging_defaults(integrations, sentry_init, capture_events, kwargs): sentry_init(integrations=integrations) events = capture_events() logger.info("bread") logger.critical("LOL", **kwargs) (event, ) = events
import logging import os import sentry_sdk from celery import Celery from sentry_sdk.integrations.celery import CeleryIntegration from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.logging import LoggingIntegration os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') from django.conf import settings # Sentry if settings.IS_PROD: sentry_logging = LoggingIntegration(level=settings.SENTRY_LOG_LEVEL, event_level=logging.ERROR) sentry_sdk.init(dsn=settings.SENTRY_DSN, integrations=[sentry_logging, DjangoIntegration(), CeleryIntegration()]) app = Celery('{{ cookiecutter.project_slug }}') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks()
import logging import os import sentry_sdk from sentry_sdk.integrations.celery import CeleryIntegration from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.redis import RedisIntegration from posthog.settings.base_variables import TEST if not TEST: if os.getenv("SENTRY_DSN"): sentry_sdk.utils.MAX_STRING_LENGTH = 10_000_000 # https://docs.sentry.io/platforms/python/ sentry_logging = sentry_logging = LoggingIntegration(level=logging.INFO, event_level=None) sentry_sdk.init( dsn=os.environ["SENTRY_DSN"], integrations=[DjangoIntegration(), CeleryIntegration(), RedisIntegration(), sentry_logging], request_bodies="always", send_default_pii=True, environment=os.getenv("SENTRY_ENVIRONMENT", "production"), )