MCPcopy Create free account
hub / github.com/StackStorm/st2 / Timer

Class Timer

st2common/st2common/metrics/base.py:95–142  ·  view source on GitHub ↗

Timer context manager for easily sending timer statistics.

Source from the content-addressed store, hash-verified

93
94
95class Timer(object):
96 """
97 Timer context manager for easily sending timer statistics.
98 """
99
100 def __init__(self, key, include_parameter=False):
101 check_key(key)
102
103 self.key = key
104 self._metrics = get_driver()
105 self._include_parameter = include_parameter
106 self._start_time = None
107
108 def send_time(self, key=None):
109 """
110 Send current time from start time.
111 """
112 time_delta = self.get_time_delta()
113
114 if key:
115 check_key(key)
116 self._metrics.time(key, time_delta.total_seconds())
117 else:
118 self._metrics.time(self.key, time_delta.total_seconds())
119
120 def get_time_delta(self):
121 """
122 Get current time delta.
123 """
124
125 return get_datetime_utc_now() - self._start_time
126
127 def __enter__(self):
128 self._start_time = get_datetime_utc_now()
129 return self
130
131 def __exit__(self, *args):
132 self.send_time()
133
134 def __call__(self, func):
135 @wraps(func)
136 def wrapper(*args, **kw):
137 with self as metrics_timer:
138 if self._include_parameter:
139 kw["metrics_timer"] = metrics_timer
140 return func(*args, **kw)
141
142 return wrapper
143
144
145class Counter(object):

Callers 15

processMethod · 0.90
_do_runMethod · 0.90
_update_statusMethod · 0.90
publishMethod · 0.90
publish_createMethod · 0.90
publish_updateMethod · 0.90
publish_deleteMethod · 0.90
publish_stateMethod · 0.90
update_executionFunction · 0.90
register_triggersFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected