Skip to content

Commit 2b33c49

Browse files
krnrYuri Lobanoff
authored andcommitted
Use canonical name for aiohttp request span name
1 parent 2d42884 commit 2b33c49

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
1212
## Unreleased
1313

14+
- `opentelemetry-instrumentation-aiohttp-server`: Use `canonical` attribute of the `Resource` as a span name.
15+
1416
### Added
1517

1618
- `opentelemetry-instrumentation-aiohttp-client`: add support for url exclusions via `OTEL_PYTHON_EXCLUDED_URLS` / `OTEL_PYTHON_AIOHTTP_CLIENT_EXCLUDED_URLS`

instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ def get_default_span_name(request: web.Request) -> str:
245245
The span name.
246246
"""
247247
span_name = request.path.strip() or f"HTTP {request.method}"
248+
if request.match_info and request.match_info.route.resource:
249+
resource = request.match_info.route.resource
250+
if resource.canonical:
251+
span_name = resource.canonical
248252
return span_name
249253

250254

instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from opentelemetry.semconv._incubating.attributes.http_attributes import (
3030
HTTP_METHOD,
3131
HTTP_STATUS_CODE,
32+
HTTP_TARGET,
3233
HTTP_URL,
3334
)
3435
from opentelemetry.test.globals_test import (
@@ -106,7 +107,15 @@ async def fixture_server_fixture(tracer, aiohttp_server, suppress):
106107
AioHttpServerInstrumentor().instrument()
107108

108109
app = aiohttp.web.Application()
109-
app.add_routes([aiohttp.web.get("/test-path", default_handler)])
110+
app.add_routes(
111+
[
112+
aiohttp.web.get("/test-path", default_handler),
113+
aiohttp.web.get("/test-path/{url_param}", default_handler),
114+
aiohttp.web.get(
115+
"/object/{object_id}/action/{another_param}", default_handler
116+
),
117+
]
118+
)
110119
if suppress:
111120
with suppress_http_instrumentation():
112121
server = await aiohttp_server(app)
@@ -170,6 +179,53 @@ async def test_status_code_instrumentation(
170179
)
171180

172181

182+
@pytest.mark.asyncio
183+
@pytest.mark.parametrize(
184+
"url, example_paths",
185+
[
186+
(
187+
"/test-path/{url_param}",
188+
(
189+
"/test-path/foo",
190+
"/test-path/bar",
191+
),
192+
),
193+
(
194+
"/object/{object_id}/action/{another_param}",
195+
(
196+
"/object/1/action/bar",
197+
"/object/234/action/baz",
198+
),
199+
),
200+
],
201+
)
202+
async def test_url_params_instrumentation(
203+
tracer,
204+
server_fixture,
205+
aiohttp_client,
206+
url,
207+
example_paths,
208+
):
209+
_, memory_exporter = tracer
210+
server, _ = server_fixture
211+
212+
assert len(memory_exporter.get_finished_spans()) == 0
213+
214+
client = await aiohttp_client(server)
215+
for path in example_paths:
216+
await client.get(path)
217+
218+
assert len(memory_exporter.get_finished_spans()) == 2
219+
220+
for request_path, span in zip(
221+
example_paths, memory_exporter.get_finished_spans()
222+
):
223+
assert url == span.name
224+
assert request_path == span.attributes[HTTP_TARGET]
225+
full_url = f"http://{server.host}:{server.port}{request_path}"
226+
assert full_url == span.attributes[HTTP_URL]
227+
228+
173229
@pytest.mark.asyncio
174230
@pytest.mark.parametrize("suppress", [True])
175231
async def test_suppress_instrumentation(

0 commit comments

Comments
 (0)