add(plugins): Added a statsd plugin for lemur (#1189)
This commit is contained in:
parent
8ca4f730e8
commit
a9baaf4da4
1
lemur/plugins/lemur_statsd/docs/requirements.txt
Normal file
1
lemur/plugins/lemur_statsd/docs/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
datadog==0.14.0
|
4
lemur/plugins/lemur_statsd/lemur_statsd/__init__.py
Normal file
4
lemur/plugins/lemur_statsd/lemur_statsd/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
try:
|
||||
VERSION = __import__('pkg_resources').get_distribution(__name__).version
|
||||
except Exception as e:
|
||||
VERSION = 'Unknown'
|
45
lemur/plugins/lemur_statsd/lemur_statsd/plugin.py
Normal file
45
lemur/plugins/lemur_statsd/lemur_statsd/plugin.py
Normal file
@ -0,0 +1,45 @@
|
||||
import lemur_statsd as plug
|
||||
|
||||
from flask import current_app
|
||||
from lemur.plugins.bases.metric import MetricPlugin
|
||||
from datadog import DogStatsd
|
||||
|
||||
|
||||
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
|
24
lemur/plugins/lemur_statsd/setup.py
Normal file
24
lemur/plugins/lemur_statsd/setup.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""Basic package information"""
|
||||
from __future__ import absolute_import
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
install_requires = [
|
||||
'lemur',
|
||||
'datadog'
|
||||
]
|
||||
|
||||
setup(
|
||||
name='lemur_statsd',
|
||||
version='1.0.0',
|
||||
author='Cloudflare Security Engineering',
|
||||
author_email='',
|
||||
include_package_data=True,
|
||||
packages=find_packages(),
|
||||
zip_safe=False,
|
||||
install_requires=install_requires,
|
||||
entry_points={
|
||||
'lemur.plugins': [
|
||||
'statsd = lemur_statsd.plugin:StatsdMetricPlugin',
|
||||
]
|
||||
}
|
||||
)
|
Loading…
Reference in New Issue
Block a user