Skip to content

Commit 30ce854

Browse files
committed
HADOOP-16496. Apply HDDS-1870 (ConcurrentModification at PrometheusMetricsSink) to Hadoop common.
This closes #1317 Reviewed-by: Bharat Viswanadham <[email protected]>
1 parent 10b4997 commit 30ce854

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/PrometheusMetricsSink.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
import java.io.IOException;
2828
import java.io.Writer;
29-
import java.util.HashMap;
3029
import java.util.Map;
30+
import java.util.concurrent.ConcurrentHashMap;
3131
import java.util.regex.Pattern;
3232

3333
import org.apache.commons.lang3.StringUtils;
@@ -42,7 +42,7 @@ public class PrometheusMetricsSink implements MetricsSink {
4242
/**
4343
* Cached output lines for each metrics.
4444
*/
45-
private Map<String, String> metricLines = new HashMap<>();
45+
private final Map<String, String> metricLines = new ConcurrentHashMap<>();
4646

4747
private static final Pattern SPLIT_PATTERN =
4848
Pattern.compile("(?<!(^|[A-Z_]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");
@@ -61,9 +61,13 @@ public void putMetrics(MetricsRecord metricsRecord) {
6161
metricsRecord.name(), metrics.name());
6262

6363
StringBuilder builder = new StringBuilder();
64-
builder.append("# TYPE " + key + " " +
65-
metrics.type().toString().toLowerCase() + "\n");
66-
builder.append(key + "{");
64+
builder.append("# TYPE ")
65+
.append(key)
66+
.append(" ")
67+
.append(metrics.type().toString().toLowerCase())
68+
.append("\n")
69+
.append(key)
70+
.append("{");
6771
String sep = "";
6872

6973
//add tags
@@ -72,13 +76,17 @@ public void putMetrics(MetricsRecord metricsRecord) {
7276

7377
//ignore specific tag which includes sub-hierarchy
7478
if (!tagName.equals("numopenconnectionsperuser")) {
75-
builder.append(
76-
sep + tagName + "=\"" + tag.value() + "\"");
79+
builder.append(sep)
80+
.append(tagName)
81+
.append("=\"")
82+
.append(tag.value())
83+
.append("\"");
7784
sep = ",";
7885
}
7986
}
8087
builder.append("} ");
8188
builder.append(metrics.value());
89+
builder.append("\n");
8290
metricLines.put(key, builder.toString());
8391

8492
}
@@ -110,7 +118,7 @@ public void init(SubsetConfiguration subsetConfiguration) {
110118

111119
public void writeMetrics(Writer writer) throws IOException {
112120
for (String line : metricLines.values()) {
113-
writer.write(line + "\n");
121+
writer.write(line);
114122
}
115123
}
116124
}

0 commit comments

Comments
 (0)