@@ -86,6 +86,23 @@ protected NumberFormat initialValue() {
8686 }
8787 };
8888
89+ private static final ThreadLocal <NumberFormat > SAMPLE_RATE_FORMATTERS = new ThreadLocal <NumberFormat >() {
90+ @ Override
91+ protected NumberFormat initialValue () {
92+ final NumberFormat numberFormatter = NumberFormat .getInstance (Locale .US );
93+ numberFormatter .setGroupingUsed (false );
94+ numberFormatter .setMinimumFractionDigits (6 );
95+
96+ if (numberFormatter instanceof DecimalFormat ) {
97+ final DecimalFormat decimalFormat = (DecimalFormat ) numberFormatter ;
98+ final DecimalFormatSymbols symbols = decimalFormat .getDecimalFormatSymbols ();
99+ symbols .setNaN ("NaN" );
100+ decimalFormat .setDecimalFormatSymbols (symbols );
101+ }
102+ return numberFormatter ;
103+ }
104+ };
105+
89106 private final String prefix ;
90107 private final DatagramChannel clientChannel ;
91108 private final StatsDClientErrorHandler handler ;
@@ -290,7 +307,7 @@ public NonBlockingStatsDClient(final String prefix, final String hostname, final
290307 public NonBlockingStatsDClient (final String prefix , final int queueSize , String [] constantTags , final StatsDClientErrorHandler errorHandler ,
291308 final Callable <InetSocketAddress > addressLookup ) throws StatsDClientException {
292309 if ((prefix != null ) && (!prefix .isEmpty ())) {
293- this .prefix = String . format ( "%s." , prefix );
310+ this .prefix = new StringBuilder ( prefix ). append ( "." ). toString ( );
294311 } else {
295312 this .prefix = "" ;
296313 }
@@ -399,7 +416,7 @@ String tagString(final String[] tags) {
399416 */
400417 @ Override
401418 public void count (final String aspect , final long delta , final String ... tags ) {
402- send (String . format ( "%s%s:%d|c%s" , prefix , aspect , delta , tagString (tags )));
419+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( delta ). append ( "|c" ). append ( tagString (tags )). toString ( ));
403420 }
404421
405422 /**
@@ -410,7 +427,7 @@ public void count(final String aspect, final long delta, final double sampleRate
410427 if (isInvalidSample (sampleRate )) {
411428 return ;
412429 }
413- send (String . format ( "%s%s:%d |c|@%f%s" , prefix , aspect , delta , sampleRate , tagString (tags )));
430+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( delta ). append ( " |c|@" ). append ( SAMPLE_RATE_FORMATTERS . get (). format ( sampleRate )). append ( tagString (tags )). toString ( ));
414431 }
415432
416433 /**
@@ -427,7 +444,7 @@ public void count(final String aspect, final long delta, final double sampleRate
427444 */
428445 @ Override
429446 public void count (final String aspect , final double delta , final String ... tags ) {
430- send (String . format ( "%s%s:%s|c%s" , prefix , aspect , NUMBER_FORMATTERS .get ().format (delta ), tagString (tags )));
447+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS .get ().format (delta )). append ( "|c" ). append ( tagString (tags )). toString ( ));
431448 }
432449
433450 /**
@@ -438,7 +455,7 @@ public void count(final String aspect, final double delta, final double sampleRa
438455 if (isInvalidSample (sampleRate )) {
439456 return ;
440457 }
441- send (String . format ( "%s%s:%s |c|@%f%s" , prefix , aspect , NUMBER_FORMATTERS . get ().format (delta ), sampleRate , tagString (tags )));
458+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS . get (). format ( delta )). append ( " |c|@" ). append ( SAMPLE_RATE_FORMATTERS . get ().format (sampleRate )). append ( tagString (tags )). toString ( ));
442459 }
443460
444461 /**
@@ -535,7 +552,7 @@ public void decrement(final String aspect, final double sampleRate, final String
535552 public void recordGaugeValue (final String aspect , final double value , final String ... tags ) {
536553 /* Intentionally using %s rather than %f here to avoid
537554 * padding with extra 0s to represent precision */
538- send (String . format ( "%s%s:%s|g%s" , prefix , aspect , NUMBER_FORMATTERS .get ().format (value ), tagString (tags )));
555+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS .get ().format (value )). append ( "|g" ). append ( tagString (tags )). toString ( ));
539556 }
540557
541558 /**
@@ -546,7 +563,7 @@ public void recordGaugeValue(final String aspect, final double value, final doub
546563 if (isInvalidSample (sampleRate )) {
547564 return ;
548565 }
549- send (String . format ( "%s%s:%s |g|@%f%s" , prefix , aspect , NUMBER_FORMATTERS . get ().format (value ), sampleRate , tagString (tags )));
566+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS . get (). format ( value )). append ( " |g|@" ). append ( SAMPLE_RATE_FORMATTERS . get ().format (sampleRate )). append ( tagString (tags )). toString ( ));
550567 }
551568
552569 /**
@@ -580,7 +597,7 @@ public void gauge(final String aspect, final double value, final double sampleRa
580597 */
581598 @ Override
582599 public void recordGaugeValue (final String aspect , final long value , final String ... tags ) {
583- send (String . format ( "%s%s:%d|g%s" , prefix , aspect , value , tagString (tags )));
600+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( value ). append ( "|g" ). append ( tagString (tags )). toString ( ));
584601 }
585602
586603 /**
@@ -591,7 +608,7 @@ public void recordGaugeValue(final String aspect, final long value, final double
591608 if (isInvalidSample (sampleRate )) {
592609 return ;
593610 }
594- send (String . format ( "%s%s:%d |g|@%f%s" , prefix , aspect , value , sampleRate , tagString (tags )));
611+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( value ). append ( " |g|@" ). append ( SAMPLE_RATE_FORMATTERS . get (). format ( sampleRate )). append ( tagString (tags )). toString ( ));
595612 }
596613
597614 /**
@@ -624,7 +641,7 @@ public void gauge(final String aspect, final long value, final double sampleRate
624641 */
625642 @ Override
626643 public void recordExecutionTime (final String aspect , final long timeInMs , final String ... tags ) {
627- send (String . format ( "%s%s:%d |ms%s" , prefix , aspect , timeInMs , tagString (tags )));
644+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( timeInMs ). append ( " |ms" ). append ( tagString (tags )). toString ( ));
628645 }
629646
630647 /**
@@ -635,7 +652,7 @@ public void recordExecutionTime(final String aspect, final long timeInMs, final
635652 if (isInvalidSample (sampleRate )) {
636653 return ;
637654 }
638- send (String . format ( "%s%s:%d |ms|@%f%s" , prefix , aspect , timeInMs , sampleRate , tagString (tags )));
655+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( timeInMs ). append ( " |ms|@" ). append ( SAMPLE_RATE_FORMATTERS . get (). format ( sampleRate )). append ( tagString (tags )). toString ( ));
639656 }
640657
641658 /**
@@ -670,7 +687,7 @@ public void time(final String aspect, final long value, final double sampleRate,
670687 public void recordHistogramValue (final String aspect , final double value , final String ... tags ) {
671688 /* Intentionally using %s rather than %f here to avoid
672689 * padding with extra 0s to represent precision */
673- send (String . format ( "%s%s:%s|h%s" , prefix , aspect , NUMBER_FORMATTERS .get ().format (value ), tagString (tags )));
690+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS .get ().format (value )). append ( "|h" ). append ( tagString (tags )). toString ( ));
674691 }
675692
676693 /**
@@ -683,7 +700,7 @@ public void recordHistogramValue(final String aspect, final double value, final
683700 }
684701 /* Intentionally using %s rather than %f here to avoid
685702 * padding with extra 0s to represent precision */
686- send (String . format ( "%s%s:%s |h|@%f%s" , prefix , aspect , NUMBER_FORMATTERS . get ().format (value ), sampleRate , tagString (tags )));
703+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS . get (). format ( value )). append ( " |h|@" ). append ( SAMPLE_RATE_FORMATTERS . get ().format (sampleRate )). append ( tagString (tags )). toString ( ));
687704 }
688705
689706 /**
@@ -716,7 +733,7 @@ public void histogram(final String aspect, final double value, final double samp
716733 */
717734 @ Override
718735 public void recordHistogramValue (final String aspect , final long value , final String ... tags ) {
719- send (String . format ( "%s%s:%d|h%s" , prefix , aspect , value , tagString (tags )));
736+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( value ). append ( "|h" ). append ( tagString (tags )). toString ( ));
720737 }
721738
722739 /**
@@ -727,7 +744,7 @@ public void recordHistogramValue(final String aspect, final long value, final do
727744 if (isInvalidSample (sampleRate )) {
728745 return ;
729746 }
730- send (String . format ( "%s%s:%d |h|@%f%s" , prefix , aspect , value , sampleRate , tagString (tags )));
747+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( value ). append ( " |h|@" ). append ( SAMPLE_RATE_FORMATTERS . get (). format ( sampleRate )). append ( tagString (tags )). toString ( ));
731748 }
732749
733750 /**
@@ -764,7 +781,7 @@ public void histogram(final String aspect, final long value, final double sample
764781 public void recordDistributionValue (final String aspect , final double value , final String ... tags ) {
765782 /* Intentionally using %s rather than %f here to avoid
766783 * padding with extra 0s to represent precision */
767- send (String . format ( "%s%s:%s|d%s" , prefix , aspect , NUMBER_FORMATTERS .get ().format (value ), tagString (tags )));
784+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS .get ().format (value )). append ( "|d" ). append ( tagString (tags )). toString ( ));
768785 }
769786
770787 /**
@@ -777,7 +794,7 @@ public void recordDistributionValue(final String aspect, final double value, fin
777794 }
778795 /* Intentionally using %s rather than %f here to avoid
779796 * padding with extra 0s to represent precision */
780- send (String . format ( "%s%s:%s |d|@%f%s" , prefix , aspect , NUMBER_FORMATTERS . get ().format (value ), sampleRate , tagString (tags )));
797+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( NUMBER_FORMATTERS . get (). format ( value )). append ( " |d|@" ). append ( SAMPLE_RATE_FORMATTERS . get ().format (sampleRate )). append ( tagString (tags )). toString ( ));
781798 }
782799
783800 /**
@@ -811,7 +828,7 @@ public void distribution(final String aspect, final double value, final double s
811828 */
812829 @ Override
813830 public void recordDistributionValue (final String aspect , final long value , final String ... tags ) {
814- send (String . format ( "%s%s:%d|d%s" , prefix , aspect , value , tagString (tags )));
831+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( value ). append ( "|d" ). append ( tagString (tags )). toString ( ));
815832 }
816833
817834 /**
@@ -822,7 +839,7 @@ public void recordDistributionValue(final String aspect, final long value, final
822839 if (isInvalidSample (sampleRate )) {
823840 return ;
824841 }
825- send (String . format ( "%s%s:%d |d|@%f%s" , prefix , aspect , value , sampleRate , tagString (tags )));
842+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( value ). append ( " |d|@" ). append ( SAMPLE_RATE_FORMATTERS . get (). format ( sampleRate )). append ( tagString (tags )). toString ( ));
826843 }
827844
828845 /**
@@ -890,8 +907,8 @@ private String eventMap(final Event event) {
890907 public void recordEvent (final Event event , final String ... tags ) {
891908 final String title = escapeEventString (prefix + event .getTitle ());
892909 final String text = escapeEventString (event .getText ());
893- send (String . format ("_e{%d,%d}:%s|%s%s%s" ,
894- title . length (), text . length (), title , text , eventMap (event ), tagString (tags )));
910+ send (new StringBuilder ("_e{" ). append ( title . length ()). append ( "," ). append ( text . length ()). append ( "}:" ). append ( title )
911+ . append ( "|" ). append ( text ). append ( eventMap (event )). append ( tagString (tags )). toString ( ));
895912 }
896913
897914 private String escapeEventString (final String title ) {
@@ -916,16 +933,16 @@ public void recordServiceCheckRun(final ServiceCheck sc) {
916933 private String toStatsDString (final ServiceCheck sc ) {
917934 // see http://docs.datadoghq.com/guides/dogstatsd/#service-checks
918935 final StringBuilder sb = new StringBuilder ();
919- sb .append (String . format ( "_sc|%s|%d" , sc .getName (), sc .getStatus () ));
936+ sb .append ("_sc|" ). append ( sc .getName ()). append ( "|" ). append ( sc .getStatus ());
920937 if (sc .getTimestamp () > 0 ) {
921- sb .append (String . format ( "|d:%d" , sc .getTimestamp () ));
938+ sb .append ("|d:" ). append ( sc .getTimestamp ());
922939 }
923940 if (sc .getHostname () != null ) {
924- sb .append (String . format ( "|h:%s" , sc .getHostname () ));
941+ sb .append ("|h:" ). append ( sc .getHostname ());
925942 }
926943 sb .append (tagString (sc .getTags ()));
927944 if (sc .getMessage () != null ) {
928- sb .append (String . format ( "|m:%s" , sc .getEscapedMessage () ));
945+ sb .append ("|m:" ). append ( sc .getEscapedMessage ());
929946 }
930947 return sb .toString ();
931948 }
@@ -962,7 +979,7 @@ public void serviceCheck(final ServiceCheck sc) {
962979 public void recordSetValue (final String aspect , final String value , final String ... tags ) {
963980 // documentation is light, but looking at dogstatsd source, we can send string values
964981 // here instead of numbers
965- send (String . format ( "%s%s:%s|s%s" , prefix , aspect , value , tagString (tags )));
982+ send (new StringBuilder ( prefix ). append ( aspect ). append ( ":" ). append ( value ). append ( "|s" ). append ( tagString (tags )). toString ( ));
966983 }
967984
968985 private void send (final String message ) {
0 commit comments