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 easier namespacing for data submission #3718

Merged
merged 2 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class __AgentCheckPy3(object):
The base class for any Agent based integrations
"""

# If defined, this will be the prefix of every metric/service check and the source type of events
__NAMESPACE__ = ''

OK, WARNING, CRITICAL, UNKNOWN = ServiceCheck

# Used by `self.http` RequestsWrapper
Expand Down Expand Up @@ -203,6 +206,12 @@ def get_instance_proxy(self, instance, uri, proxies=None):
def _context_uid(self, mtype, name, tags=None, hostname=None):
return '{}-{}-{}-{}'.format(mtype, name, tags if tags is None else hash(frozenset(tags)), hostname)

def _format_namespace(self, s):
if self.__NAMESPACE__:
return '{}.{}'.format(self.__NAMESPACE__, ensure_unicode(s))

return ensure_unicode(s)

def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_name=None):
if value is None:
# ignore metric sample
Expand Down Expand Up @@ -234,7 +243,7 @@ def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_na
self.warning(err_msg)
return

aggregator.submit_metric(self, self.check_id, mtype, ensure_unicode(name), value, tags, hostname)
aggregator.submit_metric(self, self.check_id, mtype, self._format_namespace(name), value, tags, hostname)

def gauge(self, name, value, tags=None, hostname=None, device_name=None):
self._submit_metric(aggregator.GAUGE, name, value, tags=tags, hostname=hostname, device_name=device_name)
Expand Down Expand Up @@ -281,7 +290,9 @@ def service_check(self, name, status, tags=None, hostname=None, message=None):
else:
message = ensure_unicode(message)

aggregator.submit_service_check(self, self.check_id, ensure_unicode(name), status, tags, hostname, message)
aggregator.submit_service_check(
self, self.check_id, self._format_namespace(name), status, tags, hostname, message
)

def event(self, event):
# Enforce types of some fields, considerably facilitates handling in go bindings downstream
Expand All @@ -295,12 +306,17 @@ def event(self, event):
'Error decoding unicode field `{}` to utf-8 encoded string, cannot submit event'.format(key)
)
return

if event.get('tags'):
event['tags'] = self._normalize_tags_type(event['tags'])
if event.get('timestamp'):
event['timestamp'] = int(event['timestamp'])
if event.get('aggregation_key'):
event['aggregation_key'] = ensure_unicode(event['aggregation_key'])

if self.__NAMESPACE__:
event.setdefault('source_type_name', self.__NAMESPACE__)
Copy link
Member

Choose a reason for hiding this comment

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

This is the only bit that would introduce a change in behavior. I don't really object to it, it's just worth noting.


aggregator.submit_event(self, self.check_id, event)

# TODO(olivier): implement service_metadata if it's worth it
Expand Down Expand Up @@ -459,6 +475,9 @@ class __AgentCheckPy2(object):
The base class for any Agent based integrations
"""

# If defined, this will be the prefix of every metric/service check and the source type of events
__NAMESPACE__ = ''

OK, WARNING, CRITICAL, UNKNOWN = ServiceCheck

"""
Expand Down Expand Up @@ -597,6 +616,12 @@ def get_instance_proxy(self, instance, uri, proxies=None):
def _context_uid(self, mtype, name, tags=None, hostname=None):
return '{}-{}-{}-{}'.format(mtype, name, tags if tags is None else hash(frozenset(tags)), hostname)

def _format_namespace(self, s):
if self.__NAMESPACE__:
return '{}.{}'.format(self.__NAMESPACE__, ensure_bytes(s))

return ensure_bytes(s)

def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_name=None):
if value is None:
# ignore metric sample
Expand Down Expand Up @@ -628,7 +653,7 @@ def _submit_metric(self, mtype, name, value, tags=None, hostname=None, device_na
self.warning(err_msg)
return

aggregator.submit_metric(self, self.check_id, mtype, ensure_bytes(name), value, tags, hostname)
aggregator.submit_metric(self, self.check_id, mtype, self._format_namespace(name), value, tags, hostname)

def gauge(self, name, value, tags=None, hostname=None, device_name=None):
self._submit_metric(aggregator.GAUGE, name, value, tags=tags, hostname=hostname, device_name=device_name)
Expand Down Expand Up @@ -675,7 +700,9 @@ def service_check(self, name, status, tags=None, hostname=None, message=None):
else:
message = ensure_bytes(message)

aggregator.submit_service_check(self, self.check_id, ensure_bytes(name), status, tags, hostname, message)
aggregator.submit_service_check(
self, self.check_id, self._format_namespace(name), status, tags, hostname, message
)

def event(self, event):
# Enforce types of some fields, considerably facilitates handling in go bindings downstream
Expand All @@ -689,12 +716,17 @@ def event(self, event):
"Error encoding unicode field '%s' to utf-8 encoded string, can't submit event", key
)
return

if event.get('tags'):
event['tags'] = self._normalize_tags_type(event['tags'])
if event.get('timestamp'):
event['timestamp'] = int(event['timestamp'])
if event.get('aggregation_key'):
event['aggregation_key'] = ensure_bytes(event['aggregation_key'])

if self.__NAMESPACE__:
event.setdefault('source_type_name', self.__NAMESPACE__)
Copy link
Member

Choose a reason for hiding this comment

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

ditto


aggregator.submit_event(self, self.check_id, event)

# TODO(olivier): implement service_metadata if it's worth it
Expand Down
28 changes: 28 additions & 0 deletions datadog_checks_base/tests/test_agent_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ def test_underscores_and_dots(self):


class TestMetrics:
def test_namespace(self, aggregator):
check = AgentCheck()
check.__NAMESPACE__ = 'test'

check.gauge('metric', 0)

aggregator.assert_metric('test.metric')

def test_non_float_metric(self, aggregator):
check = AgentCheck()
metric_name = 'test_metric'
Expand All @@ -121,6 +129,19 @@ def test_valid_event(self, aggregator):
check.event(event)
aggregator.assert_event('test event test event')

def test_namespace(self, aggregator):
check = AgentCheck()
check.__NAMESPACE__ = 'test'
event = {
'event_type': 'new.event',
'msg_title': 'new test event',
'aggregation_key': 'test.event',
'msg_text': 'test event test event',
'tags': None,
}
check.event(event)
aggregator.assert_event('test event test event', source_type_name='test')


class TestServiceChecks:
def test_valid_sc(self, aggregator):
Expand All @@ -147,6 +168,13 @@ def test_valid_sc(self, aggregator):
check.service_check("testservicecheckwithnonemessage", AgentCheck.OK, message=None)
aggregator.assert_service_check("testservicecheckwithnonemessage", status=AgentCheck.OK)

def test_namespace(self, aggregator):
check = AgentCheck()
check.__NAMESPACE__ = 'test'

check.service_check('service_check', AgentCheck.OK)
aggregator.assert_service_check('test.service_check', status=AgentCheck.OK)


class TestTags:
def test_default_string(self):
Expand Down