Skip to content

Commit 7d78065

Browse files
add flag for enabling/disabling extended aggregation
1 parent a54c136 commit 7d78065

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

datadog/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def initialize(
3838
statsd_host=None, # type: Optional[str]
3939
statsd_port=None, # type: Optional[int]
4040
statsd_disable_aggregation=True, # type: bool
41+
statsd_disable_extended_aggregation=True, # type: bool
4142
statsd_disable_buffering=True, # type: bool
4243
statsd_aggregation_flush_interval=0.3, # type: float
4344
statsd_use_default_route=False, # type: bool
@@ -82,6 +83,10 @@ def initialize(
8283
(default: True).
8384
:type statsd_disable_aggregation: boolean
8485
86+
:param statsd_disable_extended_aggregation: Enable/disable statsd client aggregation support for histograms, distributions and timing metrics
87+
(default: True).
88+
:type statsd_disable_extended_aggregation: boolean
89+
8590
:param statsd_aggregation_flush_interval: If aggregation is enabled, set the flush interval for
8691
aggregation/buffering
8792
(default: 0.3 seconds)
@@ -143,6 +148,10 @@ def initialize(
143148
statsd.disable_aggregation()
144149
else:
145150
statsd.enable_aggregation(statsd_aggregation_flush_interval)
151+
if statsd_disable_extended_aggregation:
152+
statsd.disable_extended_aggregation()
153+
else:
154+
statsd.enable_extended_aggregation(statsd_aggregation_flush_interval)
146155
statsd.disable_buffering = statsd_disable_buffering
147156
api._return_raw_response = return_raw_response
148157

datadog/dogstatsd/base.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def __init__(
147147
max_buffer_size=None, # type: None
148148
flush_interval=DEFAULT_BUFFERING_FLUSH_INTERVAL, # type: float
149149
disable_aggregation=True, # type: bool
150+
disable_extended_aggregation=True, # type: bool
150151
disable_buffering=True, # type: bool
151152
namespace=None, # type: Optional[Text]
152153
constant_tags=None, # type: Optional[List[str]]
@@ -236,7 +237,10 @@ def __init__(
236237
it overrides the default value.
237238
:type flush_interval: float
238239
239-
:disable_aggregation: If true, metrics (Count, Gauge, Set) are no longered aggregated by the client
240+
:disable_aggregation: If true, metrics (Count, Gauge, Set) are no longer aggregated by the client
241+
:type disable_aggregation: bool
242+
243+
:disable_extended_aggregation: If true, metrics (Histogram, Distribution, Timing) are no longer aggregated by the client
240244
:type disable_aggregation: bool
241245
242246
:disable_buffering: If set, metrics are no longered buffered by the client and
@@ -446,6 +450,7 @@ def __init__(
446450

447451
self._disable_buffering = disable_buffering
448452
self._disable_aggregation = disable_aggregation
453+
self._disable_extended_aggregation = disable_extended_aggregation
449454

450455
self._flush_interval = flush_interval
451456
self._flush_thread = None
@@ -459,7 +464,7 @@ def __init__(
459464
else:
460465
self._send = self._send_to_server
461466

462-
if not self._disable_aggregation or not self._disable_buffering:
467+
if not self._disable_aggregation or not self._disable_buffering or not self._disable_extended_aggregation:
463468
self._start_flush_thread()
464469
else:
465470
log.debug("Statsd buffering and aggregation is disabled")
@@ -559,10 +564,8 @@ def _start_flush_thread(self):
559564
def _flush_thread_loop(self, flush_interval):
560565
while not self._flush_thread_stop.is_set():
561566
time.sleep(flush_interval)
562-
if not self._disable_aggregation:
567+
if not self._disable_aggregation or not self._disable_extended_aggregation:
563568
self.flush_aggregated_metrics()
564-
# Histograms, Distribution and Timing metrics are not aggregated
565-
self.flush_buffered_metrics()
566569
if not self._disable_buffering:
567570
self.flush_buffered_metrics()
568571
self._flush_thread = threading.Thread(
@@ -582,7 +585,7 @@ def _stop_flush_thread(self):
582585
if not self._flush_thread:
583586
return
584587
try:
585-
if not self._disable_aggregation:
588+
if not self._disable_aggregation or not self._disable_extended_aggregation:
586589
self.flush_aggregated_metrics()
587590
if not self.disable_buffering:
588591
self.flush_buffered_metrics()
@@ -641,7 +644,7 @@ def disable_aggregation(self):
641644

642645
# If aggregation and buffering has been disabled, flush and kill the background thread
643646
# otherwise start up the flushing thread and enable aggregation.
644-
if self._disable_aggregation and self.disable_buffering:
647+
if self._disable_aggregation and self._disable_extended_aggregation and self.disable_buffering:
645648
self._stop_flush_thread()
646649
log.debug("Statsd aggregation is disabled")
647650

@@ -655,6 +658,30 @@ def enable_aggregation(self, flush_interval=DEFAULT_BUFFERING_FLUSH_INTERVAL):
655658
self._send = self._send_to_server
656659
self._start_flush_thread()
657660

661+
def disable_extended_aggregation(self):
662+
with self._config_lock:
663+
# If the toggle didn't change anything, this method is a noop
664+
if self._disable_extended_aggregation:
665+
return
666+
667+
self._disable_extended_aggregation = True
668+
669+
# If aggregation and buffering has been disabled, flush and kill the background thread
670+
# otherwise start up the flushing thread and enable aggregation.
671+
if self._disable_aggregation and self._disable_extended_aggregation and self.disable_buffering:
672+
self._stop_flush_thread()
673+
log.debug("Statsd aggregation is disabled")
674+
675+
def enable_extended_aggregation(self, flush_interval=DEFAULT_BUFFERING_FLUSH_INTERVAL):
676+
with self._config_lock:
677+
if not self._disable_aggregation:
678+
return
679+
self._disable_aggregation = False
680+
self._flush_interval = flush_interval
681+
if self._disable_buffering:
682+
self._send = self._send_to_server
683+
self._start_flush_thread()
684+
658685
@staticmethod
659686
def resolve_host(host, use_default_route):
660687
"""
@@ -829,11 +856,8 @@ def flush_aggregated_metrics(self):
829856
self._report(m.name, m.metric_type, m.value, m.tags, m.rate, m.timestamp)
830857

831858
buffered_metrics = self.aggregator.flush_aggregated_buffered_metrics()
832-
send_method = self._send
833-
self._send = self._send_to_buffer
834859
for m in buffered_metrics:
835860
self._report(m.name, m.metric_type, m.value, m.tags, m.rate, m.timestamp)
836-
self._send = send_method
837861

838862
def gauge(
839863
self,

0 commit comments

Comments
 (0)