From 2963add997ecdc9a2176c00e140b4d7c8f8ccef4 Mon Sep 17 00:00:00 2001 From: Andrey Balandin Date: Sun, 22 Mar 2015 03:53:49 +0300 Subject: [PATCH] fix api method name logging, also don't dump all quiz to log --- stepic_plugins/rpc.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/stepic_plugins/rpc.py b/stepic_plugins/rpc.py index 1424dfe..fd1f911 100644 --- a/stepic_plugins/rpc.py +++ b/stepic_plugins/rpc.py @@ -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 @@ -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) @@ -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'], @@ -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)