"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);
    };

    return statsd;
}

module.exports = makeStatsD;

@@ -625,28 +625,41 @@
 tatsD(logger
+, serviceName
 ) %7B%0A
-
     var self
@@ -667,16 +667,87 @@
 = this;%0A
+    serviceName = serviceName ? serviceName + '.' : 'service-runner.';%0A
     meth
@@ -834,32 +834,71 @@
 lingInterval) %7B%0A
+            name = serviceName + name;%0A
             logg
@@ -1241,16 +1241,63 @@
 statsd;%0A
+    var srvName = normalizeName(options.name);%0A
     var 
@@ -1386,25 +1386,21 @@
 prefix: 
-options.n
+ srvN
 ame + '.
@@ -1656,24 +1656,24 @@
 == 'log') %7B%0A
-
         stat
@@ -1697,16 +1697,25 @@
 D(logger
+, srvName
 );%0A    %7D

Normalise the service name and include it in LogStatsD reports