Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from myoung34/datadog
Browse files Browse the repository at this point in the history
Add datadog statsd emitter
  • Loading branch information
myoung34 authored Feb 24, 2020
2 parents 16ebb07 + f7bd51b commit 52a1357
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The Tilt supports writing to a google doc which you could use with something lik

* Webhooks
* InfluxDB
* Datadog (dogstatsd)


## Usage ##
Expand All @@ -40,6 +41,11 @@ port = 80
database = tilty
gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}}
temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}}
[datadog]
# Note: make sure that the dd agent has DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
host = statsdhost.corp.com
port = 8125
EOF
```

Expand Down
30 changes: 29 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "Tilty"
version = "0.0.1"
version = "0.3.0"
description = "A pluggable system to receive and transmit bluetooth events from the Tilt Hydrometer"
authors = ["Marcus Young <[email protected]>"]
license = "MIT"
Expand All @@ -12,6 +12,7 @@ pybluez = "^0.22.0"
requests = "^2.22"
jinja2 = "^2.11.1"
influxdb = "^5.2.3"
datadog = "^0.34.1"

[tool.poetry.dev-dependencies]
flake8 = "^3.7"
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ certifi==2019.11.28
chardet==3.0.4
Click==7.0
coverage==5.0
datadog==0.34.1
decorator==4.4.1
entrypoints==0.3
filelock==3.0.12
flake8==3.7.9
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
long_description=long_description,
long_description_content_type="text/markdown",
py_modules=['tilty', 'blescan'],
version='0.2.2',
version='0.3.0',
packages=find_packages(exclude=['tests*']),
install_requires=[
'Click',
'datadog',
'influxdb',
'Jinja2',
'pybluez',
Expand Down
5 changes: 5 additions & 0 deletions tests/mock_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def __getitem__(self, key):
'gravity_payload_template': 'gravity,color={{ color }} value={{ gravity }} {{timestamp}}', # noqa
'temperature_payload_template': 'temperature,scale=fahrenheit,color={{ color }} value={{ temp }} {{timestamp}}', # noqa
}
if self.section == 'datadog':
return {
'host': 'http://api.datadog.com',
'port': '8120',
}
return None

def has_section(self, *args, **kwargs):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_datadog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
from unittest import mock

from tilty.emitters import datadog


@mock.patch('tilty.emitters.datadog.statsd')
@mock.patch('tilty.emitters.datadog.initialize')
def test_datadog(
mock_statsd_init,
mock_statsd_client,
):
config = {
'host': 'http://statsd.google.com',
'port': '8130',
'temperature': '55',
'gravity': '1054',
'color': 'black',
}
datadog.Datadog(config=config).emit()
mock_statsd_init.mock_calls == [
mock.call(statsd_host='http://statsd.google.com', statsd_port='8130')
]
assert mock_statsd_client.mock_calls == [
mock.call.gauge('tilty.temperature', '55', tags=['color:black']),
mock.call.gauge('tilty.gravity', '1054', tags=['color:black']),
]
15 changes: 15 additions & 0 deletions tests/test_tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,18 @@ def test_scan_for_tilt_data_parse_influxdb(
),
mock.call().emit()
]


@mock.patch('tilty.emitters.datadog.Datadog')
def test_scan_for_tilt_data_parse_datadog(
mock_dd,
):
config = MockConfigParser('datadog')
tilty.emit(
config,
{'color': 'black', 'gravity': 1, 'temp': 32, 'timestamp': 155558888}
)
assert mock_dd.mock_calls == [
mock.call(config={'host': 'http://api.datadog.com', 'port': '8120', 'gravity': 1, 'temperature': 32, 'color': 'black'}), # noqa
mock.call().emit()
]
45 changes: 45 additions & 0 deletions tilty/emitters/datadog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
""" DataDog emitter """
import logging

from datadog import initialize, statsd

from tilty.common import safe_get_key

LOGGER = logging.getLogger()


class Datadog: # pylint: disable=too-few-public-methods
""" Class to represent the actual device """
def __init__(self, config):
""" Initializer
Args:
config: (dict) represents the configuration for the emitter
"""
self.temperature = config['temperature']
self.gravity = config['gravity']
self.color = config['color']
options = {
'statsd_host': config['host'],
'statsd_port': safe_get_key(config, 'port', 8125),
}
initialize(**options)

def emit(self, **kwargs): # pylint: disable=no-self-use,unused-argument
""" Initializer
Args:
"""
LOGGER.info('[datadog] posting temperature data')
statsd.gauge(
'tilty.temperature',
self.temperature,
tags=[f"color:{self.color}"]
)
LOGGER.info('[datadog] posting gravity data')
statsd.gauge(
'tilty.gravity',
self.gravity,
tags=[f"color:{self.color}"]
)
17 changes: 16 additions & 1 deletion tilty/tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from jinja2 import Template

from tilty.emitters import influxdb, webhook
from tilty.emitters import datadog, influxdb, webhook


def emit(config, tilt_data):
Expand Down Expand Up @@ -65,3 +65,18 @@ def emit(config, tilt_data):
}
_influxdb = influxdb.InfluxDB(config=_config)
_influxdb.emit()

# <start config sample>
# [datadog]
# host = 'host'
# port = 'port'
if config.has_section('datadog'):
_config = {
'host': config['datadog']['host'],
'port': config['datadog']['port'],
'gravity': tilt_data['gravity'],
'temperature': tilt_data['temp'],
'color': tilt_data['color'],
}
_datadog = datadog.Datadog(config=_config)
_datadog.emit()

0 comments on commit 52a1357

Please sign in to comment.