async def test_client_signals(tracer, fake_transport): trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) with tracer.new_trace() as span: span.name('client:signals') url = 'https://httpbin.org/get' # do not propagate headers ctx = {'span_context': span.context, 'propagate_headers': False} resp = await session.get(url, trace_request_ctx=ctx) await resp.text() assert resp.status == 200 assert az.make_context(resp.request_info.headers) is None # by default headers added ctx = {'span_context': span.context} resp = await session.get(url, trace_request_ctx=ctx) await resp.text() assert resp.status == 200 context = az.make_context(resp.request_info.headers) assert context.trace_id == span.context.trace_id await session.close() assert len(fake_transport.records) == 3 record1 = fake_transport.records[0].asdict() record2 = fake_transport.records[1].asdict() record3 = fake_transport.records[2].asdict() assert record2['parentId'] == record3['id'] assert record1['parentId'] == record3['id'] assert record3['name'] == 'client:signals'
async def test_client_signals(tracer, fake_transport): trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) with tracer.new_trace() as span: span.name('client:signals') url = 'https://httpbin.org/get' # do not propagate headers ctx = {'span_context': span.context, 'propagate_headers': False} resp = await session.get(url, trace_request_ctx=ctx) data = await resp.read() assert len(data) > 0 assert az.make_context(resp.request_info.headers) is None # by default headers added ctx = {'span_context': span.context} resp = await session.get(url, trace_request_ctx=ctx) await resp.text() assert len(data) > 0 context = az.make_context(resp.request_info.headers) assert context.trace_id == span.context.trace_id await session.close() assert len(fake_transport.records) == 3 record1 = fake_transport.records[0].asdict() record2 = fake_transport.records[1].asdict() record3 = fake_transport.records[2].asdict() assert record2['parentId'] == record3['id'] assert record1['parentId'] == record3['id'] assert record3['name'] == 'client:signals'
async def test_client_signals(tracer: az.Tracer, fake_transport: Any) -> None: trace_config = az.make_trace_config(tracer) session = aiohttp.ClientSession(trace_configs=[trace_config]) with tracer.new_trace() as span: span.name("client:signals") url = "https://httpbin.org/get" # do not propagate headers ctx = {"span_context": span.context, "propagate_headers": False} resp = await session.get(url, trace_request_ctx=ctx) data = await resp.read() assert len(data) > 0 assert az.make_context(resp.request_info.headers) is None ctx_ns = SimpleNamespace(span_context=span.context, propagate_headers=False) resp = await session.get(url, trace_request_ctx=ctx_ns) data = await resp.read() assert len(data) > 0 assert az.make_context(resp.request_info.headers) is None # by default headers added ctx = {"span_context": span.context} resp = await session.get(url, trace_request_ctx=ctx) await resp.text() assert len(data) > 0 context = az.make_context(resp.request_info.headers) assert context is not None assert context.trace_id == span.context.trace_id await session.close() assert len(fake_transport.records) == 4 record1 = fake_transport.records[0].asdict() record2 = fake_transport.records[1].asdict() record3 = fake_transport.records[2].asdict() record4 = fake_transport.records[3].asdict() assert record3["parentId"] == record4["id"] assert record2["parentId"] == record4["id"] assert record1["parentId"] == record4["id"] assert record4["name"] == "client:signals"
async def consume_message(message, tracer): await asyncio.sleep(0.1) headers = message.get("headers", None) context = az.make_context(headers) with tracer.new_child(context) as span_consumer: span_consumer.name("consumer event") span_consumer.remote_endpoint("broker", ipv4="127.0.0.1", port=9011) span_consumer.kind(az.CONSUMER) with tracer.new_child(span_consumer.context) as span_worker: span_worker.name("process event") await asyncio.sleep(0.1)
async def consume_message(message, tracer): await asyncio.sleep(0.1) headers = message.get('headers', None) context = az.make_context(headers) with tracer.new_child(context) as span_consumer: span_consumer.name('consumer event') span_consumer.remote_endpoint('broker', ipv4='127.0.0.1', port=9011) span_consumer.kind(az.CONSUMER) with tracer.new_child(span_consumer.context) as span_worker: span_worker.name('process event') await asyncio.sleep(0.1)
async def middleware_handler(request): if self.app.tracer: if request.path not in self.skip_trace: context = az.make_context(request.headers) if context is None: sampled = azah.parse_sampled(request.headers) debug = azah.parse_debug(request.headers) span = self.app.tracer.new_trace(sampled=sampled, debug=debug) else: span = self.app.tracer.join_span(context) request[SPAN_KEY] = span with span: span_name = '{0} {1}'.format(request.method.upper(), request.path) span.name(span_name) span.kind(azah.SERVER) span.tag(HTTP_PATH, request.path) span.tag(HTTP_METHOD, request.method.upper()) annotate_bytes(span, await request.read()) resp, trace_str = await self._handle( span, request, handler) if isinstance(resp, web.Response): span.tag(azah.HTTP_STATUS_CODE, resp.status) annotate_bytes(span, resp.body) if trace_str is not None: span.annotate(trace_str) return resp else: resp, trace_str = await self._handle( None, request, handler) return resp else: resp, trace_str = await self._handle(None, request, handler) return resp
def request_span(request, context): # [print(i, eval(f'request.{i}')) for i in dir(request) if hasattr(request,i) and not i.startswith('__')] context.log(DEBUG, f'REQUEST json: {request.json}, args: {request.args}') headers = request.parsed_json.get('zipkin_headers', None) if request.json else \ request.args.get('zipkin_headers', None) if headers: # calling from another service headers = json.loads(headers) span_context = az.make_context(headers) with context.tracer.new_child(span_context) as span: span.name(f'{request.method} {request.path}') request_headers = { 'http.path':request.path, 'http.method':request.method, 'http.path':request.path, 'http.route':request.url, 'peer.ip':request.remote_addr or request.ip, 'peer.port':request.port, } [span.tag(k, v) for k,v in request_headers.items()] span.kind(az.SERVER) else: # calling from end-user with context.tracer.new_trace() as span: span.name(f'{request.method} {request.path}') request_headers = { 'http.path':request.path, 'http.method':request.method, 'http.path':request.path, 'http.route':request.url, 'peer.ip':request.remote_addr or request.ip, 'peer.port':request.port, } [span.tag(k, v) for k,v in request_headers.items()] span.kind(az.CLIENT) return span
def make_context(self, headers: dict) -> dict: return make_context(headers)