def test_credential_removal(self): self.scope["server"] = ("username:[email protected]", 80) self.scope["path"] = "/status/200" attrs = otel_asgi.collect_request_attributes(self.scope) self.assertEqual( attrs[SpanAttributes.HTTP_URL], "http://httpbin.org/status/200" )
async def __call__(self, scope, receive, send): if scope["type"] not in ("http", "websocket"): return await self.app(scope, receive, send) if await self.is_filtered(scope): return await self.app(scope, receive, send) token = context.attach(propagators.extract(carrier_getter, scope)) span_name, additional_attributes = self.span_details_callback(scope) try: with self.tracer.start_as_current_span( span_name + " asgi", kind=trace.SpanKind.SERVER, ) as span: if span.is_recording(): attributes = collect_request_attributes(scope) attributes.update(additional_attributes) for key, value in attributes.items(): span.set_attribute(key, value) @wraps(receive) async def wrapped_receive(): with self.tracer.start_as_current_span( span_name + " asgi." + scope["type"] + ".receive") as receive_span: message = await receive() if receive_span.is_recording(): if message["type"] == "websocket.receive": set_status_code(receive_span, 200) receive_span.set_attribute("type", message["type"]) return message @wraps(send) async def wrapped_send(message): with self.tracer.start_as_current_span( span_name + " asgi." + scope["type"] + ".send") as send_span: if send_span.is_recording(): if message["type"] == "http.response.start": status_code = message["status"] set_status_code(send_span, status_code) set_status_code(span, status_code) elif message["type"] == "websocket.send": set_status_code(send_span, 200) set_status_code(span, 200) send_span.set_attribute("type", message["type"]) await send(message) await self.app(scope, wrapped_receive, wrapped_send) finally: context.detach(token)
def test_request_attributes(self): self.scope["query_string"] = b"foo=bar" attrs = otel_asgi.collect_request_attributes(self.scope) self.assertDictEqual( attrs, { "component": "http", "http.method": "GET", "http.host": "127.0.0.1", "http.target": "/", "http.url": "http://127.0.0.1/?foo=bar", "host.port": 80, "http.scheme": "http", "http.flavor": "1.0", "net.peer.ip": "127.0.0.1", "net.peer.port": 32767, }, )
def test_request_attributes(self): self.scope["query_string"] = b"foo=bar" headers = [] headers.append(("host".encode("utf8"), "test".encode("utf8"))) self.scope["headers"] = headers attrs = otel_asgi.collect_request_attributes(self.scope) self.assertDictEqual( attrs, { SpanAttributes.HTTP_METHOD: "GET", SpanAttributes.HTTP_HOST: "127.0.0.1", SpanAttributes.HTTP_TARGET: "/", SpanAttributes.HTTP_URL: "http://127.0.0.1/?foo=bar", SpanAttributes.NET_HOST_PORT: 80, SpanAttributes.HTTP_SCHEME: "http", SpanAttributes.HTTP_SERVER_NAME: "test", SpanAttributes.HTTP_FLAVOR: "1.0", SpanAttributes.NET_PEER_IP: "127.0.0.1", SpanAttributes.NET_PEER_PORT: 32767, }, )
def test_request_attributes(self): self.scope["query_string"] = b"foo=bar" headers = [] headers.append(("host".encode("utf8"), "test".encode("utf8"))) self.scope["headers"] = headers attrs = otel_asgi.collect_request_attributes(self.scope) self.assertDictEqual( attrs, { "component": "http", "http.method": "GET", "http.host": "127.0.0.1", "http.target": "/", "http.url": "http://127.0.0.1/?foo=bar", "host.port": 80, "http.scheme": "http", "http.server_name": "test", "http.flavor": "1.0", "net.peer.ip": "127.0.0.1", "net.peer.port": 32767, }, )
async def replaced_ot_middleware_call(self, scope, receive, send): # pylint:disable=R0914 """The ASGI application Args: scope: A ASGI environment. receive: An awaitable callable yielding dictionaries send: An awaitable callable taking a single dictionary as argument. """ if scope["type"] not in ("http", "websocket"): return await self.app(scope, receive, send) _, _, url = get_host_port_url_tuple(scope) if self.excluded_urls and self.excluded_urls.url_disabled(url): return await self.app(scope, receive, send) token = context.attach(extract(scope, getter=asgi_getter)) span_name, additional_attributes = self.default_span_details(scope) request = Request(scope, receive=receive) body = await request.body() try: with self.tracer.start_as_current_span( span_name, kind=trace.SpanKind.SERVER, ) as span: if span.is_recording(): attributes = collect_request_attributes(scope) attributes.update(additional_attributes) for key, value in attributes.items(): span.set_attribute(key, value) should_forward_to_handler = True if callable(self.server_request_hook): should_forward_to_handler = self.server_request_hook( span, scope, body) @wraps(receive) async def wrapped_receive(): with self.tracer.start_as_current_span(" ".join( (span_name, scope["type"], "receive"))) as receive_span: if callable(self.client_request_hook): self.client_request_hook(receive_span, scope) message = await receive() if receive_span.is_recording(): if message["type"] == "websocket.receive": set_status_code(receive_span, 200) receive_span.set_attribute("type", message["type"]) return message @wraps(send) async def wrapped_send(message): send_span = span if callable(self.client_response_hook): self.client_response_hook(send_span, message) if send_span.is_recording(): if message["type"] == "http.response.start": status_code = message["status"] set_status_code(span, status_code) elif message["type"] == "websocket.send": set_status_code(span, 200) elif message["type"] == 'http.response.body': pass await send(message) if should_forward_to_handler: await self.app(scope, wrapped_receive, wrapped_send) else: set_status_code(span, 403) raise HypertraceException(status_code=403) finally: context.detach(token)
def test_query_string_percent_str(self): self.scope["query_string"] = "foo%3Dbar" attrs = otel_asgi.collect_request_attributes(self.scope) self.assertEqual(attrs[SpanAttributes.HTTP_URL], "http://127.0.0.1/?foo=bar")
def test_query_string_percent_bytes(self): self.scope["query_string"] = b"foo%3Dbar" attrs = otel_asgi.collect_request_attributes(self.scope) self.assertEqual(attrs["http.url"], "http://127.0.0.1/?foo=bar")