Skip to content

Commit

Permalink
Merge pull request #233 from shargan/shargan/performance
Browse files Browse the repository at this point in the history
Speed up DogStatsd._report()
  • Loading branch information
yannmh authored Oct 30, 2017
2 parents a429c22 + 72c7062 commit b9b14d7
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
# datadog
from datadog.dogstatsd.context import TimedContextManagerDecorator
from datadog.dogstatsd.route import get_default_route
from datadog.util.compat import (
imap,
text,
)
from datadog.util.compat import text

# Logging
log = logging.getLogger('datadog.dogstatsd')
Expand Down Expand Up @@ -83,6 +80,8 @@ def __init__(self, host='localhost', port=8125, max_buffer_size=50, namespace=No
if constant_tags is None:
constant_tags = []
self.constant_tags = constant_tags + env_tags
if namespace is not None:
namespace = text(namespace)
self.namespace = namespace
self.use_ms = use_ms

Expand Down Expand Up @@ -253,8 +252,6 @@ def _report(self, metric, metric_type, value, tags, sample_rate):
if sample_rate != 1 and random() > sample_rate:
return

payload = []

# Resolve the full tag list
if self.constant_tags:
if tags:
Expand All @@ -263,20 +260,17 @@ def _report(self, metric, metric_type, value, tags, sample_rate):
tags = self.constant_tags

# Create/format the metric packet
if self.namespace:
payload.extend([self.namespace, "."])
payload.extend([metric, ":", value, "|", metric_type])

if sample_rate != 1:
payload.extend(["|@", sample_rate])

if tags:
payload.extend(["|#", ",".join(tags)])

encoded = "".join(imap(text, payload))
payload = "%s%s:%s|%s%s%s" % (
(self.namespace + ".") if self.namespace else "",
metric,
value,
metric_type,
("|@" + text(sample_rate)) if sample_rate != 1 else "",
("|#" + ",".join(tags)) if tags else "",
)

# Send it
self._send(encoded)
self._send(payload)

def _send_to_server(self, packet):
try:
Expand Down

0 comments on commit b9b14d7

Please sign in to comment.