|
8 | 8 | import pytest |
9 | 9 |
|
10 | 10 | from ddtrace.contrib.asgi import TraceMiddleware |
| 11 | +from ddtrace.contrib.asgi import span_from_scope |
11 | 12 | from ddtrace.propagation import http as http_propagation |
12 | 13 | from tests.utils import DummyTracer |
13 | 14 | from tests.utils import override_http_config |
@@ -362,3 +363,55 @@ async def test_bad_headers(scope, tracer, test_spans): |
362 | 363 | "type": "http.response.body", |
363 | 364 | "body": b"*", |
364 | 365 | } |
| 366 | + |
| 367 | + |
| 368 | +@pytest.mark.asyncio |
| 369 | +async def test_get_asgi_span(tracer, test_spans): |
| 370 | + async def test_app(scope, receive, send): |
| 371 | + message = await receive() |
| 372 | + if message.get("type") == "http.request": |
| 373 | + asgi_span = span_from_scope(scope) |
| 374 | + assert asgi_span is not None |
| 375 | + assert asgi_span.name == "asgi.request" |
| 376 | + await send({"type": "http.response.start", "status": 200, "headers": [[b"Content-Type", b"text/plain"]]}) |
| 377 | + await send({"type": "http.response.body", "body": b""}) |
| 378 | + |
| 379 | + app = TraceMiddleware(test_app, tracer=tracer) |
| 380 | + async with httpx.AsyncClient(app=app) as client: |
| 381 | + response = await client.get("http://testserver/") |
| 382 | + assert response.status_code == 200 |
| 383 | + |
| 384 | + with override_http_config("asgi", dict(trace_query_string=True)): |
| 385 | + app = TraceMiddleware(test_app, tracer=tracer) |
| 386 | + async with httpx.AsyncClient(app=app) as client: |
| 387 | + response = await client.get("http://testserver/") |
| 388 | + assert response.status_code == 200 |
| 389 | + |
| 390 | + async def test_app(scope, receive, send): |
| 391 | + message = await receive() |
| 392 | + if message.get("type") == "http.request": |
| 393 | + root = tracer.current_root_span() |
| 394 | + assert root.name == "root" |
| 395 | + asgi_span = span_from_scope(scope) |
| 396 | + assert asgi_span is not None |
| 397 | + assert asgi_span.name == "asgi.request" |
| 398 | + await send({"type": "http.response.start", "status": 200, "headers": [[b"Content-Type", b"text/plain"]]}) |
| 399 | + await send({"type": "http.response.body", "body": b""}) |
| 400 | + |
| 401 | + app = TraceMiddleware(test_app, tracer=tracer) |
| 402 | + async with httpx.AsyncClient(app=app) as client: |
| 403 | + with tracer.trace("root"): |
| 404 | + response = await client.get("http://testserver/") |
| 405 | + assert response.status_code == 200 |
| 406 | + |
| 407 | + async def test_app_no_middleware(scope, receive, send): |
| 408 | + message = await receive() |
| 409 | + if message.get("type") == "http.request": |
| 410 | + asgi_span = span_from_scope(scope) |
| 411 | + assert asgi_span is None |
| 412 | + await send({"type": "http.response.start", "status": 200, "headers": [[b"Content-Type", b"text/plain"]]}) |
| 413 | + await send({"type": "http.response.body", "body": b""}) |
| 414 | + |
| 415 | + async with httpx.AsyncClient(app=test_app_no_middleware) as client: |
| 416 | + response = await client.get("http://testserver/") |
| 417 | + assert response.status_code == 200 |
0 commit comments