Skip to content

Commit bc57cc0

Browse files
authored
fix(asgi-instrumentation): extract target after running the framework (#1461)
1 parent 155fc46 commit bc57cc0

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4141
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
4242
- Remove db.name attribute from Redis instrumentation
4343
([#1427](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1427))
44+
- `opentelemetry-instrumentation-asgi` Fix target extraction for duration metric
45+
([#1461](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1461))
4446
- Add grpc.aio instrumentation to package entry points
4547
([#1442](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1442))
4648

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,6 @@ async def __call__(self, scope, receive, send):
542542
)
543543
duration_attrs = _parse_duration_attrs(attributes)
544544

545-
target = _collect_target_attribute(scope)
546-
if target:
547-
active_requests_count_attrs[SpanAttributes.HTTP_TARGET] = target
548-
duration_attrs[SpanAttributes.HTTP_TARGET] = target
549-
550545
if scope["type"] == "http":
551546
self.active_requests_counter.add(1, active_requests_count_attrs)
552547
try:
@@ -581,6 +576,9 @@ async def __call__(self, scope, receive, send):
581576
await self.app(scope, otel_receive, otel_send)
582577
finally:
583578
if scope["type"] == "http":
579+
target = _collect_target_attribute(scope)
580+
if target:
581+
duration_attrs[SpanAttributes.HTTP_TARGET] = target
584582
duration = max(round((default_timer() - start) * 1000), 0)
585583
self.duration_histogram.record(duration, duration_attrs)
586584
self.active_requests_counter.add(

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,15 @@ def test_metric_target_attribute(self):
591591
class TestRoute:
592592
path_format = expected_target
593593

594-
self.scope["route"] = TestRoute()
595-
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
594+
async def target_asgi(scope, receive, send):
595+
assert isinstance(scope, dict)
596+
if scope["type"] == "http":
597+
await http_app(scope, receive, send)
598+
scope["route"] = TestRoute()
599+
else:
600+
raise ValueError("websockets not supported")
601+
602+
app = otel_asgi.OpenTelemetryMiddleware(target_asgi)
596603
self.seed_app(app)
597604
self.send_default_request()
598605

@@ -601,20 +608,16 @@ class TestRoute:
601608
for resource_metric in metrics_list.resource_metrics:
602609
for scope_metrics in resource_metric.scope_metrics:
603610
for metric in scope_metrics.metrics:
611+
if metric.name != "http.server.duration":
612+
continue
604613
for point in metric.data.data_points:
605614
if isinstance(point, HistogramDataPoint):
606615
self.assertEqual(
607616
point.attributes["http.target"],
608617
expected_target,
609618
)
610619
assertions += 1
611-
elif isinstance(point, NumberDataPoint):
612-
self.assertEqual(
613-
point.attributes["http.target"],
614-
expected_target,
615-
)
616-
assertions += 1
617-
self.assertEqual(assertions, 2)
620+
self.assertEqual(assertions, 1)
618621

619622
def test_no_metric_for_websockets(self):
620623
self.scope = {

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
]
4747
_recommended_attrs = {
4848
"http.server.active_requests": _active_requests_count_attrs,
49-
"http.server.duration": _duration_attrs,
49+
"http.server.duration": {*_duration_attrs, SpanAttributes.HTTP_TARGET},
5050
}
5151

5252

@@ -218,6 +218,7 @@ def test_basic_metric_success(self):
218218
"http.server_name": "testserver",
219219
"net.host.port": 80,
220220
"http.status_code": 200,
221+
"http.target": "/foobar",
221222
}
222223
expected_requests_count_attributes = {
223224
"http.method": "GET",

0 commit comments

Comments
 (0)