MediaWiki REL1_40
StatsFactory.php
Go to the documentation of this file.
1<?php
20declare( strict_types=1 );
21
22namespace Wikimedia\Stats;
23
25use InvalidArgumentException;
26use Psr\Log\LoggerInterface;
27use TypeError;
36
48
50 private string $component;
51
53 private array $staticLabelKeys = [];
54
56 private array $staticLabelValues = [];
57
59 private StatsCache $cache;
60
62 private EmitterInterface $emitter;
63
65 private LoggerInterface $logger;
66
68 private ?IBufferingStatsdDataFactory $statsdDataFactory = null;
69
78 public function __construct(
79 string $component,
80 StatsCache $cache,
81 EmitterInterface $emitter,
82 LoggerInterface $logger
83 ) {
84 $this->component = StatsUtils::normalizeString( $component );
85 $this->cache = $cache;
86 $this->emitter = $emitter;
87 $this->logger = $logger;
88 $this->validateInstanceConfig();
89 }
90
96 private function validateInstanceConfig(): void {
97 if ( $this->component == '' ) {
98 throw new InvalidArgumentException( 'Stats: component cannot be empty.' );
99 }
100 }
101
109 public function withStaticLabel( string $key, string $value ): StatsFactory {
110 if ( count( $this->cache->getAllMetrics() ) > 0 ) {
111 throw new IllegalOperationException( 'Stats: cannot set static labels when metrics are in the cache.' );
112 }
113 $key = StatsUtils::normalizeString( $key );
115 $this->staticLabelKeys[] = $key;
116 $this->staticLabelValues[] = StatsUtils::normalizeString( $value );
117 return $this;
118 }
119
120 public function withStatsdDataFactory( IBufferingStatsdDataFactory $statsdDataFactory ): StatsFactory {
121 $this->statsdDataFactory = $statsdDataFactory;
122 return $this;
123 }
124
133 public function getCounter( string $name ) {
134 return $this->getMetric( $name, CounterMetric::class );
135 }
136
145 public function getGauge( string $name ) {
146 return $this->getMetric( $name, GaugeMetric::class );
147 }
148
157 public function getTiming( string $name ) {
158 return $this->getMetric( $name, TimingMetric::class );
159 }
160
164 public function flush(): void {
165 $this->emitter->send();
166 $this->cache->clear();
167 }
168
178 private function getMetric( string $name, string $className ) {
179 $name = StatsUtils::normalizeString( $name );
180 StatsUtils::validateMetricName( $name );
181 try {
182 $metric = $this->cache->get( $this->component, $name, $className );
183 } catch ( TypeError | InvalidArgumentException | InvalidConfigurationException $ex ) {
184 // Log the condition and give the caller something that will absorb calls.
185 trigger_error( $ex->getMessage(), E_USER_WARNING );
186 return new NullMetric;
187 }
188 if ( $metric === null ) {
189 $baseMetric = new BaseMetric( $this->component, $name );
190 $metric = new $className(
191 $baseMetric
192 ->withStatsdDataFactory( $this->statsdDataFactory )
193 ->withStaticLabels( $this->staticLabelKeys, $this->staticLabelValues ),
194 $this->logger
195 );
196 $this->cache->set( $this->component, $name, $metric );
197 }
198 return $metric->fresh();
199 }
200}
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
Thrown when an unresolvable configuration condition has been requested.
Base Metric Implementation V1.
Counter Metric Implementation.
Gauge Metric Implementation.
Null Metric Implementation.
Timing Metric Implementation.
Singleton cache for Metric instances.
StatsFactory Implementation.
getGauge(string $name)
Makes a new GaugeMetric or fetches one from cache.
withStatsdDataFactory(IBufferingStatsdDataFactory $statsdDataFactory)
getTiming(string $name)
Makes a new TimingMetric or fetches one from cache.
withStaticLabel(string $key, string $value)
Adds a label key-value pair to all metrics created by this StatsFactory instance.
flush()
Send all buffered metrics to the target and destroy the cache.
__construct(string $component, StatsCache $cache, EmitterInterface $emitter, LoggerInterface $logger)
StatsFactory builds, configures, and caches Metrics.
getCounter(string $name)
Makes a new CounterMetric or fetches one from cache.
static normalizeString(string $entity)
Normalize strings to a metrics-compatible format.
static validateLabelKey(string $key)
Determines if provided string is a valid label key.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.