|
| 1 | +import os |
| 2 | + |
| 3 | +import sentry_sdk |
| 4 | +from aiohttp import web |
| 5 | +from sentry_sdk.integrations.aiohttp import AioHttpIntegration |
| 6 | + |
| 7 | + |
| 8 | +sentry_sdk.init( |
| 9 | + dsn="", |
| 10 | + environment=os.environ.get("ENV", "test"), |
| 11 | + _experiments={"trace_lifecycle": "stream"}, |
| 12 | + traces_sample_rate=1.0, |
| 13 | + profiles_sample_rate=1.0, |
| 14 | + debug=True, |
| 15 | + integrations=[ |
| 16 | + AioHttpIntegration(), |
| 17 | + ], |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +async def index(request): |
| 22 | + return web.json_response( |
| 23 | + { |
| 24 | + "hello": "world!", |
| 25 | + "error": "http://localhost:5000/error", |
| 26 | + "http-error": "http://localhost:5000/http-error", |
| 27 | + } |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +async def error(request): |
| 32 | + sentry_sdk.set_user({"id": "testuser"}) |
| 33 | + raise ValueError("help! an error!") |
| 34 | + |
| 35 | + |
| 36 | +async def http_error(request): |
| 37 | + raise web.HTTPInternalServerError(reason="something went wrong") |
| 38 | + |
| 39 | + |
| 40 | +@web.middleware |
| 41 | +async def test_middleware(request, handler): |
| 42 | + print("middleware") |
| 43 | + return await handler(request) |
| 44 | + |
| 45 | + |
| 46 | +def make_app(): |
| 47 | + app = web.Application(middlewares=[test_middleware]) |
| 48 | + app.router.add_get("/", index) |
| 49 | + app.router.add_get("/error", error) |
| 50 | + app.router.add_get("/http-error", http_error) |
| 51 | + return app |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + web.run_app(make_app(), port=5000) |
0 commit comments