def main(): app = create_app() csrf(app) # If we're on the local server, let's enable Flask debugging. # For more information: http://goo.gl/RNofH if settings.debug: app.debug = True app = get_debugged_app(app) settings.debug_profiler_enabled = profiler_config.should_profile(app) app = profiler.ProfilerWSGIMiddleware(app) CGIHandler().run(app)
def __call__(self, environ, start_response): _local.request_id = None # Never profile calls to the profiler itself to avoid endless recursion. if config.should_profile(environ) and not environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/"): # Set a random ID for this request so we can look up stats later import base64 _local.request_id = base64.urlsafe_b64encode(os.urandom(5)) # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info = None): if status.startswith("302 "): # Temporary redirect. Add request identifier to redirect location # so next rendered page can show this request's profile. headers = ProfilerWSGIMiddleware.headers_with_modified_redirect(environ, headers) # Access the profiler in closure scope profiler.temporary_redirect = True # Append headers used when displaying profiler results from ajax requests headers.append(("X-MiniProfiler-Id", _local.request_id)) headers.append(("X-MiniProfiler-QS", environ.get("QUERY_STRING"))) return start_response(status, headers, exc_info) profiler = RequestProfiler(_local.request_id, Mode.get_cookie_mode()) result = profiler.profile_start_response(self.app, environ, profiled_start_response) for value in result: yield value _local.request_id = None else: result = self.app(environ, start_response) for value in result: yield value
def __call__(self, environ, start_response): global request_id request_id = None # Start w/ a non-profiled app at the beginning of each request self.app = self.app_clean self.prof = None self.recorder = None self.temporary_redirect = False if config.should_profile(environ): # Set a random ID for this request so we can look up stats later import base64 import os request_id = base64.urlsafe_b64encode(os.urandom(5)) # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info = None): if status.startswith("302 "): # Temporary redirect. Add request identifier to redirect location # so next rendered page can show this request's profile. headers = ProfilerWSGIMiddleware.headers_with_modified_redirect(environ, headers) self.temporary_redirect = True # Append headers used when displaying profiler results from ajax requests headers.append(("X-MiniProfiler-Id", request_id)) headers.append(("X-MiniProfiler-QS", environ.get("QUERY_STRING"))) return start_response(status, headers, exc_info) # Configure AppStats output, keeping a high level of request # content so we can detect dupe RPCs more accurately from google.appengine.ext.appstats import recording recording.config.MAX_REPR = 750 # Turn on AppStats monitoring for this request old_app = self.app def wrapped_appstats_app(environ, start_response): # Use this wrapper to grab the app stats recorder for RequestStats.save() self.recorder = recording.recorder return old_app(environ, start_response) self.app = recording.appstats_wsgi_middleware(wrapped_appstats_app) # Turn on cProfile profiling for this request import cProfile self.prof = cProfile.Profile() # Get profiled wsgi result result = self.prof.runcall(lambda *args, **kwargs: self.app(environ, profiled_start_response), None, None) self.recorder = recording.recorder # If we're dealing w/ a generator, profile all of the .next calls as well if type(result) == GeneratorType: while True: try: yield self.prof.runcall(result.next) except StopIteration: break else: for value in result: yield value # Store stats for later access RequestStats(request_id, environ, self).store() # Just in case we're using up memory in the recorder and profiler self.recorder = None self.prof = None request_id = None else: result = self.app(environ, start_response) for value in result: yield value
def __call__(self, environ, start_response): # Never profile calls to the profiler itself to avoid endless recursion. if not config.should_profile(environ) or environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/"): app = self.app if self.keep_appstats: app = recording.appstats_wsgi_middleware(app) result = app(environ, start_response) for value in result: yield value return try: self._lock.acquire() # Start w/ a non-profiled app at the beginning of each request self.app = self.app_clean self.prof = None self.recorder = None self.temporary_redirect = False self.simple_timing = cookies.get_cookie_value("g-m-p-disabled") == "1" requeststore.clear_id() # Set a random ID for this request so we can look up stats later request_id = requeststore.generate_id() # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info=None): if status.startswith("302 "): # Temporary redirect. Add request identifier to redirect location # so next rendered page can show this request's profile. headers = ProfilerWSGIMiddleware.headers_with_modified_redirect(environ, headers) self.temporary_redirect = True # Append headers used when displaying profiler results from ajax requests headers.append(("X-MiniProfiler-Id", request_id)) headers.append(("X-MiniProfiler-QS", environ.get("QUERY_STRING").encode("ascii", "replace"))) return start_response(status, headers, exc_info) if self.simple_timing: # Detailed recording is disabled. Just track simple start/stop time. self.start = time.clock() result = self.app(environ, profiled_start_response) for value in result: yield value self.end = time.clock() else: # Add logging handler self.add_handler() # Turn on AppStats monitoring for this request old_app = self.app def wrapped_appstats_app(environ, start_response): # Use this wrapper to grab the app stats recorder for RequestStats.save() if recording.recorder_proxy.has_recorder_for_current_request(): self.recorder = recording.recorder_proxy.get_for_current_request() return old_app(environ, start_response) self.app = recording.appstats_wsgi_middleware(wrapped_appstats_app) # Turn on cProfile profiling for this request import cProfile self.prof = cProfile.Profile() # Get profiled wsgi result result = self.prof.runcall( lambda *args, **kwargs: self.app(environ, profiled_start_response), None, None ) # If we're dealing w/ a generator, profile all of the .next calls as well if type(result) == GeneratorType: while True: try: yield self.prof.runcall(result.next) except StopIteration: break else: for value in result: yield value self.logs = self.get_logs(self.handler) logging.getLogger().removeHandler(self.handler) self.handler.stream.close() self.handler = None # Store stats for later access RequestStats(request_id, environ, self).store() # Just in case we're using up memory in the recorder and profiler self.recorder = None self.prof = None requeststore.clear_id() finally: self._lock.release()
def __call__(self, environ, start_response): global request_id request_id = None # Start w/ a non-profiled app at the beginning of each request self.app = self.app_clean self.prof = None self.recorder = None self.temporary_redirect = False if config.should_profile(environ): # Set a random ID for this request so we can look up stats later import base64 import os request_id = base64.urlsafe_b64encode(os.urandom(5)) self.add_handler() # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info=None): if status.startswith("302 "): # Temporary redirect. Add request identifier to redirect location # so next rendered page can show this request's profile. headers = ProfilerWSGIMiddleware.headers_with_modified_redirect( environ, headers) self.temporary_redirect = True # Append headers used when displaying profiler results from ajax requests headers.append(("X-MiniProfiler-Id", request_id)) headers.append( ("X-MiniProfiler-QS", environ.get("QUERY_STRING"))) return start_response(status, headers, exc_info) # Configure AppStats output, keeping a high level of request # content so we can detect dupe RPCs more accurately # monkey patch appstats.formatting to fix string quoting bug # see http://code.google.com/p/googleappengine/issues/detail?id=5976 import unformatter.formatting import google.appengine.ext.appstats.formatting google.appengine.ext.appstats.formatting._format_value = unformatter.formatting._format_value from google.appengine.ext.appstats import recording recording.config.MAX_REPR = 750 # Turn on AppStats monitoring for this request old_app = self.app def wrapped_appstats_app(environ, start_response): # Use this wrapper to grab the app stats recorder for RequestStats.save() if hasattr(recording.recorder, "get_for_current_request"): self.recorder = recording.recorder.get_for_current_request( ) else: self.recorder = recording.recorder return old_app(environ, start_response) self.app = recording.appstats_wsgi_middleware(wrapped_appstats_app) # Turn on cProfile profiling for this request import cProfile self.prof = cProfile.Profile() # Get profiled wsgi result result = self.prof.runcall( lambda *args, **kwargs: self.app( environ, profiled_start_response), None, None) self.recorder = recording.recorder # If we're dealing w/ a generator, profile all of the .next calls as well if type(result) == GeneratorType: while True: try: yield self.prof.runcall(result.next) except StopIteration: break else: for value in result: yield value self.logs = self.get_logs(self.handler) logging.getLogger().removeHandler(self.handler) self.handler.stream.close() self.handler = None # Store stats for later access RequestStats(request_id, environ, self).store() # Just in case we're using up memory in the recorder and profiler self.recorder = None self.prof = None request_id = None else: result = self.app(environ, start_response) for value in result: yield value
def __call__(self, environ, start_response): path_info = environ.get("PATH_INFO", "") # Never profile calls to the profiler itself to avoid endless recursion. should_not_profile = (not config.should_profile(environ) or path_info.startswith("/gae_mini_profiler/") or path_info.startswith('/_ah/stats')) if should_not_profile: result = self.app(environ, start_response) for value in result: yield value return try: self._lock.acquire() # Start w/ a non-profiled app at the beginning of each request self.app = self.app_clean self.prof = None self.recorder = None self.temporary_redirect = False self.simple_timing = cookies.get_cookie_value("g-m-p-disabled") == "1" requeststore.clear_id() # Set a random ID for this request so we can look up stats later request_id = requeststore.generate_id() # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info = None): if status.startswith("302 "): # Temporary redirect. Add request identifier to redirect location # so next rendered page can show this request's profile. headers = ProfilerWSGIMiddleware.headers_with_modified_redirect(environ, headers) self.temporary_redirect = True # Append headers used when displaying profiler results from ajax requests headers.append(("X-MiniProfiler-Id", request_id)) headers.append(("X-MiniProfiler-QS", environ.get("QUERY_STRING"))) return start_response(status, headers, exc_info) if self.simple_timing: # Detailed recording is disabled. Just track simple start/stop time. self.start = time.clock() result = self.app(environ, profiled_start_response) for value in result: yield value self.end = time.clock() else: # Add logging handler self.add_handler() # Configure AppStats output, keeping a high level of request # content so we can detect dupe RPCs more accurately from google.appengine.ext.appstats import recording recording.config.MAX_REPR = 750 # Turn on AppStats monitoring for this request old_app = self.app def wrapped_appstats_app(environ, start_response): # Use this wrapper to grab the app stats recorder for RequestStats.save() if recording.recorder_proxy.has_recorder_for_current_request(): self.recorder = recording.recorder_proxy.get_for_current_request() return old_app(environ, start_response) self.app = recording.appstats_wsgi_middleware(wrapped_appstats_app) # Turn on cProfile profiling for this request import cProfile self.prof = cProfile.Profile() # Get profiled wsgi result result = self.prof.runcall(lambda *args, **kwargs: self.app(environ, profiled_start_response), None, None) # If we're dealing w/ a generator, profile all of the .next calls as well if type(result) == GeneratorType: while True: try: yield self.prof.runcall(result.next) except StopIteration: break else: for value in result: yield value self.logs = self.get_logs(self.handler) logging.getLogger().removeHandler(self.handler) self.handler.stream.close() self.handler = None # Store stats for later access RequestStats(request_id, environ, self).store() admins = config.email_profile_results_to(environ) if admins: version = environ.get("CURRENT_VERSION_ID").split('.')[0] app_id = app_identity.get_application_id() path_info = environ.get("PATH_INFO", "") mail.send_mail( 'profiles@%s.appspotmail.com' % app_id, admins, 'Profile for %s' % path_info, 'Go to https://%s-dot-%s.appspot.com/gae_mini_profiler/shared?request_id=%s' % ( version, app_id, request_id)) # Just in case we're using up memory in the recorder and profiler self.recorder = None self.prof = None requeststore.clear_id() finally: self._lock.release()
def __call__(self, environ, start_response): global request_id request_id = None # Start w/ a non-profiled app at the beginning of each request self.app = self.app_clean self.prof = None self.recorder = None self.temporary_redirect = False self.simple_timing = cookies.get_cookie_value("g-m-p-disabled") == "1" # Never profile calls to the profiler itself to avoid endless recursion. if config.should_profile(environ) and not environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/"): # Set a random ID for this request so we can look up stats later import base64 request_id = base64.urlsafe_b64encode(os.urandom(5)) # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info = None): if status.startswith("302 "): # Temporary redirect. Add request identifier to redirect location # so next rendered page can show this request's profile. headers = ProfilerWSGIMiddleware.headers_with_modified_redirect(environ, headers) self.temporary_redirect = True # Append headers used when displaying profiler results from ajax requests headers.append(("X-MiniProfiler-Id", request_id)) headers.append(("X-MiniProfiler-QS", environ.get("QUERY_STRING"))) return start_response(status, headers, exc_info) if self.simple_timing: # Detailed recording is disabled. Just track simple start/stop time. self.start = time.clock() result = self.app(environ, profiled_start_response) for value in result: yield value self.end = time.clock() else: # Add logging handler self.add_handler() # Monkey patch appstats.formatting to fix string quoting bug # See http://code.google.com/p/googleappengine/issues/detail?id=5976 import unformatter.formatting import google.appengine.ext.appstats.formatting google.appengine.ext.appstats.formatting._format_value = unformatter.formatting._format_value # Configure AppStats output, keeping a high level of request # content so we can detect dupe RPCs more accurately from google.appengine.ext.appstats import recording recording.config.MAX_REPR = 750 # Turn on AppStats monitoring for this request old_app = self.app def wrapped_appstats_app(environ, start_response): # Use this wrapper to grab the app stats recorder for RequestStats.save() if recording.recorder_proxy.has_recorder_for_current_request(): self.recorder = recording.recorder_proxy.get_for_current_request() return old_app(environ, start_response) self.app = recording.appstats_wsgi_middleware(wrapped_appstats_app) # Turn on cProfile profiling for this request import cProfile self.prof = cProfile.Profile() # Get profiled wsgi result result = self.prof.runcall(lambda *args, **kwargs: self.app(environ, profiled_start_response), None, None) # If we're dealing w/ a generator, profile all of the .next calls as well if type(result) == GeneratorType: while True: try: yield self.prof.runcall(result.next) except StopIteration: break else: for value in result: yield value self.logs = self.get_logs(self.handler) logging.getLogger().removeHandler(self.handler) self.handler.stream.close() self.handler = None # Store stats for later access RequestStats(request_id, environ, self).store() # Just in case we're using up memory in the recorder and profiler self.recorder = None self.prof = None request_id = None else: result = self.app(environ, start_response) for value in result: yield value
def __call__(self, environ, start_response): global request_id request_id = None # Start w/ a non-profiled app at the beginning of each request self.app = self.app_clean self.prof = None self.recorder = None if config.should_profile(environ): # Set a random ID for this request so we can look up stats later import base64 import os request_id = base64.urlsafe_b64encode(os.urandom(15)) # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info = None): headers.append(("X-MiniProfiler-Id", request_id)) return start_response(status, headers, exc_info) # Configure AppStats output, keeping a high level of request # content so we can detect dupe RPCs more accurately from google.appengine.ext.appstats import recording recording.config.MAX_REPR = 750 # Turn on AppStats monitoring for this request old_app = self.app def wrapped_appstats_app(environ, start_response): # Use this wrapper to grab the app stats recorder for RequestStats.save() self.recorder = recording.recorder return old_app(environ, start_response) self.app = recording.appstats_wsgi_middleware(wrapped_appstats_app) # Turn on cProfile profiling for this request import cProfile self.prof = cProfile.Profile() # Get profiled wsgi result result = self.prof.runcall(lambda *args, **kwargs: self.app(environ, profiled_start_response), None, None) self.recorder = recording.recorder # If we're dealing w/ a generator, profile all of the .next calls as well if type(result) == GeneratorType: while True: try: yield self.prof.runcall(result.next) except StopIteration: break else: for value in result: yield value # Store stats for later access RequestStats(request_id, environ, self).store() # Just in case we're using up memory in the recorder and profiler self.recorder = None self.prof = None request_id = None else: result = self.app(environ, start_response) for value in result: yield value
def __call__(self, environ, start_response): path_info = environ.get("PATH_INFO", "") # Never profile calls to the profiler itself to avoid endless recursion. should_not_profile = (not config.should_profile(environ) or path_info.startswith("/gae_mini_profiler/") or path_info.startswith('/_ah/stats')) if should_not_profile: result = self.app(environ, start_response) for value in result: yield value return try: self._lock.acquire() # Start w/ a non-profiled app at the beginning of each request self.app = self.app_clean self.prof = None self.recorder = None self.temporary_redirect = False self.simple_timing = cookies.get_cookie_value( "g-m-p-disabled") == "1" requeststore.clear_id() # Set a random ID for this request so we can look up stats later request_id = requeststore.generate_id() # Send request id in headers so jQuery ajax calls can pick # up profiles. def profiled_start_response(status, headers, exc_info=None): if status.startswith("302 "): # Temporary redirect. Add request identifier to redirect location # so next rendered page can show this request's profile. headers = ProfilerWSGIMiddleware.headers_with_modified_redirect( environ, headers) self.temporary_redirect = True # Append headers used when displaying profiler results from ajax requests headers.append(("X-MiniProfiler-Id", request_id)) headers.append( ("X-MiniProfiler-QS", environ.get("QUERY_STRING"))) return start_response(status, headers, exc_info) if self.simple_timing: # Detailed recording is disabled. Just track simple start/stop time. self.start = time.clock() result = self.app(environ, profiled_start_response) for value in result: yield value self.end = time.clock() else: # Add logging handler self.add_handler() # Configure AppStats output, keeping a high level of request # content so we can detect dupe RPCs more accurately from google.appengine.ext.appstats import recording recording.config.MAX_REPR = 750 # Turn on AppStats monitoring for this request old_app = self.app def wrapped_appstats_app(environ, start_response): # Use this wrapper to grab the app stats recorder for RequestStats.save() if recording.recorder_proxy.has_recorder_for_current_request( ): self.recorder = recording.recorder_proxy.get_for_current_request( ) return old_app(environ, start_response) self.app = recording.appstats_wsgi_middleware( wrapped_appstats_app) # Turn on cProfile profiling for this request import cProfile self.prof = cProfile.Profile() # Get profiled wsgi result result = self.prof.runcall( lambda *args, **kwargs: self.app( environ, profiled_start_response), None, None) # If we're dealing w/ a generator, profile all of the .next calls as well if type(result) == GeneratorType: while True: try: yield self.prof.runcall(result.next) except StopIteration: break else: for value in result: yield value self.logs = self.get_logs(self.handler) logging.getLogger().removeHandler(self.handler) self.handler.stream.close() self.handler = None # Store stats for later access RequestStats(request_id, environ, self).store() admins = config.email_profile_results_to(environ) if admins: version = environ.get("CURRENT_VERSION_ID").split('.')[0] app_id = app_identity.get_application_id() path_info = environ.get("PATH_INFO", "") mail.send_mail( 'profiles@%s.appspotmail.com' % app_id, admins, 'Profile for %s' % path_info, 'Go to https://%s-dot-%s.appspot.com/gae_mini_profiler/shared?request_id=%s' % (version, app_id, request_id)) # Just in case we're using up memory in the recorder and profiler self.recorder = None self.prof = None requeststore.clear_id() finally: self._lock.release()