<commit_before>'use strict';

/**
 * Wrapper around the {@link https://github.com/dscape/lynx|lynx}
 * statsd client library. Forwards basic functionality and offers some
 * convenience functions top.
 *
 * @module
 */


// public interface
module.exports = {
	init: init,
	increment: increment,
	decrement: decrement,
	createTimer: createTimer,
	setupGaugeInterval: setupGaugeInterval,
	setupTimerInterval: setupTimerInterval,
};


require('harmony-reflect');
var config = require('config');
var Lynx = require('lynx');

var STATSD_FLUSH_INT = 10000;  // statsd flush interval in ms

var lynx = getMockLynx();
var intervals = {};


/**
 * Initializes the statsd client (if monitoring is enabled in the GS
 * configuration).
 */
function init() {
	intervals = {};
	var cfg = config.get('mon:statsd');
	if (!cfg.enabled) {
		log.info('monitoring disabled, using mock statsd client');
	}
	else {
		var scope = (cfg.prefix ? cfg.prefix + '.' : '') + config.getGsid();
		log.info('initializing statsd client (scope: %s)', scope);
		lynx = new Lynx(
			config.get('mon:statsd:host'), config.get('mon:statsd:port'),
			{scope: scope, on_error: lynxErrorHandler});
	}
	startSystemMetrics();
}


function lynxErrorHandler(err) {
	// crude way of filtering out unnecessary errors for stats that aren't sent
	// because of their sample rate
	// see: https://github.com/dscape/lynx/issues/10
	if (err.message !== 'Nothing to send') {
		log.error(err, 'error in statsd client');
	}
}


/**
 * Creates a mock statsd client object that just trace logs incoming
 * stats instead of sending them off. Suitable for testing or when
 * monitoring is disabled.
 *
 * @private
 */
function getMockLynx() {
	return new Proxy({}, {
		get: function get(target, name, receiver) {
			return function dummy() {
				if (global.log) {
					log.trace({fname: name, args: arguments}, 'statsd call');
				}
			};
		},
	});
}


/**
 * Increment a counter metric by 1.
 * see {@link https://github.com/dscape/lynx/ lynx} and {@link
 * https://github.com/etsy/statsd statsd} docs for details.
 */
function increment(stats, sampleRate) {
	return lynx.increment(stats, sampleRate);
}


/**
 * Decrement a counter metric by 1.
 * see {@link https://github.com/dscape/lynx/ lynx} and {@link
 * https://github.com/etsy/statsd statsd} docs for details.
 */
function decrement(stats, sampleRate) {
	return lynx.decrement(stats, sampleRate);
}


/**
 * Create a timer object for timing measurements.<commit_msg>stub the lynx Timer object when metrics are disabled<commit_after>'use strict';

/**
 * Wrapper around the {@link https://github.com/dscape/lynx|lynx}
 * statsd client library. Forwards basic functionality and offers some
 * convenience functions top.
 *
 * @module
 */


// public interface
module.exports = {
	init: init,
	increment: increment,
	decrement: decrement,
	createTimer: createTimer,
	setupGaugeInterval: setupGaugeInterval,
	setupTimerInterval: setupTimerInterval,
};


require('harmony-reflect');
var config = require('config');
var Lynx = require('lynx');

var STATSD_FLUSH_INT = 10000;  // statsd flush interval in ms

var lynx = getMockLynx();
var intervals = {};


/**
 * Initializes the statsd client (if monitoring is enabled in the GS
 * configuration).
 */
function init() {
	intervals = {};
	var cfg = config.get('mon:statsd');
	if (!cfg.enabled) {
		log.info('monitoring disabled, using mock statsd client');
	}
	else {
		var scope = (cfg.prefix ? cfg.prefix + '.' : '') + config.getGsid();
		log.info('initializing statsd client (scope: %s)', scope);
		lynx = new Lynx(
			config.get('mon:statsd:host'), config.get('mon:statsd:port'),
			{scope: scope, on_error: lynxErrorHandler});
	}
	startSystemMetrics();
}


function lynxErrorHandler(err) {
	// crude way of filtering out unnecessary errors for stats that aren't sent
	// because of their sample rate
	// see: https://github.com/dscape/lynx/issues/10
	if (err.message !== 'Nothing to send') {
		log.error(err, 'error in statsd client');
	}
}


/**
 * Creates a mock statsd client object that just trace logs incoming
 * stats instead of sending them off. Suitable for testing or when
 * monitoring is disabled.
 *
 * @private
 */
function getMockLynx() {
	return new Proxy({}, {
		get: function get(target, name, receiver) {
			return function dummy() {
				if (global.log) {
					log.trace({fname: name, args: arguments}, 'statsd call');
					if (name === 'createTimer') {
						return {
							stop: function stop() {}
						};
					}
				}
			};
		},
	});
}


/**
 * Increment a counter metric by 1.
 * see {@link https://github.com/dscape/lynx/ lynx} and {@link
 * https://github.com/etsy/statsd statsd} docs for details.
 */
function increment(stats, sampleRate) {
	return lynx.increment(stats, sampleRate);
}


/**
 * Decrement a counter metric by 1.
 * see {@link https://github.com/dscape/lynx/ lynx} and {@link
 * https://github.com/etsy/statsd statsd} docs for details.
 */
function decrement(stats, sampleRate) {
	return lynx.decrement(stats, sampleRate);
}


/**
 * Create a timer object for timing measurements.
{'fraction_non_alphanumeric': 0.14173073132268874, 'fraction_numerical': 0.0035481963335304554, 'mean_word_length': 3.818613485280152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2524235', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1592, 'n_words': 482}