Fix encoding error for Stackdriver metrics of cumulative points

The Stackdriver API requires that the end time always be greater than the start
time for cumulative metric points.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134396192
This commit is contained in:
shikhman 2016-09-27 05:46:20 -07:00 committed by Ben McIlwain
parent 7f0cb4eae5
commit e19546ffb4
2 changed files with 70 additions and 8 deletions

View file

@ -51,6 +51,7 @@ import java.util.logging.Logger;
import javax.annotation.concurrent.NotThreadSafe; import javax.annotation.concurrent.NotThreadSafe;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import org.joda.time.DateTime;
import org.joda.time.Interval; import org.joda.time.Interval;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat; import org.joda.time.format.ISODateTimeFormat;
@ -264,10 +265,17 @@ public class StackdriverWriter implements MetricWriter {
return descriptor; return descriptor;
} }
private static TimeInterval encodeTimeInterval(Interval nativeInterval) { private static TimeInterval encodeTimeInterval(Interval nativeInterval, Kind metricKind) {
return new TimeInterval()
.setEndTime(DATETIME_FORMATTER.print(nativeInterval.getEnd())) TimeInterval encodedInterval =
.setStartTime(DATETIME_FORMATTER.print(nativeInterval.getStart())); new TimeInterval().setStartTime(DATETIME_FORMATTER.print(nativeInterval.getStart()));
DateTime endTimestamp =
nativeInterval.toDurationMillis() == 0 && metricKind != Kind.GAUGE
? nativeInterval.getEnd().plusMillis(1)
: nativeInterval.getEnd();
return encodedInterval.setEndTime(DATETIME_FORMATTER.print(endTimestamp));
} }
private static BucketOptions encodeBucketOptions(DistributionFitter fitter) { private static BucketOptions encodeBucketOptions(DistributionFitter fitter) {
@ -363,7 +371,9 @@ public class StackdriverWriter implements MetricWriter {
} }
Point encodedPoint = Point encodedPoint =
new Point().setInterval(encodeTimeInterval(point.interval())).setValue(encodedValue); new Point()
.setInterval(encodeTimeInterval(point.interval(), metric.getMetricSchema().kind()))
.setValue(encodedValue);
List<LabelDescriptor> encodedLabels = descriptor.getLabels(); List<LabelDescriptor> encodedLabels = descriptor.getLabels();
// The MetricDescriptors returned by the GCM API have null fields rather than empty lists // The MetricDescriptors returned by the GCM API have null fields rather than empty lists

View file

@ -310,12 +310,13 @@ public class StackdriverWriterTest {
} }
@Test @Test
public void getEncodedTimeSeries_simplePoint_encodes() throws Exception { public void getEncodedTimeSeries_cumulativeMetricPoint_ZeroInterval_encodesGreaterEndTime()
throws Exception {
StackdriverWriter writer = StackdriverWriter writer =
new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST); new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
MetricPoint<Long> nativePoint = MetricPoint<Long> nativePoint =
MetricPoint.create( MetricPoint.create(
metric, ImmutableList.of("foo"), new Instant(1336), new Instant(1337), 10L); metric, ImmutableList.of("foo"), new Instant(1337), new Instant(1337), 10L);
TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint); TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);
@ -325,8 +326,59 @@ public class StackdriverWriterTest {
assertThat(points).hasSize(1); assertThat(points).hasSize(1);
Point point = points.get(0); Point point = points.get(0);
assertThat(point.getValue().getInt64Value()).isEqualTo(10L); assertThat(point.getValue().getInt64Value()).isEqualTo(10L);
assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.338Z");
}
@Test
public void getEncodedTimeSeries_cumulativeMetricPoint_nonZeroInterval_encodesSameInterval()
throws Exception {
StackdriverWriter writer =
new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
MetricPoint<Long> nativePoint =
MetricPoint.create(
metric, ImmutableList.of("foo"), new Instant(1337), new Instant(1339), 10L);
TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);
assertThat(timeSeries.getValueType()).isEqualTo("INT64");
assertThat(timeSeries.getMetricKind()).isEqualTo("CUMULATIVE");
List<Point> points = timeSeries.getPoints();
assertThat(points).hasSize(1);
Point point = points.get(0);
assertThat(point.getValue().getInt64Value()).isEqualTo(10L);
assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.339Z");
}
@Test
public void getEncodedTimeSeries_gaugeMetricPoint_zeroInterval_encodesSameInterval()
throws Exception {
StackdriverWriter writer =
new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST);
Metric<Long> metric =
new StoredMetric<>(
"/name",
"desc",
"vdn",
ImmutableSet.of(LabelDescriptor.create("label", "description")),
Long.class);
when(metricDescriptorCreate.execute())
.thenReturn(StackdriverWriter.createMetricDescriptor(metric));
MetricPoint<Long> nativePoint =
MetricPoint.create(
metric, ImmutableList.of("foo"), new Instant(1337), new Instant(1337), 10L);
TimeSeries timeSeries = writer.getEncodedTimeSeries(nativePoint);
assertThat(timeSeries.getValueType()).isEqualTo("INT64");
assertThat(timeSeries.getMetricKind()).isEqualTo("GAUGE");
List<Point> points = timeSeries.getPoints();
assertThat(points).hasSize(1);
Point point = points.get(0);
assertThat(point.getValue().getInt64Value()).isEqualTo(10L);
assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.337Z");
assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.337Z"); assertThat(point.getInterval().getEndTime()).isEqualTo("1970-01-01T00:00:01.337Z");
assertThat(point.getInterval().getStartTime()).isEqualTo("1970-01-01T00:00:01.336Z");
} }
@Test @Test