5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use Liuggio\StatsdClient\StatsdClient
;
24 use Liuggio\StatsdClient\Entity\StatsdData
;
25 use Liuggio\StatsdClient\Entity\StatsdDataInterface
;
28 * A statsd client that applies the sampling rate to the data items before sending them.
32 class SamplingStatsdClient
extends StatsdClient
{
34 * Sets sampling rate for all items in $data.
35 * The sample rate specified in a StatsdData entity overrides the sample rate specified here.
39 public function appendSampleRate( $data, $sampleRate = 1 ) {
40 if ( $sampleRate < 1 ) {
41 array_walk( $data, function( $item ) use ( $sampleRate ) {
42 /** @var $item StatsdData */
43 if ( $item->getSampleRate() === 1 ) {
44 $item->setSampleRate( $sampleRate );
53 * Sample the metrics according to their sample rate and send the remaining ones.
57 public function send( $data, $sampleRate = 1 ) {
58 if ( !is_array( $data ) ) {
59 $data = array( $data );
64 foreach ( $data as $item ) {
65 if ( !( $item instanceof StatsdDataInterface
) ) {
66 throw new InvalidArgumentException(
67 'SamplingStatsdClient does not accept stringified messages' );
72 if ( $sampleRate < 1 ) {
73 $data = $this->appendSampleRate( $data, $sampleRate );
75 $data = $this->sampleData( $data );
77 $messages = array_map( 'strval', $data );
79 // reduce number of packets
80 if ( $this->getReducePacket() ) {
81 $data = $this->reduceCount( $data );
83 //failures in any of this should be silently ignored if ..
86 $fp = $this->getSender()->open();
90 foreach ( $messages as $message ) {
91 $written +
= $this->getSender()->write( $fp, $message );
93 $this->getSender()->close( $fp );
94 } catch ( Exception
$e ) {
95 $this->throwException( $e );
102 * Throw away some of the data according to the sample rate.
103 * @param StatsdDataInterface[] $data
105 * @throws LogicException
107 protected function sampleData( $data ) {
109 $mt_rand_max = mt_getrandmax();
110 foreach ( $data as $item ) {
111 $samplingRate = $item->getSampleRate();
112 if ( $samplingRate <= 0.0 ||
$samplingRate > 1.0 ) {
113 throw new LogicException( 'Sampling rate shall be within ]0, 1]' );
116 $samplingRate === 1 ||
117 ( mt_rand() / $mt_rand_max <= $samplingRate )
128 protected function throwException( Exception
$exception ) {
129 if ( !$this->getFailSilently() ) {