Example #1
0
async def cli(ctx, config: List[str], logging_config: str):
    try:
        path = join(os.getcwd(), logging_config)
        _logging_config = LoggingConfig.load(path)
    except FileNotFoundError:
        path = join(dirname(__file__), 'configs', logging_config)
        _logging_config = LoggingConfig.load(path)
    _logging_config.apply()

    _config = DipDupConfig.load(config)
    if _config.spec_version not in spec_version_to_version:
        raise ConfigurationError('Unknown `spec_version`')
    if _config.spec_version != __spec_version__ and ctx.invoked_subcommand != 'migrate':
        migration_required(_config.spec_version, __spec_version__)

    if _config.sentry:
        sentry_sdk.init(
            dsn=_config.sentry.dsn,
            integrations=[AioHttpIntegration()],
        )

    ctx.obj = CLIContext(
        config_paths=config,
        config=_config,
        logging_config=_logging_config,
    )
Example #2
0
async def test_post_body_not_read(sentry_init, aiohttp_client, loop,
                                  capture_events):
    from sentry_sdk.integrations.aiohttp import BODY_NOT_READ_MESSAGE

    sentry_init(integrations=[AioHttpIntegration()])

    body = {"some": "value"}

    async def hello(request):
        1 / 0

    app = web.Application()
    app.router.add_post("/", hello)

    events = capture_events()

    client = await aiohttp_client(app)
    resp = await client.post("/", json=body)
    assert resp.status == 500

    event, = events
    exception, = event["exception"]["values"]
    assert exception["type"] == "ZeroDivisionError"
    request = event["request"]

    assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
    assert request["method"] == "POST"
    assert request["data"] == BODY_NOT_READ_MESSAGE
async def test_traces_sampler_gets_request_object_in_sampling_context(
        sentry_init,
        aiohttp_client,
        DictionaryContaining,  # noqa:N803
        ObjectDescribedBy,  # noqa:N803
):
    traces_sampler = mock.Mock()
    sentry_init(
        integrations=[AioHttpIntegration()],
        traces_sampler=traces_sampler,
    )

    async def kangaroo_handler(request):
        return web.Response(text="dogs are great")

    app = web.Application()
    app.router.add_get("/tricks/kangaroo", kangaroo_handler)

    client = await aiohttp_client(app)
    await client.get("/tricks/kangaroo")

    traces_sampler.assert_any_call(
        DictionaryContaining({
            "aiohttp_request":
            ObjectDescribedBy(type=Request,
                              attrs={
                                  "method": "GET",
                                  "path": "/tricks/kangaroo"
                              })
        }))
Example #4
0
async def test_basic(sentry_init, aiohttp_client, loop, capture_events):
    sentry_init(integrations=[AioHttpIntegration()])

    async def hello(request):
        1 / 0

    app = web.Application()
    app.router.add_get("/", hello)

    events = capture_events()

    client = await aiohttp_client(app)
    resp = await client.get("/")
    assert resp.status == 500

    event, = events

    exception, = event["exception"]["values"]
    assert exception["type"] == "ZeroDivisionError"
    request = event["request"]
    host = request["headers"]["Host"]

    assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
    assert request["method"] == "GET"
    assert request["query_string"] == ""
    assert request["url"] == f"http://{host}/"
    assert request["headers"] == {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Host": host,
        "User-Agent": request["headers"]["User-Agent"],
    }
Example #5
0
async def test_post_body_read(sentry_init, aiohttp_client, loop,
                              capture_events):
    sentry_init(integrations=[AioHttpIntegration()])

    body = {"some": "value"}

    async def hello(request):
        await request.json()
        1 / 0

    app = web.Application()
    app.router.add_post("/", hello)

    events = capture_events()

    client = await aiohttp_client(app)
    resp = await client.post("/", json=body)
    assert resp.status == 500

    (event, ) = events
    (exception, ) = event["exception"]["values"]
    assert exception["type"] == "ZeroDivisionError"
    request = event["request"]

    assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
    assert request["method"] == "POST"
    assert request["data"] == json.dumps(body)
Example #6
0
    def run(self):
        try:

            sentry_sdk.init(
                Tokens.sentry_link.value,
                traces_sample_rate=1.0,
                integrations=[
                    AioHttpIntegration(),
                    ThreadingIntegration(),
                    LoggingIntegration(),
                    ModulesIntegration(),
                ],
            )
            log.info("Sentry Setup Done")
            log.info("Bot will now start")
            super().run(Tokens.token.value, reconnect=True)
        except discord.PrivilegedIntentsRequired:
            log.critical(
                "[Login Failure] You need to enable the server members intent on the Discord Developers Portal."
            )
        except discord.errors.LoginFailure:
            log.critical(
                "[Login Failure] The token initialsed in environment(or .env file) is invalid."
            )
        except KeyboardInterrupt:
            log.critical(
                "The bot is shutting down since force shutdown was initiated.")
        except Exception as e:
            log.critical("An exception occured, %s", e)
Example #7
0
File: app.py Project: BenWu/poucave
def init_app(checks: Checks):
    app = web.Application(
        middlewares=[middleware.error_middleware, middleware.request_summary])
    sentry_sdk.init(dsn=config.SENTRY_DSN, integrations=[AioHttpIntegration()])
    app["poucave.cache"] = utils.Cache()
    app["poucave.checks"] = checks

    app.add_routes(routes)

    app.router.add_static("/html/",
                          path=HTML_DIR,
                          name="html",
                          show_index=True)

    # Enable CORS on all routes.
    cors = aiohttp_cors.setup(
        app,
        defaults={
            config.CORS_ORIGINS:
            aiohttp_cors.ResourceOptions(allow_credentials=True,
                                         expose_headers="*",
                                         allow_headers="*")
        },
    )
    for route in list(app.router.routes()):
        cors.add(route)

    return app
Example #8
0
def setup(app: Application,
          app_name: str,
          config: AppConfig,
          package_name: Optional[str] = None) -> None:
    if not package_name:
        package_name = app_name

    app["config"] = config

    app["app_name"] = app_name
    app["hostname"] = socket.gethostname()
    app["distribution"] = pkg_resources.get_distribution(package_name)

    if config.sentry_dsn:
        sentry_sdk.init(
            dsn=str(config.sentry_dsn),
            integrations=[AioHttpIntegration()],
            release=app["distribution"].version,
        )

        with sentry_sdk.configure_scope() as scope:
            scope.set_tag("app_name", app["app_name"])

    app.middlewares.append(common_middleware)  # type: ignore

    app.router.add_get("/-/health", meta.health, name="health")
    app.router.add_get("/-/meta", meta.index, name="meta")
Example #9
0
File: main.py Project: Fozar/Sonata
def main(argv: list):
    sentry_sdk.init(
        "https://[email protected]/5406178",
        traces_sample_rate=1.0,
        integrations=[
            AioHttpIntegration(),
            LoggingIntegration(
                level=logging.INFO,  # Capture info and above as breadcrumbs
                event_level=logging.ERROR,  # Send errors as events
            ),
        ],
    )
    debug = False

    def show_help():
        print("-D or --DEBUG to debug")

    try:
        opts, args = getopt.getopt(argv, "hD", ["help", "DEBUG"])
    except getopt.GetoptError:
        show_help()
        sys.exit(2)
    for opt, arg in opts:
        if opt == "-h":
            show_help()
            sys.exit()
        elif opt in ("-D", "--DEBUG"):
            debug = True

    create_app(debug)
Example #10
0
def run(top_parser: ArgumentParser,
        input_args: Optional[List[str]] = None) -> None:
    args = top_parser.parse_args(input_args)
    loop = asyncio.get_event_loop()

    sentry_url = os.environ.get('SENTRY_URL')
    if sentry_url:
        sentry_sdk.init(dsn=sentry_url, integrations=[AioHttpIntegration()])

    handler = getattr(args, 'handler', None)
    if handler is None:
        top_parser.print_help()
    else:
        loop.add_signal_handler(signal.SIGTERM, on_term_sig)

        try:
            loop.run_until_complete(handler.run(args))
            if getattr(handler, 'run_forever', False):
                loop.run_forever()
        except KeyboardInterrupt:
            pass
        except RuntimeError as e:
            if 'Event loop stopped before Future completed' not in str(e):
                raise
        handler.shutdown()
Example #11
0
    def load(cls, env_prefix: str = 'TG_ODESLI_BOT_'):
        """Load config merging default variables and environment variables.

        :param env_prefix: prefix for environment variables
        :return: filled config object
        """
        config = cls()
        # Load environment from .env file
        dotenv.load_dotenv()
        # Update config object with environment variables
        for env_var_name, value in os.environ.items():
            if env_var_name.startswith(env_prefix):
                var_name = env_var_name[len(env_prefix):]
                # Do not override config vars in testing mode
                if hasattr(config, 'TESTING') and hasattr(config, var_name):
                    continue
                setattr(config, var_name, value)
        if config.SENTRY_DSN:
            sentry_sdk.init(
                dsn=config.SENTRY_DSN,
                integrations=[AioHttpIntegration()],
                environment=config.SENTRY_ENVIRONMENT,
            )
        if not structlog.is_configured():
            config.init_logging()
        return config
Example #12
0
def main():
    """Entry point for poetry script."""
    sentry_sdk.init(
        SENTRY_URL,
        release=Repo().head.object.hexsha,
        integrations=[AioHttpIntegration()],
    )
    bot.run(BOT_TOKEN)
def configure_sentry() -> None:
    """Configure sentry client from config files."""
    sentry_sdk.init(
        dsn=config["sentry"]["dsn"],
        integrations=[
            LoggingIntegration(level=logging.INFO, event_level=logging.ERROR),
            AioHttpIntegration(),
        ],
    )
Example #14
0
def initialize_sentry(sentry_env):
    import sentry_sdk
    from sentry_sdk.integrations.aiohttp import AioHttpIntegration

    sentry_sdk.init(
        dsn=
        "https://[email protected]/5768574",
        integrations=[AioHttpIntegration()],
        environment=sentry_env)
Example #15
0
def initialize_sentry(sentry_env):
    import sentry_sdk
    from sentry_sdk.integrations.aiohttp import AioHttpIntegration

    sentry_sdk.init(
        dsn=
        "https://[email protected]/5768575",
        integrations=[AioHttpIntegration()],
        environment=sentry_env)
Example #16
0
def install(app):
    if not app.config.sentry_dsn:
        raise ValueError('You need to set `sentry_dsn` plugin option to use `sentry` plugin')
    sentry_sdk.init(
        app.config.sentry_dsn,
        environment=os.environ.get('APP_MODE', 'dev'),
        integrations=[AioHttpIntegration()],
        release=app.config.version
    )
    app.sentry = sentry_sdk
Example #17
0
def initialize_sentry(sentry_env):
    import sentry_sdk
    from sentry_sdk.integrations.aiohttp import AioHttpIntegration

    sentry_sdk.init(
        dsn=
        "https://[email protected]/5283135",
        integrations=[AioHttpIntegration()],
        environment=sentry_env,
    )
Example #18
0
def main():

    if config.get("sentry"):
        sentry_sdk.init(dsn=config["sentry"]["dns"],
                        integrations=[AioHttpIntegration()])

    signal.signal(signal.SIGINT, handle_sigint)
    loop = asyncio.get_event_loop()

    loop.run_until_complete(init(config))
Example #19
0
def setup_sentry() -> None:
    """Set up the Sentry logging integrations."""
    sentry_logging = LoggingIntegration(level=logging.DEBUG,
                                        event_level=logging.WARNING)

    sentry_sdk.init(dsn=constants.Bot.sentry_dsn,
                    integrations=[
                        sentry_logging,
                        AioHttpIntegration(),
                        RedisIntegration(),
                    ])
Example #20
0
def initialize_sentry(sentry_env):
    import sentry_sdk
    from sentry_sdk.integrations.aiohttp import AioHttpIntegration
    from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration

    sentry_sdk.init(
        dsn=
        "https://[email protected]/5376966",
        integrations=[SqlalchemyIntegration(),
                      AioHttpIntegration()],
        environment=sentry_env)
Example #21
0
def init_sentry(sentry_cfg: Dict[str, Any]):
    if sentry_sdk is None:
        raise ImportError("`sentry` extra is not installed")
    log.debug("Initializing Sentry...")
    release = f"royalnet@{semantic}"
    sentry_sdk.init(sentry_cfg["dsn"],
                    integrations=[AioHttpIntegration(),
                                  SqlalchemyIntegration(),
                                  LoggingIntegration(event_level=None)],
                    release=release)
    log.info(f"Sentry: {release}")
Example #22
0
def main():
    sentry_dsn = os.getenv('SENTRY_DSN')
    if sentry_dsn:
        sentry_sdk.init(dsn=sentry_dsn,
                        environment="dcatd",
                        integrations=[AioHttpIntegration()],
                        ignore_errors=['HTTPTemporaryRedirect'])

    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    aio_app = application.Application()
    web.run_app(aio_app, port=aio_app.config['web']['port'])
    return 0
Example #23
0
    def __init__(self):
        super().__init__(
            command_prefix=get_prefix,
            description='The number 1 wanna be meme bot',
            case_insensitive=True,
            allowed_mentions=discord.AllowedMentions(
                roles=False,
                everyone=False)
        )

        with open('./dagbot/data/credentials.yml', 'r') as file:
            self.data = yaml.load(file, Loader=yaml.FullLoader)

        self.launch_time = None
        self.session = None
        self.pg_con = None

        self.caching = caching(self)
        self.dagpi = Client(self.data['dagpitoken'])
        self.bwordchecker = bword()
        self.bwordchecker.loadbword()
        self.useage = {}
        self.commands_called = 0

        # self.add_cog(Help(bot))

        self.load_extension("jishaku")

        extensions = [
            "text", "fun", "newimag",
            "reddit", "games", "util",
            "whysomart", "animals", "memes",
            "tags", "misc", "settings",
            "ai", "events", "errors",
            "developer", "help"
        ]
        for extension in extensions:
            try:
                self.load_extension(f"extensions.{extension}")
                print(f"loaded extension {extension}")
            except Exception as error:
                print(f"{extension} cannot be loaded due to {error}")

        self.before_invoke(self.starttyping)
        self.after_invoke(self.exittyping)
        self.loop.create_task(self.startdagbot())

        self.sentry = sentry_sdk.init(
            dsn=self.data['sentryurl'],
            integrations=[AioHttpIntegration()]
        )

        self.run(self.data['token'])
Example #24
0
    def __init__(self):
        super().__init__(command_prefix=get_prefix,
                         description='The number 1 wanna be meme bot',
                         case_insensitive=True,
                         max_messages=100,
                         strip_after_prefix=True,
                         allowed_mentions=discord.AllowedMentions(
                             roles=False, everyone=False),
                         intents=make_intents())

        self.logger = create_logger("Dagbot", logging.DEBUG)
        self.discprd_logger = create_logger('discord', logging.INFO)
        with open('./configuration.yml', 'r') as file:
            self.data = yaml.load(file, Loader=yaml.FullLoader)
        self.data.pop("PWD")
        self.logger.info("Loaded Config File")
        self.launch_time = None
        self.session = None
        self.pool = None
        self.dagpi = None
        self.repo = "https://github.com/Daggy1234/dagbot"
        self.caching = Caching(self)
        self.bwordchecker = bword()
        self.bwordchecker.loadbword()
        self.useage = {}
        self.commands_called = 0

        # self.add_cog(Help(bot))

        self.load_extension("jishaku")

        extensions = [
            "text", "fun", "newimag", "reddit", "games", "util", "whysomart",
            "animals", "memes", "tags", "misc", "settings", "ai", "events",
            "errors", "developer", "help", "automeme", "uploader"
        ]
        for extension in extensions:
            try:
                self.load_extension(f"dagbot.extensions.{extension}")
                self.logger.info(f"loaded extension {extension}")
            except Exception as error:
                self.logger.critical(
                    f"{extension} cannot be loaded due to {error}")

        self.before_invoke(self.starttyping)
        self.logger.info("Initialising Stuff")
        self.loop.create_task(self.startdagbot())
        self.socket_stats = {}
        self.sentry = sentry_sdk.init(dsn=self.data['sentryurl'],
                                      integrations=[AioHttpIntegration()],
                                      release="[email protected]")
        self.logger.info("Ready to roll")
Example #25
0
 def get_app(self) -> web.Application:
     sentry_sdk.init(
         dsn=self.config['sentry_dsn'],
         integrations=[AioHttpIntegration(),
                       SqlalchemyIntegration()])
     app = web.Application(
         middlewares=[error_middleware, validation_middleware, self.db])
     self.init_clients(app)
     self.init_postgres(app)
     self.init_routes(app)
     app.on_response_prepare.append(prepare_cors)
     app.on_shutdown.append(self.stop)
     return app
Example #26
0
def setup(app, dsn, env="dev", level=logging.ERROR, event_level=logging.ERROR):

    sentry_sdk.init(
        dsn=dsn,
        environment=env,
        integrations=[
            LoggingIntegration(
                level=level,  # Capture level and above as breadcrumbs
                event_level=event_level,  # Send event_level and above as events
            ),
            AioHttpIntegration(),
        ],
    )
Example #27
0
def initialise_sentry():
    global sentry_initialised
    if os.environ.get("SENTRY_DSN", "") and not sentry_initialised:
        sentry_sdk.init(
            os.environ.get("SENTRY_DSN"),
            integrations=[
                AwsLambdaIntegration(),
                FlaskIntegration(),
                AioHttpIntegration(),
            ],
            traces_sample_rate=0.01,
        )
        sentry_initialised = True
Example #28
0
def setup_diagnostics(coresys: CoreSys) -> None:
    """Sentry diagnostic backend."""
    sentry_logging = LoggingIntegration(level=logging.WARNING,
                                        event_level=logging.CRITICAL)

    _LOGGER.info("Initialize Supervisor Sentry")
    sentry_sdk.init(
        dsn=
        "https://[email protected]/5370612",
        before_send=lambda event, hint: filter_data(coresys, event, hint),
        max_breadcrumbs=30,
        integrations=[AioHttpIntegration(), sentry_logging],
        release=SUPERVISOR_VERSION,
    )
Example #29
0
def create_app():
    configure_logging(level=CONFIG.log_level)
    backdoor.setup()
    if CONFIG.sentry_enable:
        sentry_sdk.init(
            dsn=CONFIG.sentry_dsn,
            integrations=[AioHttpIntegration()]
        )
    api = web.Application()
    api.router.add_routes(routes)
    app = web.Application()
    app.add_subapp('/api/v1', api)
    setup_aiojobs(app, limit=5000, pending_limit=5000)
    return app
Example #30
0
def _get_sentry_integrations() -> List[object]:
    """Get integrations for Sentry based on installed packages."""
    integrations = []
    try:
        import flask
    except ImportError:
        pass
    else:
        try:
            from sentry_sdk.integrations.flask import FlaskIntegration
        except ImportError as exc:
            _LOGGER.warning("Cannot import Sentry Flask integration: %s",
                            str(exc))

        else:
            integrations.append(FlaskIntegration())
            _LOGGER.debug("Flask integration for Sentry enabled")

    try:
        import sqlalchemy
    except ImportError:
        pass
    else:
        try:
            from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
        except ImportError as exc:
            _LOGGER.warning("Cannot import Sentry SQLAlchemy integration: %s",
                            str(exc))
        else:
            integrations.append(SqlalchemyIntegration())
            _LOGGER.debug("SQLAlchemy integration for Sentry enabled")

    if sys.version_info >= (3, 7):
        # Available only for python 3.7+
        try:
            import aiohttp
        except ImportError:
            pass
        else:
            try:
                from sentry_sdk.integrations.aiohttp import AioHttpIntegration
            except ImportError as exc:
                _LOGGER.warning("Cannot import Sentry AIOHTTP integration: %s",
                                str(exc))
            else:
                integrations.append(AioHttpIntegration())
                _LOGGER.debug("AIOHTTP integration for Sentry enabled")

    return integrations