|
| 1 | +import functools |
| 2 | +import sys |
| 3 | + |
| 4 | +import pyformance.registry as global_registry |
| 5 | + |
| 6 | + |
| 7 | +def get_qualname(obj): |
| 8 | + if sys.version_info[0] > 2: |
| 9 | + return obj.__qualname__ |
| 10 | + return obj.__name__ |
| 11 | + |
| 12 | + |
| 13 | +def count_calls(original_func=None, registry=None, tags=None): |
| 14 | + """ |
| 15 | + Decorator to track the number of times a function is called. |
| 16 | +
|
| 17 | + :param original_func: the function to be decorated |
| 18 | + :type original_func: C{func} |
| 19 | +
|
| 20 | + :param registry: the registry in which to create the meter |
| 21 | +
|
| 22 | + :param tags: tags attached to the timer (e.g. {'region': 'us-west-1'}) |
| 23 | + :type tags: C{dict} |
| 24 | +
|
| 25 | + :return: the decorated function |
| 26 | + :rtype: C{func} |
| 27 | + """ |
| 28 | + |
| 29 | + def _decorate(fn): |
| 30 | + @functools.wraps(fn) |
| 31 | + def wrapper(*args, **kwargs): |
| 32 | + function_name = get_qualname(fn) |
| 33 | + metric_name = "%s_calls" % function_name |
| 34 | + |
| 35 | + _registry = registry or global_registry |
| 36 | + _histogram = _registry.counter(metric_name, tags).inc() |
| 37 | + |
| 38 | + return fn(*args, **kwargs) |
| 39 | + |
| 40 | + return wrapper |
| 41 | + |
| 42 | + if original_func: |
| 43 | + return _decorate(original_func) |
| 44 | + |
| 45 | + return _decorate |
| 46 | + |
| 47 | + |
| 48 | +def meter_calls(original_func=None, registry=None, tags=None): |
| 49 | + """ |
| 50 | + Decorator to the rate at which a function is called. |
| 51 | +
|
| 52 | + :param original_func: the function to be decorated |
| 53 | + :type original_func: C{func} |
| 54 | +
|
| 55 | + :param registry: the registry in which to create the meter |
| 56 | +
|
| 57 | + :param tags: tags attached to the timer (e.g. {'region': 'us-west-1'}) |
| 58 | + :type tags: C{dict} |
| 59 | +
|
| 60 | + :return: the decorated function |
| 61 | + :rtype: C{func} |
| 62 | + """ |
| 63 | + |
| 64 | + def _decorate(fn): |
| 65 | + @functools.wraps(fn) |
| 66 | + def wrapper(*args, **kwargs): |
| 67 | + function_name = get_qualname(fn) |
| 68 | + metric_name = "%s_calls" % function_name |
| 69 | + |
| 70 | + _registry = registry or global_registry |
| 71 | + _histogram = _registry.meter(metric_name, tags).mark() |
| 72 | + |
| 73 | + return fn(*args, **kwargs) |
| 74 | + |
| 75 | + return wrapper |
| 76 | + |
| 77 | + if original_func: |
| 78 | + return _decorate(original_func) |
| 79 | + |
| 80 | + return _decorate |
| 81 | + |
| 82 | + |
| 83 | +def hist_calls(original_func=None, registry=None, tags=None): |
| 84 | + """ |
| 85 | + Decorator to check the distribution of return values of a function. |
| 86 | +
|
| 87 | + :param original_func: the function to be decorated |
| 88 | + :type original_func: C{func} |
| 89 | +
|
| 90 | + :param registry: the registry in which to create the histogram |
| 91 | +
|
| 92 | + :param tags: tags attached to the timer (e.g. {'region': 'us-west-1'}) |
| 93 | + :type tags: C{dict} |
| 94 | +
|
| 95 | + :return: the decorated function |
| 96 | + :rtype: C{func} |
| 97 | + """ |
| 98 | + def _decorate(fn): |
| 99 | + @functools.wraps(fn) |
| 100 | + def wrapper(*args, **kwargs): |
| 101 | + function_name = get_qualname(fn) |
| 102 | + metric_name = "%s_calls" % function_name |
| 103 | + |
| 104 | + _registry = registry or global_registry |
| 105 | + _histogram = _registry.histogram(metric_name, tags) |
| 106 | + |
| 107 | + rtn = fn(*args, **kwargs) |
| 108 | + if type(rtn) in (int, float): |
| 109 | + _histogram.update(rtn) |
| 110 | + return rtn |
| 111 | + |
| 112 | + return wrapper |
| 113 | + |
| 114 | + if original_func: |
| 115 | + return _decorate(original_func) |
| 116 | + |
| 117 | + return _decorate |
| 118 | + |
| 119 | + |
| 120 | +def time_calls(original_func=None, registry=None, tags=None): |
| 121 | + """ |
| 122 | + Decorator to time the execution of the function. |
| 123 | +
|
| 124 | + :param original_func: the function to be decorated |
| 125 | + :type original_func: C{func} |
| 126 | +
|
| 127 | + :param registry: the registry in which to create the timer |
| 128 | +
|
| 129 | + :param tags: tags attached to the timer (e.g. {'region': 'us-west-1'}) |
| 130 | + :type tags: C{dict} |
| 131 | +
|
| 132 | + :return: the decorated function |
| 133 | + :rtype: C{func} |
| 134 | + """ |
| 135 | + def _decorate(fn): |
| 136 | + @functools.wraps(fn) |
| 137 | + def wrapper(*args, **kwargs): |
| 138 | + function_name = get_qualname(fn) |
| 139 | + metric_name = "%s_calls" % function_name |
| 140 | + |
| 141 | + _registry = registry or global_registry |
| 142 | + _timer = _registry.timer(metric_name, tags) |
| 143 | + |
| 144 | + with _timer.time(fn=function_name): |
| 145 | + return fn(*args, **kwargs) |
| 146 | + |
| 147 | + return wrapper |
| 148 | + |
| 149 | + if original_func: |
| 150 | + return _decorate(original_func) |
| 151 | + |
| 152 | + return _decorate |
0 commit comments