flask: add `http.target` and `http.route` to metric attributes (#2621)

This commit is contained in:
GonzaloGuasch 2024-07-22 13:33:40 -03:00 committed by GitHub
parent 6594cdf053
commit a322a0a26b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 4 deletions

View File

@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- `opentelemetry-instrumentation-flask` Add `http.route` and `http.target` to metric attributes
([#2621](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2621))
- `opentelemetry-instrumentation-sklearn` Deprecated the sklearn instrumentation
([#2708](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2708))
- `opentelemetry-instrumentation-pyramid` Record exceptions raised when serving a request

View File

@ -266,6 +266,7 @@ from opentelemetry.instrumentation.propagators import (
)
from opentelemetry.instrumentation.utils import _start_internal_or_server_span
from opentelemetry.metrics import get_meter
from opentelemetry.semconv.attributes.http_attributes import HTTP_ROUTE
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.metrics.http_metrics import (
HTTP_SERVER_REQUEST_DURATION,
@ -340,12 +341,16 @@ def _rewrapped_app(
)
active_requests_counter.add(1, active_requests_count_attrs)
request_route = None
def _start_response(status, response_headers, *args, **kwargs):
if flask.request and (
excluded_urls is None
or not excluded_urls.url_disabled(flask.request.url)
):
nonlocal request_route
request_route = flask.request.url_rule
span = flask.request.environ.get(_ENVIRON_SPAN_KEY)
propagator = get_global_response_propagator()
@ -388,6 +393,12 @@ def _rewrapped_app(
duration_attrs_old = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.DEFAULT
)
if request_route:
duration_attrs_old[SpanAttributes.HTTP_TARGET] = str(
request_route
)
duration_histogram_old.record(
max(round(duration_s * 1000), 0), duration_attrs_old
)
@ -395,6 +406,10 @@ def _rewrapped_app(
duration_attrs_new = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.HTTP
)
if request_route:
duration_attrs_new[HTTP_ROUTE] = str(request_route)
duration_histogram_new.record(
max(duration_s, 0), duration_attrs_new
)

View File

@ -85,6 +85,12 @@ def expected_attributes_new(override_attributes):
return default_attributes
_server_duration_attrs_old_copy = _server_duration_attrs_old.copy()
_server_duration_attrs_old_copy.append("http.target")
_server_duration_attrs_new_copy = _server_duration_attrs_new.copy()
_server_duration_attrs_new_copy.append("http.route")
_expected_metric_names_old = [
"http.server.active_requests",
"http.server.duration",
@ -95,11 +101,11 @@ _expected_metric_names_new = [
]
_recommended_metrics_attrs_old = {
"http.server.active_requests": _server_active_requests_count_attrs_old,
"http.server.duration": _server_duration_attrs_old,
"http.server.duration": _server_duration_attrs_old_copy,
}
_recommended_metrics_attrs_new = {
"http.server.active_requests": _server_active_requests_count_attrs_new,
"http.server.request.duration": _server_duration_attrs_new,
"http.server.request.duration": _server_duration_attrs_new_copy,
}
_server_active_requests_count_attrs_both = (
_server_active_requests_count_attrs_old
@ -109,8 +115,8 @@ _server_active_requests_count_attrs_both.extend(
)
_recommended_metrics_attrs_both = {
"http.server.active_requests": _server_active_requests_count_attrs_both,
"http.server.duration": _server_duration_attrs_old,
"http.server.request.duration": _server_duration_attrs_new,
"http.server.duration": _server_duration_attrs_old_copy,
"http.server.request.duration": _server_duration_attrs_new_copy,
}
@ -570,6 +576,7 @@ class TestProgrammatic(InstrumentationTest, WsgiTestBase):
self.client.get("/hello/756")
expected_duration_attributes = {
"http.method": "GET",
"http.target": "/hello/<int:helloid>",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
@ -595,6 +602,7 @@ class TestProgrammatic(InstrumentationTest, WsgiTestBase):
expected_duration_attributes = {
"http.request.method": "GET",
"url.scheme": "http",
"http.route": "/hello/<int:helloid>",
"network.protocol.version": "1.1",
"http.response.status_code": 200,
}