Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include created timestamp in protobuf output format #1182

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static ExpositionFormats init() {

public static ExpositionFormats init(ExporterProperties properties) {
return new ExpositionFormats(
new PrometheusProtobufWriter(),
new PrometheusProtobufWriter(properties.getIncludeCreatedTimestamps()),
new PrometheusTextFormatWriter(properties.getIncludeCreatedTimestamps()),
new OpenMetricsTextFormatWriter(
properties.getIncludeCreatedTimestamps(), properties.getExemplarsOnAllMetricTypes()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.prometheus.metrics.expositionformats.ProtobufUtil.timestampFromMillis;

import com.google.protobuf.TextFormat;
import com.google.protobuf.Timestamp;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_2.Metrics;
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
Expand Down Expand Up @@ -35,6 +36,16 @@ public class PrometheusProtobufWriter implements ExpositionFormatWriter {
public static final String CONTENT_TYPE =
"application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited";

private final boolean writeCreatedTimestamps;

public PrometheusProtobufWriter() {
this(false);
}

public PrometheusProtobufWriter(boolean writeCreatedTimestamps) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid boolean params - let's use a builder instread

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this even be configurable? It makes sense to make timestamps configurable in text format, because in text format timestamps end up as extra time series which cause extra load on the Prometheus server.

However, in protobuf format time stamps do not cause extra cost, so we could just enable them always, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fstab This makes sense. It should make almost no difference in protobuf. So I am thinking of removing the boolean-constructor (@zeitlinger) and make protobuf always include the created-timestamp (as long it is not null).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

this.writeCreatedTimestamps = writeCreatedTimestamps;
}

@Override
public boolean accepts(String acceptHeader) {
if (acceptHeader == null) {
Expand Down Expand Up @@ -149,6 +160,9 @@ private Metrics.Metric.Builder convert(CounterDataPointSnapshot data) {
counterBuilder.setExemplar(convert(data.getExemplar()));
}
addLabels(metricBuilder, data.getLabels());
if (writeCreatedTimestamps) {
counterBuilder.setCreatedTimestamp(createTimestamp(data));
}
metricBuilder.setCounter(counterBuilder.build());
setScrapeTimestamp(metricBuilder, data);
return metricBuilder;
Expand Down Expand Up @@ -209,12 +223,16 @@ private Metrics.Metric.Builder convert(HistogramSnapshot.HistogramDataPointSnaps
}
addLabels(metricBuilder, data.getLabels());
setScrapeTimestamp(metricBuilder, data);

if (data.hasCount()) {
histogramBuilder.setSampleCount(data.getCount());
}
if (data.hasSum()) {
histogramBuilder.setSampleSum(data.getSum());
}
if (writeCreatedTimestamps) {
histogramBuilder.setCreatedTimestamp(createTimestamp(data));
}
metricBuilder.setHistogram(histogramBuilder.build());
return metricBuilder;
}
Expand Down Expand Up @@ -302,6 +320,9 @@ private Metrics.Metric.Builder convert(SummarySnapshot.SummaryDataPointSnapshot
.build());
}
addLabels(metricBuilder, data.getLabels());
if (writeCreatedTimestamps) {
summaryBuilder.setCreatedTimestamp(createTimestamp(data));
}
metricBuilder.setSummary(summaryBuilder.build());
setScrapeTimestamp(metricBuilder, data);
return metricBuilder;
Expand Down Expand Up @@ -378,4 +399,11 @@ private void setScrapeTimestamp(Metrics.Metric.Builder metricBuilder, DataPointS
metricBuilder.setTimestampMs(data.getScrapeTimestampMillis());
}
}

private Timestamp createTimestamp(DataPointSnapshot data) {
final long createdTimestamp = data.getCreatedTimestampMillis();
final long seconds = createdTimestamp / 1000;
final int nanos = (int) (createdTimestamp % 1000 * 1000000);
return Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build();
}
}
Loading