daily pastebin goal
17%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. var dgram = require('dgram');
  4. var assert = require('assert');
  5. var net = require('net');
  6. var StatsD = require('../').StatsD;
  7.  
  8. /**
  9.  * Since sampling uses random, we need to patch Math.random() to always give
  10.  * a consistent result
  11.  */
  12. Math.random = function () {
  13.   return 0.42;
  14. };
  15.  
  16. function createTCPServer(onListening) {
  17.   var server = net.createServer(function (socket) {
  18.     socket.setEncoding('ascii');
  19.     socket.on('data', function (data) {
  20.       var metrics;
  21.       if (data) {
  22.         metrics = data.split('\n').filter(function (part) {
  23.           return part !== '';
  24.         });
  25.         server.emit('metrics', metrics);
  26.       }
  27.     });
  28.   });
  29.  
  30.   server.on('listening', function () {
  31.     onListening(server.address());
  32.   });
  33.  
  34.   server.listen(0, '127.0.0.1');
  35.   return server;
  36. }
  37.  
  38. function createUDPServer(onListening){
  39.   var server = dgram.createSocket("udp4");
  40.   server.on('message', function(message){
  41.     var metrics = message.toString();
  42.     server.emit('metrics', metrics);
  43.   });
  44.  
  45.   server.on('listening', function(){
  46.     onListening(server.address());
  47.   });
  48.  
  49.   server.bind(0, '127.0.0.1');
  50.   return server;
  51. }
  52.  
  53. describe('StatsD', function () {
  54.   var server;
  55.   var statsd;
  56.  
  57.   afterEach(function () {
  58.     // Remove it from the namespace to not fail other tests
  59.     delete global.statsd;
  60.     statsd = null;
  61.   });
  62.  
  63.   describe('#globalTags TCP', function () {
  64.     it('should not add global tags if they are not specified', function (done) {
  65.       server = createTCPServer(function (address) {
  66.         statsd = new StatsD({
  67.           host: address.address,
  68.           port: address.port,
  69.           protocol: 'tcp'
  70.         });
  71.         statsd.increment('test');
  72.       });
  73.       server.on('metrics', function (metrics) {
  74.         assert.equal(metrics, 'test:1|c');
  75.         server.close();
  76.         done();
  77.       });
  78.     });
  79.  
  80.     it('should add global tags if they are specified', function (done) {
  81.       server = createTCPServer(function (address) {
  82.         statsd = new StatsD({
  83.           host: address.address,
  84.           port: address.port,
  85.           global_tags: ['gtag'],
  86.           protocol: 'tcp'
  87.         });
  88.         statsd.increment('test');
  89.       });
  90.       server.on('metrics', function (metrics) {
  91.         assert.equal(metrics, 'test:1|c|#gtag');
  92.         server.close();
  93.         done();
  94.       });
  95.     });
  96.  
  97.     it('should combine global tags and metric tags', function (done) {
  98.       server = createTCPServer(function (address) {
  99.         statsd = new StatsD({
  100.           host: address.address,
  101.           port: address.port,
  102.           global_tags: ['gtag'],
  103.           protocol: 'tcp'
  104.         });
  105.         statsd.increment('test', 1337, ['foo']);
  106.       });
  107.       server.on('metrics', function (metrics) {
  108.         assert.equal(metrics, 'test:1337|c|#gtag,foo');
  109.         server.close();
  110.         done();
  111.       });
  112.     });
  113.   });
  114.  
  115.   describe('#globalTags UDP', function () {
  116.     it('should not add global tags if they are not specified', function (done) {
  117.       server = createUDPServer(function (address) {
  118.         console.log(address);
  119.         statsd = new StatsD({
  120.           host: address.address,
  121.           port: address.port,
  122.         });
  123.         statsd.increment('test');
  124.       });
  125.       server.on('metrics', function (metrics) {
  126.         assert.equal(metrics, 'test:1|c');
  127.         server.close();
  128.         done();
  129.       });
  130.     });
  131.  
  132.     it('should add global tags if they are specified', function (done) {
  133.       server = createUDPServer(function (address) {
  134.         statsd = new StatsD({
  135.           host: address.address,
  136.           port: address.port,
  137.           global_tags: ['gtag'],
  138.         });
  139.         statsd.increment('test');
  140.       });
  141.       server.on('metrics', function (metrics) {
  142.         assert.equal(metrics, 'test:1|c|#gtag');
  143.         server.close();
  144.         done();
  145.       });
  146.     });
  147.  
  148.     it('should combine global tags and metric tags', function (done) {
  149.       server = createUDPServer(function (address) {
  150.         statsd = new StatsD({
  151.           host: address.address,
  152.           port: address.port,
  153.           global_tags: ['gtag'],
  154.         });
  155.         statsd.increment('test', 1337, ['foo']);
  156.       });
  157.       server.on('metrics', function (metrics) {
  158.         assert.equal(metrics, 'test:1337|c|#gtag,foo');
  159.         server.close();
  160.         done();
  161.       });
  162.     });
  163.   });
  164.  
  165. }.bind(null, StatsD));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top