Skip to content

Commit 9777a91

Browse files
committed
naming
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 0f9cef0 commit 9777a91

File tree

9 files changed

+62
-69
lines changed

9 files changed

+62
-69
lines changed

prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterProperties.java

+15-14
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ public class ExporterProperties {
77

88
private static final String INCLUDE_CREATED_TIMESTAMPS = "includeCreatedTimestamps";
99
// milliseconds is the default - we only provide a boolean flag to avoid a breaking change
10-
private static final String TIMESTAMPS_IN_MS = "timestampsInMs";
10+
private static final String PROMETHEUS_TIMESTAMPS_IN_MS = "prometheusTimestampsInMs";
1111
private static final String EXEMPLARS_ON_ALL_METRIC_TYPES = "exemplarsOnAllMetricTypes";
1212
private static final String PREFIX = "io.prometheus.exporter";
1313

1414
private final Boolean includeCreatedTimestamps;
15-
private final Boolean timestampsInMs;
15+
private final Boolean prometheusTimestampsInMs;
1616
private final Boolean exemplarsOnAllMetricTypes;
1717

1818
private ExporterProperties(
19-
Boolean includeCreatedTimestamps, Boolean timestampsInMs, Boolean exemplarsOnAllMetricTypes) {
19+
Boolean includeCreatedTimestamps,
20+
Boolean prometheusTimestampsInMs,
21+
Boolean exemplarsOnAllMetricTypes) {
2022
this.includeCreatedTimestamps = includeCreatedTimestamps;
21-
this.timestampsInMs = timestampsInMs;
23+
this.prometheusTimestampsInMs = prometheusTimestampsInMs;
2224
this.exemplarsOnAllMetricTypes = exemplarsOnAllMetricTypes;
2325
}
2426

@@ -27,9 +29,9 @@ public boolean getIncludeCreatedTimestamps() {
2729
return includeCreatedTimestamps != null && includeCreatedTimestamps;
2830
}
2931

30-
/** Use milliseconds for timestamps in text format? Default is {@code false}. */
31-
public boolean getTimestampsInMs() {
32-
return timestampsInMs != null && timestampsInMs;
32+
/** Use milliseconds for timestamps in prometheus text format? Default is {@code false}. */
33+
public boolean getPrometheusTimestampsInMs() {
34+
return prometheusTimestampsInMs != null && prometheusTimestampsInMs;
3335
}
3436

3537
/**
@@ -48,10 +50,9 @@ static ExporterProperties load(Map<Object, Object> properties)
4850
throws PrometheusPropertiesException {
4951
Boolean includeCreatedTimestamps =
5052
Util.loadBoolean(PREFIX + "." + INCLUDE_CREATED_TIMESTAMPS, properties);
51-
Boolean timestampsInMs = Util.loadBoolean(PREFIX + "." + TIMESTAMPS_IN_MS, properties);
53+
Boolean timestampsInMs = Util.loadBoolean(PREFIX + "." + PROMETHEUS_TIMESTAMPS_IN_MS, properties);
5254
Boolean exemplarsOnAllMetricTypes =
5355
Util.loadBoolean(PREFIX + "." + EXEMPLARS_ON_ALL_METRIC_TYPES, properties);
54-
// new prop?
5556
return new ExporterProperties(
5657
includeCreatedTimestamps, timestampsInMs, exemplarsOnAllMetricTypes);
5758
}
@@ -64,7 +65,7 @@ public static class Builder {
6465

6566
private Boolean includeCreatedTimestamps;
6667
private Boolean exemplarsOnAllMetricTypes;
67-
boolean timestampsInMs;
68+
boolean prometheusTimestampsInMs;
6869

6970
private Builder() {}
7071

@@ -80,15 +81,15 @@ public Builder exemplarsOnAllMetricTypes(boolean exemplarsOnAllMetricTypes) {
8081
return this;
8182
}
8283

83-
/** See {@link #getTimestampsInMs()}. */
84-
public Builder setTimestampsInMs(boolean timestampsInMs) {
85-
this.timestampsInMs = timestampsInMs;
84+
/** See {@link #getPrometheusTimestampsInMs()}. */
85+
public Builder prometheusTimestampsInMs(boolean prometheusTimestampsInMs) {
86+
this.prometheusTimestampsInMs = prometheusTimestampsInMs;
8687
return this;
8788
}
8889

8990
public ExporterProperties build() {
9091
return new ExporterProperties(
91-
includeCreatedTimestamps, timestampsInMs, exemplarsOnAllMetricTypes);
92+
includeCreatedTimestamps, prometheusTimestampsInMs, exemplarsOnAllMetricTypes);
9293
}
9394
}
9495
}

prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterPropertiesTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void builder() {
4848
ExporterProperties.builder()
4949
.includeCreatedTimestamps(true)
5050
.exemplarsOnAllMetricTypes(true)
51+
.prometheusTimestampsInMs(false)
5152
.build();
5253
assertThat(properties.getIncludeCreatedTimestamps()).isTrue();
5354
assertThat(properties.getExemplarsOnAllMetricTypes()).isTrue();

prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class PushGateway {
8080

8181
private final URL url;
8282
private final ExpositionFormatWriter writer;
83-
private final boolean timestampsInMs;
83+
private final boolean prometheusTimestampsInMs;
8484
private final Map<String, String> requestHeaders;
8585
private final PrometheusRegistry registry;
8686
private final HttpConnectionFactory connectionFactory;
@@ -91,12 +91,12 @@ private PushGateway(
9191
URL url,
9292
HttpConnectionFactory connectionFactory,
9393
Map<String, String> requestHeaders,
94-
boolean timestampsInMs) {
94+
boolean prometheusTimestampsInMs) {
9595
this.registry = registry;
9696
this.url = url;
9797
this.requestHeaders = Collections.unmodifiableMap(new HashMap<>(requestHeaders));
9898
this.connectionFactory = connectionFactory;
99-
this.timestampsInMs = timestampsInMs;
99+
this.prometheusTimestampsInMs = prometheusTimestampsInMs;
100100
writer = getWriter(format);
101101
if (!writer.isAvailable()) {
102102
throw new RuntimeException(writer.getClass() + " is not available");
@@ -106,7 +106,9 @@ private PushGateway(
106106
@SuppressWarnings("deprecation")
107107
private ExpositionFormatWriter getWriter(Format format) {
108108
if (format == Format.PROMETHEUS_TEXT) {
109-
return PrometheusTextFormatWriter.builder().setTimestampsInMs(this.timestampsInMs).build();
109+
return PrometheusTextFormatWriter.builder()
110+
.setTimestampsInMs(this.prometheusTimestampsInMs)
111+
.build();
110112
} else {
111113
// use reflection to avoid a compile-time dependency on the expositionformats module
112114
return new PrometheusProtobufWriter();
@@ -268,7 +270,7 @@ public static class Builder {
268270
private String address;
269271
private Scheme scheme;
270272
private String job;
271-
private boolean timestampsInMs;
273+
private boolean prometheusTimestampsInMs;
272274
private final Map<String, String> requestHeaders = new HashMap<>();
273275
private PrometheusRegistry registry = PrometheusRegistry.defaultRegistry;
274276
private HttpConnectionFactory connectionFactory = new DefaultHttpConnectionFactory();
@@ -389,14 +391,15 @@ public Builder registry(PrometheusRegistry registry) {
389391
* Use milliseconds for timestamps in text format? Default is {@code false}. Can be overwritten
390392
* at runtime with the {@code io.prometheus.exporter.timestampsInMs} property.
391393
*/
392-
public Builder setTimestampsInMs(boolean timestampsInMs) {
393-
this.timestampsInMs = timestampsInMs;
394+
public Builder prometheusTimestampsInMs(boolean prometheusTimestampsInMs) {
395+
this.prometheusTimestampsInMs = prometheusTimestampsInMs;
394396
return this;
395397
}
396398

397-
private boolean getTimestampsInMs() {
399+
private boolean getPrometheusTimestampsInMs() {
398400
// accept either to opt in to timestamps in milliseconds
399-
return config.getExporterProperties().getTimestampsInMs() || this.timestampsInMs;
401+
return config.getExporterProperties().getPrometheusTimestampsInMs()
402+
|| this.prometheusTimestampsInMs;
400403
}
401404

402405
private Scheme getScheme(ExporterPushgatewayProperties properties) {
@@ -477,7 +480,7 @@ public PushGateway build() {
477480
makeUrl(properties),
478481
connectionFactory,
479482
requestHeaders,
480-
getTimestampsInMs());
483+
getPrometheusTimestampsInMs());
481484
} catch (MalformedURLException e) {
482485
throw new PrometheusPropertiesException(
483486
address + ": Invalid address. Expecting <host>:<port>");

prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BasicAuthPushGatewayTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void setUp() {
2929
.address("localhost:" + mockServerClient.getPort())
3030
.basicAuth("testUser", "testPwd")
3131
.registry(registry)
32+
.prometheusTimestampsInMs(true)
3233
.job("j")
3334
.build();
3435
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ public static ExpositionFormats init(ExporterProperties properties) {
2828
new PrometheusProtobufWriter(),
2929
PrometheusTextFormatWriter.builder()
3030
.setIncludeCreatedTimestamps(properties.getIncludeCreatedTimestamps())
31-
.setTimestampsInMs(properties.getTimestampsInMs())
31+
.setTimestampsInMs(properties.getPrometheusTimestampsInMs())
3232
.build(),
3333
OpenMetricsTextFormatWriter.builder()
3434
.setCreatedTimestampsEnabled(properties.getIncludeCreatedTimestamps())
3535
.setExemplarsOnAllMetricTypesEnabled(properties.getExemplarsOnAllMetricTypes())
36-
.setTimestampsInMs(properties.getTimestampsInMs())
3736
.build());
3837
}
3938

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

+6-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeEscapedLabelValue;
55
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLabels;
66
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLong;
7-
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeTimestamp;
7+
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeOpenMetricsTimestamp;
88

99
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
1010
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
@@ -39,7 +39,6 @@ public class OpenMetricsTextFormatWriter implements ExpositionFormatWriter {
3939

4040
public static class Builder {
4141
boolean createdTimestampsEnabled;
42-
boolean timestampsInMs = true;
4342
boolean exemplarsOnAllMetricTypesEnabled;
4443

4544
private Builder() {}
@@ -61,22 +60,15 @@ public Builder setExemplarsOnAllMetricTypesEnabled(boolean exemplarsOnAllMetricT
6160
return this;
6261
}
6362

64-
@Deprecated
65-
public Builder setTimestampsInMs(boolean timestampsInMs) {
66-
this.timestampsInMs = timestampsInMs;
67-
return this;
68-
}
69-
7063
public OpenMetricsTextFormatWriter build() {
7164
return new OpenMetricsTextFormatWriter(
72-
createdTimestampsEnabled, timestampsInMs, exemplarsOnAllMetricTypesEnabled);
65+
createdTimestampsEnabled, exemplarsOnAllMetricTypesEnabled);
7366
}
7467
}
7568

7669
public static final String CONTENT_TYPE =
7770
"application/openmetrics-text; version=1.0.0; charset=utf-8";
7871
private final boolean createdTimestampsEnabled;
79-
private final boolean timestampsInMs;
8072
private final boolean exemplarsOnAllMetricTypesEnabled;
8173

8274
/**
@@ -85,18 +77,9 @@ public OpenMetricsTextFormatWriter build() {
8577
* @deprecated this constructor is deprecated and will be removed in the next major version -
8678
* {@link #builder()} or {@link #create()} instead
8779
*/
88-
@Deprecated
8980
public OpenMetricsTextFormatWriter(
9081
boolean createdTimestampsEnabled, boolean exemplarsOnAllMetricTypesEnabled) {
91-
this(createdTimestampsEnabled, false, exemplarsOnAllMetricTypesEnabled);
92-
}
93-
94-
private OpenMetricsTextFormatWriter(
95-
boolean createdTimestampsEnabled,
96-
boolean timestampsInMs,
97-
boolean exemplarsOnAllMetricTypesEnabled) {
9882
this.createdTimestampsEnabled = createdTimestampsEnabled;
99-
this.timestampsInMs = timestampsInMs;
10083
this.exemplarsOnAllMetricTypesEnabled = exemplarsOnAllMetricTypesEnabled;
10184
}
10285

@@ -355,10 +338,10 @@ private void writeCreated(Writer writer, MetricMetadata metadata, DataPointSnaps
355338
throws IOException {
356339
if (createdTimestampsEnabled && data.hasCreatedTimestamp()) {
357340
writeNameAndLabels(writer, metadata.getPrometheusName(), "_created", data.getLabels());
358-
writeTimestamp(writer, data.getCreatedTimestampMillis(), timestampsInMs);
341+
writeOpenMetricsTimestamp(writer, data.getCreatedTimestampMillis());
359342
if (data.hasScrapeTimestamp()) {
360343
writer.write(' ');
361-
writeTimestamp(writer, data.getScrapeTimestampMillis(), timestampsInMs);
344+
writeOpenMetricsTimestamp(writer, data.getScrapeTimestampMillis());
362345
}
363346
writer.write('\n');
364347
}
@@ -391,7 +374,7 @@ private void writeScrapeTimestampAndExemplar(
391374
Writer writer, DataPointSnapshot data, Exemplar exemplar) throws IOException {
392375
if (data.hasScrapeTimestamp()) {
393376
writer.write(' ');
394-
writeTimestamp(writer, data.getScrapeTimestampMillis(), timestampsInMs);
377+
writeOpenMetricsTimestamp(writer, data.getScrapeTimestampMillis());
395378
}
396379
if (exemplar != null) {
397380
writer.write(" # ");
@@ -400,7 +383,7 @@ private void writeScrapeTimestampAndExemplar(
400383
writeDouble(writer, exemplar.getValue());
401384
if (exemplar.hasTimestamp()) {
402385
writer.write(' ');
403-
writeTimestamp(writer, exemplar.getTimestampMillis(), timestampsInMs);
386+
writeOpenMetricsTimestamp(writer, exemplar.getTimestampMillis());
404387
}
405388
}
406389
writer.write('\n');

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeEscapedLabelValue;
55
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLabels;
66
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLong;
7-
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeTimestamp;
7+
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writePrometheusTimestamp;
88

99
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
1010
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
@@ -108,7 +108,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
108108
// "summary".
109109
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
110110
for (MetricSnapshot snapshot : metricSnapshots) {
111-
if (snapshot.getDataPoints().size() > 0) {
111+
if (!snapshot.getDataPoints().isEmpty()) {
112112
if (snapshot instanceof CounterSnapshot) {
113113
writeCounter(writer, (CounterSnapshot) snapshot);
114114
} else if (snapshot instanceof GaugeSnapshot) {
@@ -128,7 +128,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
128128
}
129129
if (writeCreatedTimestamps) {
130130
for (MetricSnapshot snapshot : metricSnapshots) {
131-
if (snapshot.getDataPoints().size() > 0) {
131+
if (!snapshot.getDataPoints().isEmpty()) {
132132
if (snapshot instanceof CounterSnapshot) {
133133
writeCreated(writer, snapshot);
134134
} else if (snapshot instanceof HistogramSnapshot) {
@@ -152,14 +152,14 @@ public void writeCreated(Writer writer, MetricSnapshot snapshot) throws IOExcept
152152
metadataWritten = true;
153153
}
154154
writeNameAndLabels(writer, metadata.getPrometheusName(), "_created", data.getLabels());
155-
writeTimestamp(writer, data.getCreatedTimestampMillis(), timestampsInMs);
155+
writePrometheusTimestamp(writer, data.getCreatedTimestampMillis(), timestampsInMs);
156156
writeScrapeTimestampAndNewline(writer, data);
157157
}
158158
}
159159
}
160160

161161
private void writeCounter(Writer writer, CounterSnapshot snapshot) throws IOException {
162-
if (snapshot.getDataPoints().size() > 0) {
162+
if (!snapshot.getDataPoints().isEmpty()) {
163163
MetricMetadata metadata = snapshot.getMetadata();
164164
writeMetadata(writer, "_total", "counter", metadata);
165165
for (CounterSnapshot.CounterDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -409,7 +409,7 @@ private void writeScrapeTimestampAndNewline(Writer writer, DataPointSnapshot dat
409409
throws IOException {
410410
if (data.hasScrapeTimestamp()) {
411411
writer.write(' ');
412-
writeTimestamp(writer, data.getScrapeTimestampMillis(), timestampsInMs);
412+
writePrometheusTimestamp(writer, data.getScrapeTimestampMillis(), timestampsInMs);
413413
}
414414
writer.write('\n');
415415
}

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

+18-13
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,30 @@ static void writeDouble(Writer writer, double d) throws IOException {
2121
}
2222
}
2323

24-
static void writeTimestamp(Writer writer, long timestampMs, boolean timestampsInMs)
24+
static void writePrometheusTimestamp(Writer writer, long timestampMs, boolean timestampsInMs)
2525
throws IOException {
2626
if (timestampsInMs) {
27-
// correct as per
27+
// correct for prometheus exposition format
2828
// https://prometheus.io/docs/instrumenting/exposition_formats/#text-format-details
2929
writer.write(Long.toString(timestampMs));
3030
} else {
31-
// this is incorrect - but we need to support it for backwards compatibility
32-
writer.write(Long.toString(timestampMs / 1000L));
33-
writer.write(".");
34-
long ms = timestampMs % 1000;
35-
if (ms < 100) {
36-
writer.write("0");
37-
}
38-
if (ms < 10) {
39-
writer.write("0");
40-
}
41-
writer.write(Long.toString(ms));
31+
// incorrect for prometheus exposition format -
32+
// but we need to support it for backwards compatibility
33+
writeOpenMetricsTimestamp(writer, timestampMs);
34+
}
35+
}
36+
37+
static void writeOpenMetricsTimestamp(Writer writer, long timestampMs) throws IOException {
38+
writer.write(Long.toString(timestampMs / 1000L));
39+
writer.write(".");
40+
long ms = timestampMs % 1000;
41+
if (ms < 100) {
42+
writer.write("0");
43+
}
44+
if (ms < 10) {
45+
writer.write("0");
4246
}
47+
writer.write(Long.toString(ms));
4348
}
4449

4550
static void writeEscapedLabelValue(Writer writer, String s) throws IOException {

prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/TextFormatUtilTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void testWriteTimestamp() throws IOException {
3131

3232
private static String writeTimestamp(boolean timestampsInMs) throws IOException {
3333
StringWriter writer = new StringWriter();
34-
TextFormatUtil.writeTimestamp(writer, 1000, timestampsInMs);
34+
TextFormatUtil.writePrometheusTimestamp(writer, 1000, timestampsInMs);
3535
return writer.toString();
3636
}
3737
}

0 commit comments

Comments
 (0)