Skip to content

Commit

Permalink
Fix test for runtime metrics worker
Browse files Browse the repository at this point in the history
  • Loading branch information
majorgreys committed May 16, 2019
1 parent b2c74b9 commit 144d10d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ddtrace/internal/runtime/runtime_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class RuntimeWorker(_worker.PeriodicWorkerThread):

FLUSH_INTERVAL = 10

def __init__(self, statsd_client, flush_interval=FLUSH_INTERVAL):
def __init__(self, statsd_client, flush_interval=None):
flush_interval = self.FLUSH_INTERVAL if flush_interval is None else flush_interval
super(RuntimeWorker, self).__init__(interval=flush_interval,
name=self.__class__.__name__)
self._statsd_client = statsd_client
Expand Down
35 changes: 24 additions & 11 deletions tests/internal/runtime/test_runtime_metrics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from ddtrace.internal.runtime.runtime_metrics import (
RuntimeTags,
RuntimeMetrics,
Expand Down Expand Up @@ -43,21 +45,30 @@ def test_one_metric(self):


class TestRuntimeWorker(BaseTracerTestCase):
def test_worker_metrics(self):
self.tracer.configure(collect_metrics=True)
def test_tracer_metrics(self):
# mock dogstatsd client before configuring tracer for runtime metrics
self.tracer._dogstatsd_client = DogStatsd()
self.tracer._dogstatsd_client.socket = FakeSocket()

default_flush_interval = RuntimeWorker.FLUSH_INTERVAL
try:
# lower flush interval
RuntimeWorker.FLUSH_INTERVAL = 1/4

# configure tracer for runtime metrics
self.tracer.configure(collect_metrics=True)
finally:
# reset flush interval
RuntimeWorker.FLUSH_INTERVAL = default_flush_interval

with self.override_global_tracer(self.tracer):
self.tracer._dogstatsd_client = DogStatsd()
self.tracer._dogstatsd_client.socket = FakeSocket()

root = self.start_span('parent', service='parent')
context = root.context
self.start_span('child', service='child', child_of=context)

self.worker = RuntimeWorker(self.tracer._dogstatsd_client, 0)
self.worker.start()
self.worker.stop()
self.worker.join()
time.sleep(self.tracer._runtime_worker.interval * 2)
self.tracer._runtime_worker.stop()
self.tracer._runtime_worker.join()

# get all received metrics
received = []
Expand All @@ -69,7 +80,8 @@ def test_worker_metrics(self):
received.append(new)

# expect received all default metrics
self.assertEqual(len(received), len(DEFAULT_RUNTIME_METRICS))
# we expect more than one flush since it is also called on shutdown
assert len(received) / len(DEFAULT_RUNTIME_METRICS) > 1

# expect all metrics in default set are received
# DEV: dogstatsd gauges in form "{metric_name}:{metric_value}|g#t{tag_name}:{tag_value},..."
Expand All @@ -78,7 +90,8 @@ def test_worker_metrics(self):
DEFAULT_RUNTIME_METRICS
)

for gauge in received:
# check to last set of metrics returned to confirm tags were set
for gauge in received[-len(DEFAULT_RUNTIME_METRICS):]:
self.assertRegexpMatches(gauge, 'runtime-id:')
self.assertRegexpMatches(gauge, 'service:parent')
self.assertRegexpMatches(gauge, 'service:child')

0 comments on commit 144d10d

Please sign in to comment.