Skip to content

Commit 304c88b

Browse files
committed
Add http.target in metric attribute
1 parent 077b410 commit 304c88b

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- `opentelemetry-instrumentation-flask` Add `http.route` to metric attributes
12+
- `opentelemetry-instrumentation-flask` Add `http.route` and `http.target` to metric attributes
1313
([#2621](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2621))
1414
- `opentelemetry-instrumentation-pyramid` Record exceptions raised when serving a request
1515
([#2622](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2622))

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

+9
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ def response_hook(span: Span, status: str, response_headers: List):
266266
)
267267
from opentelemetry.instrumentation.utils import _start_internal_or_server_span
268268
from opentelemetry.metrics import get_meter
269+
from opentelemetry.semconv._incubating.attributes.http_attributes import (
270+
HTTP_TARGET,
271+
)
269272
from opentelemetry.semconv.attributes.http_attributes import HTTP_ROUTE
270273
from opentelemetry.semconv.metrics import MetricInstruments
271274
from opentelemetry.semconv.metrics.http_metrics import (
@@ -399,6 +402,9 @@ def _start_response(status, response_headers, *args, **kwargs):
399402
duration_attrs_old[HTTP_ROUTE] = wrapped_app_environ.get(
400403
_ENVIRON_REQUEST_ROUTE_KEY
401404
)
405+
duration_attrs_old[HTTP_TARGET] = wrapped_app_environ.get(
406+
_ENVIRON_REQUEST_ROUTE_KEY
407+
)
402408

403409
duration_histogram_old.record(
404410
max(round(duration_s * 1000), 0), duration_attrs_old
@@ -412,6 +418,9 @@ def _start_response(status, response_headers, *args, **kwargs):
412418
duration_attrs_new[HTTP_ROUTE] = wrapped_app_environ.get(
413419
_ENVIRON_REQUEST_ROUTE_KEY
414420
)
421+
duration_attrs_new[HTTP_TARGET] = wrapped_app_environ.get(
422+
_ENVIRON_REQUEST_ROUTE_KEY
423+
)
415424

416425
duration_histogram_new.record(
417426
max(duration_s, 0), duration_attrs_new

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ def expected_attributes_new(override_attributes):
8787

8888
_server_duration_attrs_old_copy = _server_duration_attrs_old.copy()
8989
_server_duration_attrs_old_copy.append("http.route")
90+
_server_duration_attrs_old_copy.append("http.target")
91+
92+
_server_duration_attrs_new_copy = _server_duration_attrs_new.copy()
93+
_server_duration_attrs_new_copy.append("http.target")
9094

9195
_expected_metric_names_old = [
9296
"http.server.active_requests",
@@ -102,7 +106,7 @@ def expected_attributes_new(override_attributes):
102106
}
103107
_recommended_metrics_attrs_new = {
104108
"http.server.active_requests": _server_active_requests_count_attrs_new,
105-
"http.server.request.duration": _server_duration_attrs_new,
109+
"http.server.request.duration": _server_duration_attrs_new_copy,
106110
}
107111
_server_active_requests_count_attrs_both = (
108112
_server_active_requests_count_attrs_old
@@ -113,7 +117,7 @@ def expected_attributes_new(override_attributes):
113117
_recommended_metrics_attrs_both = {
114118
"http.server.active_requests": _server_active_requests_count_attrs_both,
115119
"http.server.duration": _server_duration_attrs_old_copy,
116-
"http.server.request.duration": _server_duration_attrs_new,
120+
"http.server.request.duration": _server_duration_attrs_new_copy,
117121
}
118122

119123

@@ -574,6 +578,7 @@ def test_basic_metric_success(self):
574578
expected_duration_attributes = {
575579
"http.method": "GET",
576580
"http.route": "/hello/<int:helloid>",
581+
"http.target": "/hello/<int:helloid>",
577582
"http.host": "localhost",
578583
"http.scheme": "http",
579584
"http.flavor": "1.1",
@@ -600,6 +605,7 @@ def test_basic_metric_success_new_semconv(self):
600605
"http.request.method": "GET",
601606
"url.scheme": "http",
602607
"http.route": "/hello/<int:helloid>",
608+
"http.target": "/hello/<int:helloid>",
603609
"network.protocol.version": "1.1",
604610
"http.response.status_code": 200,
605611
}

0 commit comments

Comments
 (0)