Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datadog component #7158

Merged

Conversation

nunofgs
Copy link
Contributor

@nunofgs nunofgs commented Apr 17, 2017

Description:

Adds a datadog component. Note that while it is possible to use the existing statsd component to report to Datadog, its true power comes from using tags which requires a datadog-specific library.

I am not affiliated with Datadog in any way. Just a happy user :)

This all started when I saw a tweet by @andre.rb. Since I was already using Datadog, I decided to build a component for HA.

Here's what I've been able to achieve using this component:

datadog-board-example

With the data in Datadog I'm able to also set up alerts to email/Slack when, for example, a statistical anomaly is detected on a sensor.

Datadog also supports events, which I'm grabbing from the logbook:

datadog

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#2452

Example entry for configuration.yaml:

datadog:
  host: 127.0.0.1

Note that you need a datadog-agent running. More information about that here.

Checklist:

  • Documentation added/updated in home-assistant.github.io
  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.

@mention-bot
Copy link

@nunofgs, thanks for your PR! By analyzing the history of the files in this pull request, we identified @postlund, @MartinHjelmare and @balloob to be potential reviewers.

@homeassistant
Copy link
Contributor

Hi @nunofgs,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

for attribute, value in attributes.items():
mock_client.gauge.assert_has_calls([
mock.call("ha.sensor.%s" % attribute, value, sample_rate=1,
tags=["entity:%s" % state.entity_id])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent


for in_, out in valid.items():
state = mock.MagicMock(domain="sensor", entity_id="sensor.foo.bar",
state=in_, attributes=attributes)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent

import unittest
import voluptuous as vol

class TestDatadog(unittest.TestCase):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

@@ -17,7 +17,7 @@
from homeassistant.components import sun
from homeassistant.components.frontend import register_built_in_panel
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import (EVENT_HOMEASSISTANT_START,
from homeassistant.const import (EVENT_HOMEASSISTANT_START, EVENT_LOGBOOK_ENTRY,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (80 > 79 characters)


def setup(hass, config):
"""Setup the Datadog component."""
from datadog import initialize, statsd, DogStatsd

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'datadog.DogStatsd' imported but unused

@nunofgs nunofgs force-pushed the enhancement/add-datadog-component branch from 1c95a26 to 6d82ac1 Compare April 17, 2017 23:30
@homeassistant
Copy link
Contributor

Hi @nunofgs,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!


from tests.common import get_test_home_assistant

class TestDatadog(unittest.TestCase):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

try:
value = state_helper.state_as_number(state)
except ValueError:
_LOGGER.debug('Error sending %s: %s (tags: %s)', metric, state.state, tags)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (87 > 79 characters)

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type change in the state helper needs some thought. Simplest solution would be to move that to the data dog module. As it is now the change will create an error for current users of Influxdb component.

Otherwise looks good. Some style comments. Use python 3 string formatting consistently. Haven't looked at the tests.

if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON,
STATE_OPEN):
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON, STATE_OPEN,
STATE_HOME, STATE_PLAYING):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change of state type will create a type mismatch error for Influxdb schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally feel that, in the context of "assigning" a value to a state, STATE_HOME and STATE_PLAYING naturally fall under a "1". If anything, we should be adding more states and mapping them to 1 and 0.

However, I understand the schema issues this may bring. In fact, it's probably an issue for other components as well (logentries for example). By changing the converted state this PR is introducing a BC and possibly breaking alerts/graphs that people are using.

I'll revert this change but I really think an improved way of converting to a number should be developed, as Datadog (and possibly others) only handle numbers (not strings).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but we should take more devs opinion and it should go in another PR that targets that topic specifically. It will probably need some discussion on how to handle breakage. We've used migration scripts in the past eg for influxdb. You're welcome to start a PR or RFC issue for discussion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I would prefer it in a new PR too. Also agree that more number representations is better in the long run.

import voluptuous as vol

from homeassistant.const import (
CONF_HOST,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the whole line width until 79 characters.


statsd.event(
title="Home Assistant",
text="%%%%%% \n **%s** %s \n %%%%%%" % (name, message),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use python 3 string formatting, ie 'hello {}'.format('world').

@nunofgs nunofgs force-pushed the enhancement/add-datadog-component branch from aa53cf2 to ea0f084 Compare April 18, 2017 22:17
tags=[
"entity:{}".format(event.data.get('entity_id'),
"domain:{}".format(event.data.get('domain')
]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SyntaxError: invalid syntax

@@ -0,0 +1,118 @@
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TokenError: EOF in multi-line statement

@nunofgs nunofgs force-pushed the enhancement/add-datadog-component branch from ea0f084 to d05f83b Compare April 18, 2017 22:24
@nunofgs nunofgs force-pushed the enhancement/add-datadog-component branch from d05f83b to 4935630 Compare April 19, 2017 00:06
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked over the tests now too. Use assert_setup_component to make sure config is validated when setting up the component.

Aside from that I think it looks good. Please don't rebase/squash your branch/commits during the review process, but just push the new commits. It's a lot easier to see what changed that way. If you want, you can then squash everything when we're ready to merge, although since github now has the squash and merge button, you don't have to do that either.

}
}

with self.assertRaises(vol.Invalid):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should test this using setup_component and use the helper context manager assert_setup_component found in tests/common.py. See eg: https://github.com/home-assistant/home-assistant/blob/dev/tests/components/alarm_control_panel/test_mqtt.py#L31-L40

Also use assert_setup_component for the other tests when you setup the component, to make sure the config is validated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I grabbed this from the statsd component, which I used for the basis of my component.

Would you accept a commit that improves the test there as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but not in this PR.


def test_invalid_config(self):
"""Test invalid configuration."""
with assert_setup_component(0) as config:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variable 'config' is assigned to but never used

)
from homeassistant.components import datadog
from homeassistant.setup import setup_component
import homeassistant.components.datadog as datadog

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'datadog' from line 13

from unittest import mock
import unittest

import voluptuous as vol

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'voluptuous as vol' imported but unused

@nunofgs nunofgs force-pushed the enhancement/add-datadog-component branch from df68897 to f36196f Compare April 26, 2017 21:33
@MartinHjelmare
Copy link
Member

I think you should use assert_setup_component for all the tests, where you set up the component to make sure config is validated.

@nunofgs nunofgs force-pushed the enhancement/add-datadog-component branch from f36196f to f443185 Compare May 5, 2017 21:19
@nunofgs nunofgs force-pushed the enhancement/add-datadog-component branch from f443185 to aae2ad2 Compare May 5, 2017 21:34
@nunofgs
Copy link
Contributor Author

nunofgs commented May 5, 2017

@MartinHjelmare updated.

@MartinHjelmare
Copy link
Member

Sorry if I'm not making sense. I meant that you should use the helper assert_setup_component as a context manager for all the tests when calling setup_component.

@MartinHjelmare
Copy link
Member

Nevermind, I think this is fine. 👍

@MartinHjelmare MartinHjelmare merged commit 20ded1b into home-assistant:dev May 5, 2017
@jnewland jnewland mentioned this pull request May 6, 2017
@balloob balloob mentioned this pull request May 18, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Aug 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants