def appstats_wsgi_middleware(app):
 def appstats_wsgi_wrapper(environ, start_response):
  'Outer wrapper function around the WSGI protocol.\n\n    The top-level appstats_wsgi_middleware() function returns this\n    function to the caller instead of the app class or function passed\n    in.  When the caller calls this function (which may happen\n    multiple times, to handle multiple requests) this function\n    instantiates the app class (or calls the app function), sandwiched\n    between calls to start_recording() and end_recording() which\n    manipulate the recording state.\n\n    The signature is determined by the WSGI protocol.\n    '
  start_recording(environ)
  save_status = [None]
  datamodel_pb.AggregateRpcStatsProto.total_billed_ops_str = total_billed_ops_to_str
  datamodel_pb.IndividualRpcStatsProto.billed_ops_str = individual_billed_ops_to_str
  def appstats_start_response(status, headers, exc_info=None):
   "Inner wrapper function for the start_response() function argument.\n\n      The purpose of this wrapper is save the HTTP status (which the\n      WSGI protocol only makes available through the start_response()\n      function) into the surrounding scope.  This is done through a\n      hack because Python 2.x doesn't support assignment to nonlocal\n      variables.  If this function is called more than once, the last\n      status value will be used.\n\n      The signature is determined by the WSGI protocol.\n      "
   save_status.append(status)
   return start_response(status, headers, exc_info)
  try:
   result = app(environ, appstats_start_response)
  except Exception:
   end_recording(500)
   raise
  if (result is not None):
   for value in result:
    (yield value)
  status = save_status[(-1)]
  if (status is not None):
   status = status[:3]
  end_recording(status)
 return appstats_wsgi_wrapper

def appstats_wsgi_middleware ( app ) : def appstats_wsgi_wrapper ( environ , start_response ) : start_recording ( environ ) save_status = [ None ] datamodel_pb . AggregateRpcStatsProto . total_billed_ops_str = total_billed_ops_to_str datamodel_pb . IndividualRpcStatsProto . billed_ops_str = individual_billed_ops_to_str def appstats_start_response ( status , headers , exc_info = None ) : save_status . append ( status ) return start_response ( status , headers , exc_info ) try : result = app ( environ , appstats_start_response ) except Exception : end_recording ( 500 ) raise if ( result is not None ) : for value in result : ( yield value ) status = save_status [ ( - 1 ) ] if ( status is not None ) : status = status [ : 3 ] end_recording ( status ) return appstats_wsgi_wrapper
wsgi middleware to install the instrumentation .