|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import time |
| 3 | + |
| 4 | +from abc import abstractproperty |
| 5 | +from collections import defaultdict |
| 6 | +from threading import current_thread |
| 7 | +from gevent import GreenletExit |
| 8 | + |
| 9 | +from amplify.agent.common.context import context |
| 10 | + |
| 11 | +__author__ = "Mike Belov" |
| 12 | +__copyright__ = "Copyright (C) Nginx, Inc. All rights reserved." |
| 13 | +__license__ = "" |
| 14 | +__maintainer__ = "Mike Belov" |
| 15 | +__email__ = "dedm@nginx.com" |
| 16 | + |
| 17 | + |
| 18 | +class AbstractCollector(object): |
| 19 | + """ |
| 20 | + Abstract data collector |
| 21 | + Runs in a thread and collects specific data |
| 22 | + """ |
| 23 | + short_name = None |
| 24 | + |
| 25 | + zero_counters = tuple() |
| 26 | + |
| 27 | + def __init__(self, object=None, interval=None): |
| 28 | + self.object = object |
| 29 | + self.in_container = self.object.in_container |
| 30 | + self.interval = interval |
| 31 | + self.previous_counters = defaultdict(dict) # for deltas |
| 32 | + self.current_counters = defaultdict(int) # for aggregating |
| 33 | + self.current_latest = defaultdict(int) # for latest |
| 34 | + self.current_gauges = defaultdict(lambda: defaultdict(float)) # gauges |
| 35 | + self.methods = set() |
| 36 | + |
| 37 | + # stamp store organized by type - metric_name - stamp |
| 38 | + self.current_stamps = defaultdict(lambda: defaultdict(time.time)) |
| 39 | + |
| 40 | + def init_counters(self, counters=None): |
| 41 | + """ |
| 42 | + Helper function for sending 0 values when no data is found. |
| 43 | +
|
| 44 | + :param counters: Iterable String values of names of counters to init as |
| 45 | + 0 (default is self.zero_counters) |
| 46 | + """ |
| 47 | + counters = counters or self.zero_counters |
| 48 | + for counter in counters: |
| 49 | + self.object.statsd.incr(counter, value=0) |
| 50 | + |
| 51 | + def run(self): |
| 52 | + """ |
| 53 | + Common collector cycle |
| 54 | +
|
| 55 | + 1. Collect data |
| 56 | + 2. Sleep |
| 57 | + 3. Stop if object stopped |
| 58 | + """ |
| 59 | + # TODO: Standardize this with Managers. |
| 60 | + current_thread().name = self.short_name |
| 61 | + context.setup_thread_id() |
| 62 | + |
| 63 | + try: |
| 64 | + while True: |
| 65 | + context.inc_action_id() |
| 66 | + if self.object.running: |
| 67 | + self._collect() |
| 68 | + self._sleep() |
| 69 | + else: |
| 70 | + break |
| 71 | + |
| 72 | + # Since kill signals won't work, we raise it ourselves. |
| 73 | + raise GreenletExit |
| 74 | + except GreenletExit: |
| 75 | + context.log.debug( |
| 76 | + '%s collector for %s received exit signal' % ( |
| 77 | + self.__class__.__name__, |
| 78 | + self.object.definition_hash |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + context.teardown_thread_id() |
| 83 | + |
| 84 | + context.log.debug( |
| 85 | + '%s collector for %s teardown complete' % ( |
| 86 | + self.__class__.__name__, |
| 87 | + self.object.definition_hash |
| 88 | + ) |
| 89 | + ) |
| 90 | + except: |
| 91 | + context.log.error( |
| 92 | + '%s collector run failed' % self.object.definition_hash, |
| 93 | + exc_info=True |
| 94 | + ) |
| 95 | + raise |
| 96 | + |
| 97 | + def register(self, *methods): |
| 98 | + """ |
| 99 | + Register methods for collecting |
| 100 | + """ |
| 101 | + self.methods.update(methods) |
| 102 | + |
| 103 | + def _collect(self): |
| 104 | + """ |
| 105 | + Wrapper for actual collect process. Handles memory reporting before |
| 106 | + and after collect process. |
| 107 | + """ |
| 108 | + start_time = time.time() |
| 109 | + try: |
| 110 | + self.collect() |
| 111 | + finally: |
| 112 | + end_time = time.time() |
| 113 | + context.log.debug( |
| 114 | + '%s collect in %.3f' % ( |
| 115 | + self.object.definition_hash, |
| 116 | + end_time - start_time |
| 117 | + ) |
| 118 | + ) |
| 119 | + |
| 120 | + def _sleep(self): |
| 121 | + time.sleep(self.interval) |
| 122 | + |
| 123 | + def collect(self, *args, **kwargs): |
| 124 | + if self.zero_counters: |
| 125 | + self.init_counters() |
| 126 | + |
| 127 | + for method in self.methods: |
| 128 | + try: |
| 129 | + method(*args, **kwargs) |
| 130 | + except Exception as e: |
| 131 | + self.handle_exception(method, e) |
| 132 | + |
| 133 | + def handle_exception(self, method, exception): |
| 134 | + context.log.error('%s failed to collect: %s raised %s%s' % ( |
| 135 | + self.short_name, method.__name__, exception.__class__.__name__, |
| 136 | + ' (in container)' if self.in_container else '' |
| 137 | + )) |
| 138 | + context.log.debug('additional info:', exc_info=True) |
| 139 | + |
| 140 | + def increment_counters(self): |
| 141 | + """ |
| 142 | + Increment counter method that takes the "current_values" dictionary of |
| 143 | + metric name - value pairs increments statsd appropriately based on |
| 144 | + previous values. |
| 145 | + """ |
| 146 | + for metric_name, value in self.current_counters.items(): |
| 147 | + prev_stamp, prev_value = self.previous_counters.get( |
| 148 | + metric_name, (None, None) |
| 149 | + ) |
| 150 | + stamp = self.current_stamps['counters'][metric_name] |
| 151 | + value = self.current_counters[metric_name] |
| 152 | + |
| 153 | + if isinstance(prev_value, (int, float, complex)) and prev_stamp: |
| 154 | + value_delta = value - prev_value |
| 155 | + if value_delta >= 0: |
| 156 | + # Only increment our statsd client and send data to backend |
| 157 | + # if calculated value is non-negative. |
| 158 | + self.object.statsd.incr( |
| 159 | + metric_name, value_delta, stamp=stamp |
| 160 | + ) |
| 161 | + |
| 162 | + # Re-base the calculation for next collect |
| 163 | + self.previous_counters[metric_name] = (stamp, value) |
| 164 | + |
| 165 | + # reset counter stores |
| 166 | + self.current_counters = defaultdict(int) |
| 167 | + if self.current_stamps['counters']: |
| 168 | + del self.current_stamps['counters'] |
| 169 | + |
| 170 | + def aggregate_counters(self, counted_vars, stamp=None): |
| 171 | + """ |
| 172 | + Aggregate several counter metrics from multiple places and store their |
| 173 | + sums in a metric_name-value store. |
| 174 | +
|
| 175 | + :param counted_vars: Dict Metric_name - Value dict |
| 176 | + :param stamp: Int Timestamp of Plus collect |
| 177 | + """ |
| 178 | + for metric_name, value in counted_vars.items(): |
| 179 | + self.current_counters[metric_name] += value |
| 180 | + if stamp: |
| 181 | + self.current_stamps['counters'][metric_name] = stamp |
| 182 | + |
| 183 | + def finalize_latest(self): |
| 184 | + """ |
| 185 | + Go through stored latest variables and send them to the object statsd. |
| 186 | + """ |
| 187 | + for metric_name, value in self.current_latest.items(): |
| 188 | + stamp = self.current_stamps['latest'][metric_name] |
| 189 | + self.object.statsd.latest(metric_name, value, stamp) |
| 190 | + |
| 191 | + # reset latest store |
| 192 | + self.current_latest = defaultdict(int) |
| 193 | + if self.current_stamps['latest']: |
| 194 | + del self.current_stamps['latest'] |
| 195 | + |
| 196 | + def aggregate_latest(self, latest_vars, stamp=None): |
| 197 | + """ |
| 198 | + Aggregate several latest metrics from multiple places and store the |
| 199 | + final value in a metric_name-value store. |
| 200 | +
|
| 201 | + :param latest_vars: Dict Metric_name - Value dict |
| 202 | + :param stamp: Int Timestamp of collect |
| 203 | + """ |
| 204 | + for metric_name in latest_vars: |
| 205 | + self.current_latest[metric_name] += 1 |
| 206 | + if stamp: |
| 207 | + self.current_stamps['latest'][metric_name] = stamp |
| 208 | + |
| 209 | + def aggregate_gauges(self, gauge_vars, stamp=None): |
| 210 | + """ |
| 211 | + Aggregate several gauge metrics from multiple sources. Track their |
| 212 | + values until collection/finalize and then send the cumalitive to |
| 213 | + statsd. |
| 214 | +
|
| 215 | + Example gauge_vars: |
| 216 | + { |
| 217 | + 'gauge_name': { |
| 218 | + 'source': value |
| 219 | + 'source2': value |
| 220 | + } |
| 221 | + } |
| 222 | +
|
| 223 | + :param gauge_vars: Dict Metric_Name - Source - Value dict |
| 224 | + :param stamp: Int Timestamp of collect |
| 225 | + """ |
| 226 | + for metric_name, value_map in gauge_vars.items(): |
| 227 | + for source, value in value_map.items(): |
| 228 | + # override current gauge from source with the passed value |
| 229 | + self.current_gauges[metric_name][source] = value |
| 230 | + |
| 231 | + # save this latest stamp |
| 232 | + if stamp: |
| 233 | + self.current_stamps['gauges'][metric_name] = stamp |
| 234 | + |
| 235 | + def finalize_gauges(self): |
| 236 | + """ |
| 237 | + Iterate through the stored gauges in self.current_gauges, sum them, and |
| 238 | + then send them to statsd for reporting. |
| 239 | + """ |
| 240 | + for metric_name, value_map in self.current_gauges.items(): |
| 241 | + total_gauge = 0 |
| 242 | + for source, value in value_map.items(): |
| 243 | + total_gauge += value |
| 244 | + |
| 245 | + self.object.statsd.gauge( |
| 246 | + metric_name, |
| 247 | + total_gauge, |
| 248 | + stamp=self.current_stamps['gauges'][metric_name] |
| 249 | + ) |
| 250 | + |
| 251 | + # reset gauge stores |
| 252 | + self.current_gauges = defaultdict(lambda: defaultdict(int)) |
| 253 | + if self.current_stamps['gauges']: |
| 254 | + del self.current_stamps['gauges'] |
| 255 | + |
| 256 | + |
| 257 | +class AbstractMetaCollector(AbstractCollector): |
| 258 | + default_meta = abstractproperty() |
| 259 | + |
| 260 | + def __init__(self, **kwargs): |
| 261 | + super(AbstractMetaCollector, self).__init__(**kwargs) |
| 262 | + self.meta = {} |
| 263 | + |
| 264 | + def collect(self, *args): |
| 265 | + self.meta.update(self.default_meta) |
| 266 | + super(AbstractMetaCollector, self).collect(*args) |
| 267 | + self.object.metad.meta(self.meta) |
| 268 | + |
| 269 | + |
| 270 | +class AbstractMetricsCollector(AbstractCollector): |
| 271 | + status_metric_key = None |
| 272 | + |
| 273 | + def status_update(self): |
| 274 | + if hasattr(self, 'object') and self.status_metric_key is not None: |
| 275 | + self.object.statsd.object_status(self.status_metric_key) |
| 276 | + |
| 277 | + def collect(self, *args, **kwargs): |
| 278 | + self.status_update() |
| 279 | + super(AbstractMetricsCollector, self).collect(*args, **kwargs) |
0 commit comments