Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
22 / 28
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
UDPEmitter
78.57% covered (warning)
78.57%
22 / 28
42.86% covered (danger)
42.86%
3 / 7
17.21
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 withPayloadSize
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 withTransport
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 normalizePrefix
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 render
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 batch
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
 send
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
20declare( strict_types=1 );
21
22namespace Wikimedia\Stats\Emitters;
23
24use InvalidArgumentException;
25use UDPTransport;
26use Wikimedia\Stats\Formatters\FormatterInterface;
27use Wikimedia\Stats\Metrics\NullMetric;
28use Wikimedia\Stats\StatsCache;
29use 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 */
39class 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                $payload .= $sample . "\n";
125            } else {
126                // Send this payload and make a new one
127                $this->transport->emit( $payload );
128                $payload = '';
129            }
130        }
131        // Send what is left in the payload
132        if ( strlen( $payload ) > 0 ) {
133            $this->transport->emit( $payload );
134        }
135    }
136
137    /**
138     * @inheritDoc
139     */
140    public function send(): void {
141        $this->batch( $this->render(), $this->payloadSize );
142    }
143}