From 029ee57a8a2b8eca473f0a9e27e6e967c60d6b45 Mon Sep 17 00:00:00 2001 From: addshore Date: Mon, 11 Apr 2016 19:28:17 +0100 Subject: [PATCH] Add getStatsdDataFactory to MediawikiServices Change-Id: Iaf89d5d7887f766064266874ea7ba73362531ed2 --- includes/DefaultSettings.php | 2 +- includes/MediaWikiServices.php | 8 ++++++++ includes/ServiceWiring.php | 6 ++++++ includes/context/ContextSource.php | 6 +++++- includes/context/DerivativeContext.php | 17 ++++++----------- includes/context/IContextSource.php | 6 +++++- includes/context/RequestContext.php | 17 ++++++----------- .../phpunit/includes/MediaWikiServicesTest.php | 3 +++ 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 10e6adbe48..f5b3caa600 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -5896,7 +5896,7 @@ $wgStatsdServer = false; /** * Prefix for metric names sent to $wgStatsdServer. * - * @see RequestContext::getStats + * @see MediaWikiServices::getStatsdDataFactory * @see BufferingStatsdDataFactory * @since 1.25 */ diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php index 7b1def9eae..3f4d8ed7c7 100644 --- a/includes/MediaWikiServices.php +++ b/includes/MediaWikiServices.php @@ -6,6 +6,7 @@ use GlobalVarConfig; use Config; use Hooks; use LBFactory; +use Liuggio\StatsdClient\Factory\StatsdDataFactory; use LoadBalancer; use MediaWiki\Services\ServiceContainer; use SiteLookup; @@ -144,6 +145,13 @@ class MediaWikiServices extends ServiceContainer { return $this->getService( 'SiteStore' ); } + /** + * @return StatsdDataFactory + */ + public function getStatsdDataFactory() { + return $this->getService( 'StatsdDataFactory' ); + } + /////////////////////////////////////////////////////////////////////////// // NOTE: When adding a service getter here, don't forget to add a test // case for it in MediaWikiServicesTest::provideGetters() and in diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index d8709b95f0..7e1d4e314e 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -72,6 +72,12 @@ return [ return $services->getConfigFactory()->makeConfig( 'main' ); }, + 'StatsdDataFactory' => function( MediaWikiServices $services ) { + return new BufferingStatsdDataFactory( + rtrim( $services->getMainConfig()->get( 'StatsdMetricPrefix' ), '.' ) + ); + }, + /////////////////////////////////////////////////////////////////////////// // NOTE: When adding a service here, don't forget to add a getter function // in the MediaWikiServices class. The convenience getter should just call diff --git a/includes/context/ContextSource.php b/includes/context/ContextSource.php index 911ecdd5ab..b61787169c 100644 --- a/includes/context/ContextSource.php +++ b/includes/context/ContextSource.php @@ -18,6 +18,8 @@ * @author Happy-melon * @file */ +use Liuggio\StatsdClient\Factory\StatsdDataFactory; +use MediaWiki\MediaWikiServices; /** * The simplest way of implementing IContextSource is to hold a RequestContext as a @@ -165,8 +167,10 @@ abstract class ContextSource implements IContextSource { /** * Get the Stats object * + * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) + * * @since 1.25 - * @return BufferingStatsdDataFactory + * @return StatsdDataFactory */ public function getStats() { return $this->getContext()->getStats(); diff --git a/includes/context/DerivativeContext.php b/includes/context/DerivativeContext.php index 1b881e49dd..e77a058d1d 100644 --- a/includes/context/DerivativeContext.php +++ b/includes/context/DerivativeContext.php @@ -18,6 +18,8 @@ * @author Daniel Friesen * @file */ +use Liuggio\StatsdClient\Factory\StatsdDataFactory; +use MediaWiki\MediaWikiServices; /** * An IContextSource implementation which will inherit context from another source @@ -67,11 +69,6 @@ class DerivativeContext extends ContextSource implements MutableContext { */ private $config; - /** - * @var Stats - */ - private $stats; - /** * @var Timing */ @@ -110,14 +107,12 @@ class DerivativeContext extends ContextSource implements MutableContext { /** * Get the stats object * - * @return BufferingStatsdDataFactory + * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) + * + * @return StatsdDataFactory */ public function getStats() { - if ( !is_null( $this->stats ) ) { - return $this->stats; - } else { - return $this->getContext()->getStats(); - } + return MediaWikiServices::getInstance()->getStatsdDataFactory(); } /** diff --git a/includes/context/IContextSource.php b/includes/context/IContextSource.php index 750389ddd5..ccefc72f1b 100644 --- a/includes/context/IContextSource.php +++ b/includes/context/IContextSource.php @@ -21,6 +21,8 @@ * @file */ +use Liuggio\StatsdClient\Factory\StatsdDataFactory; + /** * Interface for objects which can provide a MediaWiki context on request * @@ -126,8 +128,10 @@ interface IContextSource { /** * Get the stats object * + * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) + * * @since 1.25 - * @return BufferingStatsdDataFactory + * @return StatsdDataFactory */ public function getStats(); diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index c8b8108830..c87798eab0 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -22,7 +22,9 @@ * @file */ +use Liuggio\StatsdClient\Factory\StatsdDataFactory; use MediaWiki\Logger\LoggerFactory; +use MediaWiki\MediaWikiServices; /** * Group all the pieces relevant to the context of a request into one instance @@ -63,11 +65,6 @@ class RequestContext implements IContextSource, MutableContext { */ private $skin; - /** - * @var \Liuggio\StatsdClient\Factory\StatsdDataFactory - */ - private $stats; - /** * @var Timing */ @@ -138,14 +135,12 @@ class RequestContext implements IContextSource, MutableContext { /** * Get the Stats object * - * @return BufferingStatsdDataFactory + * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected) + * + * @return StatsdDataFactory */ public function getStats() { - if ( $this->stats === null ) { - $prefix = rtrim( $this->getConfig()->get( 'StatsdMetricPrefix' ), '.' ); - $this->stats = new BufferingStatsdDataFactory( $prefix ); - } - return $this->stats; + return MediaWikiServices::getInstance()->getStatsdDataFactory(); } /** diff --git a/tests/phpunit/includes/MediaWikiServicesTest.php b/tests/phpunit/includes/MediaWikiServicesTest.php index 127f869663..188957569b 100644 --- a/tests/phpunit/includes/MediaWikiServicesTest.php +++ b/tests/phpunit/includes/MediaWikiServicesTest.php @@ -1,4 +1,5 @@ [ 'getMainConfig', Config::class ], 'SiteStore' => [ 'getSiteStore', SiteStore::class ], 'SiteLookup' => [ 'getSiteLookup', SiteLookup::class ], + 'StatsdDataFactory' => [ 'getStatsdDataFactory', StatsdDataFactory::class ], ]; } @@ -46,6 +48,7 @@ class MediaWikiServicesTest extends PHPUnit_Framework_TestCase { 'MainConfig' => [ 'MainConfig', Config::class ], 'SiteStore' => [ 'SiteStore', SiteStore::class ], 'SiteLookup' => [ 'SiteLookup', SiteLookup::class ], + 'StatsdDataFactory' => [ 'StatsdDataFactory', StatsdDataFactory::class ], ]; } -- 2.20.1