jenkins-bot has submitted this change and it was merged.

Change subject: Add subscribers for statsd counters and timers
......................................................................


Add subscribers for statsd counters and timers

Hooks into a statsd end point that Ori is almost done building.

Change-Id: Ic270426834506af19a1584d4d636dacceec340ef
---
M WikimediaEvents.php
M WikimediaEventsHooks.php
A modules/ext.wikimediaEvents.statsd.js
3 files changed, 90 insertions(+), 1 deletion(-)

Approvals:
  Ori.livneh: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/WikimediaEvents.php b/WikimediaEvents.php
index 3e4cfd5..dd9309d 100644
--- a/WikimediaEvents.php
+++ b/WikimediaEvents.php
@@ -25,12 +25,23 @@
        'descriptionmsg' => 'wikimediaevents-desc',
 );
 
+// Configuration
+
+/**
+ * @var bool|string: Full URI or false if not set.
+ * Data is logged to this end point as key-value pairs in the query
+ * string. Must not contain a query string.
+ *
+ * @example string: '//log.example.org/statsd'
+ */
+$wgWMEStatsdBaseUri = false;
+
 // Messages
 
 $wgMessagesDirs['WikimediaEvents'] = __DIR__ . '/i18n';
 $wgExtensionMessagesFiles['WikimediaEvents'] = __DIR__ . 
'/WikimediaEvents.i18n.php';
 
-// Configs
+// Modules
 
 $wgResourceModules += array(
        'schema.TimingData' => array(
@@ -71,6 +82,12 @@
                'remoteExtPath' => 'WikimediaEvents/modules',
                'targets' => array( 'desktop', 'mobile' ),
        ),
+       'ext.wikimediaEvents.statsd' => array(
+               'scripts'       => 'ext.wikimediaEvents.statsd.js',
+               'localBasePath' => __DIR__ . '/modules',
+               'remoteExtPath' => 'WikimediaEvents/modules',
+               'targets'       => array( 'desktop', 'mobile' ),
+       ),
        // This is for analytics code that is meant to load on all page views 
for both
        // logged in and anonymous users.  It is intended that this module 
remain
        // permanently (even if empty, to avoid errors on cached pages), and 
future code
@@ -104,6 +121,7 @@
 $wgHooks['PageContentInsertComplete'][] = 
'WikimediaEventsHooks::onPageContentInsertComplete';
 $wgHooks['EditPageBeforeConflictDiff'][] = 
'WikimediaEventsHooks::onEditPageBeforeConflictDiff';
 $wgHooks['MakeGlobalVariablesScript'][] = 
'WikimediaEventsHooks::onMakeGlobalVariablesScript';
+$wgHooks['ResourceLoaderGetConfigVars'][] = 
'WikimediaEventsHooks::onResourceLoaderGetConfigVars';
 $wgHooks['ListDefinedTags'][] = 'WikimediaEventsHooks::onListDefinedTags';
 $wgHooks['RecentChange_save'][] = 'WikimediaEventsHooks::onRecentChange_save';
 
diff --git a/WikimediaEventsHooks.php b/WikimediaEventsHooks.php
index 731dd85..f1cda83 100644
--- a/WikimediaEventsHooks.php
+++ b/WikimediaEventsHooks.php
@@ -34,6 +34,11 @@
        public static function onBeforePageDisplay( &$out, &$skin ) {
                $out->addModules( 'ext.wikimediaEvents' );
 
+               global $wgWMEStatsdBaseUri;
+               if ( $wgWMEStatsdBaseUri !== false ) {
+                       $out->addModules( 'ext.wikimediaEvents.statsd' );
+               }
+
                $user = $out->getUser();
 
                if ( $user->isAnon() ) {
@@ -347,6 +352,11 @@
                $vars['wgHHVMStart'] = self::HHVM_START * 1000;
        }
 
+       public static function onResourceLoaderGetConfigVars( &$vars ) {
+               global $wgWMEStatsdBaseUri;
+               $vars['wgWMEStatsdBaseUri'] = $wgWMEStatsdBaseUri;
+       }
+
        /**
         * Register 'HHVM' change tag.
         *
diff --git a/modules/ext.wikimediaEvents.statsd.js 
b/modules/ext.wikimediaEvents.statsd.js
new file mode 100644
index 0000000..fe3c727
--- /dev/null
+++ b/modules/ext.wikimediaEvents.statsd.js
@@ -0,0 +1,61 @@
+/*!
+ * mw.track subscribers for statsd timers and counters.
+ *
+ * Track events of the form mw.track( 'timing.foo', 1234.56 ); are logged as 
foo=1235ms
+ * The time is assumed to be in milliseconds and is rounded to the nearest 
integer.
+ *
+ * Track events of the form mw.track( 'counter.bar', 5 ); are logged as bar=5c
+ * The shorthand mw.track( 'counter.baz' ); is equivalent to mw.track( 
'counter.baz', 1 );
+ *
+ * $wgWMEStatsdBaseUri must point to a URL that accepts query strings like 
?foo=1235ms&bar=5c&baz=1c
+ */
+
+( function ( mw ) {
+       var timer = null, queue = [];
+
+       if ( !mw.config.get( 'wgWMEStatsdBaseUri' ) ) {
+               // Not configured, do nothing
+               return;
+       }
+
+       function dispatch() {
+               var i, len, values;
+               // Send events in batches of 50
+               // Note that queue is an array, not an object, because keys can 
be repeated
+               while ( queue.length ) {
+                       // Ideally we'd use .map() here, but we have to support 
old browsers that don't have it
+                       values = queue.splice( 0, 50 );
+                       for ( i = 0, len = values.length; i < len; i++ ) {
+                               values[i] = values[i].key + '=' + 
values[i].value;
+                       }
+                       ( new Image() ).src = mw.config.get( 
'wgWMEStatsdBaseUri' ) + '?' + values.join( '&' );
+               }
+       }
+
+       function schedule() {
+               if ( timer !== null ) {
+                       clearTimeout( timer );
+               }
+               // Save up events until no events occur for 2 seconds
+               timer = setTimeout( dispatch, 2000 );
+       }
+
+       mw.trackSubscribe( 'timing.', function ( topic, time ) {
+               queue.push( {
+                       key: topic.substring( 'timing.'.length ),
+                       value: Math.round( time ) + 'ms'
+               } );
+               schedule();
+       } );
+
+       mw.trackSubscribe( 'counter.', function ( topic, count ) {
+               count = Math.round( count );
+               if ( isNaN( count ) ) {
+                       count = 1;
+               }
+               queue.push( {
+                       key: topic.substring( 'counter.'.length ),
+                       value: count + 'c'
+               } );
+       } );
+}( mediaWiki ) );
\ No newline at end of file

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic270426834506af19a1584d4d636dacceec340ef
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikimediaEvents
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to