|
3 | 3 | import static io.prometheus.metrics.expositionformats.ProtobufUtil.timestampFromMillis; |
4 | 4 |
|
5 | 5 | import com.google.protobuf.TextFormat; |
| 6 | +import com.google.protobuf.Timestamp; |
6 | 7 | import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_2.Metrics; |
7 | 8 | import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets; |
8 | 9 | import io.prometheus.metrics.model.snapshots.CounterSnapshot; |
@@ -35,6 +36,16 @@ public class PrometheusProtobufWriter implements ExpositionFormatWriter { |
35 | 36 | public static final String CONTENT_TYPE = |
36 | 37 | "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited"; |
37 | 38 |
|
| 39 | + private final boolean writeCreatedTimestamps; |
| 40 | + |
| 41 | + public PrometheusProtobufWriter() { |
| 42 | + this(false); |
| 43 | + } |
| 44 | + |
| 45 | + public PrometheusProtobufWriter(boolean writeCreatedTimestamps) { |
| 46 | + this.writeCreatedTimestamps = writeCreatedTimestamps; |
| 47 | + } |
| 48 | + |
38 | 49 | @Override |
39 | 50 | public boolean accepts(String acceptHeader) { |
40 | 51 | if (acceptHeader == null) { |
@@ -149,6 +160,9 @@ private Metrics.Metric.Builder convert(CounterDataPointSnapshot data) { |
149 | 160 | counterBuilder.setExemplar(convert(data.getExemplar())); |
150 | 161 | } |
151 | 162 | addLabels(metricBuilder, data.getLabels()); |
| 163 | + if (writeCreatedTimestamps) { |
| 164 | + counterBuilder.setCreatedTimestamp(createTimestamp(data)); |
| 165 | + } |
152 | 166 | metricBuilder.setCounter(counterBuilder.build()); |
153 | 167 | setScrapeTimestamp(metricBuilder, data); |
154 | 168 | return metricBuilder; |
@@ -209,12 +223,16 @@ private Metrics.Metric.Builder convert(HistogramSnapshot.HistogramDataPointSnaps |
209 | 223 | } |
210 | 224 | addLabels(metricBuilder, data.getLabels()); |
211 | 225 | setScrapeTimestamp(metricBuilder, data); |
| 226 | + |
212 | 227 | if (data.hasCount()) { |
213 | 228 | histogramBuilder.setSampleCount(data.getCount()); |
214 | 229 | } |
215 | 230 | if (data.hasSum()) { |
216 | 231 | histogramBuilder.setSampleSum(data.getSum()); |
217 | 232 | } |
| 233 | + if (writeCreatedTimestamps) { |
| 234 | + histogramBuilder.setCreatedTimestamp(createTimestamp(data)); |
| 235 | + } |
218 | 236 | metricBuilder.setHistogram(histogramBuilder.build()); |
219 | 237 | return metricBuilder; |
220 | 238 | } |
@@ -302,6 +320,9 @@ private Metrics.Metric.Builder convert(SummarySnapshot.SummaryDataPointSnapshot |
302 | 320 | .build()); |
303 | 321 | } |
304 | 322 | addLabels(metricBuilder, data.getLabels()); |
| 323 | + if (writeCreatedTimestamps) { |
| 324 | + summaryBuilder.setCreatedTimestamp(createTimestamp(data)); |
| 325 | + } |
305 | 326 | metricBuilder.setSummary(summaryBuilder.build()); |
306 | 327 | setScrapeTimestamp(metricBuilder, data); |
307 | 328 | return metricBuilder; |
@@ -378,4 +399,11 @@ private void setScrapeTimestamp(Metrics.Metric.Builder metricBuilder, DataPointS |
378 | 399 | metricBuilder.setTimestampMs(data.getScrapeTimestampMillis()); |
379 | 400 | } |
380 | 401 | } |
| 402 | + |
| 403 | + private Timestamp createTimestamp(DataPointSnapshot data) { |
| 404 | + final long createdTimestamp = data.getCreatedTimestampMillis(); |
| 405 | + final long seconds = createdTimestamp / 1000; |
| 406 | + final int nanos = (int) (createdTimestamp % 1000 * 1000000); |
| 407 | + return Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); |
| 408 | + } |
381 | 409 | } |
0 commit comments