Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ddtrace/contrib/internal/aiohttp/patch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Optional

import aiohttp
import wrapt
Expand Down Expand Up @@ -83,7 +84,10 @@ async def _create_connection(self, req, *args, **kwargs):
@with_traced_module
async def _traced_clientsession_request(aiohttp, pin, func, instance, args, kwargs):
method: str = get_argument_value(args, kwargs, 0, "method")
url: URL = URL(get_argument_value(args, kwargs, 1, "url"))
raw_url: URL = URL(str(get_argument_value(args, kwargs, 1, "url")))
# Resolve against base_url if present, mirroring aiohttp's internal behaviour.
base_url: Optional[URL] = getattr(instance, "_base_url", None)
url: URL = base_url.join(raw_url) if base_url is not None else raw_url
params = kwargs.get("params")
headers = kwargs.get("headers") or {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
features:
- |
aiohttp: Fixed an issue where spans captured an incomplete URL (e.g. ``/status/200``)
when ``aiohttp.ClientSession`` was initialized with a ``base_url``. The span now
records the fully-resolved URL (e.g. ``http://host:port/status/200``), matching
aiohttp's internal behaviour.
16 changes: 16 additions & 0 deletions tests/contrib/aiohttp/test_aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,19 @@ async def test_trace_multiple(snapshot_context):
assert resp.status == 200
async with session.get(URL_200) as resp:
assert resp.status == 200


@pytest.mark.skipif(
tuple(int(x) for x in aiohttp.__version__.split(".")[:2]) < (3, 8),
reason="base_url parameter added in aiohttp 3.8.0",
)
@pytest.mark.asyncio
async def test_base_url(snapshot_context):
"""
When ClientSession is initialized with base_url
The full URL (base + path) is captured in the span
"""
with snapshot_context():
async with aiohttp.ClientSession(base_url="http://{}".format(SOCKET)) as session:
async with session.get("/status/200") as resp:
assert resp.status == 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[[
{
"name": "aiohttp.request",
"service": "tests.contrib.aiohttp",
"resource": "aiohttp.request",
Comment thread
quinna-h marked this conversation as resolved.
"trace_id": 0,
"span_id": 1,
"parent_id": 0,
"type": "http",
"error": 0,
"meta": {
"_dd.p.dm": "-0",
"_dd.p.tid": "654a694400000000",
"component": "aiohttp_client",
"http.method": "GET",
"http.status_code": "200",
"http.status_msg": "OK",
"http.url": "http://localhost:8001/status/200",
"language": "python",
"out.host": "localhost",
"runtime-id": "de4fa08308ec4bc3a437ed78b9f99c47",
"span.kind": "client"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 2189
},
"duration": 3943217,
"start": 1691160518131287373
},
{
"name": "TCPConnector.connect",
"service": "tests.contrib.aiohttp",
"resource": "TCPConnector.connect",
"trace_id": 0,
"span_id": 2,
"parent_id": 1,
"type": "",
"error": 0,
"meta": {
"_dd.p.tid": "654a694400000000",
"component": "aiohttp"
},
"duration": 2305283,
"start": 1691160518131963432
}]]
Loading