8
8
9
9
class DogStatsd
10
10
{
11
- const OK = 0 ;
12
- const WARNING = 1 ;
13
- const CRITICAL = 2 ;
14
- const UNKNOWN = 3 ;
11
+ public const OK = 0 ;
12
+ public const WARNING = 1 ;
13
+ public const CRITICAL = 2 ;
14
+ public const UNKNOWN = 3 ;
15
15
16
16
/**
17
17
* @var string
@@ -58,7 +58,7 @@ class DogStatsd
58
58
*/
59
59
private $ decimalPrecision ;
60
60
61
- private static $ __eventUrl = '/api/v1/events ' ;
61
+ private static $ eventUrl = '/api/v1/events ' ;
62
62
63
63
// Used for the telemetry tags
64
64
public static $ version = '1.5.2 ' ;
@@ -77,8 +77,14 @@ class DogStatsd
77
77
*/
78
78
public function __construct (array $ config = array ())
79
79
{
80
- $ this ->host = isset ($ config ['host ' ]) ? $ config ['host ' ] : (getenv ('DD_AGENT_HOST ' ) ? getenv ('DD_AGENT_HOST ' ) : 'localhost ' );
81
- $ this ->port = isset ($ config ['port ' ]) ? $ config ['port ' ] : (getenv ('DD_DOGSTATSD_PORT ' ) ? (int )getenv ('DD_DOGSTATSD_PORT ' ) : 8125 );
80
+ $ this ->host = isset ($ config ['host ' ])
81
+ ? $ config ['host ' ] : (getenv ('DD_AGENT_HOST ' )
82
+ ? getenv ('DD_AGENT_HOST ' ) : 'localhost ' );
83
+
84
+ $ this ->port = isset ($ config ['port ' ])
85
+ ? $ config ['port ' ] : (getenv ('DD_DOGSTATSD_PORT ' )
86
+ ? (int )getenv ('DD_DOGSTATSD_PORT ' ) : 8125 );
87
+
82
88
$ this ->socketPath = isset ($ config ['socket_path ' ]) ? $ config ['socket_path ' ] : null ;
83
89
84
90
$ this ->datadogHost = isset ($ config ['datadog_host ' ]) ? $ config ['datadog_host ' ] : 'https://app.datadoghq.com ' ;
@@ -102,14 +108,18 @@ public function __construct(array $config = array())
102
108
# by default the telemetry is disable
103
109
$ this ->disable_telemetry = isset ($ config ["disable_telemetry " ]) ? $ config ["disable_telemetry " ] : true ;
104
110
$ transport_type = !is_null ($ this ->socketPath ) ? "uds " : "udp " ;
105
- $ this ->telemetry_tags = $ this ->serialize_tags (array ("client " => "php " , "client_version " => self ::$ version , "client_transport " => $ transport_type ));
106
- $ this ->reset_telemetry ();
111
+ $ this ->telemetry_tags = $ this ->serializeTags (array (
112
+ "client " => "php " ,
113
+ "client_version " => self ::$ version ,
114
+ "client_transport " => $ transport_type ));
115
+
116
+ $ this ->resetTelemetry ();
107
117
}
108
118
109
119
/**
110
120
* Reset the telemetry value to zero
111
121
*/
112
- private function reset_telemetry ()
122
+ private function resetTelemetry ()
113
123
{
114
124
$ this ->metrics_sent = 0 ;
115
125
$ this ->events_sent = 0 ;
@@ -122,13 +132,13 @@ private function reset_telemetry()
122
132
/**
123
133
* Reset the telemetry value to zero
124
134
*/
125
- private function flush_telemetry ()
135
+ private function flushTelemetry ()
126
136
{
127
- if ($ this ->disable_telemetry == true ) {
128
- return "" ;
129
- }
137
+ if ($ this ->disable_telemetry == true ) {
138
+ return "" ;
139
+ }
130
140
131
- return "\ndatadog.dogstatsd.client.metrics: {$ this ->metrics_sent }|c {$ this ->telemetry_tags }"
141
+ return "\ndatadog.dogstatsd.client.metrics: {$ this ->metrics_sent }|c {$ this ->telemetry_tags }"
132
142
. "\ndatadog.dogstatsd.client.events: {$ this ->events_sent }|c {$ this ->telemetry_tags }"
133
143
. "\ndatadog.dogstatsd.client.service_checks: {$ this ->service_checks_sent }|c {$ this ->telemetry_tags }"
134
144
. "\ndatadog.dogstatsd.client.bytes_sent: {$ this ->bytes_sent }|c {$ this ->telemetry_tags }"
@@ -163,7 +173,7 @@ public function timing($stat, $time, $sampleRate = 1.0, $tags = null)
163
173
**/
164
174
public function microtiming ($ stat , $ time , $ sampleRate = 1.0 , $ tags = null )
165
175
{
166
- $ this ->timing ($ stat , $ time* 1000 , $ sampleRate , $ tags );
176
+ $ this ->timing ($ stat , $ time * 1000 , $ sampleRate , $ tags );
167
177
}
168
178
169
179
/**
@@ -286,11 +296,11 @@ public function updateStats($stats, $delta = 1, $sampleRate = 1.0, $tags = null)
286
296
* @param string|array $tags The tags to be serialize
287
297
* @return string
288
298
**/
289
- private function serialize_tags ($ tags )
299
+ private function serializeTags ($ tags )
290
300
{
291
301
$ all_tags = array_merge (
292
- $ this ->normalize_tags ($ this ->globalTags ),
293
- $ this ->normalize_tags ($ tags )
302
+ $ this ->normalizeTags ($ this ->globalTags ),
303
+ $ this ->normalizeTags ($ tags )
294
304
);
295
305
296
306
if (count ($ all_tags ) === 0 ) {
@@ -313,7 +323,7 @@ private function serialize_tags($tags)
313
323
* @param mixed $tags The tags to normalize
314
324
* @return array
315
325
*/
316
- private function normalize_tags ($ tags )
326
+ private function normalizeTags ($ tags )
317
327
{
318
328
if ($ tags === null ) {
319
329
return array ();
@@ -370,11 +380,34 @@ public function send($data, $sampleRate = 1.0, $tags = null)
370
380
}
371
381
372
382
foreach ($ sampledData as $ stat => $ value ) {
373
- $ value .= $ this ->serialize_tags ($ tags );
383
+ $ value .= $ this ->serializeTags ($ tags );
374
384
$ this ->report ("$ stat: $ value " );
375
385
}
376
386
}
377
387
388
+ /**
389
+ * @deprecated service_check will be removed in future versions in favor of serviceCheck
390
+ *
391
+ * Send a custom service check status over UDP
392
+ * @param string $name service check name
393
+ * @param int $status service check status code (see OK, WARNING,...)
394
+ * @param array|string $tags Key Value array of Tag => Value, or single tag as string
395
+ * @param string $hostname hostname to associate with this service check status
396
+ * @param string $message message to associate with this service check status
397
+ * @param int $timestamp timestamp for the service check status (defaults to now)
398
+ * @return void
399
+ **/
400
+ public function service_check ( // phpcs:ignore
401
+ $ name ,
402
+ $ status ,
403
+ $ tags = null ,
404
+ $ hostname = null ,
405
+ $ message = null ,
406
+ $ timestamp = null
407
+ ) {
408
+ $ this ->serviceCheck ($ name , $ status , $ tags , $ hostname , $ message , $ timestamp );
409
+ }
410
+
378
411
/**
379
412
* Send a custom service check status over UDP
380
413
* @param string $name service check name
@@ -385,7 +418,7 @@ public function send($data, $sampleRate = 1.0, $tags = null)
385
418
* @param int $timestamp timestamp for the service check status (defaults to now)
386
419
* @return void
387
420
**/
388
- public function service_check (
421
+ public function serviceCheck (
389
422
$ name ,
390
423
$ status ,
391
424
$ tags = null ,
@@ -401,16 +434,16 @@ public function service_check(
401
434
if ($ hostname !== null ) {
402
435
$ msg .= sprintf ("|h:%s " , $ hostname );
403
436
}
404
- $ msg .= $ this ->serialize_tags ($ tags );
437
+ $ msg .= $ this ->serializeTags ($ tags );
405
438
if ($ message !== null ) {
406
- $ msg .= sprintf ('|m:%s ' , $ this ->escape_sc_message ($ message ));
439
+ $ msg .= sprintf ('|m:%s ' , $ this ->escapeScMessage ($ message ));
407
440
}
408
441
409
442
$ this ->service_checks_sent += 1 ;
410
443
$ this ->report ($ msg );
411
444
}
412
445
413
- private function escape_sc_message ($ msg )
446
+ private function escapeScMessage ($ msg )
414
447
{
415
448
return str_replace ("m: " , "m\: " , str_replace ("\n" , "\\n " , $ msg ));
416
449
}
@@ -422,10 +455,11 @@ public function report($message)
422
455
423
456
public function flush ($ message )
424
457
{
425
- $ message .= $ this ->flush_telemetry ();
458
+ $ message .= $ this ->flushTelemetry ();
426
459
427
460
// Non - Blocking UDP I/O - Use IP Addresses!
428
- $ socket = is_null ($ this ->socketPath ) ? socket_create (AF_INET , SOCK_DGRAM , SOL_UDP ) : socket_create (AF_UNIX , SOCK_DGRAM , 0 );
461
+ $ socket = is_null ($ this ->socketPath ) ? socket_create (AF_INET , SOCK_DGRAM , SOL_UDP )
462
+ : socket_create (AF_UNIX , SOCK_DGRAM , 0 );
429
463
socket_set_nonblock ($ socket );
430
464
431
465
if (!is_null ($ this ->socketPath )) {
@@ -435,12 +469,12 @@ public function flush($message)
435
469
}
436
470
437
471
if ($ res !== false ) {
438
- $ this ->reset_telemetry ();
439
- $ this ->bytes_sent += strlen ($ message );
440
- $ this ->packets_sent += 1 ;
472
+ $ this ->resetTelemetry ();
473
+ $ this ->bytes_sent += strlen ($ message );
474
+ $ this ->packets_sent += 1 ;
441
475
} else {
442
- $ this ->bytes_dropped += strlen ($ message );
443
- $ this ->packets_dropped += 1 ;
476
+ $ this ->bytes_dropped += strlen ($ message );
477
+ $ this ->packets_dropped += 1 ;
444
478
}
445
479
446
480
socket_close ($ socket );
@@ -469,7 +503,7 @@ public function event($title, $vals = array())
469
503
470
504
// Convert tags string or array into array of tags: ie ['key:value']
471
505
if (isset ($ vals ['tags ' ])) {
472
- $ vals ['tags ' ] = explode (", " , substr ($ this ->serialize_tags ($ vals ['tags ' ]), 2 ));
506
+ $ vals ['tags ' ] = explode (", " , substr ($ this ->serializeTags ($ vals ['tags ' ]), 2 ));
473
507
}
474
508
475
509
/**
@@ -478,7 +512,7 @@ public function event($title, $vals = array())
478
512
$ success = true ;
479
513
480
514
// Get the url to POST to
481
- $ url = $ this ->datadogHost . self ::$ __eventUrl
515
+ $ url = $ this ->datadogHost . self ::$ eventUrl
482
516
. '?api_key= ' . $ this ->apiKey
483
517
. '&application_key= ' . $ this ->appKey ;
484
518
@@ -497,15 +531,15 @@ public function event($title, $vals = array())
497
531
$ response_code = (int ) curl_getinfo ($ curl , CURLINFO_HTTP_CODE );
498
532
499
533
try {
500
-
501
534
// Check for cURL errors
502
535
if ($ curlErrorNum = curl_errno ($ curl )) {
503
536
throw new \Exception ('Datadog event API call cURL issue # ' . $ curlErrorNum . ' - ' . curl_error ($ curl ));
504
537
}
505
538
506
539
// Check response code is 202
507
540
if ($ response_code !== 200 && $ response_code !== 202 ) {
508
- throw new \Exception ('Datadog event API call HTTP response not OK - ' . $ response_code . '; response body: ' . $ response_body );
541
+ throw new \Exception ('Datadog event API call HTTP response not OK - '
542
+ . $ response_code . '; response body: ' . $ response_body );
509
543
}
510
544
511
545
// Check for empty response body
@@ -515,12 +549,14 @@ public function event($title, $vals = array())
515
549
516
550
// Decode JSON response
517
551
if (!$ decodedJson = json_decode ($ response_body , true )) {
518
- throw new \Exception ('Datadog event API call did not return a body that could be decoded via json_decode ' );
552
+ throw new \Exception ('Datadog event API call did not return a body '
553
+ . ' that could be decoded via json_decode ' );
519
554
}
520
555
521
556
// Check JSON decoded "status" is OK from the Datadog API
522
557
if ($ decodedJson ['status ' ] !== 'ok ' ) {
523
- throw new \Exception ('Datadog event API response status not "ok"; response body: ' . $ response_body );
558
+ throw new \Exception ('Datadog event API response status not "ok"; response body: '
559
+ . $ response_body );
524
560
}
525
561
} catch (\Exception $ e ) {
526
562
$ success = false ;
@@ -558,10 +594,10 @@ private function eventUdp($vals)
558
594
$ fields .= (isset ($ vals ['priority ' ])) ? '|p: ' . ((string ) $ vals ['priority ' ]) : '' ;
559
595
$ fields .= (isset ($ vals ['source_type_name ' ])) ? '|s: ' . ((string ) $ vals ['source_type_name ' ]) : '' ;
560
596
$ fields .= (isset ($ vals ['alert_type ' ])) ? '|t: ' . ((string ) $ vals ['alert_type ' ]) : '' ;
561
- $ fields .= (isset ($ vals ['tags ' ])) ? $ this ->serialize_tags ($ vals ['tags ' ]) : '' ;
597
+ $ fields .= (isset ($ vals ['tags ' ])) ? $ this ->serializeTags ($ vals ['tags ' ]) : '' ;
562
598
563
599
$ title_length = strlen ($ title );
564
- $ text_length = strlen ($ textField )- 1 ;
600
+ $ text_length = strlen ($ textField ) - 1 ;
565
601
566
602
$ this ->events_sent += 1 ;
567
603
$ this ->report ('_e{ ' . $ title_length . ', ' . $ text_length . '}: ' . $ fields );
@@ -583,5 +619,4 @@ private function normalizeStat($value)
583
619
584
620
return rtrim (rtrim (number_format ((float ) $ value , $ this ->decimalPrecision , '. ' , '' ), "0 " ), ". " );
585
621
}
586
-
587
622
}
0 commit comments