| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuggio\StatsdClient\Service; |
| 4 |
|
| 5 |
use Liuggio\StatsdClient\Entity\StatsdDataInterface; |
| 6 |
use Liuggio\StatsdClient\Factory\StatsdDataFactory; |
| 7 |
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface; |
| 8 |
use Liuggio\StatsdClient\StatsdClient; |
| 9 |
use Liuggio\StatsdClient\Entity\StatsdData; |
| 10 |
|
| 11 |
/** |
| 12 |
* Simplifies access to StatsD client and factory, buffers all data. |
| 13 |
*/ |
| 14 |
class StatsdService implements StatsdDataFactoryInterface |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var \Liuggio\StatsdClient\StatsdClient |
| 18 |
*/ |
| 19 |
protected $client; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var \Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface |
| 23 |
*/ |
| 24 |
protected $factory; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var StatsdData[] |
| 28 |
*/ |
| 29 |
protected $buffer = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* @var float |
| 33 |
*/ |
| 34 |
protected $samplingRate; |
| 35 |
|
| 36 |
private $samplingFunction; |
| 37 |
|
| 38 |
/** |
| 39 |
* @param StatsdClient $client |
| 40 |
* @param StatsdDataFactoryInterface $factory |
| 41 |
* @param float $samplingRate see setSamplingRate |
| 42 |
*/ |
| 43 |
public function __construct( |
| 44 |
StatsdClient $client, |
| 45 |
StatsdDataFactoryInterface $factory = null, |
| 46 |
$samplingRate = 1 |
| 47 |
) { |
| 48 |
$this->client = $client; |
| 49 |
if (is_null($factory)) { |
| 50 |
$factory = new StatsdDataFactory(); |
| 51 |
} |
| 52 |
$this->factory = $factory; |
| 53 |
$this->setSamplingRate($samplingRate); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Actually defines the sampling rate used by the service. |
| 58 |
* If set to 0.1, the service will automatically discard 10% |
| 59 |
* of the incoming metrics. It will also automatically flag these |
| 60 |
* as sampled data to statsd. |
| 61 |
* |
| 62 |
* @param float $samplingRate |
| 63 |
*/ |
| 64 |
public function setSamplingRate($samplingRate) |
| 65 |
{ |
| 66 |
if ($samplingRate <= 0.0 || 1.0 < $samplingRate) { |
| 67 |
throw new \LogicException('Sampling rate shall be within ]0, 1]'); |
| 68 |
} |
| 69 |
$this->samplingRate = $samplingRate; |
| 70 |
$this->samplingFunction = function($min, $max){ |
| 71 |
return rand($min, $max); |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @return float |
| 77 |
*/ |
| 78 |
public function getSamplingRate() |
| 79 |
{ |
| 80 |
return $this->samplingRate; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* {@inheritdoc} |
| 85 |
*/ |
| 86 |
public function timing($key, $time) |
| 87 |
{ |
| 88 |
$this->appendToBuffer( |
| 89 |
$this->factory->timing($key, $time) |
| 90 |
); |
| 91 |
|
| 92 |
return $this; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* {@inheritdoc} |
| 97 |
*/ |
| 98 |
public function gauge($key, $value) |
| 99 |
{ |
| 100 |
$this->appendToBuffer( |
| 101 |
$this->factory->gauge($key, $value) |
| 102 |
); |
| 103 |
|
| 104 |
return $this; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* {@inheritdoc} |
| 109 |
*/ |
| 110 |
public function set($key, $value) |
| 111 |
{ |
| 112 |
$this->appendToBuffer( |
| 113 |
$this->factory->set($key, $value) |
| 114 |
); |
| 115 |
|
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* {@inheritdoc} |
| 121 |
*/ |
| 122 |
public function increment($key) |
| 123 |
{ |
| 124 |
$this->appendToBuffer( |
| 125 |
$this->factory->increment($key) |
| 126 |
); |
| 127 |
|
| 128 |
return $this; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* {@inheritdoc} |
| 133 |
*/ |
| 134 |
public function decrement($key) |
| 135 |
{ |
| 136 |
$this->appendToBuffer( |
| 137 |
$this->factory->decrement($key) |
| 138 |
); |
| 139 |
|
| 140 |
return $this; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* {@inheritdoc} |
| 145 |
*/ |
| 146 |
public function updateCount($key, $delta) |
| 147 |
{ |
| 148 |
$this->appendToBuffer( |
| 149 |
$this->factory->updateCount($key, $delta) |
| 150 |
); |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* {@inheritdoc} |
| 157 |
*/ |
| 158 |
public function produceStatsdData($key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT) |
| 159 |
{ |
| 160 |
throw new \BadFunctionCallException('produceStatsdData is not implemented'); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @param callable $samplingFunction rand() function by default. |
| 165 |
*/ |
| 166 |
public function setSamplingFunction(\Closure $samplingFunction) |
| 167 |
{ |
| 168 |
$this->samplingFunction = $samplingFunction; |
| 169 |
} |
| 170 |
|
| 171 |
private function appendToBuffer(StatsdData $data) |
| 172 |
{ |
| 173 |
if($this->samplingRate < 1){ |
| 174 |
$data->setSampleRate($this->samplingRate); |
| 175 |
$result = call_user_func($this->samplingFunction, 0 , floor(1 / $this->samplingRate)); |
| 176 |
if ($result == 0) { |
| 177 |
array_push($this->buffer, $data); |
| 178 |
}; |
| 179 |
} else { |
| 180 |
array_push($this->buffer, $data); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Send all buffered data to statsd. |
| 186 |
* |
| 187 |
* @return $this |
| 188 |
*/ |
| 189 |
public function flush() |
| 190 |
{ |
| 191 |
$this->client->send($this->buffer); |
| 192 |
$this->buffer = array(); |
| 193 |
|
| 194 |
return $this; |
| 195 |
} |
| 196 |
} |