Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/178411

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, 85 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikimediaEvents 
refs/changes/11/178411/1

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..def7641 100644
--- a/WikimediaEventsHooks.php
+++ b/WikimediaEventsHooks.php
@@ -41,6 +41,11 @@
                }
                $out->addModules( 'ext.wikimediaEvents.deprecate' );
 
+               global $wgWMEStatsdBaseUri;
+               if ( $wgWMEStatsdBaseUri !== false ) {
+                       $out->addModules( 'ext.wikimediaEvents.statsd' );
+               }
+
                $req = $out->getRequest();
                $currentCookieValue = $req->getCookie( 'hhvm', '' );
                if (
@@ -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..c8b3efc
--- /dev/null
+++ b/modules/ext.wikimediaEvents.statsd.js
@@ -0,0 +1,56 @@
+/*!
+ * 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 = [];
+
+       function dispatch() {
+               function formatEntry( entry ) {
+                       return entry.key + '=' + entry.value;
+               }
+
+               var query;
+               // Send events in batches of 50
+               // Note that queue is an array, not an object, because keys can 
be repeated
+               while ( queue.length ) {
+                       query = queue.splice( 0, 50 ).map( formatEntry ).join( 
'&' );
+                       ( new Image() ).src = mw.config.get( 
'wgWMEStatsdBaseUri' ) + '?' + query;
+               }
+       }
+
+       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: newchange
Gerrit-Change-Id: Ic270426834506af19a1584d4d636dacceec340ef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikimediaEvents
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>

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

Reply via email to