2121
2222import java .io .IOException ;
2323import java .io .Writer ;
24- import java .util .HashMap ;
2524import java .util .Map ;
25+ import java .util .concurrent .ConcurrentHashMap ;
2626import java .util .regex .Pattern ;
2727
2828import org .apache .commons .lang3 .StringUtils ;
@@ -44,7 +44,7 @@ public class PrometheusMetricsSink implements MetricsSink {
4444 /**
4545 * Cached output lines for each metrics.
4646 */
47- private Map <String , String > metricLines = new HashMap <>();
47+ private final Map <String , String > metricLines = new ConcurrentHashMap <>();
4848
4949 private static final Pattern SPLIT_PATTERN =
5050 Pattern .compile ("(?<!(^|[A-Z_]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])" );
@@ -65,9 +65,13 @@ public void putMetrics(MetricsRecord metricsRecord) {
6565 metricsRecord .name (), metrics .name ());
6666
6767 StringBuilder builder = new StringBuilder ();
68- builder .append ("# TYPE " + key + " " +
69- metrics .type ().toString ().toLowerCase () + "\n " );
70- builder .append (key + "{" );
68+ builder .append ("# TYPE " )
69+ .append (key )
70+ .append (" " )
71+ .append (metrics .type ().toString ().toLowerCase ())
72+ .append ("\n " )
73+ .append (key )
74+ .append ("{" );
7175 String sep = "" ;
7276
7377 //add tags
@@ -76,13 +80,17 @@ public void putMetrics(MetricsRecord metricsRecord) {
7680
7781 //ignore specific tag which includes sub-hierarchy
7882 if (!tagName .equals ("numopenconnectionsperuser" )) {
79- builder .append (
80- sep + tagName + "=\" " + tag .value () + "\" " );
83+ builder .append (sep )
84+ .append (tagName )
85+ .append ("=\" " )
86+ .append (tag .value ())
87+ .append ("\" " );
8188 sep = "," ;
8289 }
8390 }
8491 builder .append ("} " );
8592 builder .append (metrics .value ());
93+ builder .append ("\n " );
8694 metricLines .put (key , builder .toString ());
8795
8896 }
@@ -121,7 +129,7 @@ public void init(SubsetConfiguration subsetConfiguration) {
121129
122130 public void writeMetrics (Writer writer ) throws IOException {
123131 for (String line : metricLines .values ()) {
124- writer .write (line + " \n " );
132+ writer .write (line );
125133 }
126134 }
127135}
0 commit comments