Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send http.route attribute in flask metric #2506

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `opentelemetry-instrumentation-flask` Add http route to metric attributes
([#2506](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2506))
-
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-

- `opentelemetry-sdk-extension-aws` Register AWS resource detectors under the
`opentelemetry_resource_detector` entry point
([#2382](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2382))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def response_hook(span: Span, status: str, response_headers: List):
_ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key"
_ENVIRON_REQCTX_REF_KEY = "opentelemetry-flask.reqctx_ref_key"
_ENVIRON_TOKEN = "opentelemetry-flask.token"
_ENVIRON_REQUEST_ROUTE_KEY = "request-route_key"

_excluded_urls_from_env = get_excluded_urls("FLASK")

Expand Down Expand Up @@ -346,6 +347,11 @@ def _start_response(status, response_headers, *args, **kwargs):
excluded_urls is None
or not excluded_urls.url_disabled(flask.request.url)
):
if flask.request.url_rule:
wrapped_app_environ[_ENVIRON_REQUEST_ROUTE_KEY] = str(
flask.request.url_rule
)

span = flask.request.environ.get(_ENVIRON_SPAN_KEY)

propagator = get_global_response_propagator()
Expand Down Expand Up @@ -388,13 +394,25 @@ def _start_response(status, response_headers, *args, **kwargs):
duration_attrs_old = otel_wsgi._parse_duration_attrs(
attributes, _HTTPStabilityMode.DEFAULT
)

if wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY, None):
duration_attrs_old[SpanAttributes.HTTP_ROUTE] = (
wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY)
)

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 wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY, None):
duration_attrs_new[SpanAttributes.HTTP_ROUTE] = (
wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY)
)

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 @@ -459,6 +459,7 @@ def test_exclude_lists_from_explicit(self):
self.assertEqual(len(span_list), 1)

def test_flask_metrics(self):
_server_duration_attrs_old.append("http.route")
start = default_timer()
self.client.get("/hello/123")
self.client.get("/hello/321")
Expand Down Expand Up @@ -570,6 +571,7 @@ def test_basic_metric_success(self):
self.client.get("/hello/756")
expected_duration_attributes = {
"http.method": "GET",
"http.route": "/hello/<int:helloid>",
"http.host": "localhost",
"http.scheme": "http",
"http.flavor": "1.1",
Expand Down Expand Up @@ -597,6 +599,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