Skip to content

Commit

Permalink
fix starlette asgi behavior to only include root_path if not duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
zarirhamza committed Mar 2, 2024
1 parent 9e6e6af commit be07ae1
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 162 deletions.
14 changes: 13 additions & 1 deletion ddtrace/contrib/asgi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ def span_from_scope(scope: Mapping[str, Any]) -> Optional[Span]:
return scope.get("datadog", {}).get("request_spans", [None])[0]


def _extract_full_path(raw_path, root_path, path):
if root_path and raw_path and raw_path.startswith(root_path.encode()):
raw = raw_path.split(b"?")[0] if b"?" in raw_path else raw_path
check_path = root_path + path

if raw.decode() != check_path:
return path

return root_path + path


async def _blocked_asgi_app(scope, receive, send):
await send({"type": "http.response.start", "status": 403, "headers": []})
await send({"type": "http.response.body", "body": b""})
Expand Down Expand Up @@ -184,7 +195,8 @@ async def __call__(self, scope, receive, send):
server = scope.get("server")
scheme = scope.get("scheme", "http")
parsed_query = parse.parse_qs(bytes_to_str(scope.get("query_string", b"")))
full_path = scope.get("root_path", "") + scope.get("path", "")

full_path = _extract_full_path(scope.get("raw_path", ""), scope.get("root_path", ""), scope.get("path", ""))
if host_header:
url = "{}://{}{}".format(scheme, host_header, full_path)
elif server and len(server) == 2:
Expand Down
2 changes: 1 addition & 1 deletion riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ def select_pys(min_version=MIN_PYTHON_VERSION, max_version=MAX_PYTHON_VERSION):
"aiosqlite": latest,
"databases": latest,
"pytest-randomly": latest,
"anyio": "<4.0"
"anyio": "<4.0",
},
venvs=[
Venv(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
[[
{
"name": "starlette.background_task",
"service": "",
"resource": "custom_task",
"trace_id": 0,
"span_id": 1,
"parent_id": 0,
"type": "",
"error": 0,
"meta": {
"_dd.p.dm": "-0",
"_dd.p.tid": "65e2773000000000",
"_dd.span_links": "[{\"trace_id\": \"65e2773000000000567ea5a2ce22c2ba\", \"span_id\": \"0f75c3443dc887d3\", \"tracestate\": \"dd=s:1;t.dm:-0\", \"flags\": 1}]",
"language": "python",
"runtime-id": "f39de43db9104cf6afe785ddd2664ac7"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 15044
},
"duration": 2001768000,
"start": 1709340464158836000
}],
[
{
"name": "starlette.request",
"service": "starlette",
"resource": "GET /backgroundtask",
"trace_id": 1,
"trace_id": 0,
"span_id": 1,
"parent_id": 0,
"type": "web",
"error": 0,
"meta": {
"_dd.base_service": "",
"_dd.p.dm": "-0",
"_dd.p.tid": "65e2773000000000",
"_dd.p.tid": "65e2c4f100000000",
"component": "starlette",
"http.method": "GET",
"http.route": "/backgroundtask",
Expand All @@ -46,15 +20,15 @@
"http.useragent": "testclient",
"http.version": "1.1",
"language": "python",
"runtime-id": "f39de43db9104cf6afe785ddd2664ac7",
"runtime-id": "33c779b304044ecc8e830e7453f5536c",
"span.kind": "server"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 15044
"process_id": 44358
},
"duration": 414000,
"start": 1709340464158314000
"duration": 337000,
"start": 1709360369263514000
}]]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"meta": {
"_dd.base_service": "",
"_dd.p.dm": "-0",
"_dd.p.tid": "65e2775500000000",
"_dd.p.tid": "65e2c50600000000",
"component": "starlette",
"http.method": "GET",
"http.route": "/sub-app/hello/{name}",
Expand All @@ -20,17 +20,17 @@
"http.useragent": "testclient",
"http.version": "1.1",
"language": "python",
"runtime-id": "e41d791a495b45bc8fb6795d71b554f0",
"runtime-id": "b273c948f6414e7782d1ba93035ab5ba",
"span.kind": "server"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 15475
"process_id": 44458
},
"duration": 340000,
"start": 1709340501724456000
"duration": 343000,
"start": 1709360390669427000
},
{
"name": "starlette.request",
Expand All @@ -51,6 +51,6 @@
"http.version": "1.1",
"span.kind": "server"
},
"duration": 138000,
"start": 1709340501724671000
"duration": 143000,
"start": 1709360390669641000
}]]
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
"meta": {
"_dd.base_service": "",
"_dd.p.dm": "-0",
"_dd.p.tid": "65e2772f00000000",
"_dd.p.tid": "65e2c4e800000000",
"component": "requests",
"http.method": "GET",
"http.status_code": "200",
"http.url": "http://testserver/sub-app/hello/name",
"http.useragent": "testclient",
"language": "python",
"out.host": "testserver",
"runtime-id": "eea4a9669b3f49d296fdea727157cb02",
"runtime-id": "acccedc4c3694eb6ba36d7d69d240e13",
"span.kind": "client"
},
"metrics": {
"_dd.measured": 1,
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 15181
"process_id": 44375
},
"duration": 749000,
"start": 1709340463714758000
"duration": 1332000,
"start": 1709360360113849000
},
{
"name": "starlette.request",
Expand All @@ -43,7 +43,7 @@
"error": 0,
"meta": {
"_dd.base_service": "",
"_dd.p.tid": "65e2772f00000000",
"_dd.p.tid": "65e2c4e800000000",
"component": "starlette",
"http.method": "GET",
"http.route": "/sub-app/hello/{name}",
Expand All @@ -56,8 +56,8 @@
"metrics": {
"_dd.top_level": 1
},
"duration": 270000,
"start": 1709340463715057000
"duration": 262000,
"start": 1709360360114462000
},
{
"name": "starlette.request",
Expand All @@ -70,7 +70,7 @@
"error": 0,
"meta": {
"_dd.base_service": "",
"_dd.p.tid": "65e2772f00000000",
"_dd.p.tid": "65e2c4e800000000",
"component": "starlette",
"http.method": "GET",
"http.status_code": "200",
Expand All @@ -79,6 +79,6 @@
"http.version": "1.1",
"span.kind": "server"
},
"duration": 133000,
"start": 1709340463715208000
"duration": 126000,
"start": 1709360360114612000
}]]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"meta": {
"_dd.base_service": "",
"_dd.p.dm": "-0",
"_dd.p.tid": "65e2774a00000000",
"_dd.p.tid": "65e2c50700000000",
"component": "starlette",
"http.method": "GET",
"http.route": "/sub-app/hello/{name}",
Expand All @@ -20,17 +20,17 @@
"http.useragent": "testclient",
"http.version": "1.1",
"language": "python",
"runtime-id": "69b7bac4ef5b45a29cd782e9374b2b7e",
"runtime-id": "fcb8f22a11534a66affd8c79469822d1",
"span.kind": "server"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 15445
"process_id": 44472
},
"duration": 354000,
"start": 1709340490559381000
"duration": 381000,
"start": 1709360391705364000
},
{
"name": "starlette.request",
Expand All @@ -51,6 +51,6 @@
"http.version": "1.1",
"span.kind": "server"
},
"duration": 139000,
"start": 1709340490559608000
"duration": 153000,
"start": 1709360391705607000
}]]
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
"meta": {
"_dd.base_service": "",
"_dd.p.dm": "-0",
"_dd.p.tid": "65e2772e00000000",
"_dd.p.tid": "65e2c4e700000000",
"component": "requests",
"http.method": "GET",
"http.status_code": "200",
"http.url": "http://testserver/sub-app/hello/name",
"http.useragent": "testclient",
"language": "python",
"out.host": "testserver",
"runtime-id": "b69f241750a64400b3e354c08841d649",
"runtime-id": "022f892d422744568c349dad0ee6bc82",
"span.kind": "client"
},
"metrics": {
"_dd.measured": 1,
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 15176
"process_id": 44361
},
"duration": 730000,
"start": 1709340462577566000
"duration": 1412000,
"start": 1709360359055274000
},
{
"name": "starlette.request",
Expand All @@ -43,7 +43,7 @@
"error": 0,
"meta": {
"_dd.base_service": "",
"_dd.p.tid": "65e2772e00000000",
"_dd.p.tid": "65e2c4e700000000",
"component": "starlette",
"http.method": "GET",
"http.route": "/sub-app/hello/{name}",
Expand All @@ -56,8 +56,8 @@
"metrics": {
"_dd.top_level": 1
},
"duration": 265000,
"start": 1709340462577855000
"duration": 283000,
"start": 1709360359055950000
},
{
"name": "starlette.request",
Expand All @@ -70,7 +70,7 @@
"error": 0,
"meta": {
"_dd.base_service": "",
"_dd.p.tid": "65e2772e00000000",
"_dd.p.tid": "65e2c4e700000000",
"component": "starlette",
"http.method": "GET",
"http.status_code": "200",
Expand All @@ -80,5 +80,5 @@
"span.kind": "server"
},
"duration": 131000,
"start": 1709340462578002000
"start": 1709360359056117000
}]]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"error": 0,
"meta": {
"_dd.p.dm": "-0",
"_dd.p.tid": "65e2774b00000000",
"_dd.p.tid": "65e2c4fb00000000",
"component": "starlette",
"http.method": "GET",
"http.route": "/sub-app/hello/{name}",
Expand All @@ -19,17 +19,17 @@
"http.useragent": "testclient",
"http.version": "1.1",
"language": "python",
"runtime-id": "a08722f1fe0b4fb9bfc13b80b6b2064e",
"runtime-id": "a952f1cf0dda478f8166980176d40fac",
"span.kind": "server"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 15446
"process_id": 44454
},
"duration": 372000,
"start": 1709340491497636000
"duration": 341000,
"start": 1709360379275407000
},
{
"name": "http.server.request",
Expand All @@ -49,6 +49,6 @@
"http.version": "1.1",
"span.kind": "server"
},
"duration": 150000,
"start": 1709340491497870000
"duration": 137000,
"start": 1709360379275625000
}]]
Loading

0 comments on commit be07ae1

Please sign in to comment.