23use Liuggio\StatsdClient\Entity\StatsdData;
24use Liuggio\StatsdClient\Entity\StatsdDataInterface;
25use Liuggio\StatsdClient\StatsdClient;
26use Wikimedia\RequestTimeout\TimeoutException;
59 array_walk( $data,
static function ( $item ) use (
$samplingRates ) {
62 if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE ) ) {
63 $item->setSampleRate( $item->getSampleRate() * $rate );
83 public function send( $data, $sampleRate = 1 ) {
84 if ( !is_array( $data ) ) {
90 foreach ( $data as $item ) {
91 if ( !( $item instanceof StatsdDataInterface ) ) {
92 throw new InvalidArgumentException(
93 'SamplingStatsdClient does not accept stringified messages' );
101 $data = array_map(
'strval', $data );
104 if ( $this->getReducePacket() ) {
105 $data = $this->reduceCount( $data );
111 $fp = $this->getSender()->open();
115 foreach ( $data as $message ) {
116 $written += $this->getSender()->write( $fp, $message );
118 $this->getSender()->close( $fp );
119 }
catch ( TimeoutException $e ) {
121 }
catch ( Exception $e ) {
136 $mt_rand_max = mt_getrandmax();
137 foreach ( $data as $item ) {
138 $samplingRate = $item->getSampleRate();
139 if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
140 throw new LogicException(
'Sampling rate shall be within ]0, 1]' );
143 $samplingRate === 1 ||
144 ( mt_rand() / $mt_rand_max <= $samplingRate )
156 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...
throwException(Exception $exception)
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.