diff --git a/config.py b/config.py index 95e5524..2663650 100644 --- a/config.py +++ b/config.py @@ -12,6 +12,8 @@ # # These functions will be run once per request, so make sure they are fast. # +# If you need access to the environ use the `with_environ` decorator. +# # Example: # ...in appengine_config.py: # def gae_mini_profiler_should_profile_production(): @@ -30,13 +32,26 @@ def _should_profile_development_default(): Can be overridden in appengine_config.py""" return True +def with_environ(func): + """Decorator to distinguish should_profile_*() methods that need access + to the environ""" + func._accept_environ = True + return func + _config = lib_config.register("gae_mini_profiler", { "should_profile_production": _should_profile_production_default, "should_profile_development": _should_profile_development_default}) -def should_profile(): +def should_profile(environ): """Returns true if the current request should be profiles.""" if util.dev_server: - return _config.should_profile_development() + func = _config.should_profile_development else: - return _config.should_profile_production() + func = _config.should_profile_production + + if getattr(func, '_accept_environ', False): + args = environ, + else: + args = tuple() + + return func(*args) diff --git a/profiler.py b/profiler.py index 3b6ef16..4baffad 100644 --- a/profiler.py +++ b/profiler.py @@ -584,7 +584,7 @@ def __call__(self, environ, start_response): CurrentRequestId.set(None) # Never profile calls to the profiler itself to avoid endless recursion. - if (not config.should_profile() or + if (not config.should_profile(environ) or environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/")): result = self.app(environ, start_response) for value in result: