Skip to content

Commit 9043b47

Browse files
michaldoMichal Domagala
andauthored
Use buffered writer (#1241)
Without buffered reader, every single output character through given stack goes to "StreamEncoder public void write(int c)" and single element char array is created. It makes pressure to GC and CPU BufferedReader reduce the pressure write:130, StreamEncoder (sun.nio.cs) write:201, OutputStreamWriter (java.io) writeMetadata:338, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats) writeGauge:129, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats) write:68, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats) write:48, PrometheusOutputFormat$1 (org.springframework.boot.actuate.metrics.export.prometheus) scrape:87, PrometheusScrapeEndpoint (org.springframework.boot.actuate.metrics.export.prometheus) StreamEncoder public void write(int c) throws IOException { char[] cbuf = new char[1]; cbuf[0] = (char) c; Signed-off-by: Michal Domagala <[email protected]> Co-authored-by: Michal Domagala <[email protected]>
1 parent 5ee7af3 commit 9043b47

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
2121
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
2222
import io.prometheus.metrics.model.snapshots.UnknownSnapshot;
23+
import java.io.BufferedWriter;
2324
import java.io.IOException;
2425
import java.io.OutputStream;
2526
import java.io.OutputStreamWriter;
@@ -59,7 +60,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
5960
// See https://prometheus.io/docs/instrumenting/exposition_formats/
6061
// "unknown", "gauge", "counter", "stateset", "info", "histogram", "gaugehistogram", and
6162
// "summary".
62-
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
63+
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
6364
for (MetricSnapshot snapshot : metricSnapshots) {
6465
if (snapshot.getDataPoints().size() > 0) {
6566
if (snapshot instanceof CounterSnapshot) {
@@ -95,7 +96,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
9596
writer.flush();
9697
}
9798

98-
public void writeCreated(OutputStreamWriter writer, MetricSnapshot snapshot) throws IOException {
99+
public void writeCreated(Writer writer, MetricSnapshot snapshot) throws IOException {
99100
boolean metadataWritten = false;
100101
MetricMetadata metadata = snapshot.getMetadata();
101102
for (DataPointSnapshot data : snapshot.getDataPoints()) {
@@ -111,8 +112,7 @@ public void writeCreated(OutputStreamWriter writer, MetricSnapshot snapshot) thr
111112
}
112113
}
113114

114-
private void writeCounter(OutputStreamWriter writer, CounterSnapshot snapshot)
115-
throws IOException {
115+
private void writeCounter(Writer writer, CounterSnapshot snapshot) throws IOException {
116116
if (snapshot.getDataPoints().size() > 0) {
117117
MetricMetadata metadata = snapshot.getMetadata();
118118
writeMetadata(writer, "_total", "counter", metadata);
@@ -124,7 +124,7 @@ private void writeCounter(OutputStreamWriter writer, CounterSnapshot snapshot)
124124
}
125125
}
126126

127-
private void writeGauge(OutputStreamWriter writer, GaugeSnapshot snapshot) throws IOException {
127+
private void writeGauge(Writer writer, GaugeSnapshot snapshot) throws IOException {
128128
MetricMetadata metadata = snapshot.getMetadata();
129129
writeMetadata(writer, "", "gauge", metadata);
130130
for (GaugeSnapshot.GaugeDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -134,8 +134,7 @@ private void writeGauge(OutputStreamWriter writer, GaugeSnapshot snapshot) throw
134134
}
135135
}
136136

137-
private void writeHistogram(OutputStreamWriter writer, HistogramSnapshot snapshot)
138-
throws IOException {
137+
private void writeHistogram(Writer writer, HistogramSnapshot snapshot) throws IOException {
139138
MetricMetadata metadata = snapshot.getMetadata();
140139
writeMetadata(writer, "", "histogram", metadata);
141140
for (HistogramSnapshot.HistogramDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -182,8 +181,7 @@ private ClassicHistogramBuckets getClassicBuckets(
182181
}
183182

184183
private void writeGaugeCountSum(
185-
OutputStreamWriter writer, HistogramSnapshot snapshot, MetricMetadata metadata)
186-
throws IOException {
184+
Writer writer, HistogramSnapshot snapshot, MetricMetadata metadata) throws IOException {
187185
// Prometheus text format does not support gaugehistogram's _gcount and _gsum.
188186
// So we append _gcount and _gsum as gauge metrics.
189187
boolean metadataWritten = false;
@@ -212,8 +210,7 @@ private void writeGaugeCountSum(
212210
}
213211
}
214212

215-
private void writeSummary(OutputStreamWriter writer, SummarySnapshot snapshot)
216-
throws IOException {
213+
private void writeSummary(Writer writer, SummarySnapshot snapshot) throws IOException {
217214
boolean metadataWritten = false;
218215
MetricMetadata metadata = snapshot.getMetadata();
219216
for (SummarySnapshot.SummaryDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -248,7 +245,7 @@ private void writeSummary(OutputStreamWriter writer, SummarySnapshot snapshot)
248245
}
249246
}
250247

251-
private void writeInfo(OutputStreamWriter writer, InfoSnapshot snapshot) throws IOException {
248+
private void writeInfo(Writer writer, InfoSnapshot snapshot) throws IOException {
252249
MetricMetadata metadata = snapshot.getMetadata();
253250
writeMetadata(writer, "_info", "gauge", metadata);
254251
for (InfoSnapshot.InfoDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -258,8 +255,7 @@ private void writeInfo(OutputStreamWriter writer, InfoSnapshot snapshot) throws
258255
}
259256
}
260257

261-
private void writeStateSet(OutputStreamWriter writer, StateSetSnapshot snapshot)
262-
throws IOException {
258+
private void writeStateSet(Writer writer, StateSetSnapshot snapshot) throws IOException {
263259
MetricMetadata metadata = snapshot.getMetadata();
264260
writeMetadata(writer, "", "gauge", metadata);
265261
for (StateSetSnapshot.StateSetDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -292,8 +288,7 @@ private void writeStateSet(OutputStreamWriter writer, StateSetSnapshot snapshot)
292288
}
293289
}
294290

295-
private void writeUnknown(OutputStreamWriter writer, UnknownSnapshot snapshot)
296-
throws IOException {
291+
private void writeUnknown(Writer writer, UnknownSnapshot snapshot) throws IOException {
297292
MetricMetadata metadata = snapshot.getMetadata();
298293
writeMetadata(writer, "", "untyped", metadata);
299294
for (UnknownSnapshot.UnknownDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -303,13 +298,13 @@ private void writeUnknown(OutputStreamWriter writer, UnknownSnapshot snapshot)
303298
}
304299
}
305300

306-
private void writeNameAndLabels(
307-
OutputStreamWriter writer, String name, String suffix, Labels labels) throws IOException {
301+
private void writeNameAndLabels(Writer writer, String name, String suffix, Labels labels)
302+
throws IOException {
308303
writeNameAndLabels(writer, name, suffix, labels, null, 0.0);
309304
}
310305

311306
private void writeNameAndLabels(
312-
OutputStreamWriter writer,
307+
Writer writer,
313308
String name,
314309
String suffix,
315310
Labels labels,
@@ -327,8 +322,7 @@ private void writeNameAndLabels(
327322
}
328323

329324
private void writeMetadata(
330-
OutputStreamWriter writer, String suffix, String typeString, MetricMetadata metadata)
331-
throws IOException {
325+
Writer writer, String suffix, String typeString, MetricMetadata metadata) throws IOException {
332326
if (metadata.getHelp() != null && !metadata.getHelp().isEmpty()) {
333327
writer.write("# HELP ");
334328
writer.write(metadata.getPrometheusName());
@@ -365,7 +359,7 @@ private void writeEscapedHelp(Writer writer, String s) throws IOException {
365359
}
366360
}
367361

368-
private void writeScrapeTimestampAndNewline(OutputStreamWriter writer, DataPointSnapshot data)
362+
private void writeScrapeTimestampAndNewline(Writer writer, DataPointSnapshot data)
369363
throws IOException {
370364
if (data.hasScrapeTimestamp()) {
371365
writer.write(' ');

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/TextFormatUtil.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import io.prometheus.metrics.model.snapshots.Labels;
44
import java.io.IOException;
5-
import java.io.OutputStreamWriter;
65
import java.io.Writer;
76

87
public class TextFormatUtil {
98

10-
static void writeLong(OutputStreamWriter writer, long value) throws IOException {
9+
static void writeLong(Writer writer, long value) throws IOException {
1110
writer.append(Long.toString(value));
1211
}
1312

14-
static void writeDouble(OutputStreamWriter writer, double d) throws IOException {
13+
static void writeDouble(Writer writer, double d) throws IOException {
1514
if (d == Double.POSITIVE_INFINITY) {
1615
writer.write("+Inf");
1716
} else if (d == Double.NEGATIVE_INFINITY) {
@@ -22,7 +21,7 @@ static void writeDouble(OutputStreamWriter writer, double d) throws IOException
2221
}
2322
}
2423

25-
static void writeTimestamp(OutputStreamWriter writer, long timestampMs) throws IOException {
24+
static void writeTimestamp(Writer writer, long timestampMs) throws IOException {
2625
writer.write(Long.toString(timestampMs / 1000L));
2726
writer.write(".");
2827
long ms = timestampMs % 1000;
@@ -55,10 +54,7 @@ static void writeEscapedLabelValue(Writer writer, String s) throws IOException {
5554
}
5655

5756
static void writeLabels(
58-
OutputStreamWriter writer,
59-
Labels labels,
60-
String additionalLabelName,
61-
double additionalLabelValue)
57+
Writer writer, Labels labels, String additionalLabelName, double additionalLabelValue)
6258
throws IOException {
6359
writer.write('{');
6460
for (int i = 0; i < labels.size(); i++) {

0 commit comments

Comments
 (0)