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

Accept any Iterable as tags parameter #542

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion datadog/api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import time
import zlib

try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable

# datadog
from datadog.api import _api_version, _max_timeouts, _backoff_period
from datadog.api.exceptions import (
Expand Down Expand Up @@ -136,10 +141,14 @@ def submit(cls, method, path, api_version=None, body=None, attach_host_name=Fals
body['host'] = _host_name

# If defined, make sure tags are defined as a comma-separated string
if 'tags' in params and isinstance(params['tags'], list):
if 'tags' in params and isinstance(params['tags'], Iterable):
tag_list = normalize_tags(params['tags'])
params['tags'] = ','.join(tag_list)

# If set, make sure tags are a list and not any other iterable.
if body and isinstance(body.get("tags", None), Iterable):
body["tags"] = list(body["tags"])

# If defined, make sure monitor_ids are defined as a comma-separated string
if 'monitor_ids' in params and isinstance(params['monitor_ids'], list):
params['monitor_ids'] = ','.join(str(i) for i in params['monitor_ids'])
Expand Down
7 changes: 6 additions & 1 deletion datadog/api/monitors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable

from datadog.api.resources import GetableAPIResource, CreateableAPIResource, \
UpdatableAPIResource, ListableAPIResource, DeletableAPIResource, \
ActionAPIResource
Expand Down Expand Up @@ -54,7 +59,7 @@ def get_all(cls, **params):
:returns: Dictionary representing the API's JSON response
"""
for p in ['group_states', 'tags', 'monitor_tags']:
if p in params and isinstance(params[p], list):
if p in params and isinstance(params[p], Iterable):
params[p] = ','.join(params[p])

return super(Monitor, cls).get_all(**params)
Expand Down
7 changes: 6 additions & 1 deletion datadog/api/synthetics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable

from datadog.api.exceptions import ApiError
from datadog.api.resources import (
CreateableAPIResource,
Expand Down Expand Up @@ -55,7 +60,7 @@ def get_all_tests(cls, **params):
"""

for p in ["locations", "tags"]:
if p in params and isinstance(params[p], list):
if p in params and isinstance(params[p], Iterable):
params[p] = ",".join(params[p])

# API path = "synthetics/tests"
Expand Down
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 @@ -151,12 +151,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 @@ -182,6 +182,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
5 changes: 3 additions & 2 deletions tests/integration/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def test_tags(self, dog, get_with_retry, freezer):
)

# The response from `update` can be flaky, so let's test that it work by getting the tags
dog.Tag.update(hostname, tags=["test_tag:3"], source="datadog")
# Testing Iterable tags as well
dog.Tag.update(hostname, tags=iter(["test_tag:3"]), source="datadog")
get_with_retry(
"Tag",
hostname,
Expand Down Expand Up @@ -114,7 +115,7 @@ def test_events(self, dog, get_with_retry, freezer):
assert event["event"]["alert_type"] == "success"

event = dog.Event.create(
title="test tags", text="test tags", tags=["test_tag:1", "test_tag:2"]
title="test tags", text="test tags", tags=set(["test_tag:1", "test_tag:2"])
)
assert "test_tag:1" in event["event"]["tags"]
assert "test_tag:2" in event["event"]["tags"]
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ def api():
return api


@pytest.fixture(scope='module')
def vcr(vcr):
"""Override VCR matcher."""
from vcr.matchers import _get_transformer, _identity, read_body

def normalize(obj, sort=False):
if isinstance(obj, dict):
return {k: normalize(v, k == "tags") for k, v in obj.items()}
if isinstance(obj, list):
x = (normalize(x) for x in obj)
return sorted(x) if sort else list(x)
return obj

def body(r1, r2):
transformer = _get_transformer(r1)
r2_transformer = _get_transformer(r2)
if transformer != r2_transformer:
transformer = _identity
assert normalize(transformer(read_body(r1))) == normalize(transformer(read_body(r2)))

vcr.matchers["body"] = body
return vcr


@pytest.fixture(scope='module')
def vcr_config():
return dict(
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 @@ -492,11 +492,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 @@ -535,11 +535,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