summaryrefslogtreecommitdiff
path: root/www/wiki/includes/libs/stats/SamplingStatsdClient.php
blob: 6494c26302d6529c791d33a9b6eafa826432c5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
 * Copyright 2015
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

use Liuggio\StatsdClient\StatsdClient;
use Liuggio\StatsdClient\Entity\StatsdData;
use Liuggio\StatsdClient\Entity\StatsdDataInterface;

/**
 * A statsd client that applies the sampling rate to the data items before sending them.
 *
 * @since 1.26
 */
class SamplingStatsdClient extends StatsdClient {
	protected $samplingRates = [];

	/**
	 * Sampling rates as an associative array of patterns and rates.
	 * Patterns are Unix shell patterns (e.g. 'MediaWiki.api.*').
	 * Rates are sampling probabilities (e.g. 0.1 means 1 in 10 events are sampled).
	 * @param array $samplingRates
	 * @since 1.28
	 */
	public function setSamplingRates( array $samplingRates ) {
		$this->samplingRates = $samplingRates;
	}

	/**
	 * Sets sampling rate for all items in $data.
	 * The sample rate specified in a StatsdData entity overrides the sample rate specified here.
	 *
	 * @inheritDoc
	 */
	public function appendSampleRate( $data, $sampleRate = 1 ) {
		$samplingRates = $this->samplingRates;
		if ( !$samplingRates && $sampleRate !== 1 ) {
			$samplingRates = [ '*' => $sampleRate ];
		}
		if ( $samplingRates ) {
			array_walk( $data, function ( $item ) use ( $samplingRates ) {
				/** @var StatsdData $item */
				foreach ( $samplingRates as $pattern => $rate ) {
					if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE ) ) {
						$item->setSampleRate( $item->getSampleRate() * $rate );
						break;
					}
				}
			} );
		}

		return $data;
	}

	/**
	 * Send the metrics over UDP
	 * Sample the metrics according to their sample rate and send the remaining ones.
	 *
	 * @param StatsdDataInterface|StatsdDataInterface[] $data message(s) to sent
	 *        strings are not allowed here as sampleData requires a StatsdDataInterface
	 * @param int $sampleRate
	 *
	 * @return int the data sent in bytes
	 */
	public function send( $data, $sampleRate = 1 ) {
		if ( !is_array( $data ) ) {
			$data = [ $data ];
		}
		if ( !$data ) {
			return;
		}
		foreach ( $data as $item ) {
			if ( !( $item instanceof StatsdDataInterface ) ) {
				throw new InvalidArgumentException(
					'SamplingStatsdClient does not accept stringified messages' );
			}
		}

		// add sampling
		$data = $this->appendSampleRate( $data, $sampleRate );
		$data = $this->sampleData( $data );

		$data = array_map( 'strval', $data );

		// reduce number of packets
		if ( $this->getReducePacket() ) {
			$data = $this->reduceCount( $data );
		}

		// failures in any of this should be silently ignored if ..
		$written = 0;
		try {
			$fp = $this->getSender()->open();
			if ( !$fp ) {
				return;
			}
			foreach ( $data as $message ) {
				$written += $this->getSender()->write( $fp, $message );
			}
			$this->getSender()->close( $fp );
		} catch ( Exception $e ) {
			$this->throwException( $e );
		}

		return $written;
	}

	/**
	 * Throw away some of the data according to the sample rate.
	 * @param StatsdDataInterface[] $data
	 * @return StatsdDataInterface[]
	 * @throws LogicException
	 */
	protected function sampleData( $data ) {
		$newData = [];
		$mt_rand_max = mt_getrandmax();
		foreach ( $data as $item ) {
			$samplingRate = $item->getSampleRate();
			if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
				throw new LogicException( 'Sampling rate shall be within ]0, 1]' );
			}
			if (
				$samplingRate === 1 ||
				( mt_rand() / $mt_rand_max <= $samplingRate )
			) {
				$newData[] = $item;
			}
		}
		return $newData;
	}

	/**
	 * @inheritDoc
	 */
	protected function throwException( Exception $exception ) {
		if ( !$this->getFailSilently() ) {
			throw $exception;
		}
	}
}