20declare( strict_types=1 );
25use InvalidArgumentException;
26use Psr\Log\LoggerInterface;
50 private string $component;
53 private array $staticLabelKeys = [];
56 private array $staticLabelValues = [];
65 private LoggerInterface $logger;
82 LoggerInterface $logger
85 $this->cache = $cache;
86 $this->emitter = $emitter;
87 $this->logger = $logger;
88 $this->validateInstanceConfig();
96 private function validateInstanceConfig(): void {
97 if ( $this->component ==
'' ) {
98 throw new InvalidArgumentException(
'Stats: component cannot be empty.' );
110 if ( count( $this->cache->getAllMetrics() ) > 0 ) {
115 $this->staticLabelKeys[] = $key;
121 $this->statsdDataFactory = $statsdDataFactory;
134 return $this->getMetric( $name, CounterMetric::class );
146 return $this->getMetric( $name, GaugeMetric::class );
158 return $this->getMetric( $name, TimingMetric::class );
165 $this->emitter->send();
166 $this->cache->clear();
178 private function getMetric(
string $name,
string $className ) {
179 $name = StatsUtils::normalizeString( $name );
180 StatsUtils::validateMetricName( $name );
182 $metric = $this->cache->get( $this->component, $name, $className );
183 }
catch ( TypeError | InvalidArgumentException | InvalidConfigurationException $ex ) {
185 trigger_error( $ex->getMessage(), E_USER_WARNING );
186 return new NullMetric;
188 if ( $metric ===
null ) {
189 $baseMetric =
new BaseMetric( $this->component, $name );
190 $metric =
new $className(
192 ->withStatsdDataFactory( $this->statsdDataFactory )
193 ->withStaticLabels( $this->staticLabelKeys, $this->staticLabelValues ),
196 $this->cache->set( $this->component, $name, $metric );
198 return $metric->fresh();
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.