Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 3.64 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. if (!Date.now) {
  4.         Date.now = function() {
  5.                 return (new Date()).getTime();
  6.         };
  7. }
  8.  
  9.  
  10. var StatsD = (function($, undefined) {
  11.        
  12.         var activeTimers = { };
  13.         var queuedData = [ ];
  14.         var flushTimer = undefined;
  15.        
  16.         var configOpts = {
  17.                 url: '/statsd',
  18.                 flushInterval: 1000
  19.         };
  20.        
  21.         /**
  22.          * Update the client configuration of StatsD.
  23.          * @param opts an object containing new configuration values
  24.          */
  25.         function config(opts) {
  26.                 $.extend(configOpts, opts);
  27.         }
  28.        
  29.         /**
  30.          * Update a stat by an arbitrary amount.
  31.          * @param {String} stat the name of the stat to update
  32.          * @param {Number} delta the amount to update the stat by
  33.          * @param {Number} [sampleRate] a number between 0 and 1 indicating what percentage of this stat to sample
  34.          */
  35.         function update(stat, delta, sampleRate) {
  36.                 queue(stat, delta+"|c", sampleRate);
  37.         }
  38.  
  39.         /**
  40.          * Increment a stat by 1.
  41.          * @param {String} stat the name of the stat to update
  42.          * @param {Number} [sampleRate] a number between 0 and 1 indicating what percentage of this stat to sample
  43.          */
  44.         function increment(stat, sampleRate) {
  45.                 update(stat, 1, sampleRate);
  46.         }
  47.  
  48.         /**
  49.          * Decrement a stat by 1.
  50.          * @param {String} stat the name of the stat to update
  51.          * @param {Number} [sampleRate] a number between 0 and 1 indicating what percentage of this stat to sample
  52.          */
  53.         function decrement(stat, sampleRate) {
  54.                 update(stat, -1, sampleRate);
  55.         }
  56.  
  57.         /**
  58.          * Log timing information.
  59.          * @param {String} stat the name of the stat to update
  60.          * @param {Number} ms the time (in ms) to log
  61.          * @param {Number} [sampleRate] a number between 0 and 1 indicating what percentage of this stat to record
  62.          */
  63.         function timing(stat, ms, sampleRate) {
  64.                 send(stat, ms+"|ms", sampleRate);
  65.         }
  66.  
  67.         /**
  68.          * Start a timer that, when completed with stopTiming(), will log timing information.
  69.          * @param {String} stat the name of the stat to update
  70.          */
  71.         function startTiming(stat) {
  72.                 activeTimers[stat] = Date.now();
  73.         }
  74.        
  75.         /**
  76.          * Stop a running timer and log the amount of time (in ms) since it was started.
  77.          * @param {String} stat the name of the stat to update
  78.          * @param {Number} [sampleRate] a number between 0 and 1 indicating what percentage of this stat to sample
  79.          */
  80.         function stopTiming(stat, sampleRate) {
  81.                 var start = activeTimers[stat];
  82.                 delete activeTimers[stat];
  83.                 if (start) {
  84.                         timing(stat, Date.now() - start);
  85.                 }
  86.         }
  87.        
  88.         /**
  89.          * Queues up a piece of StatsD data to be sent to the server.
  90.          * @param {String} method the type of data that is being queues
  91.          * @param {String} stat the name of the stat being queued
  92.          * @param {String} value the value of the stat being queued
  93.          * @param {Number} [sampleRate] a number between 0 and 1 indicating what percentage of this stat to sample
  94.          */
  95.         function queue(stat, value, sampleRate) {
  96.                 var data = stat+":"+value;
  97.                
  98.                 if (sampleRate !== undefined && sampleRate != 1) {
  99.                         if (sampleRate < Math.random()) {
  100.                                 return;
  101.                         }
  102.                         data = data+"|@"+sampleRate;
  103.                 }
  104.                
  105.                 queuedData.push(data);
  106.                
  107.                 if (configOpts.flushInterval <= 0) {
  108.                         flush();
  109.                 } else if (!flushTimer) {
  110.                         flushTimer = setTimeout(flush, configOpts.flushInterval);
  111.                 }
  112.         }
  113.        
  114.         /**
  115.          * Send any queued StatsD data to the server. This data is
  116.          * automatically sent at a regular interval specificed by the
  117.          * flushInterval config setting.
  118.          */
  119.         function flush() {
  120.                 clearTimeout(flushTimer);
  121.                 flushTimer = 0;
  122.                
  123.                 var data = queuedData.join('\n');
  124.                 queuedData.splice(0, queuedData.length);
  125.                
  126.                 $.ajax({
  127.                         type: "POST",
  128.                         url: configOpts.url,
  129.                         contentType: 'text/plain',
  130.                         data: data
  131.                 });
  132.         }
  133.        
  134.         return {
  135.                 config: config,
  136.                 update: update,
  137.                 increment: increment,
  138.                 decrement: decrement,
  139.                 timing: timing,
  140.                 startTiming: startTiming,
  141.                 stopTiming: stopTiming,
  142.                 flush: flush
  143.         };
  144.        
  145. })(jQuery);