| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
declare( strict_types=1 ); |
| 21 |
|
| 22 |
namespace Wikimedia\Stats; |
| 23 |
|
| 24 |
use Wikimedia\Stats\Emitters\EmitterInterface; |
| 25 |
use Wikimedia\Stats\Emitters\NullEmitter; |
| 26 |
use Wikimedia\Stats\Emitters\UDPEmitter; |
| 27 |
use Wikimedia\Stats\Exceptions\UnsupportedFormatException; |
| 28 |
use Wikimedia\Stats\Formatters\DogStatsdFormatter; |
| 29 |
use Wikimedia\Stats\Formatters\FormatterInterface; |
| 30 |
use Wikimedia\Stats\Formatters\NullFormatter; |
| 31 |
use Wikimedia\Stats\Formatters\StatsdFormatter; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class OutputFormats { |
| 40 |
|
| 41 |
public const NULL = 1; |
| 42 |
public const STATSD = 2; |
| 43 |
public const DOGSTATSD = 3; |
| 44 |
|
| 45 |
private const SUPPORTED_FORMATS = [ |
| 46 |
'null' => self::NULL, |
| 47 |
'statsd' => self::STATSD, |
| 48 |
'dogstatsd' => self::DOGSTATSD |
| 49 |
]; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static function getFormatFromString( string $format ): int { |
| 58 |
if ( self::SUPPORTED_FORMATS[$format] ?? false ) { |
| 59 |
return self::SUPPORTED_FORMATS[$format]; |
| 60 |
} |
| 61 |
throw new UnsupportedFormatException( |
| 62 |
"Format '" . $format . "' not supported. Expected one of " |
| 63 |
. json_encode( array_keys( self::SUPPORTED_FORMATS ) ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static function getNewFormatter( int $format ): FormatterInterface { |
| 74 |
switch ( $format ) { |
| 75 |
case self::DOGSTATSD: |
| 76 |
return new DogStatsdFormatter(); |
| 77 |
case self::STATSD: |
| 78 |
return new StatsdFormatter(); |
| 79 |
case self::NULL: |
| 80 |
return new NullFormatter(); |
| 81 |
default: |
| 82 |
throw new UnsupportedFormatException( 'Unsupported metrics format. Got format: ' . $format ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static function getNewEmitter( |
| 96 |
string $prefix, |
| 97 |
StatsCache $cache, |
| 98 |
FormatterInterface $formatter, |
| 99 |
?string $target = null |
| 100 |
): EmitterInterface { |
| 101 |
switch ( get_class( $formatter ) ) { |
| 102 |
case StatsdFormatter::class: |
| 103 |
case DogStatsdFormatter::class: |
| 104 |
return new UDPEmitter( $prefix, $cache, $formatter, $target ); |
| 105 |
case NullFormatter::class: |
| 106 |
return new NullEmitter; |
| 107 |
default: |
| 108 |
throw new UnsupportedFormatException( |
| 109 |
'Unsupported metrics format. Got format: ' . get_class( $formatter ) |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
} |