Skip to content

Commit

Permalink
Change env variable to nonlocal var
Browse files Browse the repository at this point in the history
  • Loading branch information
GonzaloGuasch committed Jul 19, 2024
1 parent 588199d commit cea918d
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ 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 = "opentelemetry-flask.request-route_key"

_excluded_urls_from_env = get_excluded_urls("FLASK")

Expand Down Expand Up @@ -342,16 +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)
):
if flask.request.url_rule:
wrapped_app_environ[_ENVIRON_REQUEST_ROUTE_KEY] = str(
flask.request.url_rule
)
nonlocal request_route
request_route = str(flask.request.url_rule)

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

Expand Down Expand Up @@ -396,10 +395,9 @@ def _start_response(status, response_headers, *args, **kwargs):
attributes, _HTTPStabilityMode.DEFAULT
)

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

duration_histogram_old.record(
max(round(duration_s * 1000), 0), duration_attrs_old
)
Expand All @@ -408,10 +406,8 @@ def _start_response(status, response_headers, *args, **kwargs):
attributes, _HTTPStabilityMode.HTTP
)

if wrapped_app_environ.get(_ENVIRON_REQUEST_ROUTE_KEY, None):
duration_attrs_new[HTTP_ROUTE] = wrapped_app_environ.get(
_ENVIRON_REQUEST_ROUTE_KEY
)
if request_route:
duration_attrs_new[HTTP_ROUTE] = request_route

duration_histogram_new.record(
max(duration_s, 0), duration_attrs_new
Expand Down

0 comments on commit cea918d

Please sign in to comment.