Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_context_manager(self):
fake_socket = FakeSocket()
with DogStatsd() as statsd:
statsd.socket = fake_socket
statsd.gauge('page.views', 123)
statsd.timing('timer', 123)
assert_equal('page.views:123|g\ntimer:123|ms', fake_socket.recv())
def test_timed_decorator_threaded(self, mock_time):
"""
`timed` decorator plays well with concurrent threads.
"""
# Create a DogStatsd client with a mocked socket
statsd = DogStatsd()
statsd.socket = self.socket
# Set up the mocked time
mock_time.return_value = 0
# Method to time
@statsd.timed("foo")
def bar():
"""
Wait 5 time units and return.
"""
initial_time = mock_time.return_value
while mock_time.return_value < initial_time + 2:
pass
# Run the method within multiple threads
def test_entity_tag_and_tags_from_environment_and_constant(self):
with preserve_environment_variable('DATADOG_TAGS'):
os.environ['DATADOG_TAGS'] = 'country:china,age:45,blue'
with preserve_environment_variable('DD_ENTITY_ID'):
os.environ['DD_ENTITY_ID'] = '04652bb7-19b7-11e9-9cc6-42010a9c016d'
statsd = DogStatsd(constant_tags=['country:canada', 'red'])
statsd.socket = FakeSocket()
statsd.gauge('gt', 123.4)
assert_equal('gt:123.4|g|#country:canada,red,country:china,age:45,blue,dd.internal.entity_id:04652bb7-19b7-11e9-9cc6-42010a9c016d', statsd.socket.recv())
def test_entity_tag_from_environment_and_constant(self):
with preserve_environment_variable('DD_ENTITY_ID'):
os.environ['DD_ENTITY_ID'] = '04652bb7-19b7-11e9-9cc6-42010a9c016d'
statsd = DogStatsd(constant_tags=['country:canada', 'red'])
statsd.socket = FakeSocket()
statsd.gauge('gt', 123.4)
assert_equal('gt:123.4|g|#country:canada,red,dd.internal.entity_id:04652bb7-19b7-11e9-9cc6-42010a9c016d', statsd.socket.recv())
def test_tags_from_environment_and_constant(self):
with preserve_environment_variable('DATADOG_TAGS'):
os.environ['DATADOG_TAGS'] = 'country:china,age:45,blue'
statsd = DogStatsd(constant_tags=['country:canada', 'red'])
statsd.socket = FakeSocket()
statsd.gauge('gt', 123.4)
assert_equal('gt:123.4|g|#country:canada,red,country:china,age:45,blue', statsd.socket.recv())
def test_histogram(self):
"""
Histograms can be submitted from concurrent threads.
"""
# Create a DogStatsd client with a mocked socket
statsd = DogStatsd()
statsd.socket = self.socket
# Samples
values = set(range(10000))
# Submit metrics from different threads
self._submit_with_multiple_threads(statsd, "histogram", values)
# All metrics were properly submitted
self.assertMetrics(values)
def test_send_batch_metrics(self):
"""
Metrics can be buffered, submitted from concurrent threads.
"""
with DogStatsd() as batch_statsd:
# Create a DogStatsd buffer client with a mocked socket
batch_statsd.socket = self.socket
# Samples
values = set(range(10000))
# Submit metrics from different threads
self._submit_with_multiple_threads(batch_statsd, "gauge", values)
# All metrics were properly submitted
self.assertMetrics(values)
def test_default_route(self):
"""
Dogstatsd host can be dynamically set to the default route.
"""
# Setup
statsd = DogStatsd(use_default_route=True)
# Assert
assert_equal(statsd.host, "172.17.0.1")
def test_tags_from_environment(self):
with preserve_environment_variable('DATADOG_TAGS'):
os.environ['DATADOG_TAGS'] = 'country:china,age:45,blue'
statsd = DogStatsd()
statsd.socket = FakeSocket()
statsd.gauge('gt', 123.4)
assert_equal('gt:123.4|g|#country:china,age:45,blue', statsd.socket.recv())
def test_dogstatsd_initialization_with_env_vars(self):
"""
Dogstatsd can retrieve its config from env vars when
not provided in constructor.
"""
# Setup
with preserve_environment_variable('DD_AGENT_HOST'):
os.environ['DD_AGENT_HOST'] = 'myenvvarhost'
with preserve_environment_variable('DD_DOGSTATSD_PORT'):
os.environ['DD_DOGSTATSD_PORT'] = '4321'
statsd = DogStatsd()
# Assert
assert_equal(statsd.host, "myenvvarhost")
assert_equal(statsd.port, 4321)