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
{
33 protected $samplingRates = [];
36 * Sampling rates as an associative array of patterns and rates.
37 * Patterns are Unix shell patterns (e.g. 'MediaWiki.api.*').
38 * Rates are sampling probabilities (e.g. 0.1 means 1 in 10 events are sampled).
39 * @param array $samplingRates
42 public function setSamplingRates( array $samplingRates ) {
43 $this->samplingRates
= $samplingRates;
47 * Sets sampling rate for all items in $data.
48 * The sample rate specified in a StatsdData entity overrides the sample rate specified here.
52 public function appendSampleRate( $data, $sampleRate = 1 ) {
53 $samplingRates = $this->samplingRates
;
54 if ( !$samplingRates && $sampleRate !== 1 ) {
55 $samplingRates = [ '*' => $sampleRate ];
57 if ( $samplingRates ) {
58 array_walk( $data, function( $item ) use ( $samplingRates ) {
59 /** @var $item StatsdData */
60 foreach ( $samplingRates as $pattern => $rate ) {
61 if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE
) ) {
62 $item->setSampleRate( $item->getSampleRate() * $rate );
73 * Send the metrics over UDP
74 * Sample the metrics according to their sample rate and send the remaining ones.
76 * @param StatsdDataInterface|StatsdDataInterface[] $data message(s) to sent
77 * strings are not allowed here as sampleData requires a StatsdDataInterface
78 * @param int $sampleRate
80 * @return integer the data sent in bytes
82 public function send( $data, $sampleRate = 1 ) {
83 if ( !is_array( $data ) ) {
89 foreach ( $data as $item ) {
90 if ( !( $item instanceof StatsdDataInterface
) ) {
91 throw new InvalidArgumentException(
92 'SamplingStatsdClient does not accept stringified messages' );
97 $data = $this->appendSampleRate( $data, $sampleRate );
98 $data = $this->sampleData( $data );
100 $data = array_map( 'strval', $data );
102 // reduce number of packets
103 if ( $this->getReducePacket() ) {
104 $data = $this->reduceCount( $data );
107 // failures in any of this should be silently ignored if ..
110 $fp = $this->getSender()->open();
114 foreach ( $data as $message ) {
115 $written +
= $this->getSender()->write( $fp, $message );
117 $this->getSender()->close( $fp );
118 } catch ( Exception
$e ) {
119 $this->throwException( $e );
126 * Throw away some of the data according to the sample rate.
127 * @param StatsdDataInterface[] $data
128 * @return StatsdDataInterface[]
129 * @throws LogicException
131 protected function sampleData( $data ) {
133 $mt_rand_max = mt_getrandmax();
134 foreach ( $data as $item ) {
135 $samplingRate = $item->getSampleRate();
136 if ( $samplingRate <= 0.0 ||
$samplingRate > 1.0 ) {
137 throw new LogicException( 'Sampling rate shall be within ]0, 1]' );
140 $samplingRate === 1 ||
141 ( mt_rand() / $mt_rand_max <= $samplingRate )
152 protected function throwException( Exception
$exception ) {
153 if ( !$this->getFailSilently() ) {