Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.57% |
22 / 28 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
UDPEmitter | |
78.57% |
22 / 28 |
|
42.86% |
3 / 7 |
17.21 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
withPayloadSize | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
withTransport | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
normalizePrefix | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
render | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
batch | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
4.25 | |||
send | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * @file |
18 | */ |
19 | |
20 | declare( strict_types=1 ); |
21 | |
22 | namespace Wikimedia\Stats\Emitters; |
23 | |
24 | use InvalidArgumentException; |
25 | use UDPTransport; |
26 | use Wikimedia\Stats\Formatters\FormatterInterface; |
27 | use Wikimedia\Stats\Metrics\NullMetric; |
28 | use Wikimedia\Stats\StatsCache; |
29 | use Wikimedia\Stats\StatsUtils; |
30 | |
31 | /** |
32 | * Metrics UDP Emitter Implementation |
33 | * |
34 | * Leverages UDPTransport to emit wire-formatted metrics. |
35 | * |
36 | * @author Cole White |
37 | * @since 1.41 |
38 | */ |
39 | class UDPEmitter implements EmitterInterface { |
40 | |
41 | private string $prefix; |
42 | private StatsCache $cache; |
43 | private FormatterInterface $formatter; |
44 | private ?UDPTransport $transport; |
45 | private int $payloadSize; |
46 | |
47 | public function __construct( string $prefix, StatsCache $cache, FormatterInterface $formatter, ?string $target ) { |
48 | $this->prefix = $this->normalizePrefix( $prefix ); |
49 | $this->cache = $cache; |
50 | $this->formatter = $formatter; |
51 | $this->transport = $target ? UDPTransport::newFromString( $target ) : null; |
52 | $this->payloadSize = UDPTransport::MAX_PAYLOAD_SIZE; |
53 | } |
54 | |
55 | /** |
56 | * Sets payload size for batching. |
57 | * |
58 | * @param int $payloadSize |
59 | * @return UDPEmitter |
60 | */ |
61 | public function withPayloadSize( int $payloadSize ): UDPEmitter { |
62 | $this->payloadSize = $payloadSize; |
63 | return $this; |
64 | } |
65 | |
66 | /** |
67 | * Overrides the transport. |
68 | * |
69 | * @param UDPTransport $transport |
70 | * @return UDPEmitter |
71 | */ |
72 | public function withTransport( UDPTransport $transport ): UDPEmitter { |
73 | $this->transport = $transport; |
74 | return $this; |
75 | } |
76 | |
77 | private function normalizePrefix( string $prefix ): string { |
78 | if ( $prefix === '' ) { |
79 | throw new InvalidArgumentException( 'UDPEmitter: Prefix cannot be empty.' ); |
80 | } |
81 | return StatsUtils::normalizeString( $prefix ); |
82 | } |
83 | |
84 | /** |
85 | * Renders metrics and samples through the formatter and returns a string[] of wire-formatted metric samples. |
86 | */ |
87 | private function render(): array { |
88 | $output = []; |
89 | foreach ( $this->cache->getAllMetrics() as $metric ) { |
90 | // Skip NullMetric instances. |
91 | if ( get_class( $metric ) === NullMetric::class ) { |
92 | continue; |
93 | } |
94 | foreach ( $this->formatter->getFormattedSamples( $this->prefix, $metric ) as $formatted ) { |
95 | $output[] = $formatted; |
96 | } |
97 | } |
98 | return $output; |
99 | } |
100 | |
101 | /** |
102 | * Batch the array of samples into payload of payloadSize and |
103 | * emit them via the configured transport. |
104 | * |
105 | * @param array $samples |
106 | * @param int $payloadSize |
107 | * @return void |
108 | */ |
109 | private function batch( array $samples, int $payloadSize ): void { |
110 | $payload = ''; |
111 | foreach ( $samples as $sample ) { |
112 | if ( strlen( $payload ) + strlen( $sample ) + 1 > $payloadSize ) { |
113 | // Send this payload and make a new one |
114 | $this->transport->emit( $payload ); |
115 | $payload = ''; |
116 | } |
117 | $payload .= $sample . "\n"; |
118 | } |
119 | // Send what is left in the payload |
120 | if ( strlen( $payload ) > 0 ) { |
121 | $this->transport->emit( $payload ); |
122 | } |
123 | } |
124 | |
125 | /** |
126 | * @inheritDoc |
127 | */ |
128 | public function send(): void { |
129 | $this->batch( $this->render(), $this->payloadSize ); |
130 | } |
131 | } |