From: Stanislav Malyshev Date: Fri, 26 May 2017 00:23:44 +0000 (-0700) Subject: Refactor Statsd classes to enable null collector to work. X-Git-Tag: 1.31.0-rc.0~3103^2 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=7fdc3d09a3b6bbb7ae046f46e5dbf65fd7aca8e4;p=lhc%2Fweb%2Fwiklou.git Refactor Statsd classes to enable null collector to work. The following changes are added: - Created MediawikiStatsdDataFactory interface - Added hasData() method to see if there are any data to send - Added getData() method to fetch data - Made service infrastructure use MediawikiStatsdDataFactory interface - Made wfLogProfilingData() use MediawikiStatsdDataFactory interface - Added capability to enable/disable buffering collector Bug: T166354 Change-Id: I2874175647e987996a9a399829b3319674471aaa --- diff --git a/autoload.php b/autoload.php index fbdee83ba8..018c4fc518 100644 --- a/autoload.php +++ b/autoload.php @@ -960,6 +960,7 @@ $wgAutoloadLocalClasses = [ 'MediaWiki\\Widget\\TitleInputWidget' => __DIR__ . '/includes/widget/TitleInputWidget.php', 'MediaWiki\\Widget\\UserInputWidget' => __DIR__ . '/includes/widget/UserInputWidget.php', 'MediaWiki\\Widget\\UsersMultiselectWidget' => __DIR__ . '/includes/widget/UsersMultiselectWidget.php', + 'MediawikiStatsdDataFactory' => __DIR__ . '/includes/libs/stats/MediawikiStatsdDataFactory.php', 'MemCachedClientforWiki' => __DIR__ . '/includes/compat/MemcachedClientCompat.php', 'MemcLockManager' => __DIR__ . '/includes/libs/lockmanager/MemcLockManager.php', 'MemcachedBagOStuff' => __DIR__ . '/includes/libs/objectcache/MemcachedBagOStuff.php', diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 9c70639bc8..8f2c24efa3 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1191,7 +1191,8 @@ function wfLogProfilingData() { $profiler->logData(); $config = $context->getConfig(); - if ( $config->get( 'StatsdServer' ) ) { + $stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); + if ( $config->get( 'StatsdServer' ) && $stats->hasData() ) { try { $statsdServer = explode( ':', $config->get( 'StatsdServer' ) ); $statsdHost = $statsdServer[0]; @@ -1199,9 +1200,7 @@ function wfLogProfilingData() { $statsdSender = new SocketSender( $statsdHost, $statsdPort ); $statsdClient = new SamplingStatsdClient( $statsdSender, true, false ); $statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) ); - $statsdClient->send( - MediaWikiServices::getInstance()->getStatsdDataFactory()->getBuffer() - ); + $statsdClient->send( $stats->getData() ); } catch ( Exception $ex ) { MWExceptionHandler::logException( $ex ); } diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php index 3bf6d78c86..b63c769b08 100644 --- a/includes/MediaWikiServices.php +++ b/includes/MediaWikiServices.php @@ -9,9 +9,9 @@ use EventRelayerGroup; use GenderCache; use GlobalVarConfig; use Hooks; +use MediawikiStatsdDataFactory; use Wikimedia\Rdbms\LBFactory; use LinkCache; -use Liuggio\StatsdClient\Factory\StatsdDataFactory; use Wikimedia\Rdbms\LoadBalancer; use MediaHandlerFactory; use MediaWiki\Linker\LinkRenderer; @@ -446,7 +446,7 @@ class MediaWikiServices extends ServiceContainer { /** * @since 1.27 - * @return StatsdDataFactory + * @return MediawikiStatsdDataFactory */ public function getStatsdDataFactory() { return $this->getService( 'StatsdDataFactory' ); diff --git a/includes/context/ContextSource.php b/includes/context/ContextSource.php index 135c9b23eb..2264670cfa 100644 --- a/includes/context/ContextSource.php +++ b/includes/context/ContextSource.php @@ -170,7 +170,7 @@ abstract class ContextSource implements IContextSource { * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) * * @since 1.25 - * @return StatsdDataFactory + * @return MediawikiStatsdDataFactory */ public function getStats() { return MediaWikiServices::getInstance()->getStatsdDataFactory(); diff --git a/includes/context/DerivativeContext.php b/includes/context/DerivativeContext.php index e77a058d1d..29395101d0 100644 --- a/includes/context/DerivativeContext.php +++ b/includes/context/DerivativeContext.php @@ -109,7 +109,7 @@ class DerivativeContext extends ContextSource implements MutableContext { * * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) * - * @return StatsdDataFactory + * @return MediawikiStatsdDataFactory */ public function getStats() { return MediaWikiServices::getInstance()->getStatsdDataFactory(); diff --git a/includes/context/IContextSource.php b/includes/context/IContextSource.php index ccefc72f1b..8e9fc6f6a9 100644 --- a/includes/context/IContextSource.php +++ b/includes/context/IContextSource.php @@ -131,7 +131,7 @@ interface IContextSource { * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) * * @since 1.25 - * @return StatsdDataFactory + * @return MediawikiStatsdDataFactory */ public function getStats(); diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index 3dfa456400..0e1de504e5 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -138,7 +138,7 @@ class RequestContext implements IContextSource, MutableContext { * * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) * - * @return StatsdDataFactory + * @return MediawikiStatsdDataFactory */ public function getStats() { return MediaWikiServices::getInstance()->getStatsdDataFactory(); diff --git a/includes/libs/stats/BufferingStatsdDataFactory.php b/includes/libs/stats/BufferingStatsdDataFactory.php index 9c18b10f10..f687254e3c 100644 --- a/includes/libs/stats/BufferingStatsdDataFactory.php +++ b/includes/libs/stats/BufferingStatsdDataFactory.php @@ -32,8 +32,17 @@ use Liuggio\StatsdClient\Factory\StatsdDataFactory; * * @since 1.25 */ -class BufferingStatsdDataFactory extends StatsdDataFactory { +class BufferingStatsdDataFactory extends StatsdDataFactory implements MediawikiStatsdDataFactory { protected $buffer = []; + /** + * Collection enabled? + * @var bool + */ + protected $enabled = true; + /** + * @var string + */ + private $prefix; public function __construct( $prefix ) { parent::__construct(); @@ -49,6 +58,7 @@ class BufferingStatsdDataFactory extends StatsdDataFactory { * * @param string $key * @since 1.26 + * @return string */ private static function normalizeMetricKey( $key ) { $key = preg_replace( '/[:.]+/', '.', $key ); @@ -61,6 +71,9 @@ class BufferingStatsdDataFactory extends StatsdDataFactory { $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT ) { $entity = $this->produceStatsdDataEntity(); + if ( !$this->enabled ) { + return $entity; + } if ( $key !== null ) { $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" ); $entity->setKey( $key ); @@ -79,9 +92,35 @@ class BufferingStatsdDataFactory extends StatsdDataFactory { } /** + * @deprecated Use getData() * @return StatsdData[] */ public function getBuffer() { return $this->buffer; } + + /** + * Check whether this data factory has any data. + * @return boolean + */ + public function hasData() { + return !empty( $this->buffer ); + } + + /** + * Return data from the factory. + * @return StatsdData[] + */ + public function getData() { + return $this->buffer; + } + + /** + * Set collection enable status. + * @param bool $enabled Will collection be enabled? + * @return void + */ + public function setEnabled( $enabled ) { + $this->enabled = $enabled; + } } diff --git a/includes/libs/stats/MediawikiStatsdDataFactory.php b/includes/libs/stats/MediawikiStatsdDataFactory.php new file mode 100644 index 0000000000..d560f18694 --- /dev/null +++ b/includes/libs/stats/MediawikiStatsdDataFactory.php @@ -0,0 +1,28 @@ + [ 'MainConfig', Config::class ], 'SiteStore' => [ 'SiteStore', SiteStore::class ], 'SiteLookup' => [ 'SiteLookup', SiteLookup::class ], - 'StatsdDataFactory' => [ 'StatsdDataFactory', StatsdDataFactory::class ], + 'StatsdDataFactory' => [ 'StatsdDataFactory', MediawikiStatsdDataFactory::class ], 'InterwikiLookup' => [ 'InterwikiLookup', InterwikiLookup::class ], 'EventRelayerGroup' => [ 'EventRelayerGroup', EventRelayerGroup::class ], 'SearchEngineFactory' => [ 'SearchEngineFactory', SearchEngineFactory::class ],