Skip to content

Commit

Permalink
flask: add http.target and http.route to metric attributes (#2621)
Browse files Browse the repository at this point in the history
  • Loading branch information
GonzaloGuasch authored Jul 22, 2024
1 parent 6594cdf commit a322a0a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def response_hook(span: Span, status: str, response_headers: List):
)
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,
Expand Down Expand Up @@ -340,12 +341,16 @@ def _wrapped_app(wrapped_app_environ, start_response):
)

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()
Expand Down Expand Up @@ -388,13 +393,23 @@ def _start_response(status, response_headers, *args, **kwargs):
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
)
if duration_histogram_new:
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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -95,11 +101,11 @@ def expected_attributes_new(override_attributes):
]
_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
Expand All @@ -109,8 +115,8 @@ def expected_attributes_new(override_attributes):
)
_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,
}


Expand Down Expand Up @@ -570,6 +576,7 @@ def test_basic_metric_success(self):
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",
Expand All @@ -595,6 +602,7 @@ def test_basic_metric_success_new_semconv(self):
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,
}
Expand Down

0 comments on commit a322a0a

Please sign in to comment.