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 * @license GPL-2.0-or-later
4 * @file
5 */
6
7declare( strict_types=1 );
8
9namespace Wikimedia\Stats\Emitters;
10
11use InvalidArgumentException;
12use Wikimedia\Stats\Formatters\FormatterInterface;
13use Wikimedia\Stats\Metrics\NullMetric;
14use Wikimedia\Stats\StatsCache;
15use Wikimedia\Stats\StatsUtils;
16use Wikimedia\UDPTransport;
17
18/**
19 * Metrics UDP Emitter Implementation
20 *
21 * Leverages UDPTransport to emit wire-formatted metrics.
22 *
23 * @author Cole White
24 * @since 1.41
25 */
26class UDPEmitter implements EmitterInterface {
27
28    private string $prefix;
29    private StatsCache $cache;
30    private FormatterInterface $formatter;
31    private ?UDPTransport $transport;
32    private int $payloadSize;
33
34    public function __construct( string $prefix, StatsCache $cache, FormatterInterface $formatter, ?string $target ) {
35        $this->prefix = $this->normalizePrefix( $prefix );
36        $this->cache = $cache;
37        $this->formatter = $formatter;
38        $this->transport = $target ? UDPTransport::newFromString( $target ) : null;
39        $this->payloadSize = UDPTransport::MAX_PAYLOAD_SIZE;
40    }
41
42    /**
43     * Sets payload size for batching.
44     *
45     * @param int $payloadSize
46     * @return UDPEmitter
47     */
48    public function withPayloadSize( int $payloadSize ): UDPEmitter {
49        $this->payloadSize = $payloadSize;
50        return $this;
51    }
52
53    /**
54     * Overrides the transport.
55     *
56     * @param UDPTransport $transport
57     * @return UDPEmitter
58     */
59    public function withTransport( UDPTransport $transport ): UDPEmitter {
60        $this->transport = $transport;
61        return $this;
62    }
63
64    private function normalizePrefix( string $prefix ): string {
65        if ( $prefix === '' ) {
66            throw new InvalidArgumentException( 'UDPEmitter: Prefix cannot be empty.' );
67        }
68        return StatsUtils::normalizeString( $prefix );
69    }
70
71    /**
72     * Renders metrics and samples through the formatter and returns a string[] of wire-formatted metric samples.
73     */
74    private function render(): array {
75        $output = [];
76        foreach ( $this->cache->getAllMetrics() as $metric ) {
77            // Skip NullMetric instances.
78            if ( get_class( $metric ) === NullMetric::class ) {
79                continue;
80            }
81            foreach ( $this->formatter->getFormattedSamples( $this->prefix, $metric ) as $formatted ) {
82                $output[] = $formatted;
83            }
84        }
85        return $output;
86    }
87
88    /**
89     * Batch the array of samples into payload of payloadSize and
90     * emit them via the configured transport.
91     *
92     * @param array $samples
93     * @param int $payloadSize
94     * @return void
95     */
96    private function batch( array $samples, int $payloadSize ): void {
97        $payload = '';
98        foreach ( $samples as $sample ) {
99            if ( strlen( $payload ) + strlen( $sample ) + 1 > $payloadSize ) {
100                // Send this payload and make a new one
101                $this->transport->emit( $payload );
102                $payload = '';
103            }
104            $payload .= $sample . "\n";
105        }
106        // Send what is left in the payload
107        if ( strlen( $payload ) > 0 ) {
108            $this->transport->emit( $payload );
109        }
110    }
111
112    /**
113     * @inheritDoc
114     */
115    public function send(): void {
116        $this->batch( $this->render(), $this->payloadSize );
117    }
118}