7declare( strict_types=1 );
11use InvalidArgumentException;
28 private string $prefix;
32 private int $payloadSize;
35 $this->prefix = $this->normalizePrefix( $prefix );
36 $this->cache = $cache;
37 $this->formatter = $formatter;
49 $this->payloadSize = $payloadSize;
60 $this->transport = $transport;
64 private function normalizePrefix(
string $prefix ): string {
65 if ( $prefix ===
'' ) {
66 throw new InvalidArgumentException(
'UDPEmitter: Prefix cannot be empty.' );
68 return StatsUtils::normalizeString( $prefix );
74 private function render(): array {
76 foreach ( $this->cache->getAllMetrics() as $metric ) {
78 if ( get_class( $metric ) === NullMetric::class ) {
81 foreach ( $this->formatter->getFormattedSamples( $this->prefix, $metric ) as $formatted ) {
82 $output[] = $formatted;
96 private function batch( array $samples,
int $payloadSize ): void {
98 foreach ( $samples as $sample ) {
99 if ( strlen( $payload ) + strlen( $sample ) + 1 > $payloadSize ) {
101 $this->transport->emit( $payload );
104 $payload .= $sample .
"\n";
107 if ( strlen( $payload ) > 0 ) {
108 $this->transport->emit( $payload );
116 $this->batch( $this->render(), $this->payloadSize );
if(!defined('MW_SETUP_CALLBACK'))
A generic class to send a message over UDP.
static newFromString( $info)