| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuggio\StatsdClient; |
| 4 |
|
| 5 |
use Liuggio\StatsdClient\Sender\SenderInterface; |
| 6 |
use Liuggio\StatsdClient\Entity\StatsdDataInterface; |
| 7 |
use Liuggio\StatsdClient\Exception\InvalidArgumentException; |
| 8 |
|
| 9 |
class StatsdClient implements StatsdClientInterface |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var boolean |
| 13 |
*/ |
| 14 |
private $failSilently; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var \Liuggio\StatsdClient\Sender\SenderInterface |
| 18 |
*/ |
| 19 |
private $sender; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var boolean |
| 23 |
*/ |
| 24 |
private $reducePacket; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @param \Liuggio\StatsdClient\Sender\SenderInterface $sender |
| 30 |
* @param Boolean $reducePacket |
| 31 |
* @param Boolean $fail_silently |
| 32 |
*/ |
| 33 |
public function __construct(SenderInterface $sender, $reducePacket = true, $fail_silently = true) |
| 34 |
{ |
| 35 |
$this->sender = $sender; |
| 36 |
$this->reducePacket = $reducePacket; |
| 37 |
$this->failSilently = $fail_silently; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Throws an exc only if failSilently if getFailSilently is false. |
| 42 |
* |
| 43 |
* @param \Exception $exception |
| 44 |
* |
| 45 |
* @throws \Exception |
| 46 |
*/ |
| 47 |
private function throwException(\Exception $exception) |
| 48 |
{ |
| 49 |
if (!$this->getFailSilently()) { |
| 50 |
throw $exception; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* This function reduces the number of packets,the reduced has the maximum dimension of self::MAX_UDP_SIZE_STR |
| 56 |
* Reference: |
| 57 |
* https://github.com/etsy/statsd/blob/master/README.md |
| 58 |
* All metrics can also be batch send in a single UDP packet, separated by a newline character. |
| 59 |
* |
| 60 |
* @param array $reducedMetrics |
| 61 |
* @param array $metric |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
private static function doReduce($reducedMetrics, $metric) |
| 66 |
{ |
| 67 |
$metricLength = strlen($metric); |
| 68 |
$lastReducedMetric = count($reducedMetrics) > 0 ? end($reducedMetrics) : null; |
| 69 |
|
| 70 |
if ($metricLength >= self::MAX_UDP_SIZE_STR |
| 71 |
|| null === $lastReducedMetric |
| 72 |
|| strlen($newMetric = $lastReducedMetric . "\n" . $metric) > self::MAX_UDP_SIZE_STR |
| 73 |
) { |
| 74 |
$reducedMetrics[] = $metric; |
| 75 |
} else { |
| 76 |
array_pop($reducedMetrics); |
| 77 |
$reducedMetrics[] = $newMetric; |
| 78 |
} |
| 79 |
|
| 80 |
return $reducedMetrics; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* this function reduce the amount of data that should be send |
| 86 |
* |
| 87 |
* @param mixed $arrayData |
| 88 |
* |
| 89 |
* @return mixed $arrayData |
| 90 |
*/ |
| 91 |
public function reduceCount($arrayData) |
| 92 |
{ |
| 93 |
if (is_array($arrayData)) { |
| 94 |
$arrayData = array_reduce($arrayData, "self::doReduce", array()); |
| 95 |
} |
| 96 |
|
| 97 |
return $arrayData; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Reference: https://github.com/etsy/statsd/blob/master/README.md |
| 102 |
* Sampling 0.1 |
| 103 |
* Tells StatsD that this counter is being sent sampled every 1/10th of the time. |
| 104 |
* |
| 105 |
* @param mixed $data |
| 106 |
* @param int $sampleRate |
| 107 |
* |
| 108 |
* @return mixed $data |
| 109 |
*/ |
| 110 |
public function appendSampleRate($data, $sampleRate = 1) |
| 111 |
{ |
| 112 |
if ($sampleRate < 1) { |
| 113 |
array_walk($data, function(&$message, $key) use ($sampleRate) { |
| 114 |
$message = sprintf('%s|@%s', $message, $sampleRate); |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
return $data; |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* Send the metrics over UDP |
| 123 |
* |
| 124 |
* {@inheritDoc} |
| 125 |
*/ |
| 126 |
public function send($data, $sampleRate = 1) |
| 127 |
{ |
| 128 |
// check format |
| 129 |
if ($data instanceof StatsdDataInterface || is_string($data)) { |
| 130 |
$data = array($data); |
| 131 |
} |
| 132 |
if (!is_array($data) || empty($data)) { |
| 133 |
return; |
| 134 |
} |
| 135 |
// add sampling |
| 136 |
if ($sampleRate < 1) { |
| 137 |
$data = $this->appendSampleRate($data, $sampleRate); |
| 138 |
} |
| 139 |
// reduce number of packets |
| 140 |
if ($this->getReducePacket()) { |
| 141 |
$data = $this->reduceCount($data); |
| 142 |
} |
| 143 |
//failures in any of this should be silently ignored if .. |
| 144 |
try { |
| 145 |
$fp = $this->getSender()->open(); |
| 146 |
if (!$fp) { |
| 147 |
return; |
| 148 |
} |
| 149 |
$written = 0; |
| 150 |
foreach ($data as $key => $message) { |
| 151 |
$written += $this->getSender()->write($fp, $message); |
| 152 |
} |
| 153 |
$this->getSender()->close($fp); |
| 154 |
} catch (\Exception $e) { |
| 155 |
$this->throwException($e); |
| 156 |
} |
| 157 |
|
| 158 |
return $written; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param boolean $failSilently |
| 163 |
*/ |
| 164 |
public function setFailSilently($failSilently) |
| 165 |
{ |
| 166 |
$this->failSilently = $failSilently; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @return boolean |
| 171 |
*/ |
| 172 |
public function getFailSilently() |
| 173 |
{ |
| 174 |
return $this->failSilently; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @param \Liuggio\StatsdClient\Sender\SenderInterface $sender |
| 179 |
*/ |
| 180 |
public function setSender(SenderInterface $sender) |
| 181 |
{ |
| 182 |
$this->sender = $sender; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @return \Liuggio\StatsdClient\Sender\SenderInterface |
| 187 |
*/ |
| 188 |
public function getSender() |
| 189 |
{ |
| 190 |
return $this->sender; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @param boolean $reducePacket |
| 195 |
*/ |
| 196 |
public function setReducePacket($reducePacket) |
| 197 |
{ |
| 198 |
$this->reducePacket = $reducePacket; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @return boolean |
| 203 |
*/ |
| 204 |
public function getReducePacket() |
| 205 |
{ |
| 206 |
return $this->reducePacket; |
| 207 |
} |
| 208 |
|
| 209 |
} |