/**
 * 
 */
package com.krux.stdlib.statsd;

import java.net.InetAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.krux.stdlib.KruxStdLib;

/**
 * @author cass
 * 
 */
public class KruxStatsdClient extends StatsdClient {

    final static Logger log = LoggerFactory.getLogger( KruxStatsdClient.class );

    private String keyNamespace;
    private String statsdSuffix;

    public KruxStatsdClient( String host, int port, Logger logger, KruxStdLib stdLib ) throws Exception {
        super( host, port, logger );
        init(stdLib);
    }

    protected void init(KruxStdLib stdLib) {
        keyNamespace = stdLib.getStatsdEnv().toLowerCase() + "." + stdLib.getAppName().toLowerCase() + ".";
        try {
            String hostName = InetAddress.getLocalHost().getHostName().toLowerCase();
            if ( hostName.contains( "." ) ) {
                String[] parts = hostName.split( "\\." );
                hostName = parts[0];
            }
            statsdSuffix = "." + hostName;
        } catch ( Exception e ) {
            log.warn( "Cannot get a real hostname, defaulting to something stupid" );
            statsdSuffix = "." + "unknown";
        }
    }

    public boolean gauge( String key, long value ) {
        return stat( StatsdStatType.GAUGE, key, value, 1.0 );
    }

    public boolean stat( StatsdStatType type, String key, long value, double sampleRate ) {
        return super.stat( type, fullKey( key ), value, sampleRate );
    }

    private String fullKey( String appKey ) {
        return keyNamespace + appKey.toLowerCase() + statsdSuffix;
    }

}

@@ -362,41 +362,8 @@
 ace;
-%0A    private String statsdSuffix;
 %0A%0A  
@@ -689,480 +689,8 @@
 .%22;%0A
-        try %7B%0A            String hostName = InetAddress.getLocalHost().getHostName().toLowerCase();%0A            if ( hostName.contains( %22.%22 ) ) %7B%0A                String%5B%5D parts = hostName.split( %22%5C%5C.%22 );%0A                hostName = parts%5B0%5D;%0A            %7D%0A            statsdSuffix = %22.%22 + hostName;%0A        %7D catch ( Exception e ) %7B%0A            log.warn( %22Cannot get a real hostname, defaulting to something stupid%22 );%0A            statsdSuffix = %22.%22 + %22unknown%22;%0A        %7D%0A
     
@@ -1083,30 +1083,15 @@
 se()
- + statsdSuffix
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -1087,11 +1087,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Stop appending `.hostname` to statsd key