def _urllib_open_wrapper(func, *args, **keywords): """ Wraps urllib.request.url_open """ if not _should_be_instrumented( state='external', enable_if='web', disable_if='model'): return func(*args, **keywords) url = get_parameter(1, 'fullurl', *args, **keywords) if hasattr(url, 'get_full_url'): url = url.get_full_url() scheme = url.split(':')[0] if ':' in url else 'unknown' Timing.push_timer() try: context.push_state('external') telemetry.count('external.{}.requests'.format(scheme)) a = func(*args, **keywords) if a.getcode(): # Not meaningful for ftp etc telemetry.count('external.{}.status.%ixx'.format(scheme) % floor(a.getcode() / 100)) except: telemetry.count('external.{}.errors'.format(scheme)) raise finally: context.pop_state('external') elapsed, _ = Timing.pop_timer() telemetry.record('external.{}.response.latency'.format(scheme), elapsed) # Return a wrapped object so we can time subsequent read, readline etc calls return _response_wrapper(scheme, a)
def _wrapped_call(metric, func, *args, **keywords): """ Times and executes arbitrary method """ state = 'external' if not _should_be_instrumented(state, enable_if='web', disable_if='model'): return func(*args, **keywords) Timing.push_timer() try: context.push_state(state) return func(*args, **keywords) finally: context.pop_state(state) elapsed, _ = Timing.pop_timer() telemetry.record(metric, elapsed)
def _wrapped_call(metric, func, *args, **keywords): """ Times and executes arbitrary method """ state = 'external' if not _should_be_instrumented(state, enable_if='web', disable_if='model'): return func(*args, **keywords) Timing.push_timer() try: context.push_state(state) return func(*args, **keywords) finally: context.pop_state(state) elapsed, _ = Timing.pop_timer() telemetry.record(metric, elapsed)
def _session_send_wrapper(func, *args, **keywords): if not _should_be_instrumented(state="external", enable_if="web", disable_if="model"): return func(*args, **keywords) telemetry.count("external.http.requests") Timing.push_timer() try: context.push_state("external") a = func(*args, **keywords) telemetry.count("external.http.status.%ixx" % floor(a.status_code / 100)) return a except: telemetry.count("external.http.errors") raise finally: context.pop_state("external") elapsed, _ = Timing.pop_timer() telemetry.record("external.http.response.latency", elapsed)
def _session_send_wrapper(func, *args, **keywords): if not _should_be_instrumented( state='external', enable_if='web', disable_if='model'): return func(*args, **keywords) telemetry.count('external.http.requests') Timing.push_timer() try: context.push_state('external') a = func(*args, **keywords) telemetry.count('external.http.status.%ixx' % floor(a.status_code / 100)) return a except: telemetry.count('external.http.errors') raise finally: context.pop_state('external') elapsed, _ = Timing.pop_timer() telemetry.record('external.http.response.latency', elapsed)