Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / libs / SamplingStatsdClient.php
blob2e780c97287343e95fa35cd929ad08d9fe1528c7
1 <?php
2 /**
3 * Copyright 2015
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
20 * @file
23 use Liuggio\StatsdClient\StatsdClient;
24 use Liuggio\StatsdClient\Entity\StatsdData;
25 use Liuggio\StatsdClient\Entity\StatsdDataInterface;
27 /**
28 * A statsd client that applies the sampling rate to the data items before sending them.
30 * @since 1.26
32 class SamplingStatsdClient extends StatsdClient {
33 /**
34 * Sets sampling rate for all items in $data.
35 * The sample rate specified in a StatsdData entity overrides the sample rate specified here.
37 * {@inheritDoc}
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 );
46 } );
49 return $data;
53 * Send the metrics over UDP
54 * Sample the metrics according to their sample rate and send the remaining ones.
56 * @param StatsdDataInterface|StatsdDataInterface[] $data message(s) to sent
57 * strings are not allowed here as sampleData requires a StatsdDataInterface
58 * @param int $sampleRate
60 * @return integer the data sent in bytes
62 public function send( $data, $sampleRate = 1 ) {
63 if ( !is_array( $data ) ) {
64 $data = [ $data ];
66 if ( !$data ) {
67 return;
69 foreach ( $data as $item ) {
70 if ( !( $item instanceof StatsdDataInterface ) ) {
71 throw new InvalidArgumentException(
72 'SamplingStatsdClient does not accept stringified messages' );
76 // add sampling
77 if ( $sampleRate < 1 ) {
78 $data = $this->appendSampleRate( $data, $sampleRate );
80 $data = $this->sampleData( $data );
82 $data = array_map( 'strval', $data );
84 // reduce number of packets
85 if ( $this->getReducePacket() ) {
86 $data = $this->reduceCount( $data );
89 // failures in any of this should be silently ignored if ..
90 $written = 0;
91 try {
92 $fp = $this->getSender()->open();
93 if ( !$fp ) {
94 return;
96 foreach ( $data as $message ) {
97 $written += $this->getSender()->write( $fp, $message );
99 $this->getSender()->close( $fp );
100 } catch ( Exception $e ) {
101 $this->throwException( $e );
104 return $written;
108 * Throw away some of the data according to the sample rate.
109 * @param StatsdDataInterface[] $data
110 * @return StatsdDataInterface[]
111 * @throws LogicException
113 protected function sampleData( $data ) {
114 $newData = [];
115 $mt_rand_max = mt_getrandmax();
116 foreach ( $data as $item ) {
117 $samplingRate = $item->getSampleRate();
118 if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
119 throw new LogicException( 'Sampling rate shall be within ]0, 1]' );
121 if (
122 $samplingRate === 1 ||
123 ( mt_rand() / $mt_rand_max <= $samplingRate )
125 $newData[] = $item;
128 return $newData;
132 * {@inheritDoc}
134 protected function throwException( Exception $exception ) {
135 if ( !$this->getFailSilently() ) {
136 throw $exception;