Skip to content

Commit

Permalink
HDDS-1870. ConcurrentModification at PrometheusMetricsSink (apache#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai authored and ahussein committed Oct 29, 2019
1 parent 1ef16cc commit e9da2b0
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

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

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

StringBuilder builder = new StringBuilder();
builder.append("# TYPE " + key + " " +
metrics.type().toString().toLowerCase() + "\n");
builder.append(key + "{");
builder.append("# TYPE ")
.append(key)
.append(" ")
.append(metrics.type().toString().toLowerCase())
.append("\n")
.append(key)
.append("{");
String sep = "";

//add tags
Expand All @@ -76,13 +80,17 @@ public void putMetrics(MetricsRecord metricsRecord) {

//ignore specific tag which includes sub-hierarchy
if (!tagName.equals("numopenconnectionsperuser")) {
builder.append(
sep + tagName + "=\"" + tag.value() + "\"");
builder.append(sep)
.append(tagName)
.append("=\"")
.append(tag.value())
.append("\"");
sep = ",";
}
}
builder.append("} ");
builder.append(metrics.value());
builder.append("\n");
metricLines.put(key, builder.toString());

}
Expand Down Expand Up @@ -121,7 +129,7 @@ public void init(SubsetConfiguration subsetConfiguration) {

public void writeMetrics(Writer writer) throws IOException {
for (String line : metricLines.values()) {
writer.write(line + "\n");
writer.write(line);
}
}
}

0 comments on commit e9da2b0

Please sign in to comment.