Skip to content
This repository was archived by the owner on May 27, 2024. It is now read-only.

Commit 7acec0c

Browse files
committed
fix PHPCS issues
1 parent 12f8407 commit 7acec0c

File tree

3 files changed

+90
-47
lines changed

3 files changed

+90
-47
lines changed

src/BatchedDogStatsd.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ class BatchedDogStatsd extends DogStatsd
2222
public function __construct(array $config = array())
2323
{
2424
# by default the telemetry is enabled for BatchedDogStatsd
25-
if (!isset($config["disable_telemetry"]))
26-
{
27-
$config["disable_telemetry"] = false;
28-
}
29-
parent::__construct($config);
25+
if (!isset($config["disable_telemetry"])) {
26+
$config["disable_telemetry"] = false;
27+
}
28+
parent::__construct($config);
3029
}
3130

3231
/**
@@ -41,6 +40,15 @@ public function report($message)
4140
}
4241
}
4342

43+
/**
44+
* @deprecated flush_buffer will be removed in future versions in favor of flushBuffer
45+
*/
46+
public function flush_buffer() // phpcs:ignore
47+
{
48+
$this->flushBuffer();
49+
}
50+
51+
4452
public function flushBuffer()
4553
{
4654
$this->flush(join("\n", static::$buffer));

src/DogStatsd.php

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
class DogStatsd
1010
{
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;
1515

1616
/**
1717
* @var string
@@ -58,7 +58,7 @@ class DogStatsd
5858
*/
5959
private $decimalPrecision;
6060

61-
private static $__eventUrl = '/api/v1/events';
61+
private static $eventUrl = '/api/v1/events';
6262

6363
// Used for the telemetry tags
6464
public static $version = '1.5.2';
@@ -77,8 +77,14 @@ class DogStatsd
7777
*/
7878
public function __construct(array $config = array())
7979
{
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+
8288
$this->socketPath = isset($config['socket_path']) ? $config['socket_path'] : null;
8389

8490
$this->datadogHost = isset($config['datadog_host']) ? $config['datadog_host'] : 'https://app.datadoghq.com';
@@ -102,14 +108,18 @@ public function __construct(array $config = array())
102108
# by default the telemetry is disable
103109
$this->disable_telemetry = isset($config["disable_telemetry"]) ? $config["disable_telemetry"] : true;
104110
$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();
107117
}
108118

109119
/**
110120
* Reset the telemetry value to zero
111121
*/
112-
private function reset_telemetry()
122+
private function resetTelemetry()
113123
{
114124
$this->metrics_sent = 0;
115125
$this->events_sent = 0;
@@ -122,13 +132,13 @@ private function reset_telemetry()
122132
/**
123133
* Reset the telemetry value to zero
124134
*/
125-
private function flush_telemetry()
135+
private function flushTelemetry()
126136
{
127-
if ($this->disable_telemetry == true) {
128-
return "";
129-
}
137+
if ($this->disable_telemetry == true) {
138+
return "";
139+
}
130140

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}"
132142
. "\ndatadog.dogstatsd.client.events:{$this->events_sent}|c{$this->telemetry_tags}"
133143
. "\ndatadog.dogstatsd.client.service_checks:{$this->service_checks_sent}|c{$this->telemetry_tags}"
134144
. "\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)
163173
**/
164174
public function microtiming($stat, $time, $sampleRate = 1.0, $tags = null)
165175
{
166-
$this->timing($stat, $time*1000, $sampleRate, $tags);
176+
$this->timing($stat, $time * 1000, $sampleRate, $tags);
167177
}
168178

169179
/**
@@ -286,11 +296,11 @@ public function updateStats($stats, $delta = 1, $sampleRate = 1.0, $tags = null)
286296
* @param string|array $tags The tags to be serialize
287297
* @return string
288298
**/
289-
private function serialize_tags($tags)
299+
private function serializeTags($tags)
290300
{
291301
$all_tags = array_merge(
292-
$this->normalize_tags($this->globalTags),
293-
$this->normalize_tags($tags)
302+
$this->normalizeTags($this->globalTags),
303+
$this->normalizeTags($tags)
294304
);
295305

296306
if (count($all_tags) === 0) {
@@ -313,7 +323,7 @@ private function serialize_tags($tags)
313323
* @param mixed $tags The tags to normalize
314324
* @return array
315325
*/
316-
private function normalize_tags($tags)
326+
private function normalizeTags($tags)
317327
{
318328
if ($tags === null) {
319329
return array();
@@ -370,11 +380,34 @@ public function send($data, $sampleRate = 1.0, $tags = null)
370380
}
371381

372382
foreach ($sampledData as $stat => $value) {
373-
$value .= $this->serialize_tags($tags);
383+
$value .= $this->serializeTags($tags);
374384
$this->report("$stat:$value");
375385
}
376386
}
377387

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+
378411
/**
379412
* Send a custom service check status over UDP
380413
* @param string $name service check name
@@ -385,7 +418,7 @@ public function send($data, $sampleRate = 1.0, $tags = null)
385418
* @param int $timestamp timestamp for the service check status (defaults to now)
386419
* @return void
387420
**/
388-
public function service_check(
421+
public function serviceCheck(
389422
$name,
390423
$status,
391424
$tags = null,
@@ -401,16 +434,16 @@ public function service_check(
401434
if ($hostname !== null) {
402435
$msg .= sprintf("|h:%s", $hostname);
403436
}
404-
$msg .= $this->serialize_tags($tags);
437+
$msg .= $this->serializeTags($tags);
405438
if ($message !== null) {
406-
$msg .= sprintf('|m:%s', $this->escape_sc_message($message));
439+
$msg .= sprintf('|m:%s', $this->escapeScMessage($message));
407440
}
408441

409442
$this->service_checks_sent += 1;
410443
$this->report($msg);
411444
}
412445

413-
private function escape_sc_message($msg)
446+
private function escapeScMessage($msg)
414447
{
415448
return str_replace("m:", "m\:", str_replace("\n", "\\n", $msg));
416449
}
@@ -422,10 +455,11 @@ public function report($message)
422455

423456
public function flush($message)
424457
{
425-
$message .= $this->flush_telemetry();
458+
$message .= $this->flushTelemetry();
426459

427460
// 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);
429463
socket_set_nonblock($socket);
430464

431465
if (!is_null($this->socketPath)) {
@@ -435,12 +469,12 @@ public function flush($message)
435469
}
436470

437471
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;
441475
} else {
442-
$this->bytes_dropped += strlen($message);
443-
$this->packets_dropped += 1;
476+
$this->bytes_dropped += strlen($message);
477+
$this->packets_dropped += 1;
444478
}
445479

446480
socket_close($socket);
@@ -469,7 +503,7 @@ public function event($title, $vals = array())
469503

470504
// Convert tags string or array into array of tags: ie ['key:value']
471505
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));
473507
}
474508

475509
/**
@@ -478,7 +512,7 @@ public function event($title, $vals = array())
478512
$success = true;
479513

480514
// Get the url to POST to
481-
$url = $this->datadogHost . self::$__eventUrl
515+
$url = $this->datadogHost . self::$eventUrl
482516
. '?api_key=' . $this->apiKey
483517
. '&application_key=' . $this->appKey;
484518

@@ -497,15 +531,15 @@ public function event($title, $vals = array())
497531
$response_code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
498532

499533
try {
500-
501534
// Check for cURL errors
502535
if ($curlErrorNum = curl_errno($curl)) {
503536
throw new \Exception('Datadog event API call cURL issue #' . $curlErrorNum . ' - ' . curl_error($curl));
504537
}
505538

506539
// Check response code is 202
507540
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);
509543
}
510544

511545
// Check for empty response body
@@ -515,12 +549,14 @@ public function event($title, $vals = array())
515549

516550
// Decode JSON response
517551
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');
519554
}
520555

521556
// Check JSON decoded "status" is OK from the Datadog API
522557
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);
524560
}
525561
} catch (\Exception $e) {
526562
$success = false;
@@ -558,10 +594,10 @@ private function eventUdp($vals)
558594
$fields .= (isset($vals['priority'])) ? '|p:' . ((string) $vals['priority']) : '';
559595
$fields .= (isset($vals['source_type_name'])) ? '|s:' . ((string) $vals['source_type_name']) : '';
560596
$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']) : '';
562598

563599
$title_length = strlen($title);
564-
$text_length = strlen($textField)-1;
600+
$text_length = strlen($textField) - 1;
565601

566602
$this->events_sent += 1;
567603
$this->report('_e{' . $title_length . ',' . $text_length . '}:' . $fields);
@@ -583,5 +619,4 @@ private function normalizeStat($value)
583619

584620
return rtrim(rtrim(number_format((float) $value, $this->decimalPrecision, '.', ''), "0"), ".");
585621
}
586-
587622
}

tests/UnitTests/BatchedDogStatsdTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function setUp()
1515
// Flush the buffer to reset state for next test
1616
BatchedDogStatsd::$maxBufferLength = 50;
1717
$batchedDog = new BatchedDogStatsd();
18-
$batchedDog->flush_buffer();
18+
$batchedDog->flushBuffer();
1919

2020
// Reset the SocketSpy state to get clean assertions.
2121
// @see \DataDog\SocketSpy

0 commit comments

Comments
 (0)