diff --git a/java/google/registry/monitoring/metrics/Counter.java b/java/google/registry/monitoring/metrics/Counter.java index 51b413a1f..d19f95353 100644 --- a/java/google/registry/monitoring/metrics/Counter.java +++ b/java/google/registry/monitoring/metrics/Counter.java @@ -49,17 +49,25 @@ public final class Counter extends AbstractMetric } @VisibleForTesting - void incrementBy(Number offset, ImmutableList labelValues) { - values.addAndGet(labelValues, offset.longValue()); + void incrementBy(long offset, ImmutableList labelValues) { + values.addAndGet(labelValues, offset); } @Override public final void incrementBy(long offset, String... labelValues) { checkArgument(labelValues.length == this.getMetricSchema().labels().size(), LABEL_COUNT_ERROR); + checkArgument(offset >= 0, "The offset provided must be non-negative"); incrementBy(offset, ImmutableList.copyOf(labelValues)); } + @Override + public final void increment(String... labelValues) { + checkArgument(labelValues.length == this.getMetricSchema().labels().size(), LABEL_COUNT_ERROR); + + incrementBy(1L, ImmutableList.copyOf(labelValues)); + } + /** * Returns a snapshot of the metric's values. The timestamp of each {@link MetricPoint} will be * the last modification time for that tuple of label values. diff --git a/java/google/registry/monitoring/metrics/IncrementableMetric.java b/java/google/registry/monitoring/metrics/IncrementableMetric.java index 5f017b188..c4ee92f2f 100644 --- a/java/google/registry/monitoring/metrics/IncrementableMetric.java +++ b/java/google/registry/monitoring/metrics/IncrementableMetric.java @@ -17,19 +17,35 @@ package google.registry.monitoring.metrics; /** * A {@link Metric} which can be incremented. * - *

This is a view into a {@link Counter} to provide compile-time checking to disallow re-setting - * the metric, which is useful for metrics which should be monotonic. + *

This is a view into a {@link Counter} to provide compile-time checking to disallow arbitrarily + * setting the metric, which is useful for metrics which should be monotonically increasing. */ public interface IncrementableMetric extends Metric { /** - * Increments a metric for a given set of label values. + * Increments a metric by 1 for the given set of label values. * - *

If the metric is undefined for given label values, it will first be set to zero. + * Use this method rather than {@link IncrementableMetric#incrementBy(long, String...)} if the + * increment value is zero, as it will be slightly more performant. + * + *

If the metric is undefined for given label values, it will be incremented from zero. * *

The metric's timestamp will be updated to the current time for the given label values. * *

The count of {@code labelValues} must be equal to the underlying metric's count of labels. */ + void increment(String... labelValues); + + /** + * Increments a metric by the given non-negative offset for the given set of label values. + * + *

If the metric is undefined for given label values, it will be incremented from zero. + * + *

The metric's timestamp will be updated to the current time for the given label values. + * + *

The count of {@code labelValues} must be equal to the underlying metric's count of labels. + * + * @throws IllegalArgumentException if the offset is negative. + */ void incrementBy(long offset, String... labelValues); } diff --git a/java/google/registry/monitoring/metrics/MetricReporter.java b/java/google/registry/monitoring/metrics/MetricReporter.java index 9eefb6a92..1b197519f 100644 --- a/java/google/registry/monitoring/metrics/MetricReporter.java +++ b/java/google/registry/monitoring/metrics/MetricReporter.java @@ -104,7 +104,7 @@ public class MetricReporter extends AbstractScheduledService { for (Metric metric : metricRegistry.getRegisteredMetrics()) { points.addAll(metric.getTimestampedValues()); logger.fine(String.format("Enqueued metric %s", metric)); - MetricMetrics.pushedPoints.incrementBy(1, + MetricMetrics.pushedPoints.increment( metric.getMetricSchema().kind().name(), metric.getValueClass().toString()); } @@ -112,7 +112,7 @@ public class MetricReporter extends AbstractScheduledService { logger.severe("writeQueue full, dropped a reporting interval of points"); } - MetricMetrics.pushIntervals.incrementBy(1); + MetricMetrics.pushIntervals.increment(); } @Override diff --git a/java/google/registry/monitoring/metrics/StackdriverWriter.java b/java/google/registry/monitoring/metrics/StackdriverWriter.java index bd2264009..4a2b4aa2a 100644 --- a/java/google/registry/monitoring/metrics/StackdriverWriter.java +++ b/java/google/registry/monitoring/metrics/StackdriverWriter.java @@ -195,7 +195,7 @@ public class StackdriverWriter implements MetricWriter { monitoringClient.projects().timeSeries().create(projectResource, request).execute(); for (TimeSeries timeSeries : timeSeriesList) { - pushedPoints.incrementBy(1, timeSeries.getMetricKind(), timeSeries.getValueType()); + pushedPoints.increment(timeSeries.getMetricKind(), timeSeries.getValueType()); } logger.info(String.format("Flushed %d metrics to Stackdriver", timeSeriesList.size())); }