1+ import errno
12import logging
23import os
3- import socket
4- import errno
54import re
5+ import socket
66from threading import Lock
77
8-
98MIN_SEND_BUFFER_SIZE = 32 * 1024
109log = logging .getLogger ("datadog_lambda.dogstatsd" )
1110
@@ -55,14 +54,17 @@ def _get_udp_socket(cls, host, port):
5554
5655 return sock
5756
58- def distribution (self , metric , value , tags = None ):
57+ def distribution (self , metric , value , tags = None , timestamp = None ):
5958 """
60- Send a global distribution value, optionally setting tags.
59+ Send a global distribution value, optionally setting tags. The optional
60+ timestamp should be an integer representing seconds since the epoch
61+ (January 1, 1970, 00:00:00 UTC).
6162
6263 >>> statsd.distribution("uploaded.file.size", 1445)
6364 >>> statsd.distribution("album.photo.count", 26, tags=["gender:female"])
65+ >>> statsd.distribution("historic.file.count", 5, timestamp=int(datetime(2020, 2, 14, 12, 0, 0).timestamp()))
6466 """
65- self ._report (metric , "d" , value , tags )
67+ self ._report (metric , "d" , value , tags , timestamp )
6668
6769 def close_socket (self ):
6870 """
@@ -84,20 +86,21 @@ def normalize_tags(self, tag_list):
8486 for tag in tag_list
8587 ]
8688
87- def _serialize_metric (self , metric , metric_type , value , tags ):
89+ def _serialize_metric (self , metric , metric_type , value , tags , timestamp ):
8890 # Create/format the metric packet
89- return "%s:%s|%s%s" % (
91+ return "%s:%s|%s%s%s " % (
9092 metric ,
9193 value ,
9294 metric_type ,
9395 ("|#" + "," .join (self .normalize_tags (tags ))) if tags else "" ,
96+ ("|T" + str (timestamp )) if timestamp is not None else "" ,
9497 )
9598
96- def _report (self , metric , metric_type , value , tags ):
99+ def _report (self , metric , metric_type , value , tags , timestamp ):
97100 if value is None :
98101 return
99102
100- payload = self ._serialize_metric (metric , metric_type , value , tags )
103+ payload = self ._serialize_metric (metric , metric_type , value , tags , timestamp )
101104
102105 # Send it
103106 self ._send_to_server (payload )
0 commit comments