Make ApiStashEdit use statsd metrics
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 16 Dec 2015 19:42:12 +0000 (11:42 -0800)
committerOri.livneh <ori@wikimedia.org>
Fri, 18 Dec 2015 02:59:55 +0000 (02:59 +0000)
Change-Id: Iffdeae7f867490e3c69c4b6d79a5f844cb7fce23

includes/api/ApiStashEdit.php

index 208e9ca..701f0a9 100644 (file)
@@ -252,6 +252,7 @@ class ApiStashEdit extends ApiBase {
        public static function checkCache( Title $title, Content $content, User $user ) {
                $cache = ObjectCache::getLocalClusterInstance();
                $logger = LoggerFactory::getInstance( 'StashEdit' );
+               $stats = RequestContext::getMain()->getStats();
 
                $key = self::getStashKey( $title, $content, $user );
                $editInfo = $cache->get( $key );
@@ -267,19 +268,20 @@ class ApiStashEdit extends ApiBase {
                                $editInfo = $cache->get( $key );
                                $dbw->unlock( $key, __METHOD__ );
                        }
-                       $sec = microtime( true ) - $start;
-                       if ( $sec > .01 ) {
-                               $logger->warning( "Waited $sec seconds on '$key'." );
-                       }
+
+                       $timeMs = 1000 * max( 0, microtime( true ) - $start );
+                       $stats->timing( 'editstash.lock-wait-time', $timeMs );
                }
 
                if ( !is_object( $editInfo ) || !$editInfo->output ) {
+                       $stats->increment( 'editstash.cache-misses' );
                        $logger->debug( "No cache value for key '$key'." );
                        return false;
                }
 
                $time = wfTimestamp( TS_UNIX, $editInfo->output->getTimestamp() );
                if ( ( time() - $time ) <= 3 ) {
+                       $stats->increment( 'editstash.cache-hits' );
                        $logger->debug( "Timestamp-based cache hit for key '$key'." );
                        return $editInfo; // assume nothing changed
                }
@@ -308,6 +310,7 @@ class ApiStashEdit extends ApiBase {
                        }
 
                        if ( $changed || $res->numRows() != $templateUses ) {
+                               $stats->increment( 'editstash.cache-misses' );
                                $logger->info( "Stale cache for key '$key'; template changed." );
                                return false;
                        }
@@ -331,11 +334,13 @@ class ApiStashEdit extends ApiBase {
                        }
 
                        if ( $changed || $res->numRows() != count( $files ) ) {
+                               $stats->increment( 'editstash.cache-misses' );
                                $logger->info( "Stale cache for key '$key'; file changed." );
                                return false;
                        }
                }
 
+               $stats->increment( 'editstash.cache-hits' );
                $logger->debug( "Cache hit for key '$key'." );
 
                return $editInfo;