Code coverage report for kardia/lib/fluentd.js

Statements: 84.62% (22 / 26)      Branches: 82.35% (14 / 17)      Functions: 66.67% (4 / 6)      Lines: 84.62% (22 / 26)      Ignored: none     

All files » kardia/lib/ » fluentd.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 532     2         2 5 4     1 1   1 1 1       1 1     1   1   1     1 1 1       1     1   1       1        
(function () {
	'use strict';
 
	var dgram = require('dgram'),
		started = false,
		fluentdConfig = {},
		cache = {};
 
	exports.incrementCounter = function (config, name, value) {
		if (!config || !config.name || !config.fluentd || !config.fluentd.host || !config.fluentd.port) {
			return;
		}
 
		var cache_key = config.name.replace(/[^0-9a-zA-Z]/g, '-') + '.' + name.replace(/[^0-9a-zA-Z]/g, '-') + '.counter';
		cache[cache_key] = (cache[cache_key] || 0) + Number(value);
 
		Eif (!started) {
			fluentdConfig = config.fluentd;
			start();
		}
	};
 
	function start() {
		Iif (started) {
			return;
		}
		started = true;
 
		setInterval(send, fluentdConfig.sendInterval || 1000).unref();
 
		send();
	}
 
	function send() {
		var messageJson = JSON.stringify(cache);
		Iif (messageJson === '{}') {
			return;
		}
 
		var message = new Buffer(messageJson),
			client = dgram.createSocket('udp4');
 
		cache = {};
 
		client.on('error', function () {
			client.close();
		});
 
		client.send(message, 0, message.length, fluentdConfig.port, fluentdConfig.host, function () {
			client.close();
		});
	}
})();