1818from datadog .dogstatsd .context import TimedContextManagerDecorator
1919from datadog .dogstatsd .route import get_default_route
2020from datadog .util .compat import text
21- from datadog .util .config import get_version
2221
2322# Logging
2423log = logging .getLogger ('datadog.dogstatsd' )
@@ -36,7 +35,7 @@ class DogStatsd(object):
3635
3736 def __init__ (self , host = DEFAULT_HOST , port = DEFAULT_PORT , max_buffer_size = 50 , namespace = None ,
3837 constant_tags = None , use_ms = False , use_default_route = False ,
39- socket_path = None , default_sample_rate = 1 , disable_telemetry = False ):
38+ socket_path = None , default_sample_rate = 1 ):
4039 """
4140 Initialize a DogStatsd object.
4241
@@ -107,12 +106,10 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, max_buffer_size=50, nam
107106 self .socket_path = socket_path
108107 self .host = None
109108 self .port = None
110- transport = "uds"
111109 else :
112110 self .socket_path = None
113111 self .host = self .resolve_host (host , use_default_route )
114112 self .port = int (port )
115- transport = "udp"
116113
117114 # Socket
118115 self .socket = None
@@ -135,21 +132,6 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, max_buffer_size=50, nam
135132 self .use_ms = use_ms
136133 self .default_sample_rate = default_sample_rate
137134
138- # init telemetry
139- self ._client_tags = [
140- "client:py" ,
141- "client_version:{}" .format (get_version ()),
142- "client_transport:{}" .format (transport ),
143- ]
144- self ._reset_telementry ()
145- self ._telemetry = not disable_telemetry
146-
147- def disable_telemetry (self ):
148- self ._telemetry = False
149-
150- def enable_telemetry (self ):
151- self ._telemetry = True
152-
153135 def __enter__ (self ):
154136 self .open_buffer (self .max_buffer_size )
155137 return self
@@ -321,17 +303,6 @@ def close_socket(self):
321303 log .error ("Unexpected error: %s" , str (e ))
322304 self .socket = None
323305
324- def _serialize_metric (self , metric , metric_type , value , tags , sample_rate = 1 ):
325- # Create/format the metric packet
326- return "%s%s:%s|%s%s%s" % (
327- (self .namespace + "." ) if self .namespace else "" ,
328- metric ,
329- value ,
330- metric_type ,
331- ("|@" + text (sample_rate )) if sample_rate != 1 else "" ,
332- ("|#" + "," .join (tags )) if tags else "" ,
333- )
334-
335306 def _report (self , metric , metric_type , value , tags , sample_rate ):
336307 """
337308 Create a metric packet and send it.
@@ -341,9 +312,6 @@ def _report(self, metric, metric_type, value, tags, sample_rate):
341312 if value is None :
342313 return
343314
344- if self ._telemetry :
345- self .metrics_count += 1
346-
347315 if sample_rate is None :
348316 sample_rate = self .default_sample_rate
349317
@@ -352,53 +320,27 @@ def _report(self, metric, metric_type, value, tags, sample_rate):
352320
353321 # Resolve the full tag list
354322 tags = self ._add_constant_tags (tags )
355- payload = self ._serialize_metric (metric , metric_type , value , tags , sample_rate )
323+
324+ # Create/format the metric packet
325+ payload = "%s%s:%s|%s%s%s" % (
326+ (self .namespace + "." ) if self .namespace else "" ,
327+ metric ,
328+ value ,
329+ metric_type ,
330+ ("|@" + text (sample_rate )) if sample_rate != 1 else "" ,
331+ ("|#" + "," .join (tags )) if tags else "" ,
332+ )
356333
357334 # Send it
358335 self ._send (payload )
359336
360- def _reset_telementry (self ):
361- self .metrics_count = 0
362- self .events_count = 0
363- self .service_checks_count = 0
364- self .bytes_sent = 0
365- self .bytes_dropped = 0
366- self .packets_sent = 0
367- self .packets_dropped = 0
368-
369- def _flush_telemetry (self ):
370- telemetry_tags = self ._add_constant_tags (self ._client_tags )
371- return "\n %s\n %s\n %s\n %s\n %s\n %s\n %s" % (
372- self ._serialize_metric ("datadog.dogstatsd.client.metrics" ,
373- "c" , self .metrics_count , telemetry_tags ),
374- self ._serialize_metric ("datadog.dogstatsd.client.events" ,
375- "c" , self .events_count , telemetry_tags ),
376- self ._serialize_metric ("datadog.dogstatsd.client.service_checks" ,
377- "c" , self .service_checks_count , telemetry_tags ),
378- self ._serialize_metric ("datadog.dogstatsd.client.bytes_sent" ,
379- "c" , self .bytes_sent , telemetry_tags ),
380- self ._serialize_metric ("datadog.dogstatsd.client.bytes_dropped" ,
381- "c" , self .bytes_dropped , telemetry_tags ),
382- self ._serialize_metric ("datadog.dogstatsd.client.packets_sent" ,
383- "c" , self .packets_sent , telemetry_tags ),
384- self ._serialize_metric ("datadog.dogstatsd.client.packets_dropped" ,
385- "c" , self .packets_dropped , telemetry_tags ),
386- )
387-
388337 def _send_to_server (self , packet ):
389- if self ._telemetry :
390- packet += self ._flush_telemetry ()
391338 try :
392339 # If set, use socket directly
393340 (self .socket or self .get_socket ()).send (packet .encode (self .encoding ))
394- if self ._telemetry :
395- self ._reset_telementry ()
396- self .packets_sent += 1
397- self .bytes_sent += len (packet )
398- return
399341 except socket .timeout :
400342 # dogstatsd is overflowing, drop the packets (mimicks the UDP behaviour)
401- pass
343+ return
402344 except (socket .herror , socket .gaierror ) as se :
403345 log .warning ("Error submitting packet: {}, dropping the packet and closing the socket" .format (se ))
404346 self .close_socket ()
@@ -410,10 +352,7 @@ def _send_to_server(self, packet):
410352 self .close_socket ()
411353 except Exception as e :
412354 log .error ("Unexpected error: %s" , str (e ))
413-
414- if self ._telemetry :
415- self .bytes_dropped += len (packet )
416- self .packets_dropped += 1
355+ return
417356
418357 def _send_to_buffer (self , packet ):
419358 self .buffer .append (packet )
@@ -466,8 +405,6 @@ def event(self, title, text, alert_type=None, aggregation_key=None,
466405 raise Exception (u'Event "%s" payload is too big (more than 8KB), '
467406 'event discarded' % title )
468407
469- if self ._telemetry :
470- self .events_count += 1
471408 self ._send (string )
472409
473410 def service_check (self , check_name , status , tags = None , timestamp = None ,
@@ -493,8 +430,6 @@ def service_check(self, check_name, status, tags=None, timestamp=None,
493430 if message :
494431 string = u'{0}|m:{1}' .format (string , message )
495432
496- if self ._telemetry :
497- self .service_checks_count += 1
498433 self ._send (string )
499434
500435 def _add_constant_tags (self , tags ):
0 commit comments