Permalink
Browse files

Let users use appengine_config to override should_profile settings

  • Loading branch information...
1 parent 64e7078 commit ce2e38ef3f0d60ff405679786529981f46c0e655 @kamens kamens committed Jan 4, 2013
Showing with 37 additions and 17 deletions.
  1. +35 −12 config.py
  2. +2 −5 profiler.py
View
@@ -1,17 +1,40 @@
-from google.appengine.api import users
+import os
-# Customize should_profile to return true whenever a request should be
-# profiled. This function will be run once per request, so make sure its
-# contents are fast.
+from google.appengine.api import lib_config
+# These should_profile functions return true whenever a request should be
+# profiled.
+#
+# You can override these functions in appengine_config.py. See example below
+# and https://developers.google.com/appengine/docs/python/tools/appengineconfig
+#
+# These functions will be run once per request, so make sure they are fast.
+#
+# Example:
+# ...in appengine_config.py:
+# def gae_mini_profiler_should_profile_production():
+# from google.appengine.api import users
+# return users.is_current_user_admin()
-class ProfilerConfigProduction:
- @staticmethod
- def should_profile(environ):
- return True
+def _should_profile_production_default():
+ """Default to disabling in production if this function isn't overridden.
+ Can be overridden in appengine_config.py"""
+ return False
-class ProfilerConfigDevelopment:
- @staticmethod
- def should_profile(environ):
- return True
+def _should_profile_development_default():
+ """Default to enabling in development if this function isn't overridden.
+
+ Can be overridden in appengine_config.py"""
+ return True
+
+_config = lib_config.register("gae_mini_profiler", {
+ "should_profile_production": _should_profile_production_default,
+ "should_profile_development": _should_profile_development_default})
+
+def should_profile():
+ """Returns true if the current request should be profiles."""
+ if os.environ["SERVER_SOFTWARE"].startswith("Devel"):
+ return _config.should_profile_development()
+ else:
+ return _config.should_profile_production()
View
@@ -32,12 +32,9 @@
import gae_mini_profiler.config
from gae_mini_profiler import util
+from gae_mini_profiler import config
dev_server = os.environ["SERVER_SOFTWARE"].startswith("Devel")
-if dev_server:
- config = gae_mini_profiler.config.ProfilerConfigDevelopment
-else:
- config = gae_mini_profiler.config.ProfilerConfigProduction
class CurrentRequestId(object):
@@ -413,7 +410,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(environ) or
+ if (not config.should_profile() or
environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/")):
result = self.app(environ, start_response)
for value in result:

0 comments on commit ce2e38e

Please sign in to comment.