def get_cookie_mode(): """Get the profiler mode requested by current request's cookies.""" mode = cookies.get_cookie_value("g-m-p-mode") if (mode not in [ Mode.SIMPLE, Mode.CPU_ONLY, Mode.RPC_ONLY, Mode.CPU_AND_RPC]): mode = Mode.CPU_AND_RPC return mode
def get_mode(environ): """Get the profiler mode requested by current request's headers & cookies.""" if "HTTP_G_M_P_MODE" in environ: mode = environ["HTTP_G_M_P_MODE"] else: mode = cookies.get_cookie_value("g-m-p-mode") if (mode not in [ Mode.SIMPLE, Mode.CPU_INSTRUMENTED, Mode.CPU_SAMPLING, Mode.RPC_ONLY, Mode.RPC_AND_CPU_INSTRUMENTED, Mode.RPC_AND_CPU_SAMPLING ]): mode = Mode.RPC_AND_CPU_INSTRUMENTED return mode
def get_mode(environ): """Get the profiler mode requested by current request's headers & cookies.""" if "HTTP_G_M_P_MODE" in environ: mode = environ["HTTP_G_M_P_MODE"] else: mode = cookies.get_cookie_value("g-m-p-mode") if (mode not in [ Mode.SIMPLE, Mode.CPU_INSTRUMENTED, Mode.CPU_SAMPLING, Mode.RPC_ONLY, Mode.RPC_AND_CPU_INSTRUMENTED, Mode.RPC_AND_CPU_SAMPLING]): mode = Mode.RPC_AND_CPU_INSTRUMENTED return mode
def get_mode(environ, cookies): """Returns the profiling mode in by current request's headers & cookies. If not set explicitly, calls gae_mini_profiler_get_default_mode_development / production""" if "HTTP_G_M_P_MODE" in environ: mode = environ["HTTP_G_M_P_MODE"] else: mode = cookies.get_cookie_value("g-m-p-mode") if (mode in [ Mode.SIMPLE, Mode.CPU_INSTRUMENTED, Mode.CPU_SAMPLING, Mode.CPU_LINEBYLINE, Mode.RPC_ONLY, Mode.RPC_AND_CPU_INSTRUMENTED, Mode.RPC_AND_CPU_SAMPLING, Mode.RPC_AND_CPU_LINEBYLINE]): return mode if _DEVELOPMENT_SERVER: return _mode.get_default_mode_development() else: return _mode.get_default_mode_production()
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): 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 get_user_id(): return int(get_cookie_value("persistent").split("%")[0])
def get_auth_token(): return get_cookie_value("auth-token")
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