| 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 InvalidArgumentException; |
| 25 |
use Psr\Log\LoggerInterface; |
| 26 |
use Psr\Log\NullLogger; |
| 27 |
use TypeError; |
| 28 |
use Wikimedia\Stats\Emitters\EmitterInterface; |
| 29 |
use Wikimedia\Stats\Emitters\NullEmitter; |
| 30 |
use Wikimedia\Stats\Exceptions\InvalidConfigurationException; |
| 31 |
use Wikimedia\Stats\Metrics\BaseMetric; |
| 32 |
use Wikimedia\Stats\Metrics\CounterMetric; |
| 33 |
use Wikimedia\Stats\Metrics\GaugeMetric; |
| 34 |
use Wikimedia\Stats\Metrics\NullMetric; |
| 35 |
use Wikimedia\Stats\Metrics\TimingMetric; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class StatsFactory { |
| 48 |
|
| 49 |
|
| 50 |
private string $component; |
| 51 |
|
| 52 |
|
| 53 |
private StatsCache $cache; |
| 54 |
|
| 55 |
|
| 56 |
private EmitterInterface $emitter; |
| 57 |
|
| 58 |
|
| 59 |
private LoggerInterface $logger; |
| 60 |
|
| 61 |
|
| 62 |
private ?IBufferingStatsdDataFactory $statsdDataFactory = null; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function __construct( |
| 73 |
StatsCache $cache, |
| 74 |
EmitterInterface $emitter, |
| 75 |
LoggerInterface $logger, |
| 76 |
string $component = '' |
| 77 |
) { |
| 78 |
$this->component = StatsUtils::normalizeString( $component ); |
| 79 |
$this->cache = $cache; |
| 80 |
$this->emitter = $emitter; |
| 81 |
$this->logger = $logger; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function withComponent( string $component ): StatsFactory { |
| 91 |
$statsFactory = new StatsFactory( $this->cache, $this->emitter, $this->logger, $component ); |
| 92 |
return $statsFactory->withStatsdDataFactory( $this->statsdDataFactory ); |
| 93 |
} |
| 94 |
|
| 95 |
public function withStatsdDataFactory( ?IBufferingStatsdDataFactory $statsdDataFactory ): StatsFactory { |
| 96 |
$this->statsdDataFactory = $statsdDataFactory; |
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function getCounter( string $name ) { |
| 109 |
return $this->getMetric( $name, CounterMetric::class ); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function getGauge( string $name ) { |
| 121 |
return $this->getMetric( $name, GaugeMetric::class ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function getTiming( string $name ) { |
| 133 |
return $this->getMetric( $name, TimingMetric::class ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function flush(): void { |
| 140 |
$this->trackUsage(); |
| 141 |
$this->emitter->send(); |
| 142 |
$this->cache->clear(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private function trackUsage(): void { |
| 149 |
$accumulator = 0; |
| 150 |
foreach ( $this->cache->getAllMetrics() as $metric ) { |
| 151 |
$accumulator += $metric->getSampleCount(); |
| 152 |
} |
| 153 |
$this->getCounter( 'stats_buffered_total' ) |
| 154 |
->copyToStatsdAt( 'stats.statslib.buffered' ) |
| 155 |
->incrementBy( $accumulator ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private function getMetric( string $name, string $className ) { |
| 168 |
$name = StatsUtils::normalizeString( $name ); |
| 169 |
StatsUtils::validateMetricName( $name ); |
| 170 |
try { |
| 171 |
$metric = $this->cache->get( $this->component, $name, $className ); |
| 172 |
} catch ( TypeError | InvalidArgumentException | InvalidConfigurationException $ex ) { |
| 173 |
|
| 174 |
trigger_error( $ex->getMessage(), E_USER_WARNING ); |
| 175 |
return new NullMetric; |
| 176 |
} |
| 177 |
if ( $metric === null ) { |
| 178 |
$baseMetric = new BaseMetric( $this->component, $name ); |
| 179 |
$metric = new $className( $baseMetric->withStatsdDataFactory( $this->statsdDataFactory ), $this->logger ); |
| 180 |
$this->cache->set( $this->component, $name, $metric ); |
| 181 |
} |
| 182 |
return $metric->fresh(); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static function newNull(): self { |
| 196 |
return new self( new StatsCache(), new NullEmitter(), new NullLogger() ); |
| 197 |
} |
| 198 |
} |