<commit_before>"use strict";
var TXStatsD = require('node-txstatsd');
var StatsD = require('node-statsd');

var nameCache = {};
function normalizeName (name) {
    // See https://github.com/etsy/statsd/issues/110
    // Only [\w_.-] allowed, with '.' being the hierarchy separator.
    var res = nameCache[name];
    if (res) {
        return res;
    } else {
        nameCache[name] = name.replace( /[^\/a-zA-Z0-9\.\-]/g, '-' )
            .replace(/\//g, '_');
        return nameCache[name];
    }
}

// A simple console reporter. Useful for development.
var methods = ['timing','increment','decrement','gauge','unique'];
function LogStatsD(logger) {
    var self = this;
    methods.forEach(function(method) {
        self[method] = function(name, value, samplingInterval) {
            logger.log('trace/metrics', {
                message: [method, name, value].join(':'),
                method: method,
                name: name,
                value: value,
                samplingInterval: samplingInterval
            });
        };
    });
}

// Minimal StatsD wrapper
function makeStatsD(options, logger) {
    var statsd;
    var statsdOptions = {
        host: options.host,
        port: options.port,
        prefix: options.name + '.',
        suffix: '',
        txstatsd  : true,
        globalize : false,
        cacheDns  : true,
        mock      : false
    };
    if (options.type === 'txstatsd') {
        statsd = new TXStatsD(statsdOptions);
    } else if (options.type === 'log') {
        statsd = new LogStatsD(logger);
    } else {
        statsd = new StatsD(statsdOptions);
    }

    // Add a static utility method for stat name normalization
    statsd.normalizeName = normalizeName;

    // Also add a small utility to send a delta given a startTime
    statsd.endTiming = function endTiming(names, startTime, samplingInterval) {
        return this.timing(names, Date.now() - startTime, samplingInterval);
    };
<commit_msg>Stats: Normalise the service name and include it in LogStatsD reports<commit_after>"use strict";
var TXStatsD = require('node-txstatsd');
var StatsD = require('node-statsd');

var nameCache = {};
function normalizeName (name) {
    // See https://github.com/etsy/statsd/issues/110
    // Only [\w_.-] allowed, with '.' being the hierarchy separator.
    var res = nameCache[name];
    if (res) {
        return res;
    } else {
        nameCache[name] = name.replace( /[^\/a-zA-Z0-9\.\-]/g, '-' )
            .replace(/\//g, '_');
        return nameCache[name];
    }
}

// A simple console reporter. Useful for development.
var methods = ['timing','increment','decrement','gauge','unique'];
function LogStatsD(logger, serviceName) {
    var self = this;
    serviceName = serviceName ? serviceName + '.' : 'service-runner.';
    methods.forEach(function(method) {
        self[method] = function(name, value, samplingInterval) {
            name = serviceName + name;
            logger.log('trace/metrics', {
                message: [method, name, value].join(':'),
                method: method,
                name: name,
                value: value,
                samplingInterval: samplingInterval
            });
        };
    });
}

// Minimal StatsD wrapper
function makeStatsD(options, logger) {
    var statsd;
    var srvName = normalizeName(options.name);
    var statsdOptions = {
        host: options.host,
        port: options.port,
        prefix:  srvName + '.',
        suffix: '',
        txstatsd  : true,
        globalize : false,
        cacheDns  : true,
        mock      : false
    };
    if (options.type === 'txstatsd') {
        statsd = new TXStatsD(statsdOptions);
    } else if (options.type === 'log') {
        statsd = new LogStatsD(logger, srvName);
    } else {
        statsd = new StatsD(statsdOptions);
    }

    // Add a static utility method for stat name normalization
    statsd.normalizeName = normalizeName;

    // Also add a small utility to send a delta given a startTime
    statsd.endTiming = function endTiming(names, startTime, samplingInterval) {
        return this.timing(names, Date.now() - startTime, samplingInterval);
    };

{'fraction_non_alphanumeric': 0.13438924886009118, 'fraction_numerical': 0.0023998080153587713, 'mean_word_length': 2.2486360093530786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5344008', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1209, 'n_words': 312}