Make statsd counts for MWCallableUpdate actually useful
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 22 Jul 2016 02:32:09 +0000 (19:32 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 22 Jul 2016 06:52:09 +0000 (23:52 -0700)
Include the method that made the callback in the key.

Change-Id: Ifc7c486ee5c8d57b2516456569bb724cf7dc2b99

autoload.php
includes/deferred/AtomicSectionUpdate.php
includes/deferred/AutoCommitUpdate.php
includes/deferred/CallableUpdate.php
includes/deferred/DeferrableCallback.php [new file with mode: 0644]
includes/deferred/DeferredUpdates.php

index 5da06d8..09a2928 100644 (file)
@@ -330,6 +330,7 @@ $wgAutoloadLocalClasses = [
        'DateFormats' => __DIR__ . '/maintenance/language/date-formats.php',
        'DateFormatter' => __DIR__ . '/includes/parser/DateFormatter.php',
        'DeadendPagesPage' => __DIR__ . '/includes/specials/SpecialDeadendpages.php',
+       'DeferrableCallback' => __DIR__ . '/includes/deferred/DeferrableCallback.php',
        'DeferrableUpdate' => __DIR__ . '/includes/deferred/DeferrableUpdate.php',
        'DeferredStringifier' => __DIR__ . '/includes/libs/DeferredStringifier.php',
        'DeferredUpdates' => __DIR__ . '/includes/deferred/DeferredUpdates.php',
index ccbd6b0..0da5d7d 100644 (file)
@@ -4,7 +4,7 @@
  * Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
  * @since 1.27
  */
-class AtomicSectionUpdate implements DeferrableUpdate {
+class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
        /** @var IDatabase */
        private $dbw;
        /** @var string */
@@ -39,4 +39,8 @@ class AtomicSectionUpdate implements DeferrableUpdate {
                        $this->callback = null;
                }
        }
+
+       public function getOrigin() {
+               return $this->fname;
+       }
 }
index ddf2bb8..ef5903b 100644 (file)
@@ -4,7 +4,7 @@
  * Deferrable Update for closure/callback updates that should use auto-commit mode
  * @since 1.28
  */
-class AutoCommitUpdate implements DeferrableUpdate {
+class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback {
        /** @var IDatabase */
        private $dbw;
        /** @var string */
@@ -53,4 +53,8 @@ class AutoCommitUpdate implements DeferrableUpdate {
                        $this->callback = null;
                }
        }
+
+       public function getOrigin() {
+               return $this->fname;
+       }
 }
index 4b19c20..d63c292 100644 (file)
@@ -3,22 +3,26 @@
 /**
  * Deferrable Update for closure/callback
  */
-class MWCallableUpdate implements DeferrableUpdate {
-       /** @var Closure|callable */
+class MWCallableUpdate implements DeferrableUpdate, DeferrableCallback {
+       /** @var callable */
        private $callback;
+       /** @var string */
+       private $fname;
 
        /**
         * @param callable $callback
-        * @throws InvalidArgumentException
+        * @param string $fname Calling method
         */
-       public function __construct( $callback ) {
-               if ( !is_callable( $callback ) ) {
-                       throw new InvalidArgumentException( 'Not a valid callback/closure!' );
-               }
+       public function __construct( callable $callback, $fname = 'unknown' ) {
                $this->callback = $callback;
+               $this->fname = $fname;
        }
 
        public function doUpdate() {
                call_user_func( $this->callback );
        }
+
+       public function getOrigin() {
+               return $this->fname;
+       }
 }
diff --git a/includes/deferred/DeferrableCallback.php b/includes/deferred/DeferrableCallback.php
new file mode 100644 (file)
index 0000000..2eb0d5d
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * Callback wrapper that has an originating method
+ *
+ * @since 1.28
+ */
+interface DeferrableCallback {
+       /**
+        * @return string Originating method name
+        */
+       function getOrigin();
+}
index 1552777..9768838 100644 (file)
@@ -61,7 +61,7 @@ class DeferredUpdates {
        }
 
        /**
-        * Add a callable update.  In a lot of cases, we just need a callback/closure,
+        * Add a callable update. In a lot of cases, we just need a callback/closure,
         * defining a new DeferrableUpdate object is not necessary
         *
         * @see MWCallableUpdate::__construct()
@@ -70,7 +70,7 @@ class DeferredUpdates {
         * @param integer $type DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
         */
        public static function addCallableUpdate( $callable, $type = self::POSTSEND ) {
-               self::addUpdate( new MWCallableUpdate( $callable ), $type );
+               self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller() ), $type );
        }
 
        /**
@@ -143,7 +143,11 @@ class DeferredUpdates {
                                } else {
                                        $otherUpdates[] = $update;
                                }
-                               $stats->increment( 'deferred_updates.' . $method . '.' . get_class( $update ) );
+
+                               $name = $update instanceof DeferrableCallback
+                                       ? get_class( $update ) . '-' . $update->getOrigin()
+                                       : get_class( $update );
+                               $stats->increment( 'deferred_updates.' . $method . '.' . $name );
                        }
 
                        // Delegate DataUpdate execution to the DataUpdate class