Skip to content

Accept any Iterable as tags parameter #542

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
10 changes: 5 additions & 5 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,12 @@ def service_check(self, check_name, status, tags=None, timestamp=None,
self._send(string)

def _add_constant_tags(self, tags):
result = []
if tags:
result.extend(tags)
if self.constant_tags:
if tags:
return tags + self.constant_tags
else:
return self.constant_tags
return tags
result.extend(self.constant_tags)
return result


statsd = DogStatsd()
11 changes: 6 additions & 5 deletions datadog/threadstats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ def event(self, title, text, alert_type=None, aggregation_key=None,
"""
if not self._disabled:
# Append all client level tags to every event
event_tags = tags
event_tags = []
if tags:
event_tags.extend(tags)
if self.constant_tags:
if tags:
event_tags = tags + self.constant_tags
else:
event_tags = self.constant_tags
event_tags.extend(self.constant_tags)

event_tags = event_tags if event_tags else None

self._event_aggregator.add_event(
title=title, text=text, alert_type=alert_type, aggregation_key=aggregation_key,
Expand Down
1 change: 1 addition & 0 deletions datadog/threadstats/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def __init__(self, roll_up_interval=10):
def add_point(self, metric, tags, timestamp, value, metric_class, sample_rate=1, host=None):
# The sample rate is currently ignored for in process stuff
interval = timestamp - timestamp % self._roll_up_interval
tags = list(tags) if tags else None
key = (metric, host, tuple(sorted(tags)) if tags else None)
with self._lock:
if key not in self._metrics[interval]:
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ def test_gauge_constant_tags_with_metric_level_tags_twice(self):
self.statsd.gauge('gauge', 123.4, tags=metric_level_tag)
assert_equal_telemetry('gauge:123.4|g|#foo:bar,bar:baz', self.recv(), telemetry=telemetry_metrics(tags="bar:baz"))

def test_gauge_constant_tags_with_iterable_metric_tags(self):
metric_level_tag = iter(('foo:bar',))
self.statsd.constant_tags = ['bar:baz']
self.statsd.gauge('gauge', 123.4, tags=metric_level_tag)
assert_equal_telemetry('gauge:123.4|g|#foo:bar,bar:baz', self.recv(), telemetry=telemetry_metrics(tags="bar:baz"))

@staticmethod
def assert_almost_equal(a, b, delta):
assert 0 <= abs(a - b) <= delta, "%s - %s not within %s" % (a, b, delta)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/threadstats/test_threadstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ def test_tags(self):
# Post the same metric with different tags.
dog.gauge('gauge', 10, timestamp=100.0)
dog.gauge('gauge', 15, timestamp=100.0, tags=['env:production', 'db'])
dog.gauge('gauge', 20, timestamp=100.0, tags=['env:staging'])
dog.gauge('gauge', 20, timestamp=100.0, tags=iter(['env:staging']))

dog.increment('counter', timestamp=100.0)
dog.increment('counter', timestamp=100.0, tags=['env:production', 'db'])
dog.increment('counter', timestamp=100.0, tags=['env:staging'])
dog.increment('counter', timestamp=100.0, tags=set(['env:staging']))

dog.flush(200.0)

Expand Down Expand Up @@ -497,11 +497,11 @@ def test_constant_tags(self):
# Post the same metric with different tags.
dog.gauge("gauge", 10, timestamp=100.0)
dog.gauge("gauge", 15, timestamp=100.0, tags=["env:production", 'db'])
dog.gauge("gauge", 20, timestamp=100.0, tags=["env:staging"])
dog.gauge("gauge", 20, timestamp=100.0, tags=iter(["env:staging"]))

dog.increment("counter", timestamp=100.0)
dog.increment("counter", timestamp=100.0, tags=["env:production", 'db'])
dog.increment("counter", timestamp=100.0, tags=["env:staging"])
dog.increment("counter", timestamp=100.0, tags=set(["env:staging"]))

dog.flush(200.0)

Expand Down