From 633fde4378905bffb967b30857257427cced4228 Mon Sep 17 00:00:00 2001 From: Mayur Kale Date: Fri, 19 Oct 2018 10:05:26 -0700 Subject: Plugs-in the DerivedLongGauge and DerivedDoubleGauge into the registry (#1505) * Plugs-in the DerivedLongGauge into the registry * Plugs-in the DerivedDoubleGauge into the registry --- .../io/opencensus/metrics/DerivedDoubleGauge.java | 1 - .../io/opencensus/metrics/DerivedLongGauge.java | 1 - .../java/io/opencensus/metrics/MetricRegistry.java | 60 ++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) (limited to 'api/src/main/java') diff --git a/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java index 10119eef..3aaca153 100644 --- a/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java +++ b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java @@ -40,7 +40,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; * List labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * List labelValues = Arrays.asList(LabelValue.create("Inbound")); * - * // TODO(mayurkale): Plugs-in the DerivedDoubleGauge into the registry. * DerivedDoubleGauge gauge = metricRegistry.addDerivedDoubleGauge( * "queue_size", "Pending jobs in a queue", "1", labelKeys); * diff --git a/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java index 43943fa6..621873f9 100644 --- a/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java +++ b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java @@ -40,7 +40,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; * List labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * List labelValues = Arrays.asList(LabelValue.create("Inbound")); * - * // TODO(mayurkale): Plugs-in the DerivedLongGauge into the registry. * DerivedLongGauge gauge = metricRegistry.addDerivedLongGauge( * "queue_size", "Pending jobs in a queue", "1", labelKeys); * diff --git a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java index 557f886a..5be15594 100644 --- a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java +++ b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java @@ -17,6 +17,8 @@ package io.opencensus.metrics; import io.opencensus.common.ExperimentalApi; +import io.opencensus.common.ToDoubleFunction; +import io.opencensus.common.ToLongFunction; import io.opencensus.internal.Utils; import java.util.List; @@ -63,6 +65,40 @@ public abstract class MetricRegistry { public abstract DoubleGauge addDoubleGauge( String name, String description, String unit, List labelKeys); + /** + * Builds a new derived long gauge to be added to the registry. This is more convenient form when + * you want to define a gauge by executing a {@link ToLongFunction} on an object. + * + * @param name the name of the metric. + * @param description the description of the metric. + * @param unit the unit of the metric. + * @param labelKeys the list of the label keys. + * @throws NullPointerException if {@code labelKeys} is null OR any element of {@code labelKeys} + * is null OR {@code name}, {@code description}, {@code unit} is null. + * @throws IllegalArgumentException if different metric with the same name already registered. + * @since 0.17 + */ + @ExperimentalApi + public abstract DerivedLongGauge addDerivedLongGauge( + String name, String description, String unit, List labelKeys); + + /** + * Builds a new derived double gauge to be added to the registry. This is more convenient form + * when you want to define a gauge by executing a {@link ToDoubleFunction} on an object. + * + * @param name the name of the metric. + * @param description the description of the metric. + * @param unit the unit of the metric. + * @param labelKeys the list of the label keys. + * @throws NullPointerException if {@code labelKeys} is null OR any element of {@code labelKeys} + * is null OR {@code name}, {@code description}, {@code unit} is null. + * @throws IllegalArgumentException if different metric with the same name already registered. + * @since 0.17 + */ + @ExperimentalApi + public abstract DerivedDoubleGauge addDerivedDoubleGauge( + String name, String description, String unit, List labelKeys); + static MetricRegistry newNoopMetricRegistry() { return new NoopMetricRegistry(); } @@ -92,5 +128,29 @@ public abstract class MetricRegistry { Utils.checkNotNull(unit, "unit"), labelKeys); } + + @Override + public DerivedLongGauge addDerivedLongGauge( + String name, String description, String unit, List labelKeys) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + return DerivedLongGauge.newNoopDerivedLongGauge( + Utils.checkNotNull(name, "name"), + Utils.checkNotNull(description, "description"), + Utils.checkNotNull(unit, "unit"), + labelKeys); + } + + @Override + public DerivedDoubleGauge addDerivedDoubleGauge( + String name, String description, String unit, List labelKeys) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + return DerivedDoubleGauge.newNoopDerivedDoubleGauge( + Utils.checkNotNull(name, "name"), + Utils.checkNotNull(description, "description"), + Utils.checkNotNull(unit, "unit"), + labelKeys); + } } } -- cgit v1.2.3