所以我试图在使用nameko时通过RPC调用添加装饰器。
我的代码看起来像这样:
@rpc
@validate_sort_by(['fruit_name','tree_height'])
def get_trees_list(self, farm_name, sort_by=None):
# A function that returns a list sorted by sort_by variable
validate_sort_by
验证参数的给定值sort_by
是否是作为参数传递给装饰器的列表之一。下面是代码:
def validate_sort_by(provided_sort_by_list):
def decorator(function):
@wraps(function)
def wrapper(*args, **kwargs):
sort_by_passed = args[1]
if not sort in provided_sort_by_list:
raise BadRequestError('Wrong sort by column')
return function(*args, **kwargs)
return wrapper
return decorator
当我调用 rpc 时,就结果而言一切正常。但是我已经启用了 rpc 日志记录nameko-tracer
。该模块引发错误:
Failed to log entrypoint trace
sorts-598494557-ftdqz sorts Traceback (most recent call last):
sorts-598494557-ftdqz sorts File "/usr/local/lib/python3.6/site-packages/nameko_tracer/dependency.py", line 64, in worker_setup
sorts-598494557-ftdqz sorts extra=extra)
sorts-598494557-ftdqz sorts File "/usr/local/lib/python3.6/logging/__init__.py", line 1636, in info
sorts-598494557-ftdqz sorts self.log(INFO, msg, *args, **kwargs)
sorts-598494557-ftdqz sorts File "/usr/local/lib/python3.6/logging/__init__.py", line 1673, in log
sorts-598494557-ftdqz sorts msg, kwargs = self.process(msg, kwargs)
sorts-598494557-ftdqz sorts File "/usr/local/lib/python3.6/site-packages/nameko_tracer/adapters.py", line 54, in process
sorts-598494557-ftdqz sorts call_args, call_args_redacted = self.get_call_args(worker_ctx)
sorts-598494557-ftdqz sorts File "/usr/local/lib/python3.6/site-packages/nameko_tracer/adapters.py", line 98, in get_call_args
sorts-598494557-ftdqz sorts del call_args['self']
sorts-598494557-ftdqz sorts KeyError: 'self'
现在我确实浏览了库代码的确切文件行。这里的问题似乎是它正在获取错误的 args dict,这可能是包装函数的问题。我能做些什么来解决这个错误?