Skip to content

Commit b89a721

Browse files
cmaunecmaune
andauthored
feat: adds createdTimestamp to counters exposed via prometheus protouf protocol (#1532)
#1498 Signed-off-by: cmaune <[email protected]> Co-authored-by: cmaune <[email protected]>
1 parent f07bd95 commit b89a721

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

integration-tests/it-exporter/it-exporter-test/src/test/java/io/prometheus/metrics/it/exporter/test/ExporterIT.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.net.URISyntaxException;
1111
import java.net.URLEncoder;
1212
import java.util.List;
13+
import java.util.regex.Pattern;
1314
import org.junit.jupiter.api.Test;
1415
import org.junit.jupiter.params.ParameterizedTest;
1516
import org.junit.jupiter.params.provider.CsvSource;
@@ -101,11 +102,22 @@ public void testPrometheusProtobufDebugFormat(String format, String expected) th
101102
assertThat(response.status).isEqualTo(200);
102103
assertContentType(
103104
"text/plain;charset=utf-8", response.getHeader("Content-Type").replace(" ", ""));
104-
assertThat(response.stringBody().trim())
105-
.isEqualTo(
106-
Resources.toString(Resources.getResource(expected), UTF_8)
107-
.trim()
108-
.replace("<app>", sampleApp));
105+
106+
String actualResponse = response.stringBody().trim();
107+
String expectedResponse =
108+
Resources.toString(Resources.getResource(expected), UTF_8)
109+
.trim()
110+
.replace("<app>", sampleApp);
111+
112+
if ("prometheus-protobuf".equals(format)) {
113+
assertThat(actualResponse)
114+
.matches(
115+
Pattern.quote(expectedResponse)
116+
.replace("<CREATED_TIMESTAMP_SECONDS>", "\\E\\d+\\Q")
117+
.replace("<CREATED_TIMESTAMP_NANOS>", "\\E\\d+\\Q"));
118+
} else {
119+
assertThat(actualResponse).isEqualTo(expectedResponse);
120+
}
109121
}
110122

111123
@Test

integration-tests/it-exporter/it-exporter-test/src/test/resources/debug-protobuf.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ type: COUNTER
3737
metric {
3838
counter {
3939
value: 17.0
40+
created_timestamp {
41+
seconds: <CREATED_TIMESTAMP_SECONDS>
42+
nanos: <CREATED_TIMESTAMP_NANOS>
43+
}
4044
}
4145
}
4246

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public void testTotalStrippedFromName(String name) {
120120
Metrics.MetricFamily protobufData =
121121
new PrometheusProtobufWriterImpl().convert(counter.collect(), EscapingScheme.ALLOW_UTF8);
122122
assertThat(ProtobufUtil.shortDebugString(protobufData))
123-
.isEqualTo(
124-
"name: \"my_counter_seconds_total\" type: COUNTER metric { counter { value: 0.0 } }");
123+
.matches(
124+
"^name: \"my_counter_seconds_total\" type: COUNTER metric \\{ counter \\{ value: 0.0 created_timestamp \\{ seconds: \\d+ nanos: \\d+ } } }$");
125125
}
126126

127127
@Test

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ private Metrics.Metric.Builder convert(CounterDataPointSnapshot data, EscapingSc
130130
if (data.getExemplar() != null) {
131131
counterBuilder.setExemplar(convert(data.getExemplar(), scheme));
132132
}
133+
if (data.hasCreatedTimestamp()) {
134+
counterBuilder.setCreatedTimestamp(
135+
ProtobufUtil.timestampFromMillis(data.getCreatedTimestampMillis()));
136+
}
133137
Metrics.Metric.Builder metricBuilder = Metrics.Metric.newBuilder();
134138
addLabels(metricBuilder, data.getLabels(), scheme);
135139
metricBuilder.setCounter(counterBuilder.build());

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public void testCounterComplete() throws IOException {
192192
+ "value: 0.8 "
193193
+ exemplar1protoString
194194
+ " "
195+
+ "created_timestamp { seconds: 1672850385 nanos: 800000000 } "
195196
+ "} "
196197
+ "timestamp_ms: 1672850685829 "
197198
+ "} "
@@ -202,6 +203,7 @@ public void testCounterComplete() throws IOException {
202203
+ "value: 0.9 "
203204
+ exemplar2protoString
204205
+ " "
206+
+ "created_timestamp { seconds: 1672850285 } "
205207
+ "} "
206208
+ "timestamp_ms: 1672850585820 "
207209
+ "}";

0 commit comments

Comments
 (0)