def wrap_client_index_method(wrapped, instance, args, kwargs): # elasticsearch-py 7.5.1 changed the order of arguments for client methods, # so to be safe we need to inspect the wrapped method's positional # arguments to see if we should pull it from there if "index" in kwargs: index = kwargs["index"] else: unwrapped = unwrap_decorators(wrapped) pos_args = get_pos_args(unwrapped) try: index_index = pos_args.index("index") except ValueError: # pragma: no cover # This guards against the method not accepting an 'index' argument # but they all do - for now index = "" else: try: index = args[index_index - 1] # subtract 'self' except IndexError: index = "" if isinstance(index, (list, tuple)): index = ",".join(index) if index == "": index = "Unknown" index = index.title() camel_name = "".join(c.title() for c in wrapped.__name__.split("_")) operation = "Elasticsearch/{}/{}".format(index, camel_name) tracked_request = TrackedRequest.instance() with tracked_request.span(operation=operation, ignore_children=True): return wrapped(*args, **kwargs)
def test_get_pos_args_kwargs(): def foo(bar, baz=None): pass assert get_pos_args(foo) == ["bar", "baz"]