23use Liuggio\StatsdClient\Entity\StatsdData;
24use Liuggio\StatsdClient\Entity\StatsdDataInterface;
25use Liuggio\StatsdClient\StatsdClient;
26use Wikimedia\RequestTimeout\TimeoutException;
60 array_walk( $data,
static function ( $item ) use (
$samplingRates ) {
63 if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE ) ) {
64 $item->setSampleRate( $item->getSampleRate() * $rate );
84 public function send( $data, $sampleRate = 1 ) {
85 if ( !is_array( $data ) ) {
91 foreach ( $data as $item ) {
92 if ( !( $item instanceof StatsdDataInterface ) ) {
93 throw new InvalidArgumentException(
94 'SamplingStatsdClient does not accept stringified messages' );
102 $data = array_map(
'strval', $data );
105 if ( $this->getReducePacket() ) {
106 $data = $this->reduceCount( $data );
112 $fp = $this->getSender()->open();
116 foreach ( $data as $message ) {
117 $written += $this->getSender()->write( $fp, $message );
119 $this->getSender()->close( $fp );
120 }
catch ( TimeoutException $e ) {
122 }
catch ( Exception $e ) {
123 $this->throwException( $e );
137 $mt_rand_max = mt_getrandmax();
138 foreach ( $data as $item ) {
139 $samplingRate = $item->getSampleRate();
140 if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
141 throw new LogicException(
'Sampling rate shall be within ]0, 1]' );
144 $samplingRate === 1 ||
145 ( mt_rand() / $mt_rand_max <= $samplingRate )
159 private function throwException( Exception $exception ) {
160 if ( !$this->getFailSilently() ) {
A statsd client that applies the sampling rate to the data items before sending them.
setSamplingRates(array $samplingRates)
Sampling rates as an associative array of patterns and rates.
send( $data, $sampleRate=1)
Send the metrics over UDP Sample the metrics according to their sample rate and send the remaining on...
sampleData( $data)
Throw away some of the data according to the sample rate.
appendSampleRate( $data, $sampleRate=1)
Sets sampling rate for all items in $data.