async def test_basic(zipkin_url: str, client: aiohttp.ClientSession, loop: asyncio.AbstractEventLoop) -> None: endpoint = az.create_endpoint("simple_service", ipv4="127.0.0.1", port=80) interval = 50 tracer = await az.create( zipkin_url, endpoint, sample_rate=1.0, send_interval=interval, ) with tracer.new_trace(sampled=True) as span: span.name("root_span") span.tag("span_type", "root") span.kind(az.CLIENT) span.annotate("SELECT * FROM") await asyncio.sleep(0.1) span.annotate("start end sql") # close forced sending data to server regardless of send interval await tracer.close() trace_id = span.context.trace_id url = URL(zipkin_url).with_path("/zipkin/api/v2/traces") data = await _retry_zipkin_client(url, client) assert any(s["traceId"] == trace_id for trace in data for s in trace), data
async def test_lost_spans(fake_zipkin: Any, loop: asyncio.AbstractEventLoop) -> None: endpoint = az.create_endpoint("simple_service", ipv4="127.0.0.1", port=80) tr = azt.Transport( fake_zipkin.url, send_interval=0.01, send_max_size=100, send_attempt_count=2, send_timeout=ClientTimeout(total=1), ) fake_zipkin.next_errors.append("disconnect") fake_zipkin.next_errors.append("disconnect") tracer = await az.create_custom(endpoint, tr) with tracer.new_trace(sampled=True) as span: span.name("root_span") span.kind(az.CLIENT) await asyncio.sleep(1) await tracer.close() data = fake_zipkin.get_received_data() assert len(data) == 0
async def test_basic(zipkin_url, client, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) interval = 50 tracer = az.create(zipkin_url, endpoint, sample_rate=1.0, send_inteval=interval, loop=loop) with tracer.new_trace(sampled=True) as span: span.name('root_span') span.tag('span_type', 'root') span.kind(az.CLIENT) span.annotate('SELECT * FROM') await asyncio.sleep(0.1) span.annotate('start end sql') # close forced sending data to server regardless of send interval await tracer.close() # give zipkin time to process maessage :(( await asyncio.sleep(1) trace_id = span.context.trace_id url = URL(zipkin_url).with_path('/zipkin/api/v1/traces') resp = await client.get(url) data = await resp.json() assert any(s['traceId'] == trace_id for trace in data for s in trace)
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
def tracer(transport): endpoint = az.create_endpoint("dummy-service") sampler = az.Sampler(sample_rate=1.0) tracer = az.Tracer(transport, sampler, endpoint) tok = _tracer_ctx_var.set(tracer) yield tracer _tracer_ctx_var.reset(tok)
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 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_leak_in_transport(zipkin_url: str, client: aiohttp.ClientSession, loop: asyncio.AbstractEventLoop) -> None: tracemalloc.start() endpoint = az.create_endpoint("simple_service") tracer = await az.create( zipkin_url, endpoint, sample_rate=1, send_interval=0.0001, ) await asyncio.sleep(5) gc.collect() snapshot1 = tracemalloc.take_snapshot() await asyncio.sleep(10) gc.collect() snapshot2 = tracemalloc.take_snapshot() top_stats = snapshot2.compare_to(snapshot1, "lineno") count = sum(s.count for s in top_stats) await tracer.close() assert count < 400 # in case of leak this number is around 901452
async def test_retry(fake_zipkin: Any, loop: asyncio.AbstractEventLoop) -> None: endpoint = az.create_endpoint("simple_service", ipv4="127.0.0.1", port=80) tr = azt.Transport( fake_zipkin.url, send_interval=0.01, send_max_size=100, send_attempt_count=3, send_timeout=ClientTimeout(total=1), ) fake_zipkin.next_errors.append("disconnect") fake_zipkin.next_errors.append("timeout") waiter = fake_zipkin.wait_data(1) tracer = await az.create_custom(endpoint, tr) with tracer.new_trace(sampled=True) as span: span.name("root_span") span.kind(az.CLIENT) await waiter await tracer.close() data = fake_zipkin.get_received_data() trace_id = span.context.trace_id assert any(s["traceId"] == trace_id for trace in data for s in trace), data
async def test_basic(jaeger_url, jaeger_api_url, client, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) interval = 50 tracer = await az.create(jaeger_url, endpoint, sample_rate=1.0, send_inteval=interval, loop=loop) with tracer.new_trace(sampled=True) as span: span.name('jaeger_span') span.tag('span_type', 'root') span.kind(az.CLIENT) span.annotate('SELECT * FROM') await asyncio.sleep(0.1) span.annotate('start end sql') # close forced sending data to server regardless of send interval await tracer.close() trace_id = span.context.trace_id[-16:] url = URL(jaeger_api_url) / 'api' / 'traces' / trace_id resp = await client.get(url, headers={'Content-Type': 'application/json'}) assert resp.status == 200 data = await resp.json() assert data['data'][0]['traceID'] in trace_id
async def test_basic( jaeger_url: str, jaeger_api_url: str, client: aiohttp.ClientSession, loop: asyncio.AbstractEventLoop, ) -> None: endpoint = az.create_endpoint("simple_service", ipv4="127.0.0.1", port=80) interval = 50 tracer = await az.create( jaeger_url, endpoint, sample_rate=1.0, send_interval=interval, ) with tracer.new_trace(sampled=True) as span: span.name("jaeger_span") span.tag("span_type", "root") span.kind(az.CLIENT) span.annotate("SELECT * FROM") await asyncio.sleep(0.1) span.annotate("start end sql") # close forced sending data to server regardless of send interval await tracer.close() trace_id = span.context.trace_id[-16:] url = URL(jaeger_api_url) / "api" / "traces" / trace_id resp = await client.get(url, headers={"Content-Type": "application/json"}) assert resp.status == 200 data = await resp.json() assert data["data"][0]["traceID"] in trace_id
async def test_batches(fake_zipkin: Any, loop: asyncio.AbstractEventLoop) -> None: endpoint = az.create_endpoint("simple_service", ipv4="127.0.0.1", port=80) tr = azt.Transport( fake_zipkin.url, send_interval=0.01, send_max_size=2, send_timeout=ClientTimeout(total=1), ) tracer = await az.create_custom(endpoint, tr) with tracer.new_trace(sampled=True) as span: span.name("root_span") span.kind(az.CLIENT) with span.new_child("child_1", az.CLIENT): pass with span.new_child("child_2", az.CLIENT): pass # close forced sending data to server regardless of send interval await tracer.close() data = fake_zipkin.get_received_data() trace_id = span.context.trace_id assert len(data[0]) == 2 assert len(data[1]) == 1 assert data[0][0]["name"] == "child_1" assert data[0][1]["name"] == "child_2" assert data[1][0]["name"] == "root_span" assert any(s["traceId"] == trace_id for trace in data for s in trace), data
async def test_batches(fake_zipkin, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) tr = azt.Transport(fake_zipkin.url, send_interval=0.01, send_max_size=2, send_timeout=ClientTimeout(total=1)) tracer = await az.create_custom(endpoint, tr) with tracer.new_trace(sampled=True) as span: span.name('root_span') span.kind(az.CLIENT) with span.new_child('child_1', az.CLIENT): pass with span.new_child('child_2', az.CLIENT): pass # close forced sending data to server regardless of send interval await tracer.close() data = fake_zipkin.get_received_data() trace_id = span.context.trace_id assert len(data[0]) == 2 assert len(data[1]) == 1 assert data[0][0]['name'] == 'child_1' assert data[0][1]['name'] == 'child_2' assert data[1][0]['name'] == 'root_span' assert any(s['traceId'] == trace_id for trace in data for s in trace), data
async def test_exception_in_span(zipkin_url, client, loop): endpoint = az.create_endpoint('error_service', ipv4='127.0.0.1', port=80) interval = 50 tracer = az.create(zipkin_url, endpoint, send_inteval=interval, loop=loop) def func(span): with span: span.name('root_span') raise RuntimeError('foo') span = tracer.new_trace(sampled=True) with pytest.raises(RuntimeError): func(span) await tracer.close() # give zipkin time to process maessage :(( await asyncio.sleep(1) url = URL(zipkin_url).with_path('/zipkin/api/v1/traces') resp = await client.get(url) data = await resp.json() expected = { 'endpoint': { 'ipv4': '127.0.0.1', 'port': 80, 'serviceName': 'error_service' }, 'key': 'error', 'value': 'foo' } assert any(expected in s['binaryAnnotations'] for trace in data for s in trace)
async def test_send_full_batch(fake_zipkin, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) tr = azt.Transport(fake_zipkin.url, send_interval=60, send_max_size=2, send_timeout=ClientTimeout(total=1)) tracer = await az.create_custom(endpoint, tr) waiter = fake_zipkin.wait_data(1) with tracer.new_trace(sampled=True) as span: span.name('root_span') span.kind(az.CLIENT) await asyncio.sleep(1, loop=loop) data = fake_zipkin.get_received_data() assert len(data) == 0 with tracer.new_trace(sampled=True) as span: span.name('root_span') span.kind(az.CLIENT) # batch is full here await waiter data = fake_zipkin.get_received_data() assert len(data) == 1 # close forced sending data to server regardless of send interval await tracer.close()
async def test_retry(fake_zipkin, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) tr = azt.Transport(fake_zipkin.url, send_interval=0.01, send_max_size=100, send_attempt_count=3, send_timeout=ClientTimeout(total=1)) fake_zipkin.next_errors.append('disconnect') fake_zipkin.next_errors.append('timeout') waiter = fake_zipkin.wait_data(1) tracer = await az.create_custom(endpoint, tr) with tracer.new_trace(sampled=True) as span: span.name('root_span') span.kind(az.CLIENT) await waiter await tracer.close() data = fake_zipkin.get_received_data() trace_id = span.context.trace_id assert any(s['traceId'] == trace_id for trace in data for s in trace), data
async def init_tracer(self) -> az.Tracer: endpoint = az.create_endpoint(self.config.service_name) tracer = await az.create( f"http://{self.config.host}:{self.config.port}/api/v2/spans", endpoint, sample_rate=self.config.sample_rate, ) return tracer
async def start(self, logger: 'ipapp.logger.Logger') -> None: endpoint = az.create_endpoint(self.cfg.name) sampler = az.Sampler(sample_rate=self.cfg.sample_rate) transport = azt.Transport( self.cfg.addr, send_interval=self.cfg.send_interval, ) self.tracer = az.Tracer(transport, sampler, endpoint)
async def configure_tracing(self, span_name, span_kind=SERVER, tags=None, annotations=None ) -> Optional[ConsoleMeTracerObject]: if not config.get("tracing.enabled", False): return if not random() * 100 <= config.get("tracing.sample_rate", 0.1): # nosec return if not tags: tags = [] if not annotations: annotations = [] zipkin_address = config.get( "tracing.zipkin_address", "http://127.0.0.1:9411/api/v2/spans").format( region=config.region, environment=config.get("environment")) endpoint = az.create_endpoint( config.get("tracing.application_name", "consoleme")) # The tracer's sample rate is 100% because we are pre-sampling our requests self.tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) self.primary_span = self.tracer.new_trace(sampled=True) self.headers = self.primary_span.context.make_headers() self.log_data = { "function": f"{__name__}.{self.__class__.__name__}.{sys._getframe().f_code.co_name}", "message": "Starting trace", "trace_id": self.primary_span.context.trace_id, "zipkin_address": zipkin_address, "tags": tags, "hostname": config.hostname, } log.debug(self.log_data) self.primary_span.kind(span_kind) self.primary_span.start() self.primary_span.name(span_name) self.primary_span.tag("HOSTNAME", config.hostname) for k, v in tags.items(): self.primary_span.tag(k, v) for annotation in annotations: self.primary_span.annotate(annotation) # Configure sys/threading.settrace to use our trace_calls function for tracing # Note: This is expensive, and should definitely not run for every request sys.settrace(self.trace_calls) threading.settrace(self.trace_calls) return ConsoleMeTracerObject(primary_span=self.primary_span, tracer=self.tracer, headers=self.headers)
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
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
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 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 setup_zipkin(app, loop, context): endpoint = az.create_endpoint(sanic_zipkin.service, ipv4=sanic_zipkin.host, port=sanic_zipkin.port) context.tracer = await az.create(sanic_zipkin.zipkin_address, endpoint, sample_rate=1.0) trace_config = az.make_trace_config(context.tracer) context.aio_session = aiohttp.ClientSession(trace_configs=[trace_config]) context.span = [] context.zipkin_headers = []
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
async def ctx(app: web.Application): endpoint = az.create_endpoint(app["app_name"], ipv4=host, port=port) tracer = await az.create(app["config"].zipkin.get_address(), local_endpoint=endpoint, sample_rate=1.0) app["tracer"] = tracer yield await tracer.close()
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
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
def setup_tracing( app: web.Application, *, service_name: str, host: str, port: int, jaeger_base_url: Union[URL, str], skip_routes: Optional[Iterable[AbstractRoute]] = None, ) -> bool: """ Sets up this service for a distributed tracing system using zipkin (https://zipkin.io/) and Jaeger (https://www.jaegertracing.io/) """ zipkin_address = URL(f"{jaeger_base_url}") / "api/v2/spans" log.debug( "Setting up tracing for %s at %s:%d -> %s", service_name, host, port, zipkin_address, ) endpoint = az.create_endpoint(service_name, ipv4=host, port=port) # TODO: move away from aiozipkin to OpenTelemetrySDK # https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asgi/asgi.html # see issue [#2715](https://github.com/ITISFoundation/osparc-simcore/issues/2715) # creates / closes tracer async def _tracer_cleanup_context(app: web.Application): app[APP_AIOZIPKIN_KEY] = await az.create( f"{zipkin_address}", endpoint, sample_rate=1.0 ) yield if APP_AIOZIPKIN_KEY in app: await app[APP_AIOZIPKIN_KEY].close() app.cleanup_ctx.append(_tracer_cleanup_context) # adds middleware to tag spans (when used, tracer should be ready) m = middleware_maker( skip_routes=skip_routes, tracer_key=APP_AIOZIPKIN_KEY, request_key=REQUEST_AIOZIPKIN_KEY, ) app.middlewares.append(m) # # WARNING: adds a middleware that should be the outermost since # # it expects stream responses while we allow data returns from a handler # az.setup(app, tracer, skip_routes=skip_routes) return True
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('/', 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('/', 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_basic_context_manager(zipkin_url, client, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) interval = 50 async with az.create(zipkin_url, endpoint, sample_rate=1.0, send_interval=interval) as tracer: with tracer.new_trace(sampled=True) as span: span.name('root_span') await asyncio.sleep(0.1) trace_id = span.context.trace_id url = URL(zipkin_url).with_path('/zipkin/api/v2/traces') data = await _retry_zipkin_client(url, client) assert any(s['traceId'] == trace_id for trace in data for s in trace), data
async def test_zipkin_error(client, loop, caplog): endpoint = az.create_endpoint('error_service', ipv4='127.0.0.1', port=80) interval = 50 zipkin_url = 'https://httpbin.org/status/404' async with az.create(zipkin_url, endpoint, sample_rate=1.0, send_interval=interval, loop=loop) as tracer: with tracer.new_trace(sampled=True) as span: span.kind(az.CLIENT) await asyncio.sleep(0.0) assert len(caplog.records) == 1 msg = 'zipkin responded with code: ' assert msg in str(caplog.records[0].exc_info) t = ('aiozipkin', logging.ERROR, 'Can not send spans to zipkin') assert caplog.record_tuples == [t]
async def run(): # setup zipkin client zipkin_address = 'http://127.0.0.1:9411/api/v2/spans' # address and name of current machine for better trace information endpoint = az.create_endpoint('minimal_example', ipv4='127.0.0.1') # creates tracer object that tracer all calls if you want sample # only 50% just set sample_rate=0.5 async with az.create(zipkin_address, endpoint, sample_rate=1.0) as tracer: # create and setup new trace with tracer.new_trace() as span: # here we just add name to the span for better search in UI span.name('root::span') # imitate long SQL query await asyncio.sleep(0.1) print('Done, check zipkin UI')
async def test_exception_in_span(zipkin_url, client, loop): endpoint = az.create_endpoint('error_service', ipv4='127.0.0.1', port=80) interval = 50 async with az.create(zipkin_url, endpoint, send_interval=interval, loop=loop) as tracer: def func(span): with span: span.name('root_span') raise RuntimeError('foo') span = tracer.new_trace(sampled=True) with pytest.raises(RuntimeError): func(span) url = URL(zipkin_url).with_path('/zipkin/api/v2/traces') data = await _retry_zipkin_client(url, client) assert any({'error': 'foo'} == s.get('tags', {}) for trace in data for s in trace)
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 test_leak_in_transport(zipkin_url, client, loop): tracemalloc.start() endpoint = az.create_endpoint('simple_service') tracer = await az.create(zipkin_url, endpoint, sample_rate=1, send_interval=0.0001, loop=loop) await asyncio.sleep(5) gc.collect() snapshot1 = tracemalloc.take_snapshot() await asyncio.sleep(10) gc.collect() snapshot2 = tracemalloc.take_snapshot() top_stats = snapshot2.compare_to(snapshot1, 'lineno') count = sum(s.count for s in top_stats) await tracer.close() assert count < 400 # in case of leak this number is around 901452
async def test_basic(zipkin_url, client, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) interval = 50 tracer = await az.create(zipkin_url, endpoint, sample_rate=1.0, send_interval=interval, loop=loop) with tracer.new_trace(sampled=True) as span: span.name('root_span') span.tag('span_type', 'root') span.kind(az.CLIENT) span.annotate('SELECT * FROM') await asyncio.sleep(0.1) span.annotate('start end sql') # close forced sending data to server regardless of send interval await tracer.close() trace_id = span.context.trace_id url = URL(zipkin_url).with_path('/zipkin/api/v2/traces') data = await _retry_zipkin_client(url, client) assert any(s['traceId'] == trace_id for trace in data for s in trace), data
async def run(): # setup zipkin client zipkin_address = 'http://127.0.0.1:9411/api/v2/spans' endpoint = az.create_endpoint( 'simple_service', ipv4='127.0.0.1', port=8080 ) # creates tracer object that traces all calls, if you want sample # only 50% just set sample_rate=0.5 tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0) # create and setup new trace with tracer.new_trace(sampled=True) as span: span.name('root_span') span.tag('span_type', 'root') span.kind(az.CLIENT) span.annotate('SELECT * FROM') # imitate long SQL query await asyncio.sleep(0.1) span.annotate('start end sql') # create child span with tracer.new_child(span.context) as nested_span: nested_span.name('nested_span_1') nested_span.kind(az.CLIENT) nested_span.tag('span_type', 'inner1') nested_span.remote_endpoint('remote_service_1') await asyncio.sleep(0.01) # create other child span with tracer.new_child(span.context) as nested_span: nested_span.name('nested_span_2') nested_span.kind(az.CLIENT) nested_span.remote_endpoint('remote_service_2') nested_span.tag('span_type', 'inner2') await asyncio.sleep(0.01) await tracer.close() print('-' * 100) print('Check zipkin UI for produced traces: http://localhost:9411/zipkin')
async def test_basic(jaeger_url, jaeger_api_url, client, loop): endpoint = az.create_endpoint('simple_service', ipv4='127.0.0.1', port=80) interval = 50 tracer = await az.create(jaeger_url, endpoint, sample_rate=1.0, send_interval=interval, loop=loop) with tracer.new_trace(sampled=True) as span: span.name('jaeger_span') span.tag('span_type', 'root') span.kind(az.CLIENT) span.annotate('SELECT * FROM') await asyncio.sleep(0.1) span.annotate('start end sql') # close forced sending data to server regardless of send interval await tracer.close() trace_id = span.context.trace_id[-16:] url = URL(jaeger_api_url) / 'api' / 'traces' / trace_id resp = await client.get(url, headers={'Content-Type': 'application/json'}) assert resp.status == 200 data = await resp.json() assert data['data'][0]['traceID'] in trace_id