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

internal: use histogram and counters for metrics #1134

Merged
merged 1 commit into from
Nov 15, 2019
Merged
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
31 changes: 18 additions & 13 deletions ddtrace/internal/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,24 @@ def flush_queue(self):
if self._send_stats:
# Statistics about the queue length, size and number of spans
self.dogstatsd.increment('datadog.tracer.flushes')
self.dogstatsd.histogram('datadog.tracer.flush.traces', traces_queue_length)
self.dogstatsd.histogram('datadog.tracer.flush.spans', traces_queue_spans)
self._histogram_with_total('datadog.tracer.flush.traces', traces_queue_length)
self._histogram_with_total('datadog.tracer.flush.spans', traces_queue_spans)

# Statistics about the filtering
self.dogstatsd.histogram('datadog.tracer.flush.traces_filtered', traces_filtered)
self._histogram_with_total('datadog.tracer.flush.traces_filtered', traces_filtered)

# Statistics about API
self.dogstatsd.histogram('datadog.tracer.api.requests', len(traces_responses))
self.dogstatsd.histogram('datadog.tracer.api.errors',
len(list(t for t in traces_responses
if isinstance(t, Exception))))
self._histogram_with_total('datadog.tracer.api.requests', len(traces_responses))

self._histogram_with_total('datadog.tracer.api.errors',
len(list(t for t in traces_responses if isinstance(t, Exception))))
for status, grouped_responses in itertools.groupby(
sorted((t for t in traces_responses if not isinstance(t, Exception)),
key=lambda r: r.status),
key=lambda r: r.status):
self.dogstatsd.histogram('datadog.tracer.api.responses',
len(list(grouped_responses)),
tags=['status:%d' % status])
self._histogram_with_total('datadog.tracer.api.responses',
len(list(grouped_responses)),
tags=['status:%d' % status])

# Statistics about the writer thread
if hasattr(time, 'thread_time'):
Expand All @@ -133,6 +133,11 @@ def flush_queue(self):
self._last_thread_time = new_thread_time
self.dogstatsd.histogram('datadog.tracer.writer.cpu_time', diff)

def _histogram_with_total(self, name, value, tags=None):
"""Helper to add metric as a histogram and with a `.total` counter"""
self.dogstatsd.histogram(name, value, tags=tags)
self.dogstatsd.increment('%s.total' % (name, ), value, tags=tags)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just you need name but if you want to decorate it with fancy parentheses, who am I to judge?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is habit because I don't trust that someone won't pass in name as a tuple... thank you type checking :p

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they pass a tuple, they'll get a TypeError which makes them possible to fix their code right away.

Now, if they pass a tuple, they have no error and good luck debugging that…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is called being "robust" 😆


def run_periodic(self):
if self._send_stats:
self.dogstatsd.gauge('datadog.tracer.heartbeat', 1)
Expand All @@ -146,9 +151,9 @@ def run_periodic(self):
# Statistics about the rate at which spans are inserted in the queue
dropped, enqueued, enqueued_lengths = self._trace_queue.reset_stats()
self.dogstatsd.gauge('datadog.tracer.queue.max_length', self._trace_queue.maxsize)
self.dogstatsd.histogram('datadog.tracer.queue.dropped.traces', dropped)
self.dogstatsd.histogram('datadog.tracer.queue.enqueued.traces', enqueued)
self.dogstatsd.histogram('datadog.tracer.queue.enqueued.spans', enqueued_lengths)
self.dogstatsd.increment('datadog.tracer.queue.dropped.traces', dropped)
self.dogstatsd.increment('datadog.tracer.queue.enqueued.traces', enqueued)
self.dogstatsd.increment('datadog.tracer.queue.enqueued.spans', enqueued_lengths)

def on_shutdown(self):
try:
Expand Down
47 changes: 27 additions & 20 deletions tests/internal/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,29 @@ def test_dogstatsd(self):

assert [
mock.call('datadog.tracer.flushes'),
mock.call('datadog.tracer.flush.traces.total', 11, tags=None),
mock.call('datadog.tracer.flush.spans.total', 77, tags=None),
mock.call('datadog.tracer.flush.traces_filtered.total', 0, tags=None),
mock.call('datadog.tracer.api.requests.total', 11, tags=None),
mock.call('datadog.tracer.api.errors.total', 0, tags=None),
mock.call('datadog.tracer.api.responses.total', 11, tags=['status:200']),
mock.call('datadog.tracer.queue.dropped.traces', 0),
mock.call('datadog.tracer.queue.enqueued.traces', 11),
mock.call('datadog.tracer.queue.enqueued.spans', 77),
mock.call('datadog.tracer.shutdown'),
] == self.dogstatsd.increment.mock_calls

histogram_calls = [
mock.call('datadog.tracer.flush.traces', 11),
mock.call('datadog.tracer.flush.spans', 77),
mock.call('datadog.tracer.flush.traces_filtered', 0),
mock.call('datadog.tracer.api.requests', 11),
mock.call('datadog.tracer.api.errors', 0),
mock.call('datadog.tracer.flush.traces', 11, tags=None),
mock.call('datadog.tracer.flush.spans', 77, tags=None),
mock.call('datadog.tracer.flush.traces_filtered', 0, tags=None),
mock.call('datadog.tracer.api.requests', 11, tags=None),
mock.call('datadog.tracer.api.errors', 0, tags=None),
mock.call('datadog.tracer.api.responses', 11, tags=['status:200']),
]
if hasattr(time, 'thread_time'):
histogram_calls.append(mock.call('datadog.tracer.writer.cpu_time', mock.ANY))

histogram_calls += [
mock.call('datadog.tracer.queue.dropped.traces', 0),
mock.call('datadog.tracer.queue.enqueued.traces', 11),
mock.call('datadog.tracer.queue.enqueued.spans', 77),
]
assert histogram_calls == self.dogstatsd.histogram.mock_calls

def test_dogstatsd_failing_api(self):
Expand All @@ -167,24 +171,27 @@ def test_dogstatsd_failing_api(self):

assert [
mock.call('datadog.tracer.flushes'),
mock.call('datadog.tracer.flush.traces.total', 11, tags=None),
mock.call('datadog.tracer.flush.spans.total', 77, tags=None),
mock.call('datadog.tracer.flush.traces_filtered.total', 0, tags=None),
mock.call('datadog.tracer.api.requests.total', 1, tags=None),
mock.call('datadog.tracer.api.errors.total', 1, tags=None),
mock.call('datadog.tracer.queue.dropped.traces', 0),
mock.call('datadog.tracer.queue.enqueued.traces', 11),
mock.call('datadog.tracer.queue.enqueued.spans', 77),
mock.call('datadog.tracer.shutdown'),
] == self.dogstatsd.increment.mock_calls

histogram_calls = [
mock.call('datadog.tracer.flush.traces', 11),
mock.call('datadog.tracer.flush.spans', 77),
mock.call('datadog.tracer.flush.traces_filtered', 0),
mock.call('datadog.tracer.api.requests', 1),
mock.call('datadog.tracer.api.errors', 1),
mock.call('datadog.tracer.flush.traces', 11, tags=None),
mock.call('datadog.tracer.flush.spans', 77, tags=None),
mock.call('datadog.tracer.flush.traces_filtered', 0, tags=None),
mock.call('datadog.tracer.api.requests', 1, tags=None),
mock.call('datadog.tracer.api.errors', 1, tags=None),
]
if hasattr(time, 'thread_time'):
histogram_calls.append(mock.call('datadog.tracer.writer.cpu_time', mock.ANY))

histogram_calls += [
mock.call('datadog.tracer.queue.dropped.traces', 0),
mock.call('datadog.tracer.queue.enqueued.traces', 11),
mock.call('datadog.tracer.queue.enqueued.spans', 77),
]
assert histogram_calls == self.dogstatsd.histogram.mock_calls


Expand Down