50
50
DEFAULT_PORT = 8125
51
51
52
52
# Buffering-related values (in seconds)
53
- DEFAULT_BUFFERING_FLUSH_INTERVAL = 0.3
53
+ DEFAULT_FLUSH_INTERVAL = 0.3
54
54
MIN_FLUSH_INTERVAL = 0.0001
55
55
56
- # Aggregation-related values (in seconds)
57
- DEFAULT_AGGREGATION_FLUSH_INTERVAL = 2
58
56
# Env var to enable/disable sending the container ID field
59
57
ORIGIN_DETECTION_ENABLED = "DD_ORIGIN_DETECTION_ENABLED"
60
58
@@ -147,8 +145,8 @@ def __init__(
147
145
host = DEFAULT_HOST , # type: Text
148
146
port = DEFAULT_PORT , # type: int
149
147
max_buffer_size = None , # type: None
150
- flush_interval = DEFAULT_BUFFERING_FLUSH_INTERVAL , # type: float
151
- disable_aggregating = True , # type: bool
148
+ flush_interval = DEFAULT_FLUSH_INTERVAL , # type: float
149
+ disable_aggregation = True , # type: bool
152
150
disable_buffering = True , # type: bool
153
151
namespace = None , # type: Optional[Text]
154
152
constant_tags = None , # type: Optional[List[str]]
@@ -238,8 +236,8 @@ def __init__(
238
236
it overrides the default value.
239
237
:type flush_interval: float
240
238
241
- :disable_aggregating : If true, metrics (Count, Gauge, Set) are no longered aggregated by the client
242
- :type disable_aggregating : bool
239
+ :disable_aggregation : If true, metrics (Count, Gauge, Set) are no longered aggregated by the client
240
+ :type disable_aggregation : bool
243
241
244
242
:disable_buffering: If set, metrics are no longered buffered by the client and
245
243
all data is sent synchronously to the server
@@ -447,34 +445,24 @@ def __init__(
447
445
self ._config_lock = RLock ()
448
446
449
447
self ._disable_buffering = disable_buffering
450
- self ._disable_aggregating = disable_aggregating
448
+ self ._disable_aggregation = disable_aggregation
451
449
452
450
self ._flush_interval = flush_interval
453
451
self ._flush_thread = None
454
452
self ._flush_thread_stop = threading .Event ()
455
453
self .aggregator = Aggregator ()
456
454
# Indicates if the process is about to fork, so we shouldn't start any new threads yet.
457
455
self ._forking = False
458
- # Currently, we do not allow both aggregation and buffering, we may revisit this in the future
459
- if self ._disable_buffering and self ._disable_aggregating :
460
- self ._send = self ._send_to_server
461
- log .debug ("Statsd buffering and aggregation is disabled" )
462
- elif self ._disable_aggregating :
463
- # Start the flush thread if buffering is enabled and the interval is above
464
- # a reasonable range. This both prevents thrashing and allow us to use "0.0"
465
- # as a value for disabling the automatic flush timer as well.
456
+
457
+ if not self ._disable_buffering :
466
458
self ._send = self ._send_to_buffer
467
- self ._start_flush_thread (
468
- self ._flush_interval ,
469
- self .flush_buffered_metrics ,
470
- )
471
459
else :
472
460
self ._send = self ._send_to_server
473
- self . _disable_buffering = True
474
- self ._start_flush_thread (
475
- self ._flush_interval ,
476
- self . flush_aggregated_metrics ,
477
- )
461
+
462
+ if not self . _disable_aggregation or not self ._disable_buffering :
463
+ self ._start_flush_thread ()
464
+ else :
465
+ log . debug ( "Statsd buffering and aggregation is disabled" )
478
466
479
467
self ._queue = None
480
468
self ._sender_thread = None
@@ -551,30 +539,14 @@ def enable_telemetry(self):
551
539
self ._telemetry = True
552
540
553
541
# Note: Invocations of this method should be thread-safe
554
- def _start_flush_thread (
555
- self ,
556
- flush_interval ,
557
- flush_function ,
558
- ):
559
- if (self ._disable_buffering or not self ._disable_aggregating ) and flush_function == self .flush_buffered_metrics :
560
- log .debug ("Statsd periodic buffer flush is disabled" )
542
+ def _start_flush_thread (self ):
543
+ if self ._disable_aggregation and self .disable_buffering :
544
+ log .debug ("Statsd periodic buffer and aggregation flush is disabled" )
561
545
return
562
- if (
563
- self ._disable_aggregating
564
- and flush_function == self .flush_aggregated_metrics
565
- ):
566
- log .debug ("Statsd periodic aggregating flush is disabled" )
567
- return
568
-
569
- flush_type = ""
570
- if self ._disable_buffering :
571
- flush_type = "aggregation"
572
- else :
573
- flush_type = "buffering"
574
546
575
- if flush_interval <= MIN_FLUSH_INTERVAL :
547
+ if self . _flush_interval <= MIN_FLUSH_INTERVAL :
576
548
log .debug (
577
- "the set flush interval for %s is less then the minimum" , flush_type
549
+ "the set flush interval is less then the minimum"
578
550
)
579
551
return
580
552
@@ -587,30 +559,31 @@ def _start_flush_thread(
587
559
def _flush_thread_loop (self , flush_interval ):
588
560
while not self ._flush_thread_stop .is_set ():
589
561
time .sleep (flush_interval )
590
- flush_function ()
591
-
562
+ if not self ._disable_aggregation :
563
+ self .flush_aggregated_metrics ()
564
+ if not self ._disable_buffering :
565
+ self .flush_buffered_metrics ()
592
566
self ._flush_thread = threading .Thread (
593
567
name = "{}_flush_thread" .format (self .__class__ .__name__ ),
594
568
target = _flush_thread_loop ,
595
- args = (self , flush_interval ,),
569
+ args = (self , self . _flush_interval ,),
596
570
)
597
571
self ._flush_thread .daemon = True
598
572
self ._flush_thread .start ()
599
573
log .debug (
600
- "Statsd %s flush thread registered with period of %s" ,
601
- flush_type ,
602
- flush_interval ,
574
+ "Statsd flush thread registered with period of %s" ,
575
+ self ._flush_interval ,
603
576
)
604
577
605
578
# Note: Invocations of this method should be thread-safe
606
579
def _stop_flush_thread (self ):
607
580
if not self ._flush_thread :
608
581
return
609
582
try :
610
- if self ._disable_aggregating :
611
- self .flush_buffered_metrics ()
612
- else :
583
+ if not self ._disable_aggregation :
613
584
self .flush_aggregated_metrics ()
585
+ if not self .disable_buffering :
586
+ self .flush_buffered_metrics ()
614
587
finally :
615
588
pass
616
589
@@ -645,43 +618,40 @@ def disable_buffering(self, is_disabled):
645
618
646
619
self ._disable_buffering = is_disabled
647
620
648
- # If buffering has been disabled, flush and kill the background thread
621
+ # If buffering (and aggregation) has been disabled, flush and kill the background thread
649
622
# otherwise start up the flushing thread and enable the buffering.
650
623
if is_disabled :
651
624
self ._send = self ._send_to_server
652
- self ._stop_flush_thread ()
625
+ if self ._disable_aggregation and self .disable_buffering :
626
+ self ._stop_flush_thread ()
653
627
log .debug ("Statsd buffering is disabled" )
654
628
else :
655
629
self ._send = self ._send_to_buffer
656
- self ._start_flush_thread (
657
- self ._flush_interval ,
658
- self .flush_buffered_metrics ,
659
- )
630
+ self ._start_flush_thread ()
660
631
661
632
def disable_aggregation (self ):
662
633
with self ._config_lock :
663
634
# If the toggle didn't change anything, this method is a noop
664
- if self ._disable_aggregating :
635
+ if self ._disable_aggregation :
665
636
return
666
637
667
- self ._disable_aggregating = True
638
+ self ._disable_aggregation = True
668
639
669
- # If aggregation has been disabled, flush and kill the background thread
640
+ # If aggregation and buffering has been disabled, flush and kill the background thread
670
641
# otherwise start up the flushing thread and enable aggregation.
671
- self ._stop_flush_thread ()
642
+ if self ._disable_aggregation and self .disable_buffering :
643
+ self ._stop_flush_thread ()
672
644
log .debug ("Statsd aggregation is disabled" )
673
645
674
- def enable_aggregation (self , aggregation_flush_interval = DEFAULT_AGGREGATION_FLUSH_INTERVAL ):
646
+ def enable_aggregation (self , flush_interval = DEFAULT_FLUSH_INTERVAL ):
675
647
with self ._config_lock :
676
- if not self ._disable_aggregating :
648
+ if not self ._disable_aggregation :
677
649
return
678
- self ._disable_aggregating = False
679
- self ._flush_interval = aggregation_flush_interval
680
- self ._send = self ._send_to_server
681
- self ._start_flush_thread (
682
- self ._flush_interval ,
683
- self .flush_aggregated_metrics ,
684
- )
650
+ self ._disable_aggregation = False
651
+ self ._flush_interval = flush_interval
652
+ if self ._disable_buffering :
653
+ self ._send = self ._send_to_server
654
+ self ._start_flush_thread ()
685
655
686
656
@staticmethod
687
657
def resolve_host (host , use_default_route ):
@@ -867,7 +837,7 @@ def gauge(
867
837
>>> statsd.gauge("users.online", 123)
868
838
>>> statsd.gauge("active.connections", 1001, tags=["protocol:http"])
869
839
"""
870
- if self ._disable_aggregating :
840
+ if self ._disable_aggregation :
871
841
self ._report (metric , "g" , value , tags , sample_rate )
872
842
else :
873
843
self .aggregator .gauge (metric , value , tags , sample_rate )
@@ -890,7 +860,7 @@ def gauge_with_timestamp(
890
860
>>> statsd.gauge("users.online", 123, 1713804588)
891
861
>>> statsd.gauge("active.connections", 1001, 1713804588, tags=["protocol:http"])
892
862
"""
893
- if self ._disable_aggregating :
863
+ if self ._disable_aggregation :
894
864
self ._report (metric , "g" , value , tags , sample_rate , timestamp )
895
865
else :
896
866
self .aggregator .gauge (metric , value , tags , sample_rate , timestamp )
@@ -908,7 +878,7 @@ def count(
908
878
909
879
>>> statsd.count("page.views", 123)
910
880
"""
911
- if self ._disable_aggregating :
881
+ if self ._disable_aggregation :
912
882
self ._report (metric , "c" , value , tags , sample_rate )
913
883
else :
914
884
self .aggregator .count (metric , value , tags , sample_rate )
@@ -930,7 +900,7 @@ def count_with_timestamp(
930
900
931
901
>>> statsd.count("files.transferred", 124, timestamp=1713804588)
932
902
"""
933
- if self ._disable_aggregating :
903
+ if self ._disable_aggregation :
934
904
self ._report (metric , "c" , value , tags , sample_rate , timestamp )
935
905
else :
936
906
self .aggregator .count (metric , value , tags , sample_rate , timestamp )
@@ -949,7 +919,7 @@ def increment(
949
919
>>> statsd.increment("page.views")
950
920
>>> statsd.increment("files.transferred", 124)
951
921
"""
952
- if self ._disable_aggregating :
922
+ if self ._disable_aggregation :
953
923
self ._report (metric , "c" , value , tags , sample_rate )
954
924
else :
955
925
self .aggregator .count (metric , value , tags , sample_rate )
@@ -969,7 +939,7 @@ def decrement(
969
939
>>> statsd.decrement("active.connections", 2)
970
940
"""
971
941
metric_value = - value if value else value
972
- if self ._disable_aggregating :
942
+ if self ._disable_aggregation :
973
943
self ._report (metric , "c" , metric_value , tags , sample_rate )
974
944
else :
975
945
self .aggregator .count (metric , metric_value , tags , sample_rate )
@@ -1080,7 +1050,7 @@ def set(self, metric, value, tags=None, sample_rate=None):
1080
1050
1081
1051
>>> statsd.set("visitors.uniques", 999)
1082
1052
"""
1083
- if self ._disable_aggregating :
1053
+ if self ._disable_aggregation :
1084
1054
self ._report (metric , "s" , value , tags , sample_rate )
1085
1055
else :
1086
1056
self .aggregator .set (metric , value , tags , sample_rate )
@@ -1533,16 +1503,7 @@ def pre_fork(self):
1533
1503
1534
1504
def post_fork_parent (self ):
1535
1505
"""Restore the client state after a fork in the parent process."""
1536
- if self ._disable_aggregating :
1537
- self ._start_flush_thread (
1538
- self ._flush_interval ,
1539
- self .flush_buffered_metrics ,
1540
- )
1541
- else :
1542
- self ._start_flush_thread (
1543
- self ._flush_interval ,
1544
- self .flush_aggregated_metrics ,
1545
- )
1506
+ self ._start_flush_thread ()
1546
1507
self ._start_sender_thread ()
1547
1508
self ._config_lock .release ()
1548
1509
@@ -1565,16 +1526,7 @@ def post_fork_child(self):
1565
1526
self .close_socket ()
1566
1527
1567
1528
with self ._config_lock :
1568
- if self ._disable_aggregating :
1569
- self ._start_flush_thread (
1570
- self ._flush_interval ,
1571
- self .flush_buffered_metrics ,
1572
- )
1573
- else :
1574
- self ._start_flush_thread (
1575
- self ._flush_interval ,
1576
- self .flush_aggregated_metrics ,
1577
- )
1529
+ self ._start_flush_thread ()
1578
1530
self ._start_sender_thread ()
1579
1531
1580
1532
def stop (self ):
@@ -1587,9 +1539,9 @@ def stop(self):
1587
1539
1588
1540
self .disable_background_sender ()
1589
1541
self ._disable_buffering = True
1590
- self ._disable_aggregating = True
1591
- self .flush_buffered_metrics ()
1542
+ self ._disable_aggregation = True
1592
1543
self .flush_aggregated_metrics ()
1544
+ self .flush_buffered_metrics ()
1593
1545
self .close_socket ()
1594
1546
1595
1547
0 commit comments