From 3b9faadf7d924e3983ef4d43dcc2a6e4737a7044 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Fri, 28 Jul 2017 11:09:20 -0700 Subject: [PATCH] Log usage statistics Bug: T170345 Change-Id: I7ec1214800062d9a22cd3fe0ce2ee9031fc9e21e --- includes/Hooks.php | 4 ++-- includes/LoginNotify.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/includes/Hooks.php b/includes/Hooks.php index 9685022..fd3a78d 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -151,7 +151,7 @@ class Hooks { * * @param User $user The user who logged in. */ - public static function doSuccessfulLogin( User $user ) { + private static function doSuccessfulLogin( User $user ) { $loginNotify = new LoginNotify(); $loginNotify->clearCounters( $user ); $loginNotify->sendSuccessNotice( $user ); @@ -163,7 +163,7 @@ class Hooks { * * @param User $user The user that failed to log in. */ - public static function doFailedLogin( User $user ) { + private static function doFailedLogin( User $user ) { $loginNotify = new LoginNotify(); $loginNotify->recordFailure( $user ); } diff --git a/includes/LoginNotify.php b/includes/LoginNotify.php index 4060072..91a1938 100644 --- a/includes/LoginNotify.php +++ b/includes/LoginNotify.php @@ -12,6 +12,7 @@ use BagOStuff; use CentralAuthUser; use Config; use EchoEvent; +use IBufferingStatsdDataFactory; use JobQueueGroup; use JobSpecification; use MediaWiki\MediaWikiServices; @@ -55,6 +56,8 @@ class LoginNotify implements LoggerAwareInterface { private $gSalt; /** @var string */ private $secret; + /** @var IBufferingStatsdDataFactory */ + private $stats; /** * Constructor @@ -83,6 +86,8 @@ class LoginNotify implements LoggerAwareInterface { $log = LoggerFactory::getInstance( 'LoginNotify' ); $this->log = $log; + + $this->stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); } /** @@ -618,6 +623,7 @@ class LoginNotify implements LoggerAwareInterface { $this->config->get( 'LoginNotifyExpiryNewIP' ) ); if ( $count ) { + $this->incrStats( 'fail.unknown.notifications' ); $this->sendNotice( $user, 'login-fail-new', $count ); } } @@ -637,6 +643,7 @@ class LoginNotify implements LoggerAwareInterface { $this->config->get( 'LoginNotifyExpiryKnownIP' ) ); if ( $count ) { + $this->incrStats( 'fail.known.notifications' ); $this->sendNotice( $user, 'login-fail-known', $count ); } } @@ -704,6 +711,7 @@ class LoginNotify implements LoggerAwareInterface { * @param User $user User in question */ public function recordFailure( User $user ) { + $this->incrStats( 'fail.total' ); $known = $this->isKnownSystemFast( $user, $user->getRequest() ); if ( $known === self::USER_KNOWN ) { $this->recordLoginFailureFromKnownSystem( $user ); @@ -737,6 +745,7 @@ class LoginNotify implements LoggerAwareInterface { if ( !$this->config->get( 'LoginNotifyEnableOnSuccess' ) ) { return; } + $this->incrStats( 'success.total' ); $result = $this->isKnownSystemFast( $user, $user->getRequest() ); if ( $result !== self::USER_KNOWN ) { $this->createJob( DeferredChecksJob::TYPE_LOGIN_SUCCESS, @@ -755,6 +764,7 @@ class LoginNotify implements LoggerAwareInterface { public function sendSuccessNoticeDeferred( User $user, $subnet, $resultSoFar ) { $isKnown = $this->isKnownSystemSlow( $user, $subnet, $resultSoFar ); if ( !$isKnown ) { + $this->incrStats( 'success.notifications' ); $this->sendNotice( $user, 'login-success' ); } } @@ -779,4 +789,13 @@ class LoginNotify implements LoggerAwareInterface { ); JobQueueGroup::singleton()->lazyPush( $job ); } + + /** + * Increments the given statistic + * + * @param string $metric + */ + private function incrStats( $metric ) { + $this->stats->increment( "loginnotify.$metric" ); + } }