Skip to content

Commit

Permalink
fix api method name logging, also don't dump all quiz to log
Browse files Browse the repository at this point in the history
  • Loading branch information
andrvb committed Mar 22, 2015
1 parent 0e32c01 commit 2963add
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions stepic_plugins/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import threading
import time
import uuid

import structlog

from base64 import b64encode
from functools import wraps

import structlog
from oslo import messaging
from oslo.config import cfg

Expand All @@ -35,10 +33,19 @@ def __new__(mcs, name, bases, dct):

@classmethod
def log_method_wrapper(mcs, method):
def compact(value):
if isinstance(value, (tuple, list)):
return type(value)([compact(v) for v in value])
elif isinstance(value, dict):
return {k: compact(v) for k, v in value.items()}
elif isinstance(value, str):
return value if len(value) < 100 else value[:100] + '...'
return value

@wraps(method)
def wrapper(self, *args, **kwargs):
call_id = str(uuid.uuid4())[:8]
log = logger.bind(method=method.__name__, args=args, kwargs=kwargs,
log = logger.bind(method=method.__name__, args=compact(args), kwargs=compact(kwargs),
call_id=call_id, version=self.target.version)
if self.target.namespace:
log = log.bind(namespace=self.target.namespace)
Expand All @@ -51,13 +58,26 @@ def wrapper(self, *args, **kwargs):
finally:
duration = int((time.time() - start_time) * 1000) / 1000
log.info("RPC method call finished", duration=duration)

return wrapper


def expected_exceptions(*exceptions):
def outer(func):
@wraps(func)
@messaging.expected_exceptions(*exceptions)
def inner(*args, **kwargs):
return func(*args, **kwargs)

return inner

return outer


class QuizEndpoint(metaclass=LoggedEndpointMetaclass):
target = messaging.Target(namespace='quiz', version='0.1')

@messaging.expected_exceptions(KeyError, FormatError)
@expected_exceptions(KeyError, FormatError)
def _quiz_instance(self, ctxt):
quiz_class = load_by_name(ctxt['name'])
return quiz_class(ctxt['source'],
Expand All @@ -69,15 +89,15 @@ def ping(self, ctxt, msg):
def validate_source(self, ctxt):
self._quiz_instance(ctxt)

@messaging.expected_exceptions(PluginError)
@expected_exceptions(PluginError)
def async_init(self, ctxt):
return self._quiz_instance(ctxt).async_init()

@messaging.expected_exceptions(PluginError)
@expected_exceptions(PluginError)
def generate(self, ctxt):
return self._quiz_instance(ctxt).generate()

@messaging.expected_exceptions(FormatError)
@expected_exceptions(FormatError)
def clean_reply(self, ctxt, reply, dataset):
return self._quiz_instance(ctxt).clean_reply(reply, dataset=dataset)

Expand Down

0 comments on commit 2963add

Please sign in to comment.