Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / libs / SamplingStatsdClient.php
1 <?php
2 /**
3 * Copyright 2015
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use Liuggio\StatsdClient\StatsdClient;
24 use Liuggio\StatsdClient\Entity\StatsdData;
25 use Liuggio\StatsdClient\Entity\StatsdDataInterface;
26
27 /**
28 * A statsd client that applies the sampling rate to the data items before sending them.
29 *
30 * @since 1.26
31 */
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.
36 *
37 * {@inheritDoc}
38 */
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 );
45 }
46 } );
47 }
48
49 return $data;
50 }
51
52 /**
53 * Sample the metrics according to their sample rate and send the remaining ones.
54 *
55 * {@inheritDoc}
56 */
57 public function send( $data, $sampleRate = 1 ) {
58 if ( !is_array( $data ) ) {
59 $data = array( $data );
60 }
61 if ( !$data ) {
62 return;
63 }
64 foreach ( $data as $item ) {
65 if ( !( $item instanceof StatsdDataInterface ) ) {
66 throw new InvalidArgumentException(
67 'SamplingStatsdClient does not accept stringified messages' );
68 }
69 }
70
71 // add sampling
72 if ( $sampleRate < 1 ) {
73 $data = $this->appendSampleRate( $data, $sampleRate );
74 }
75 $data = $this->sampleData( $data );
76
77 $messages = array_map( 'strval', $data );
78
79 // reduce number of packets
80 if ( $this->getReducePacket() ) {
81 $data = $this->reduceCount( $data );
82 }
83 //failures in any of this should be silently ignored if ..
84 $written = 0;
85 try {
86 $fp = $this->getSender()->open();
87 if ( !$fp ) {
88 return;
89 }
90 foreach ( $messages as $message ) {
91 $written += $this->getSender()->write( $fp, $message );
92 }
93 $this->getSender()->close( $fp );
94 } catch ( Exception $e ) {
95 $this->throwException( $e );
96 }
97
98 return $written;
99 }
100
101 /**
102 * Throw away some of the data according to the sample rate.
103 * @param StatsdDataInterface[] $data
104 * @return array
105 * @throws LogicException
106 */
107 protected function sampleData( $data ) {
108 $newData = array();
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]' );
114 }
115 if (
116 $samplingRate === 1 ||
117 ( mt_rand() / $mt_rand_max <= $samplingRate )
118 ) {
119 $newData[] = $item;
120 }
121 }
122 return $newData;
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 protected function throwException( Exception $exception ) {
129 if ( !$this->getFailSilently() ) {
130 throw $exception;
131 }
132 }
133 }