aboutsummaryrefslogtreecommitdiffstats
path: root/includes/libs/Stats/Emitters/UDPEmitter.php
blob: e266299ba342bcfb9ad3ac85b09bddbc7ddc4d60 (plain) (blame)
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
<?php
/**
 * 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
 */

declare( strict_types=1 );

namespace Wikimedia\Stats\Emitters;

use InvalidArgumentException;
use UDPTransport;
use Wikimedia\Stats\Formatters\FormatterInterface;
use Wikimedia\Stats\Metrics\NullMetric;
use Wikimedia\Stats\StatsCache;
use Wikimedia\Stats\StatsUtils;

/**
 * Metrics UDP Emitter Implementation
 *
 * Leverages UDPTransport to emit wire-formatted metrics.
 *
 * @author Cole White
 * @since 1.41
 */
class UDPEmitter implements EmitterInterface {

	private string $prefix;
	private StatsCache $cache;
	private FormatterInterface $formatter;
	private ?UDPTransport $transport;
	private int $payloadSize;

	public function __construct( string $prefix, StatsCache $cache, FormatterInterface $formatter, ?string $target ) {
		$this->prefix = $this->normalizePrefix( $prefix );
		$this->cache = $cache;
		$this->formatter = $formatter;
		$this->transport = $target ? UDPTransport::newFromString( $target ) : null;
		$this->payloadSize = UDPTransport::MAX_PAYLOAD_SIZE;
	}

	/**
	 * Sets payload size for batching.
	 *
	 * @param int $payloadSize
	 * @return UDPEmitter
	 */
	public function withPayloadSize( int $payloadSize ): UDPEmitter {
		$this->payloadSize = $payloadSize;
		return $this;
	}

	/**
	 * Overrides the transport.
	 *
	 * @param UDPTransport $transport
	 * @return UDPEmitter
	 */
	public function withTransport( UDPTransport $transport ): UDPEmitter {
		$this->transport = $transport;
		return $this;
	}

	private function normalizePrefix( string $prefix ): string {
		if ( $prefix === '' ) {
			throw new InvalidArgumentException( 'UDPEmitter: Prefix cannot be empty.' );
		}
		return StatsUtils::normalizeString( $prefix );
	}

	/**
	 * Renders metrics and samples through the formatter and returns a string[] of wire-formatted metric samples.
	 */
	private function render(): array {
		$output = [];
		foreach ( $this->cache->getAllMetrics() as $metric ) {
			// Skip NullMetric instances.
			if ( get_class( $metric ) === NullMetric::class ) {
				continue;
			}
			foreach ( $this->formatter->getFormattedSamples( $this->prefix, $metric ) as $formatted ) {
				$output[] = $formatted;
			}
		}
		return $output;
	}

	/**
	 * Batch the array of samples into payload of payloadSize and
	 * emit them via the configured transport.
	 *
	 * @param array $samples
	 * @param int $payloadSize
	 * @return void
	 */
	private function batch( array $samples, int $payloadSize ): void {
		$payload = '';
		foreach ( $samples as $sample ) {
			if ( strlen( $payload ) + strlen( $sample ) + 1 > $payloadSize ) {
				// Send this payload and make a new one
				$this->transport->emit( $payload );
				$payload = '';
			}
			$payload .= $sample . "\n";
		}
		// Send what is left in the payload
		if ( strlen( $payload ) > 0 ) {
			$this->transport->emit( $payload );
		}
	}

	/**
	 * @inheritDoc
	 */
	public function send(): void {
		$this->batch( $this->render(), $this->payloadSize );
	}
}