Smalyshev has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/405372 )

Change subject: Add some logging for SPARQL request timing and result count
......................................................................

Add some logging for SPARQL request timing and result count

Bug: T184840
Change-Id: I94f1fdd1a6c3ee7cfc62813a095d714eb899ad29
---
M includes/Query/DeepcatFeature.php
M includes/Search/SearchContext.php
M includes/Searcher.php
3 files changed, 47 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch 
refs/changes/72/405372/1

diff --git a/includes/Query/DeepcatFeature.php 
b/includes/Query/DeepcatFeature.php
index b542a11..ce9bbef 100644
--- a/includes/Query/DeepcatFeature.php
+++ b/includes/Query/DeepcatFeature.php
@@ -4,6 +4,7 @@
 use CirrusSearch\Search\SearchContext;
 use Config;
 use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
 use MediaWiki\Sparql\SparqlClient;
 use MediaWiki\Sparql\SparqlException;
 use Title;
@@ -46,6 +47,11 @@
         */
        const TIMEOUT = 3;
        /**
+        * Stats key for SPARQL requests
+        */
+       const STATSD_KEY = 'CirrusSearch.deepcat.sparql';
+
+       /**
         * @param Config $config
         * @param SparqlClient $client
         */
@@ -87,6 +93,7 @@
                        return [ null, false ];
                }
 
+               $startQueryTime = microtime( true );
                try {
                        $categories = $this->fetchCategories( $value );
                } catch ( SparqlException $e ) {
@@ -96,6 +103,7 @@
                                ->warning( 'Deepcat SPARQL Exception: ' . 
$e->getMessage() );
                        $categories = [ $value ];
                }
+               $this->logRequest( $startQueryTime, $categories, $context );
 
                if ( empty( $categories ) ) {
                        return [ null, false ];
@@ -120,6 +128,23 @@
        }
 
        /**
+        * Record stats data for the request.
+        * @param float $startQueryTime
+        * @param array $results
+        * @param SearchContext $context
+        */
+       private function logRequest( $startQueryTime, $results, SearchContext 
$context ) {
+               $timeTaken = intval( 1000 * ( microtime( true ) - 
$startQueryTime ) );
+               
MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
+                       self::STATSD_KEY, $timeTaken
+               );
+               $context->addLogData( [
+                       'sparqlQueryTime' => $timeTaken,
+                       'sparqlQueryResults' => count( $results ),
+               ] );
+       }
+
+       /**
         * Get child categories using SPARQL service.
         * @param string $rootCategory Category to start looking from
         * @return string[] List of subcategories.
diff --git a/includes/Search/SearchContext.php 
b/includes/Search/SearchContext.php
index b656fa9..74c6eef 100644
--- a/includes/Search/SearchContext.php
+++ b/includes/Search/SearchContext.php
@@ -171,6 +171,11 @@
         * @var Escaper $escaper
         */
        private $escaper;
+       /**
+        * Additional data to put into request log.
+        * @var array
+        */
+       private $extraLogData = [];
 
        /**
         * @var int[] weights of different syntaxes
@@ -888,4 +893,20 @@
        public function getAggregations() {
                return $this->aggs;
        }
+
+       /**
+        * Add extra request log data.
+        * @param $data
+        */
+       public function addLogData( $data ) {
+               $this->extraLogData[] = $data;
+       }
+
+       /**
+        * Get extra log data.
+        * @return array
+        */
+       public function getLogData() {
+               return $this->extraLogData;
+       }
 }
diff --git a/includes/Searcher.php b/includes/Searcher.php
index 1a5b2a8..c502448 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -581,7 +581,7 @@
                                'suggestion' => 
$this->searchContext->getSuggest() ? '' : null,
                                // Used syntax
                                'syntax' => 
$this->searchContext->getSyntaxUsed(),
-                       ]
+                       ] + $this->searchContext->getLogData()
                );
 
                if ( $this->returnQuery ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/405372
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I94f1fdd1a6c3ee7cfc62813a095d714eb899ad29
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Smalyshev <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to