| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuggio\StatsdClient\Entity; |
| 4 |
|
| 5 |
use Liuggio\StatsdClient\Entity\StatsdDataInterface; |
| 6 |
|
| 7 |
class StatsdData implements StatsdDataInterface |
| 8 |
{ |
| 9 |
|
| 10 |
private $key; |
| 11 |
private $value; |
| 12 |
private $metric; |
| 13 |
private $sampleRate = 1; |
| 14 |
|
| 15 |
/** |
| 16 |
* @param string $key |
| 17 |
*/ |
| 18 |
public function setKey($key) |
| 19 |
{ |
| 20 |
$this->key = $key; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function getKey() |
| 27 |
{ |
| 28 |
return $this->key; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param int $value |
| 33 |
*/ |
| 34 |
public function setValue($value) |
| 35 |
{ |
| 36 |
$this->value = $value; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @return int |
| 41 |
*/ |
| 42 |
public function getValue() |
| 43 |
{ |
| 44 |
return $this->value; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
public function setMetric($metric) |
| 49 |
{ |
| 50 |
$this->metric = $metric; |
| 51 |
} |
| 52 |
|
| 53 |
public function getMetric() |
| 54 |
{ |
| 55 |
return $this->metric; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @param float $sampleRate |
| 60 |
*/ |
| 61 |
public function setSampleRate($sampleRate) |
| 62 |
{ |
| 63 |
$this->sampleRate = $sampleRate; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return float |
| 68 |
*/ |
| 69 |
public function getSampleRate() |
| 70 |
{ |
| 71 |
return $this->sampleRate; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param bool $withMetric |
| 76 |
* |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
public function getMessage($withMetric = true) |
| 80 |
{ |
| 81 |
if (!$withMetric) { |
| 82 |
$result = sprintf('%s:%s', $this->getKey(), $this->getValue()); |
| 83 |
} else { |
| 84 |
$result = sprintf('%s:%s|%s', $this->getKey(), $this->getValue(), $this->getMetric()); |
| 85 |
} |
| 86 |
|
| 87 |
$sampleRate = $this->getSampleRate(); |
| 88 |
if($sampleRate < 1){ |
| 89 |
$result.= "|@$sampleRate"; |
| 90 |
} |
| 91 |
|
| 92 |
return $result; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
public function __toString() |
| 99 |
{ |
| 100 |
return $this->getMessage(); |
| 101 |
} |
| 102 |
} |