Force endTimestamp to be strictly greater than or equal to startTimestamp

This fixes an error that we've been seeing from Stackdriver.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132358790
This commit is contained in:
shikhman 2016-09-06 13:59:43 -07:00 committed by Ben McIlwain
parent daca7d65c2
commit 0e8d9e3859

View file

@ -21,6 +21,7 @@ import static google.registry.monitoring.metrics.MetricsUtils.newConcurrentHashM
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.common.util.concurrent.AtomicLongMap; import com.google.common.util.concurrent.AtomicLongMap;
import com.google.common.util.concurrent.Striped; import com.google.common.util.concurrent.Striped;
import google.registry.monitoring.metrics.MetricSchema.Kind; import google.registry.monitoring.metrics.MetricSchema.Kind;
@ -131,6 +132,12 @@ public final class Counter extends AbstractMetric<Long>
valueLocks.get(labelValues).unlock(); valueLocks.get(labelValues).unlock();
} }
// There is an opportunity for endTimestamp to be less than startTimestamp if
// one of the modification methods is called on a value before the lock for that value is
// acquired but after getTimestampedValues has been invoked. Just set endTimestamp equal to
// startTimestamp if that happens.
endTimestamp = Ordering.natural().max(startTimestamp, endTimestamp);
timestampedValues.add( timestampedValues.add(
MetricPoint.create(this, labelValues, startTimestamp, endTimestamp, entry.getValue())); MetricPoint.create(this, labelValues, startTimestamp, endTimestamp, entry.getValue()));