1- import pdb
21import re
3- import sys
42import time
53
64import flask
75import flask .cli
86import jinja2
9- from flask import _app_ctx_stack , current_app , request
7+ from flask import g , request
108from flask .cli import ScriptInfo
119from werkzeug .exceptions import BadRequest
1210from werkzeug .middleware .dispatcher import DispatcherMiddleware
1614from appmap ._implementation .env import Env
1715from appmap ._implementation .event import HttpServerRequestEvent , HttpServerResponseEvent
1816from appmap ._implementation .flask import app as remote_recording_app
19- from appmap ._implementation .recorder import Recorder
2017from appmap ._implementation .web_framework import AppmapMiddleware
2118from appmap ._implementation .web_framework import TemplateHandler as BaseTemplateHandler
2219
@@ -65,9 +62,6 @@ class AppmapFlask(AppmapMiddleware):
6562 ```
6663 """
6764
68- def __init__ (self ):
69- super ().__init__ ()
70-
7165 def init_app (self , app ):
7266 if DetectEnabled .should_enable ("remote" ):
7367 app .wsgi_app = DispatcherMiddleware (
@@ -87,13 +81,15 @@ def before_request_main(self, rec, request):
8781 Metadata .add_framework ("flask" , flask .__version__ )
8882 np = None
8983 if request .url_rule :
84+ # pragma pylint: disable=line-too-long
9085 # Transform request.url to the expected normalized-path form. For example,
9186 # "/post/<username>/<post_id>/summary" becomes "/post/{username}/{post_id}/summary".
9287 # Notes:
9388 # * the value of `repr` of this rule begins with "<Rule '/post/<username>/<post_id>/summary'"
9489 # * the variable names in a rule can only contain alphanumerics:
9590 # * flask 1: https://github.com/pallets/werkzeug/blob/1dde4b1790f9c46b7122bb8225e6b48a5b22a615/src/werkzeug/routing.py#L143
9691 # * flask 2: https://github.com/pallets/werkzeug/blob/99f328cf2721e913bd8a3128a9cdd95ca97c334c/src/werkzeug/routing/rules.py#L56
92+ # pragma pylint: enable=line-too-long
9793 r = repr (request .url_rule )
9894 np = NP_PARAMS .findall (r )[0 ].translate (NP_PARAM_DELIMS )
9995
@@ -107,10 +103,10 @@ def before_request_main(self, rec, request):
107103 )
108104 rec .add_event (call_event )
109105
110- appctx = _app_ctx_stack .top
111- appctx . appmap_request_event = call_event
112- appctx . appmap_request_start = time . monotonic ()
113-
106+ # Flask 2 removed the suggestion to use _app_ctx_stack.top, and instead says extensions
107+ # should use g with a private property.
108+ g . _appmap_request_event = call_event # pylint: disable=protected-access
109+ g . _appmap_request_start = time . monotonic () # pylint: disable=protected-access
114110 return None , None
115111
116112 def after_request (self , response ):
@@ -128,10 +124,9 @@ def after_request(self, response):
128124 )
129125
130126 def after_request_main (self , rec , response , start , call_event_id ):
131- appctx = _app_ctx_stack .top
132- parent_id = appctx .appmap_request_event .id
133- duration = time .monotonic () - appctx .appmap_request_start
134-
127+ parent_id = g ._appmap_request_event .id # pylint: disable=protected-access
128+ start = g ._appmap_request_start # pylint: disable=protected-access
129+ duration = time .monotonic () - start
135130 return_event = HttpServerResponseEvent (
136131 parent_id = parent_id ,
137132 elapsed = duration ,
0 commit comments