Skip to content

Commit

Permalink
Improve logging for remoting
Browse files Browse the repository at this point in the history
Thanks to stumitchell for the inspiration.
  • Loading branch information
njoyce committed Jan 25, 2015
1 parent a2d6dee commit cb2377a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
30 changes: 30 additions & 0 deletions pyamf/remoting/amf0.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class RequestProcessor(object):
def __init__(self, gateway):
self.gateway = gateway

@property
def logger(self):
if not self.gateway.logger:
return None

return self.gateway.logger

def authenticateRequest(self, request, service_request, *args, **kwargs):
"""
Authenticates the request against the service.
Expand Down Expand Up @@ -87,6 +94,11 @@ def __call__(self, request, *args, **kwargs):
request.target
)
except gateway.UnknownServiceError:
if self.logger:
self.logger.error(
'Unknown endpoint %r' % (request.target,)
)

return self.buildErrorResponse(request)

# we have a valid service, now attempt authentication
Expand All @@ -100,6 +112,12 @@ def __call__(self, request, *args, **kwargs):
except (SystemExit, KeyboardInterrupt):
raise
except:
if self.logger:
self.logger.exception(
'Unexpected error while authenticating request %r',
request.target
)

return self.buildErrorResponse(request)

if not authd:
Expand All @@ -118,6 +136,12 @@ def __call__(self, request, *args, **kwargs):
except (SystemExit, KeyboardInterrupt):
raise
except:
if self.logger:
self.logger.exception(
'Unexpected error while pre-processing request %r',
request.target
)

return self.buildErrorResponse(request)

try:
Expand All @@ -133,6 +157,12 @@ def __call__(self, request, *args, **kwargs):
except (SystemExit, KeyboardInterrupt):
raise
except:
if self.logger:
self.logger.exception(
'Unexpected error while processing request %r',
request.target
)

return self.buildErrorResponse(request)


Expand Down
44 changes: 35 additions & 9 deletions pyamf/remoting/amf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class RequestProcessor(object):
def __init__(self, gateway):
self.gateway = gateway

@property
def logger(self):
if not self.gateway.logger:
return None

return self.gateway.logger

def buildErrorResponse(self, request, error=None):
"""
Builds an error response.
Expand Down Expand Up @@ -175,17 +182,18 @@ def _processAsyncMessage(self, amf_request, ro_request, **kwargs):
def _processRemotingMessage(self, amf_request, ro_request, **kwargs):
ro_response = generate_acknowledgement(ro_request)

service_name = ro_request.operation

if hasattr(ro_request, 'destination') and ro_request.destination:
service_name = '%s.%s' % (ro_request.destination, service_name)

service_request = self.gateway.getServiceRequest(amf_request,
service_name)
service_name = get_service_name(ro_request)
service_request = self.gateway.getServiceRequest(
amf_request,
service_name
)

# fire the preprocessor (if there is one)
self.gateway.preprocessRequest(service_request, *ro_request.body,
**kwargs)
self.gateway.preprocessRequest(
service_request,
*ro_request.body,
**kwargs
)

ro_response.body = self.gateway.callServiceRequest(
service_request,
Expand All @@ -212,5 +220,23 @@ def __call__(self, amf_request, **kwargs):
except (KeyboardInterrupt, SystemExit):
raise
except:
if self.logger:
self.logger.exception(
'Unexpected error while processing request %r',
get_service_name(ro_request)
)

return remoting.Response(self.buildErrorResponse(ro_request),
status=remoting.STATUS_ERROR)


def get_service_name(ro_request):
"""
Returns the full service name of a RemoteObject request.
"""
service_name = ro_request.operation

if hasattr(ro_request, 'destination') and ro_request.destination:
service_name = '%s.%s' % (ro_request.destination, service_name)

return service_name

0 comments on commit cb2377a

Please sign in to comment.