Don't fallback from uk to ru
[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 * Send the metrics over UDP
54 * Sample the metrics according to their sample rate and send the remaining ones.
55 *
56 * @param StatsdDataInterface|StatsdDataInterface[] $data message(s) to sent
57 * strings are not allowed here as sampleData requires a StatsdDataInterface
58 * @param int $sampleRate
59 *
60 * @return integer the data sent in bytes
61 */
62 public function send( $data, $sampleRate = 1 ) {
63 if ( !is_array( $data ) ) {
64 $data = [ $data ];
65 }
66 if ( !$data ) {
67 return;
68 }
69 foreach ( $data as $item ) {
70 if ( !( $item instanceof StatsdDataInterface ) ) {
71 throw new InvalidArgumentException(
72 'SamplingStatsdClient does not accept stringified messages' );
73 }
74 }
75
76 // add sampling
77 if ( $sampleRate < 1 ) {
78 $data = $this->appendSampleRate( $data, $sampleRate );
79 }
80 $data = $this->sampleData( $data );
81
82 $data = array_map( 'strval', $data );
83
84 // reduce number of packets
85 if ( $this->getReducePacket() ) {
86 $data = $this->reduceCount( $data );
87 }
88
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;
95 }
96 foreach ( $data as $message ) {
97 $written += $this->getSender()->write( $fp, $message );
98 }
99 $this->getSender()->close( $fp );
100 } catch ( Exception $e ) {
101 $this->throwException( $e );
102 }
103
104 return $written;
105 }
106
107 /**
108 * Throw away some of the data according to the sample rate.
109 * @param StatsdDataInterface[] $data
110 * @return StatsdDataInterface[]
111 * @throws LogicException
112 */
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]' );
120 }
121 if (
122 $samplingRate === 1 ||
123 ( mt_rand() / $mt_rand_max <= $samplingRate )
124 ) {
125 $newData[] = $item;
126 }
127 }
128 return $newData;
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 protected function throwException( Exception $exception ) {
135 if ( !$this->getFailSilently() ) {
136 throw $exception;
137 }
138 }
139 }