Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleanup and documentation for the recording module

  • Loading branch information...
commit f157f4beafb5e46e9bdada4cc2bc68f88c3114d4 1 parent 216f7e1
Ben Darnell authored
Showing with 34 additions and 12 deletions.
  1. +34 −12 tornado_tracing/recording.py
View
46 tornado_tracing/recording.py
@@ -16,8 +16,6 @@
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
from google.appengine.ext.appstats import recording
- from google.appengine.ext.appstats.recording import (
- start_recording, end_recording, pre_call_hook, post_call_hook)
from tornado.httpclient import AsyncHTTPClient
from tornado.options import define, options
from tornado.stack_context import StackContext
@@ -25,7 +23,34 @@
define('enable_appstats', type=bool, default=False)
+# These methods from the appengine recording module are a part of our
+# public API.
+
+# start_recording(wsgi_environ) creates a recording context
+start_recording = recording.start_recording
+# end_recording(http_status) terminates a recording context
+end_recording = recording.end_recording
+
+# pre/post_call_hook(service, method, request, response) mark the
+# beginning/end of a time span to record in the trace.
+pre_call_hook = recording.pre_call_hook
+post_call_hook = recording.post_call_hook
+
+def save():
+ '''Returns an object that can be passed to restore() to resume
+ a suspended record.
+ '''
+ return recording.recorder
+
+def restore(recorder):
+ '''Reactivates a previously-saved recording context.'''
+ recording.recorder = recorder
+
+
class RecordingRequestHandler(RequestHandler):
+ '''RequestHandler subclass that establishes a recording context for each
+ request.
+ '''
def __init__(self, *args, **kwargs):
super(RecordingRequestHandler, self).__init__(*args, **kwargs)
self.__recorder = None
@@ -33,10 +58,10 @@ def __init__(self, *args, **kwargs):
def _execute(self, transforms, *args, **kwargs):
if options.enable_appstats:
start_recording(tornado.wsgi.WSGIContainer.environ(self.request))
- recorder = recording.recorder
+ recorder = save()
@contextlib.contextmanager
def transfer_recorder():
- recording.recorder = recorder
+ restore(recorder)
yield
with StackContext(transfer_recorder):
super(RecordingRequestHandler, self)._execute(transforms,
@@ -51,14 +76,17 @@ def finish(self, chunk=None):
end_recording(self._status_code)
class RecordingFallbackHandler(tornado.web.FallbackHandler):
+ '''FallbackHandler subclass that establishes a recording context for
+ each request.
+ '''
def prepare(self):
if options.enable_appstats:
recording.start_recording(
tornado.wsgi.WSGIContainer.environ(self.request))
- recorder = recording.recorder
+ recorder = save()
@contextlib.contextmanager
def transfer_recorder():
- recording.recorder = recorder
+ restore(recorder)
yield
with StackContext(transfer_recorder):
super(RecordingFallbackHandler, self).prepare()
@@ -66,12 +94,6 @@ def transfer_recorder():
else:
super(RecordingFallbackHandler, self).prepare()
-def save():
- return recording.recorder
-
-def restore(recorder):
- recording.recorder = recorder
-
def _recording_request(request):
if isinstance(request, tornado.httpclient.HTTPRequest):
return request.url
Please sign in to comment.
Something went wrong with that request. Please try again.