class Stats: MESSAGE_FLUSHED_METRIC = "message_flushed" TASK_EXECUTED_TIME_METRIC = "task_executed" def __init__(self, configuration: Configuration) -> None: jsonschema.validate( configuration, { "type": "object", "properties": { "host": {"type": "string"}, "port": {"type": "integer"}, "message_sampling_rate": {"type": "number", "minimum": 0.0, "maximum": 1.0}, "task_sampling_rate": {"type": "number", "minimum": 0.0, "maximum": 1.0}, }, "required": ["host", "port"], }, ) self.__dogstatsd = DogStatsd( host=configuration["host"], port=configuration["port"], namespace=METRIC_PREFIX, ) self.__message_sampling_rate: float = configuration.get("message_sampling_rate", 1.0) self.__task_sampling_rate: float = configuration.get("task_sampling_rate", 1.0) def message_flushed(self, start: float) -> None: self.__record_simple_interval( start, self.MESSAGE_FLUSHED_METRIC, self.__message_sampling_rate ) def task_executed(self, start: float, tasktype: str) -> None: tag = "%s:%s" % ("tasktype", tasktype) self.__record_simple_interval( start, self.TASK_EXECUTED_TIME_METRIC, self.__task_sampling_rate, [tag] ) def __record_simple_interval( self, start: float, metric: str, sample_rate: float, tags: list = None ) -> None: now = time.time() duration = int((now - start) * 1000) try: self.__dogstatsd.timing( metric, duration, tags=tags, sample_rate=sample_rate ) except Exception as e: logger.exception(e)
class StatsdMetricPlugin(MetricPlugin): title = "Statsd" slug = "statsd-metrics" description = "Adds support for sending metrics to Statsd" version = plug.VERSION def __init__(self): host = current_app.config.get("STATSD_HOST") port = current_app.config.get("STATSD_PORT") prefix = current_app.config.get("STATSD_PREFIX") self.statsd = DogStatsd(host=host, port=port, namespace=prefix) def submit(self, metric_name, metric_type, metric_value, metric_tags=None, options=None): valid_types = ["COUNTER", "GAUGE", "TIMER"] tags = [] if metric_type.upper() not in valid_types: raise Exception( "Invalid Metric Type for Statsd, '{metric}' choose from: {options}" .format(metric=metric_type, options=",".join(valid_types))) if metric_tags: if not isinstance(metric_tags, dict): raise Exception( "Invalid Metric Tags for Statsd: Tags must be in dict format" ) else: tags = map(lambda e: "{0}:{1}".format(*e), metric_tags.items()) if metric_type.upper() == "COUNTER": self.statsd.increment(metric_name, metric_value, tags) elif metric_type.upper() == "GAUGE": self.statsd.gauge(metric_name, metric_value, tags) elif metric_type.upper() == "TIMER": self.statsd.timing(metric_name, metric_value, tags) return
class StatsdMetricPlugin(MetricPlugin): title = 'Statsd' slug = 'statsd-metrics' description = 'Adds support for sending metrics to Statsd' version = plug.VERSION def __init__(self): host = current_app.config.get('STATSD_HOST') port = current_app.config.get('STATSD_PORT') prefix = current_app.config.get('STATSD_PREFIX') self.statsd = DogStatsd(host=host, port=port, namespace=prefix) def submit(self, metric_name, metric_type, metric_value, metric_tags=None, options=None): valid_types = ['COUNTER', 'GAUGE', 'TIMER'] tags = [] if metric_type.upper() not in valid_types: raise Exception( "Invalid Metric Type for Statsd, '{metric}' choose from: {options}".format( metric=metric_type, options=','.join(valid_types) ) ) if metric_tags: if not isinstance(metric_tags, dict): raise Exception("Invalid Metric Tags for Statsd: Tags must be in dict format") else: tags = map(lambda e: "{0}:{1}".format(*e), metric_tags.items()) if metric_type.upper() == 'COUNTER': self.statsd.increment(metric_name, metric_value, tags) elif metric_type.upper() == 'GAUGE': self.statsd.gauge(metric_name, metric_value, tags) elif metric_type.upper() == 'TIMER': self.statsd.timing(metric_name, metric_value, tags) return