def _traced_cmd(self, method_name, *args, **kwargs): """Run and trace the given command. Any pymemcache exception is caught and span error information is set. The exception is then reraised for the application to handle appropriately. Relevant tags are set in the span. """ method = getattr(self.__wrapped__, method_name) p = Pin.get_from(self) # if the pin does not exist or is not enabled, shortcut if not p or not p.enabled(): return method(*args, **kwargs) with p.tracer.trace( memcachedx.CMD, service=p.service, resource=method_name, span_type=memcachedx.TYPE, ) as span: # try to set relevant tags, catch any exceptions so we don't mess # with the application try: span.set_tags(p.tags) vals = _get_query_string(args) query = "{}{}{}".format(method_name, " " if vals else "", vals) span.set_tag(memcachedx.QUERY, query) except Exception: log.debug("Error setting relevant pymemcache tags") try: return method(*args, **kwargs) except ( MemcacheClientError, MemcacheServerError, MemcacheUnknownCommandError, MemcacheUnknownError, MemcacheIllegalInputError, ): (typ, val, tb) = sys.exc_info() span.set_exc_info(typ, val, tb) reraise(typ, val, tb)
def test_reraise(self): # ensure the `raise` function is Python 2/3 compatible with pytest.raises(Exception) as ex: try: raise Exception("Ouch!") except Exception: # original exception we want to re-raise (typ, val, tb) = sys.exc_info() try: # this exception doesn't allow a re-raise, and we need # to use the previous one collected via `exc_info()` raise Exception("Obfuscate!") except Exception: pass # this call must be Python 2 and 3 compatible raise reraise(typ, val, tb) assert ex.value.args[0] == "Ouch!"
def test_reraise(self): # ensure the `raise` function is Python 2/3 compatible with assert_raises(Exception) as ex: try: raise Exception('Ouch!') except Exception as e: # original exception we want to re-raise (typ, val, tb) = sys.exc_info() try: # this exception doesn't allow a re-raise, and we need # to use the previous one collected via `exc_info()` raise Exception('Obfuscate!') except Exception: pass # this call must be Python 2 and 3 compatible raise reraise(typ, val, tb) eq_(ex.exception.args[0], 'Ouch!')