async def init(argv): ap = argparse.ArgumentParser() commandline.standard_argparse_options(ap, default_config='./config/geonames.yaml') # # define your command-line arguments here # options = ap.parse_args(argv) config = commandline.config_from_options(options, TRAFARET) # setup application and extensions app = web.Application() endpoint = az.create_endpoint('geonames_app', ipv4='127.0.0.1', port=9001) zipkin_address = 'http://127.0.0.1:9411' tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) # load config from yaml file in current dir app['config'] = config # create connection to the database app.on_startup.append(init_pg) # shutdown db connection on exit app.on_cleanup.append(close_pg) # setup views and routes setup_routes(app) return app
async def test_middleware_with_default_transport(tracer, fake_transport): app = web.Application() az.setup(app, tracer) async def handler(request): return web.Response(body=b'data') req = make_mocked_request('GET', '/aa', headers={'token': 'x'}, app=app) req.match_info.route.resource.canonical = '/{pid}' middleware = middleware_maker() await middleware(req, handler) span = az.request_span(req) assert span assert len(fake_transport.records) == 1 rec = fake_transport.records[0] assert rec.asdict()['tags'][az.HTTP_ROUTE] == '/{pid}' # noop span does not produce records headers = {'X-B3-Sampled': '0'} req_noop = make_mocked_request('GET', '/', headers=headers, app=app) await middleware(req_noop, handler) span = az.request_span(req_noop) assert span assert len(fake_transport.records) == 1
async def test_middleware_with_default_transport(tracer: az.Tracer, fake_transport: Any) -> None: app = web.Application() az.setup(app, tracer) async def handler(request: web.Request) -> web.StreamResponse: return web.Response(body=b"data") req = make_mocked_request("GET", "/aa", headers={"token": "x"}, app=app) assert req.match_info.route.resource is not None req.match_info.route.resource.canonical = "/{pid}" # type: ignore[misc] middleware = middleware_maker() await middleware(req, handler) span = az.request_span(req) assert span assert len(fake_transport.records) == 1 rec = fake_transport.records[0] assert rec.asdict()["tags"][az.HTTP_ROUTE] == "/{pid}" # noop span does not produce records headers = {"X-B3-Sampled": "0"} req_noop = make_mocked_request("GET", "/", headers=headers, app=app) await middleware(req_noop, handler) span = az.request_span(req_noop) assert span assert len(fake_transport.records) == 1
async def test_middleware_with_default_transport(tracer, fake_transport): app = web.Application() az.setup(app, tracer) async def handler(request): return web.Response(body=b'data') req = make_mocked_request('GET', '/aa', headers={'token': 'x'}, app=app) req.match_info.route.resource.canonical = '/{pid}' middleware_factory = middleware_maker() middleware = await middleware_factory(app, handler) await middleware(req) span = az.request_span(req) assert span assert len(fake_transport.records) == 1 rec = fake_transport.records[0] assert rec.asdict()['tags'][az.HTTP_ROUTE] == '/{pid}' # noop span does not produce records headers = {'X-B3-Sampled': '0'} req_noop = make_mocked_request('GET', '/', headers=headers, app=app) await middleware(req_noop) span = az.request_span(req_noop) assert span assert len(fake_transport.records) == 1
async def make_app(): app = web.Application() app.router.add_get("/api/v1/data", handler) app.router.add_get("/", handler) endpoint = az.create_endpoint("service_a", ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) app["session"] = session async def close_session(app): await app["session"].close() app.on_cleanup.append(close_session) az.setup(app, tracer) TEMPLATES_ROOT = pathlib.Path(__file__).parent / "templates" aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(str(TEMPLATES_ROOT))) return app
async def test_middleware_with_valid_ip(tracer, version, address_in, address_out): if address_out is None: address_out = address_in app = web.Application() az.setup(app, tracer) # Fake transport transp = Mock() transp.get_extra_info.return_value = (address_in, '0') async def handler(request): return web.Response(body=b'data') req = make_mocked_request('GET', '/', headers={'token': 'x'}, transport=transp, app=app) middleware_factory = middleware_maker() middleware = await middleware_factory(app, handler) with patch('aiozipkin.span.Span.remote_endpoint') as mocked_remote_ep: await middleware(req) assert mocked_remote_ep.call_count == 1 args, kwargs = mocked_remote_ep.call_args assert kwargs[version] == address_out
def test_basic_setup(tracer): app = web.Application() az.setup(app, tracer) fetched_tracer = az.get_tracer(app) assert len(app.middlewares) == 1 assert tracer is fetched_tracer
async def test_middleware_with_valid_ip(tracer: az.Tracer, version: str, address_in: str, address_out: Optional[str]) -> None: if address_out is None: address_out = address_in app = web.Application() az.setup(app, tracer) # Fake transport transp = Mock() transp.get_extra_info.return_value = (address_in, "0") async def handler(request: web.Request) -> web.StreamResponse: return web.Response(body=b"data") req = make_mocked_request("GET", "/", headers={"token": "x"}, transport=transp, app=app) middleware = middleware_maker() with patch("aiozipkin.span.Span.remote_endpoint") as mocked_remote_ep: await middleware(req, handler) assert mocked_remote_ep.call_count == 1 args, kwargs = mocked_remote_ep.call_args assert kwargs[version] == address_out
async def make_app(): app = web.Application() app.router.add_get('/api/v1/data', handler) app.router.add_get('/', handler) endpoint = az.create_endpoint('service_a', ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) app['session'] = session async def close_session(app): await app['session'].close() app.on_cleanup.append(close_session) az.setup(app, tracer) TEMPLATES_ROOT = pathlib.Path(__file__).parent / 'templates' aiohttp_jinja2.setup( app, loader=jinja2.FileSystemLoader(str(TEMPLATES_ROOT)) ) return app
async def test_middleware_with_valid_ip(tracer, version, address_in, address_out): if address_out is None: address_out = address_in app = web.Application() az.setup(app, tracer) # Fake transport transp = Mock() transp.get_extra_info.return_value = (address_in, '0') async def handler(request): return web.Response(body=b'data') req = make_mocked_request('GET', '/', headers={'token': 'x'}, transport=transp, app=app) middleware = middleware_maker() with patch('aiozipkin.span.Span.remote_endpoint') as mocked_remote_ep: await middleware(req, handler) assert mocked_remote_ep.call_count == 1 args, kwargs = mocked_remote_ep.call_args assert kwargs[version] == address_out
def make_app(): app = web.Application() app.router.add_get('/api/v1/data', handler) zipkin_address = 'http://127.0.0.1:9411' endpoint = az.create_endpoint('service_d') tracer = az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(): app = web.Application() app.router.add_get('/api/v1/data', handler) zipkin_address = 'http://127.0.0.1:9411/api/v2/spans' endpoint = az.create_endpoint('service_d', ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def test_middleware_cleanup_app(tracer): fut = asyncio.Future() fut.set_result(None) with patch.object(tracer, 'close', return_value=fut) as mocked_close: app = web.Application() az.setup(app, tracer) app.freeze() await app.cleanup() assert mocked_close.call_count == 1
async def make_app(): app = web.Application() app.router.add_get("/api/v1/data", handler) zipkin_address = "http://127.0.0.1:9411/api/v2/spans" endpoint = az.create_endpoint("service_c", ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
def make_app(host, port, loop): app = web.Application() endpoint = az.create_endpoint('aiohttp_server', ipv4=host, port=port) tracer = az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) app.router.add_get('/', handle) app.router.add_get('/api/v1/posts/{entity_id}', handle) return app
def setup_tracing(app: web.Application, app_name: str, host: str, port: str, config: Dict) -> bool: zipkin_address = f"{config['zipkin_endpoint']}/api/v2/spans" endpoint = az.create_endpoint(app_name, ipv4=host, port=port) loop = asyncio.get_event_loop() tracer = loop.run_until_complete( az.create(zipkin_address, endpoint, sample_rate=1.0)) az.setup(app, tracer) return True
async def make_app(host, port): app = web.Application() app.add_routes([web.get("/", get)]) endpoint = az.create_endpoint("backend", ipv4=host, port=port) zipkin_address = "http://zipkin:9411/api/v1/spans" tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
def make_app(host, port): app = web.Application() app.router.add_post('/consume', handler) aiojobs.aiohttp.setup(app) zipkin_address = 'http://127.0.0.1:9411' endpoint = az.create_endpoint('backend_broker', ipv4=host, port=port) tracer = az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(host, port): app = web.Application() app.router.add_post('/consume', handler) aiojobs.aiohttp.setup(app) zipkin_address = 'http://127.0.0.1:9411/api/v2/spans' endpoint = az.create_endpoint('backend_broker', ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(host, port): app = web.Application() app.router.add_post("/consume", handler) aiojobs.aiohttp.setup(app) zipkin_address = "http://127.0.0.1:9411/api/v2/spans" endpoint = az.create_endpoint("backend_broker", ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(host, port): app = web.Application() app.router.add_get('/', index) session = aiohttp.ClientSession() app['session'] = session zipkin_address = 'http://127.0.0.1:9411' endpoint = az.create_endpoint('frontend', ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(host, port): app = web.Application() app.router.add_get('/', index) session = aiohttp.ClientSession() app['session'] = session zipkin_address = 'http://127.0.0.1:9411/api/v2/spans' endpoint = az.create_endpoint('frontend', ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(host, port): app = web.Application() app.router.add_get("/", index) session = aiohttp.ClientSession() app["session"] = session zipkin_address = "http://127.0.0.1:9411/api/v2/spans" endpoint = az.create_endpoint("frontend", ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(host, port): app = web.Application() app.router.add_get('/', handle) # here we aquire reference to route, so later we can command # aiozipkin not to trace it skip_route = app.router.add_get('/status', not_traced_handle) endpoint = az.create_endpoint('aiohttp_server', ipv4=host, port=port) zipkin_address = 'http://127.0.0.1:9411' tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer, skip_routes=[skip_route]) return app
async def make_app(host: str, port: int) -> web.Application: app = web.Application() app.router.add_get("/", handle) # here we aquire reference to route, so later we can command # aiozipkin not to trace it skip_route = app.router.add_get("/status", not_traced_handle) endpoint = az.create_endpoint("aiohttp_server", ipv4=host, port=port) zipkin_address = "http://127.0.0.1:9411/api/v2/spans" tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer, skip_routes=[skip_route]) return app
async def test_middleware_with_handler_404(tracer: az.Tracer) -> None: app = web.Application() az.setup(app, tracer) async def handler(request: web.Request) -> web.StreamResponse: raise HTTPNotFound req = make_mocked_request("GET", "/", headers={"token": "x"}, app=app) middleware = middleware_maker() with pytest.raises(HTTPException): await middleware(req, handler)
async def test_middleware_with_handler_404(tracer): app = web.Application() az.setup(app, tracer) async def handler(request): raise HTTPNotFound req = make_mocked_request('GET', '/', headers={'token': 'x'}, app=app) middleware = middleware_maker() with pytest.raises(HTTPException): await middleware(req, handler)
def make_app(): app = web.Application() app.router.add_get('/api/v1/data', handler) app.router.add_get('/', index) session = aiohttp.ClientSession() app['session'] = session zipkin_address = 'http://127.0.0.1:9411' endpoint = az.create_endpoint('service_a') tracer = az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) return app
async def make_app(host, port): app = web.Application() app.router.add_get('/', handle) # here we aquire reference to route, so later we can command # aiozipkin not to trace it skip_route = app.router.add_get('/status', not_traced_handle) endpoint = az.create_endpoint('aiohttp_server', ipv4=host, port=port) zipkin_address = 'http://127.0.0.1:9411/api/v2/spans' tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer, skip_routes=[skip_route]) return app
async def client(aiohttp_client: Any, tracer: az.Tracer) -> Any: app = web.Application() app.router.add_get("/simple", handler) app.router.add_get("/error", error_handler) trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) app["session"] = session az.setup(app, tracer) c = await aiohttp_client(app) yield c await session.close()
async def client(test_client, tracer): app = web.Application() app.router.add_get('/simple', handler) app.router.add_get('/error', error_handler) trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) app['session'] = session az.setup(app, tracer) c = await test_client(app) await yield_(c) await session.close()
async def run_web_server(listen_addr, port, dists_dir, config, generator_manager, tracer): trailing_slash_redirect = normalize_path_middleware(append_slash=True) app = web.Application(middlewares=[trailing_slash_redirect]) app.config = config app.generator_manager = generator_manager setup_metrics(app) app.router.add_get("/", handle_index, name="index") app.router.add_static("/dists", dists_dir, show_index=True) app.router.add_post("/publish", handle_publish, name="publish") app.router.add_get("/last-publish", handle_last_publish, name="last-publish") app.router.add_get("/health", handle_health, name="health") aiozipkin.setup(app, tracer) runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, listen_addr, port) await site.start()
async def test_middleware_with_not_skip_route(tracer, fake_transport): async def handler(request): return web.Response(body=b'data') app = web.Application() skip_route = app.router.add_get('/', handler) az.setup(app, tracer) match_info = Mock() match_info.route = skip_route req = make_mocked_request('GET', '/', headers={'token': 'x'}, app=app) req._match_info = match_info middleware = middleware_maker(skip_routes=[skip_route]) await middleware(req, handler) assert len(fake_transport.records) == 0
async def run_web_server(app, listen_addr, port, tracer): setup_metrics(app) async def connect_artifact_manager(app): await app.artifact_manager.__aenter__() app.on_startup.append(connect_artifact_manager) aiozipkin.setup(app, tracer) runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, listen_addr, port) try: await site.start() while True: await asyncio.sleep(3600) finally: await runner.cleanup()
async def test_middleware_with_not_skip_route(tracer: az.Tracer, fake_transport: Any) -> None: async def handler(request: web.Request) -> web.StreamResponse: return web.Response(body=b"data") app = web.Application() skip_route = app.router.add_get("/", handler) az.setup(app, tracer) match_info = Mock() match_info.route = skip_route req = make_mocked_request("GET", "/", headers={"token": "x"}, app=app) req._match_info = match_info middleware = middleware_maker(skip_routes=[skip_route]) await middleware(req, handler) assert len(fake_transport.records) == 0
async def make_app(): app = web.Application() app.router.add_get('/api/v1/data', handler) zipkin_address = 'http://127.0.0.1:9411/api/v2/spans' endpoint = az.create_endpoint('service_b', ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) app['session'] = session async def close_session(app): await app['session'].close() app.on_cleanup.append(close_session) return app
async def make_app(host, port): app = web.Application() app.add_routes([web.get("/", get)]) endpoint = az.create_endpoint("frontend", ipv4=host, port=port) zipkin_address = "http://zipkin:9411/api/v1/spans" tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) trace_config = az.make_trace_config(tracer) session = ClientSession(trace_configs=[trace_config]) app["session"] = session async def close_session(app): await app["session"].close() app.on_cleanup.append(close_session) az.setup(app, tracer) return app
async def make_app(): app = web.Application() app.router.add_get('/api/v1/data', handler) zipkin_address = 'http://127.0.0.1:9411' endpoint = az.create_endpoint('service_b', ipv4=host, port=port) tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) az.setup(app, tracer) trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) app['session'] = session async def close_session(app): await app['session'].close() app.on_cleanup.append(close_session) return app