Debian Codegraph @

includes/libs/Stats/Emitters/UDPEmitter.php

raw symbols SCIP
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 /** @var string */
42 private string $prefix;
43
44 /** @var StatsCache */
45 private StatsCache $cache;
46
47 /** @var FormatterInterface */
48 private FormatterInterface $formatter;
49
50 /** @var UDPTransport|null */
51 private ?UDPTransport $transport;
52
53 /** @var int */
54 private int $payloadSize;
55
56 public function __construct( string $prefix, StatsCache $cache, FormatterInterface $formatter, ?string $target ) {
57 $this->prefix = $this->normalizePrefix( $prefix );
58 $this->cache = $cache;
59 $this->formatter = $formatter;
60 $this->transport = $target ? UDPTransport::newFromString( $target ) : null;
61 $this->payloadSize = UDPTransport::MAX_PAYLOAD_SIZE;
62 }
63
64 /**
65 * Sets payload size for batching.
66 *
67 * @param int $payloadSize
68 * @return UDPEmitter
69 */
70 public function withPayloadSize( int $payloadSize ): UDPEmitter {
71 $this->payloadSize = $payloadSize;
72 return $this;
73 }
74
75 /**
76 * Overrides the transport.
77 *
78 * @param UDPTransport $transport
79 * @return UDPEmitter
80 */
81 public function withTransport( UDPTransport $transport ): UDPEmitter {
82 $this->transport = $transport;
83 return $this;
84 }
85
86 private function normalizePrefix( string $prefix ): string {
87 if ( $prefix === '' ) {
88 throw new InvalidArgumentException( 'UDPEmitter: Prefix cannot be empty.' );
89 }
90 return StatsUtils::normalizeString( $prefix );
91 }
92
93 /**
94 * Renders metrics and samples through the formatter and returns a string[] of wire-formatted metric samples.
95 *
96 * @return array
97 */
98 private function render(): array {
99 $output = [];
100 foreach ( $this->cache->getAllMetrics() as $metric ) {
101 // Skip NullMetric instances.
102 if ( get_class( $metric ) === NullMetric::class ) {
103 continue;
104 }
105 foreach ( $this->formatter->getFormattedSamples( $this->prefix, $metric ) as $formatted ) {
106 $output[] = $formatted;
107 }
108 }
109 return $output;
110 }
111
112 /**
113 * Batch the array of samples into payload of payloadSize and
114 * emit them via the configured transport.
115 *
116 * @param array $samples
117 * @param int $payloadSize
118 * @return void
119 */
120 private function batch( array $samples, int $payloadSize ): void {
121 $payload = '';
122 foreach ( $samples as $sample ) {
123 if ( strlen( $payload ) + strlen( $sample ) + 1 > $payloadSize ) {
124 // Send this payload and make a new one
125 $this->transport->emit( $payload );
126 $payload = '';
127 }
128 $payload .= $sample . "\n";
129 }
130 // Send what is left in the payload
131 if ( strlen( $payload ) > 0 ) {
132 $this->transport->emit( $payload );
133 }
134 }
135
136 /**
137 * @inheritDoc
138 */
139 public function send(): void {
140 $this->batch( $this->render(), $this->payloadSize );
141 }
142 }