Skip to content

Commit

Permalink
Merge pull request #279 from wearefair/marshall/service-check-constan…
Browse files Browse the repository at this point in the history
…t-tags

Append all statsd client level tags to status checks.
  • Loading branch information
yannmh committed Jun 26, 2018
2 parents 14dabcb + b40f5df commit e37e1d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
23 changes: 13 additions & 10 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,7 @@ def _report(self, metric, metric_type, value, tags, sample_rate):
return

# Resolve the full tag list
if self.constant_tags:
if tags:
tags = tags + self.constant_tags
else:
tags = self.constant_tags
tags = self._add_constant_tags(tags)

# Create/format the metric packet
payload = "%s%s:%s|%s%s%s" % (
Expand Down Expand Up @@ -323,11 +319,7 @@ def event(self, title, text, alert_type=None, aggregation_key=None,
text = self._escape_event_content(text)

# Append all client level tags to every event
if self.constant_tags:
if tags:
tags += self.constant_tags
else:
tags = self.constant_tags
tags = self._add_constant_tags(tags)

string = u'_e{%d,%d}:%s|%s' % (len(title), len(text), title, text)
if date_happened:
Expand Down Expand Up @@ -362,6 +354,9 @@ def service_check(self, check_name, status, tags=None, timestamp=None,

string = u'_sc|{0}|{1}'.format(check_name, status)

# Append all client level tags to every status check
tags = self._add_constant_tags(tags)

if timestamp:
string = u'{0}|d:{1}'.format(string, timestamp)
if hostname:
Expand All @@ -373,5 +368,13 @@ def service_check(self, check_name, status, tags=None, timestamp=None,

self._send(string)

def _add_constant_tags(self, tags):
if self.constant_tags:
if tags:
return tags + self.constant_tags
else:
return self.constant_tags
return tags


statsd = DogStatsd()
22 changes: 21 additions & 1 deletion tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import socket
import time
import unittest

# 3p
from mock import (
Expand Down Expand Up @@ -59,7 +60,7 @@ def send(self, payload):
raise socket.error("Socket error")


class TestDogStatsd(object):
class TestDogStatsd(unittest.TestCase):

def setUp(self):
"""
Expand Down Expand Up @@ -208,6 +209,25 @@ def test_service_check(self):
u'_sc|my_check.name|{0}|d:{1}|h:i-abcd1234|#key1:val1,key2:val2|m:{2}'
.format(self.statsd.WARNING, now, u"♬ †øU \\n†øU ¥ºu|m\: T0µ ♪"), self.recv())

def test_service_check_constant_tags(self):
self.statsd.constant_tags = ['bar:baz', 'foo']
now = int(time.time())
self.statsd.service_check(
'my_check.name', self.statsd.WARNING,
timestamp=now,
hostname='i-abcd1234', message=u"♬ †øU \n†øU ¥ºu|m: T0µ ♪")
t.assert_equal(
u'_sc|my_check.name|{0}|d:{1}|h:i-abcd1234|#bar:baz,foo|m:{2}'
.format(self.statsd.WARNING, now, u"♬ †øU \\n†øU ¥ºu|m\: T0µ ♪"), self.recv())

self.statsd.service_check(
'my_check.name', self.statsd.WARNING,
tags=['key1:val1', 'key2:val2'], timestamp=now,
hostname='i-abcd1234', message=u"♬ †øU \n†øU ¥ºu|m: T0µ ♪")
t.assert_equal(
u'_sc|my_check.name|{0}|d:{1}|h:i-abcd1234|#key1:val1,key2:val2,bar:baz,foo|m:{2}'
.format(self.statsd.WARNING, now, u"♬ †øU \\n†øU ¥ºu|m\: T0µ ♪"), self.recv())

def test_metric_namespace(self):
"""
Namespace prefixes all metric names.
Expand Down

0 comments on commit e37e1d5

Please sign in to comment.