Skip to content

Commit

Permalink
fix(asgi-instrumentation): extract target after running the framework (
Browse files Browse the repository at this point in the history
  • Loading branch information
sk- committed Dec 5, 2022
1 parent 155fc46 commit bc57cc0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1424](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1424))
- Remove db.name attribute from Redis instrumentation
([#1427](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1427))
- `opentelemetry-instrumentation-asgi` Fix target extraction for duration metric
([#1461](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1461))
- Add grpc.aio instrumentation to package entry points
([#1442](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1442))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,6 @@ async def __call__(self, scope, receive, send):
)
duration_attrs = _parse_duration_attrs(attributes)

target = _collect_target_attribute(scope)
if target:
active_requests_count_attrs[SpanAttributes.HTTP_TARGET] = target
duration_attrs[SpanAttributes.HTTP_TARGET] = target

if scope["type"] == "http":
self.active_requests_counter.add(1, active_requests_count_attrs)
try:
Expand Down Expand Up @@ -581,6 +576,9 @@ async def __call__(self, scope, receive, send):
await self.app(scope, otel_receive, otel_send)
finally:
if scope["type"] == "http":
target = _collect_target_attribute(scope)
if target:
duration_attrs[SpanAttributes.HTTP_TARGET] = target
duration = max(round((default_timer() - start) * 1000), 0)
self.duration_histogram.record(duration, duration_attrs)
self.active_requests_counter.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,15 @@ def test_metric_target_attribute(self):
class TestRoute:
path_format = expected_target

self.scope["route"] = TestRoute()
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
async def target_asgi(scope, receive, send):
assert isinstance(scope, dict)
if scope["type"] == "http":
await http_app(scope, receive, send)
scope["route"] = TestRoute()
else:
raise ValueError("websockets not supported")

app = otel_asgi.OpenTelemetryMiddleware(target_asgi)
self.seed_app(app)
self.send_default_request()

Expand All @@ -601,20 +608,16 @@ class TestRoute:
for resource_metric in metrics_list.resource_metrics:
for scope_metrics in resource_metric.scope_metrics:
for metric in scope_metrics.metrics:
if metric.name != "http.server.duration":
continue
for point in metric.data.data_points:
if isinstance(point, HistogramDataPoint):
self.assertEqual(
point.attributes["http.target"],
expected_target,
)
assertions += 1
elif isinstance(point, NumberDataPoint):
self.assertEqual(
point.attributes["http.target"],
expected_target,
)
assertions += 1
self.assertEqual(assertions, 2)
self.assertEqual(assertions, 1)

def test_no_metric_for_websockets(self):
self.scope = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
]
_recommended_attrs = {
"http.server.active_requests": _active_requests_count_attrs,
"http.server.duration": _duration_attrs,
"http.server.duration": {*_duration_attrs, SpanAttributes.HTTP_TARGET},
}


Expand Down Expand Up @@ -218,6 +218,7 @@ def test_basic_metric_success(self):
"http.server_name": "testserver",
"net.host.port": 80,
"http.status_code": 200,
"http.target": "/foobar",
}
expected_requests_count_attributes = {
"http.method": "GET",
Expand Down

0 comments on commit bc57cc0

Please sign in to comment.