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 support for DD_TAGS environment variable #703

Open
wants to merge 3 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
5 changes: 4 additions & 1 deletion datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def __init__(
:envvar DATADOG_TAGS: Tags to attach to every metric reported by dogstatsd client.
:type DATADOG_TAGS: comma-delimited string

:envvar DD_TAGS: Additional tags to attach to every metric reported by dogstatsd client.
:type DD_TAGS: comma-delimited string

:envvar DD_ENTITY_ID: Tag to identify the client entity.
:type DD_ENTITY_ID: string

Expand Down Expand Up @@ -272,7 +275,7 @@ def __init__(
self.encoding = "utf-8"

# Options
env_tags = [tag for tag in os.environ.get("DATADOG_TAGS", "").split(",") if tag]
env_tags = [tag for tag in os.environ.get("DATADOG_TAGS", os.environ.get("DD_TAGS", "")).split(",") if tag]
# Inject values of DD_* environment variables as global tags.
for var, tag_name in DD_ENV_TAGS_MAPPING.items():
value = os.environ.get(var, "")
Expand Down
5 changes: 4 additions & 1 deletion datadog/threadstats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def __init__(self, namespace="", constant_tags=None, compress_payload=False):
:envvar DATADOG_TAGS: Tags to attach to every metric reported by ThreadStats client
:type DATADOG_TAGS: comma-delimited string

:envvar DD_TAGS: Additional tags to attach to every metric reported by ThreadStats client
:type DD_TAGS: comma-delimited string

:envvar DD_ENV: the env of the service running the ThreadStats client.
If set, it is appended to the constant (global) tags of the client.
:type DD_ENV: string
Expand All @@ -69,7 +72,7 @@ def __init__(self, namespace="", constant_tags=None, compress_payload=False):
"""
# Parameters
self.namespace = namespace
env_tags = [tag for tag in os.environ.get("DATADOG_TAGS", "").split(",") if tag]
env_tags = [tag for tag in os.environ.get("DATADOG_TAGS", os.environ.get("DD_TAGS", "")).split(",") if tag]
for var, tag_name in DD_ENV_TAGS_MAPPING.items():
value = os.environ.get(var, "")
if value:
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,27 @@ def test_tags_from_environment_and_constant(self):
self.assertEqual(metric, dogstatsd.socket.recv())
self.assertEqual(telemetry_metrics(tags=tags, bytes_sent=len(metric)), dogstatsd.socket.recv())

def test_tags_from_environment_dd_tags(self):
with preserve_environment_variable('DD_TAGS'):
os.environ['DD_TAGS'] = 'country:china,age:45,blue'
dogstatsd = DogStatsd(telemetry_min_flush_interval=0)
dogstatsd.socket = FakeSocket()
dogstatsd.gauge('gt', 123.4)
metric = 'gt:123.4|g|#country:china,age:45,blue\n'
self.assertEqual(metric, dogstatsd.socket.recv())
self.assertEqual(telemetry_metrics(tags="country:china,age:45,blue", bytes_sent=len(metric)), dogstatsd.socket.recv())

def test_tags_from_environment_and_constant_dd_tags(self):
with preserve_environment_variable('DD_TAGS'):
os.environ['DD_TAGS'] = 'country:china,age:45,blue'
dogstatsd = DogStatsd(constant_tags=['country:canada', 'red'], telemetry_min_flush_interval=0)
dogstatsd.socket = FakeSocket()
dogstatsd.gauge('gt', 123.4)
tags = "country:canada,red,country:china,age:45,blue"
metric = 'gt:123.4|g|#' + tags + '\n'
self.assertEqual(metric, dogstatsd.socket.recv())
self.assertEqual(telemetry_metrics(tags=tags, bytes_sent=len(metric)), dogstatsd.socket.recv())

def test_entity_tag_from_environment(self):
with preserve_environment_variable('DD_ENTITY_ID'):
os.environ['DD_ENTITY_ID'] = '04652bb7-19b7-11e9-9cc6-42010a9c016d'
Expand Down
91 changes: 91 additions & 0 deletions tests/unit/threadstats/test_threadstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,97 @@ def test_tags_from_environment_and_constant(self):
assert event['tags'] == [event1_tag] + constant_tags + test_tags
dog.start(flush_interval=1, roll_up_interval=1)

def test_tags_from_environment_dd_tags(self):
test_tags = ['country:china', 'age:45', 'blue']
with preserve_environment_variable('DD_TAGS'):
os.environ['DD_TAGS'] = ','.join(test_tags)
dog = ThreadStats()
dog.start(roll_up_interval=10, flush_in_thread=False)
reporter = dog.reporter = MemoryReporter()

# Add two events
event1_title = "Event 1 title"
event2_title = "Event 1 title"
event1_text = "Event 1 text"
event2_text = "Event 2 text"
dog.event(event1_title, event1_text)
dog.event(event2_title, event2_text)

# Flush and test
dog.flush()
event1, event2 = reporter.events
assert event1['title'] == event1_title
assert event1['text'] == event1_text
assert event1['tags'] == test_tags
assert event2['title'] == event2_title
assert event2['text'] == event2_text
assert event2['text'] == event2_text
assert event2['tags'] == test_tags

# Test more parameters
reporter.events = []
event1_priority = "low"
event1_date_happened = 1375296969
event1_tag = "Event 2 tag"
dog.event(event1_title, event1_text, priority=event1_priority,
date_happened=event1_date_happened, tags=[event1_tag])

# Flush and test
dog.flush()
event, = reporter.events
assert event['title'] == event1_title
assert event['text'] == event1_text
assert event['priority'] == event1_priority
assert event['date_happened'] == event1_date_happened
assert event['tags'] == [event1_tag] + test_tags
dog.start(flush_interval=1, roll_up_interval=1)

def test_tags_from_environment_and_constant_dd_tags(self):
test_tags = ['country:china', 'age:45', 'blue']
constant_tags = ['country:canada', 'red']
with preserve_environment_variable('DD_TAGS'):
os.environ['DD_TAGS'] = ','.join(test_tags)
dog = ThreadStats(constant_tags=constant_tags)
dog.start(roll_up_interval=10, flush_in_thread=False)
reporter = dog.reporter = MemoryReporter()

# Add two events
event1_title = "Event 1 title"
event2_title = "Event 1 title"
event1_text = "Event 1 text"
event2_text = "Event 2 text"
dog.event(event1_title, event1_text)
dog.event(event2_title, event2_text)

# Flush and test
dog.flush()
event1, event2 = reporter.events
assert event1['title'] == event1_title
assert event1['text'] == event1_text
assert event1['tags'] == constant_tags + test_tags
assert event2['title'] == event2_title
assert event2['text'] == event2_text
assert event2['text'] == event2_text
assert event2['tags'] == constant_tags + test_tags

# Test more parameters
reporter.events = []
event1_priority = "low"
event1_date_happened = 1375296969
event1_tag = "Event 2 tag"
dog.event(event1_title, event1_text, priority=event1_priority,
date_happened=event1_date_happened, tags=[event1_tag])

# Flush and test
dog.flush()
event, = reporter.events
assert event['title'] == event1_title
assert event['text'] == event1_text
assert event['priority'] == event1_priority
assert event['date_happened'] == event1_date_happened
assert event['tags'] == [event1_tag] + constant_tags + test_tags
dog.start(flush_interval=1, roll_up_interval=1)

def test_tags_from_environment_env_service_version(self):
test_tags = set(['env:staging', 'service:food', 'version:1.2.3'])
with EnvVars(
Expand Down