diff --git a/java/google/registry/monitoring/metrics/MetricRegistry.java b/java/google/registry/monitoring/metrics/MetricRegistry.java index 9494c259b..c1b69b187 100644 --- a/java/google/registry/monitoring/metrics/MetricRegistry.java +++ b/java/google/registry/monitoring/metrics/MetricRegistry.java @@ -103,6 +103,33 @@ public interface MetricRegistry { String valueDisplayName, ImmutableSet labels); + /** + * Returns a new {@link EventMetric}. + * + *

This metric type is intended for recording aspects of an "event" -- things like latency or + * payload size. + * + *

The metric's values are {@link Distribution} instances which are updated via {@link + * EventMetric#record(double, String...)}. + * + *

The metric is thread-safe. The metric will be registered at the time of creation and + * collected for subsequent write intervals. + * + * @param name name of the metric. Should be in the form of '/foo/bar'. + * @param description human readable description of the metric. + * @param valueDisplayName human readable description of the metric's value type. + * @param labels list of the metric's labels. + * @param distributionFitter fit to apply to the underlying {@link Distribution} instances of this + * metric. + * @throws IllegalStateException if a metric of the same name is already registered. + */ + EventMetric newEventMetric( + String name, + String description, + String valueDisplayName, + ImmutableSet labels, + DistributionFitter distributionFitter); + /** * Fetches a snapshot of the currently registered metrics * diff --git a/java/google/registry/monitoring/metrics/MetricRegistryImpl.java b/java/google/registry/monitoring/metrics/MetricRegistryImpl.java index 097e183ec..854f4ff80 100644 --- a/java/google/registry/monitoring/metrics/MetricRegistryImpl.java +++ b/java/google/registry/monitoring/metrics/MetricRegistryImpl.java @@ -42,6 +42,21 @@ public final class MetricRegistryImpl implements MetricRegistry { return INSTANCE; } + @Override + public EventMetric newEventMetric( + String name, + String description, + String valueDisplayName, + ImmutableSet labels, + DistributionFitter distributionFitter) { + EventMetric metric = + new EventMetric(name, description, valueDisplayName, distributionFitter, labels); + registerMetric(name, metric); + logger.info("Registered new event metric: " + name); + + return metric; + } + @Override @CanIgnoreReturnValue public Metric newGauge( diff --git a/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java b/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java index 80fdfa73c..6c44e295a 100644 --- a/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java +++ b/javatests/google/registry/monitoring/metrics/MetricRegistryImplTest.java @@ -138,6 +138,29 @@ public class MetricRegistryImplTest { ImmutableSet.of(label))); } + @Test + public void testNewEventMetric_createsEventMetric() { + DistributionFitter fitter = CustomFitter.create(ImmutableSet.of(0.0)); + EventMetric testMetric = + MetricRegistryImpl.getDefault() + .newEventMetric( + "/test_metric", + "test_description", + "test_valuedisplayname", + ImmutableSet.of(label), + fitter); + + assertThat(testMetric.getValueClass()).isSameAs(Distribution.class); + assertThat(testMetric.getMetricSchema()) + .isEqualTo( + MetricSchema.create( + "/test_metric", + "test_description", + "test_valuedisplayname", + Kind.CUMULATIVE, + ImmutableSet.of(label))); + } + @Test public void testRegister_duplicateMetric_throwsException() { SettableMetric testMetric =