Remove metric registry and fix examples. (#238)

This commit is contained in:
Bogdan Drutu 2019-05-07 09:34:18 -07:00 committed by GitHub
parent 3cc0af53af
commit 750e16c5e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 214 additions and 308 deletions

View File

@ -23,50 +23,33 @@ import javax.annotation.concurrent.ThreadSafe;
* Counter metric, to report instantaneous measurement of a double value. Cumulative values can go * Counter metric, to report instantaneous measurement of a double value. Cumulative values can go
* up or stay the same, but can never go down. Cumulative values cannot be negative. * up or stay the same, but can never go down. Cumulative values cannot be negative.
* *
* <p>Example 1: Create a Cumulative with default labels. * <p>Example:
* *
* <pre>{@code * <pre>{@code
* class YourClass { * class YourClass {
* *
* private static final Meter meter = Metrics.getMeter(); * private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build(); * private static final CounterDouble counter =
* * meter.
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * .counterDoubleBuilder("processed_jobs")
* * .setDescription("Processed jobs")
* CounterDouble cumulative = metricRegistry.addDoubleCumulative("processed_jobs", * .setUnit("1")
* "Processed jobs", "1", labelKeys); * .setLabelKeys(Collections.singletonList(LabelKey.create("Name", "desc")))
* * .build();
* // It is recommended to keep a reference of a TimeSeries. * // It is recommended to keep a reference of a TimeSeries.
* CounterDouble.TimeSeries defaultTimeSeries = cumulative.getDefaultTimeSeries(); * private static final CounterDouble.TimeSeries inboundTimeSeries =
* counter.getOrCreateTimeSeries(Collections.singletonList(LabelValue.create("SomeWork")));
* private static final CounterDouble.TimeSeries defaultTimeSeries =
* counter.getDefaultTimeSeries();
* *
* void doWork() { * void doDefaultWork() {
* // Your code here. * // Your code here.
* defaultPoint.add(10); * defaultTimeSeries.add(10);
* } * }
* *
* }
* }</pre>
*
* <p>Example 2: You can also use labels (keys and values) to track different types of metric.
*
* <pre>{@code
* class YourClass {
*
* private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build();
*
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
* List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
*
* CounterDouble cumulative = metricRegistry.addDoubleCumulative("processed_jobs",
* "Processed jobs", "1", labelKeys);
*
* // It is recommended to keep a reference of a TimeSeries.
* CounterDouble.TimeSeries inboundTimeSeries = cumulative.getOrCreateTimeSeries(labelValues);
*
* void doSomeWork() { * void doSomeWork() {
* // Your code here. * // Your code here.
* inboundPoint.set(15); * inboundTimeSeries.set(15);
* } * }
* *
* } * }

View File

@ -24,50 +24,32 @@ import javax.annotation.concurrent.ThreadSafe;
* Counter metric, to report instantaneous measurement of a long value. Cumulative values can go up * Counter metric, to report instantaneous measurement of a long value. Cumulative values can go up
* or stay the same, but can never go down. Cumulative values cannot be negative. * or stay the same, but can never go down. Cumulative values cannot be negative.
* *
* <p>Example 1: Create a Cumulative with default labels. * <p>Example:
* *
* <pre>{@code * <pre>{@code
* class YourClass { * class YourClass {
* *
* private static final Meter meter = Metrics.getMeter(); * private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build(); * private static final CounterLong counter =
* * meter.
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * .counterLongBuilder("processed_jobs")
* * .setDescription("Processed jobs")
* CounterLong cumulative = metricRegistry.addLongCumulative("processed_jobs", * .setUnit("1")
* "Processed jobs", "1", labelKeys); * .setLabelKeys(Collections.singletonList(LabelKey.create("Name", "desc")))
* * .build();
* // It is recommended to keep a reference of a TimeSeries. * // It is recommended to keep a reference of a TimeSeries.
* CounterLong.TimeSeries defaultTimeSeries = cumulative.getDefaultTimeSeries(); * private static final CounterLong.TimeSeries inboundTimeSeries =
* counter.getOrCreateTimeSeries(Collections.singletonList(LabelValue.create("SomeWork")));
* private static final CounterLong.TimeSeries defaultTimeSeries = counter.getDefaultTimeSeries();
* *
* void doWork() { * void doDefaultWork() {
* // Your code here. * // Your code here.
* defaultPoint.add(10); * defaultTimeSeries.add(10);
* } * }
* *
* }
* }</pre>
*
* <p>Example 2: You can also use labels (keys and values) to track different types of metric.
*
* <pre>{@code
* class YourClass {
*
* private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build();
*
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
* List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
*
* CounterLong cumulative = metricRegistry.addLongCumulative("processed_jobs",
* "Processed jobs", "1", labelKeys);
*
* // It is recommended to keep a reference of a TimeSeries.
* CounterLong.TimeSeries inboundTimeSeries = cumulative.getOrCreateTimeSeries(labelValues);
*
* void doSomeWork() { * void doSomeWork() {
* // Your code here. * // Your code here.
* inboundPoint.set(15); * inboundTimeSeries.set(15);
* } * }
* *
* } * }

View File

@ -24,50 +24,32 @@ import javax.annotation.concurrent.ThreadSafe;
* Gauge metric, to report instantaneous measurement of a double value. Gauges can go both up and * Gauge metric, to report instantaneous measurement of a double value. Gauges can go both up and
* down. The gauges values can be negative. * down. The gauges values can be negative.
* *
* <p>Example 1: Create a Gauge with default labels. * <p>Example:
* *
* <pre>{@code * <pre>{@code
* class YourClass { * class YourClass {
* *
* private static final Meter meter = Metrics.getMeter(); * private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build(); * private static final GaugeDouble gauge =
* * meter
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * .gaugeDoubleBuilder("processed_jobs")
* * .setDescription("Processed jobs")
* GaugeDouble gauge = metricRegistry.addDoubleGauge("queue_size", * .setUnit("1")
* "Pending jobs", "1", labelKeys); * .setLabelKeys(Collections.singletonList(LabelKey.create("Name", "desc")))
* * .build();
* // It is recommended to keep a reference of a TimeSeries. * // It is recommended to keep a reference of a TimeSeries.
* GaugeDouble.TimeSeries defaultTimeSeries = gauge.getDefaultTimeSeries(); * private static final GaugeDouble.TimeSeries inboundTimeSeries =
* gauge.getOrCreateTimeSeries(Collections.singletonList(LabelValue.create("SomeWork")));
* private static final GaugeDouble.TimeSeries defaultTimeSeries = gauge.getDefaultTimeSeries();
* *
* void doWork() { * void doDefault() {
* // Your code here. * // Your code here.
* defaultPoint.add(10); * defaultTimeSeries.add(10);
* } * }
* *
* }
* }</pre>
*
* <p>Example 2: You can also use labels(keys and values) to track different types of metric.
*
* <pre>{@code
* class YourClass {
*
* private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build();
*
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
* List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
*
* GaugeDouble gauge = metricRegistry.addDoubleGauge("queue_size",
* "Pending jobs", "1", labelKeys);
*
* // It is recommended to keep a reference of a TimeSeries.
* GaugeDouble.TimeSeries inboundTimeSeries = gauge.getOrCreateTimeSeries(labelValues);
*
* void doSomeWork() { * void doSomeWork() {
* // Your code here. * // Your code here.
* inboundPoint.set(15); * inboundTimeSeries.set(15);
* } * }
* *
* } * }

View File

@ -23,48 +23,32 @@ import javax.annotation.concurrent.ThreadSafe;
* Gauge metric, to report instantaneous measurement of an long value. Gauges can go both up and * Gauge metric, to report instantaneous measurement of an long value. Gauges can go both up and
* down. The gauges values can be negative. * down. The gauges values can be negative.
* *
* <p>Example 1: Create a Gauge with default labels. * <p>Example:
* *
* <pre>{@code * <pre>{@code
* class YourClass { * class YourClass {
* *
* private static final Meter meter = Metrics.getMeter(); * private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build(); * private static final GaugeLong gauge =
* * meter
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); * .gaugeLongBuilder("processed_jobs")
* * .setDescription("Processed jobs")
* GaugeLong gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys); * .setUnit("1")
* * .setLabelKeys(Collections.singletonList(LabelKey.create("Name", "desc")))
* .build();
* // It is recommended to keep a reference of a TimeSeries. * // It is recommended to keep a reference of a TimeSeries.
* GaugeLong.TimeSeries defaultTimeSeries = gauge.getDefaultTimeSeries(); * private static final GaugeLong.TimeSeries inboundTimeSeries =
* gauge.getOrCreateTimeSeries(Collections.singletonList(LabelValue.create("SomeWork")));
* private static final GaugeLong.TimeSeries defaultTimeSeries = gauge.getDefaultTimeSeries();
* *
* void doWork() { * void doDefault() {
* // Your code here. * // Your code here.
* defaultPoint.add(10); * defaultTimeSeries.add(10);
* } * }
* *
* }
* }</pre>
*
* <p>Example 2: You can also use labels(keys and values) to track different types of metric.
*
* <pre>{@code
* class YourClass {
*
* private static final Meter meter = Metrics.getMeter();
* private static final MetricRegistry metricRegistry = meter.metricRegistryBuilder().build();
*
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
* List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
*
* GaugeLong gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys);
*
* // It is recommended to keep a reference of a TimeSeries.
* GaugeLong.TimeSeries inboundTimeSeries = gauge.getOrCreateTimeSeries(labelValues);
*
* void doSomeWork() { * void doSomeWork() {
* // Your code here. * // Your code here.
* inboundPoint.set(15); * inboundTimeSeries.set(15);
* } * }
* *
* } * }

View File

@ -16,13 +16,50 @@
package io.opentelemetry.metrics; package io.opentelemetry.metrics;
/** Entry point fot metrics API, this object allows to create new {@link MetricRegistry}. */ /** Entry point fot metrics API, this object allows to record measurements and {@link Metric}s. */
public interface Meter { public interface Meter {
/** /**
* Returns a new builder for a {@code MetricRegistry}. * Returns a builder for a {@link GaugeLong} to be added to the registry.
* *
* @return a new builder for a {@code MetricRegistry}. * @param name the name of the metric.
* @return a {@code GaugeLong.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/ */
MetricRegistry.Builder metricRegistryBuilder(); GaugeLong.Builder gaugeLongBuilder(String name);
/**
* Returns a builder for a {@link GaugeDouble} to be added to the registry.
*
* @param name the name of the metric.
* @return a {@code GaugeDouble.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/
GaugeDouble.Builder gaugeDoubleBuilder(String name);
/**
* Returns a builder for a {@link CounterDouble} to be added to the registry.
*
* @param name the name of the metric.
* @return a {@code CounterDouble.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/
CounterDouble.Builder counterDoubleBuilder(String name);
/**
* Returns a builder for a {@link CounterLong} to be added to the registry.
*
* @param name the name of the metric.
* @return a {@code CounterLong.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/
CounterLong.Builder counterLongBuilder(String name);
} }

View File

@ -16,6 +16,7 @@
package io.opentelemetry.metrics; package io.opentelemetry.metrics;
import io.opentelemetry.resource.Resource;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -79,7 +80,7 @@ public interface Metric<T> {
interface Builder<B extends Builder<B, V>, V> { interface Builder<B extends Builder<B, V>, V> {
/** /**
* Sets the description of the Metric. * Sets the description of the {@code Metric}.
* *
* <p>Default value is {@code ""}. * <p>Default value is {@code ""}.
* *
@ -89,7 +90,7 @@ public interface Metric<T> {
B setDescription(String description); B setDescription(String description);
/** /**
* Sets the unit of the Metric. * Sets the unit of the {@code Metric}.
* *
* <p>Default value is {@code "1"}. * <p>Default value is {@code "1"}.
* *
@ -118,6 +119,32 @@ public interface Metric<T> {
*/ */
B setConstantLabels(Map<LabelKey, LabelValue> constantLabels); B setConstantLabels(Map<LabelKey, LabelValue> constantLabels);
/**
* Sets the name of the component that reports this {@code Metric}.
*
* <p>The final name of the reported metric will be <code>component + "_" + name</code> if the
* component is not empty.
*
* <p>It is recommended to always set a component name for all the metrics, because some
* implementations may filter based on the component.
*
* @param component the name of the component that reports these metrics.
* @return this.
*/
B setComponent(String component);
/**
* Sets the {@code Resource} associated with this {@code Metric}.
*
* <p>This should be set only when reporting out-of-band metrics, otherwise the implementation
* will set the {@code Resource} for in-process metrics (or user can do that when initialize the
* {@code Meter}).
*
* @param resource the {@code Resource} associated with this {@code Metric}.
* @return this.
*/
B setResource(Resource resource);
/** /**
* Builds and returns a metric with the desired options. * Builds and returns a metric with the desired options.
* *

View File

@ -1,104 +0,0 @@
/*
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.metrics;
import io.opentelemetry.resource.Resource;
/**
* Creates and manages a set of metrics for a library/application.
*
* @since 0.1.0
*/
public interface MetricRegistry {
/**
* Returns a builder for a {@link GaugeLong} to be added to the registry.
*
* @param name the name of the metric.
* @return a {@code GaugeLong.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/
GaugeLong.Builder gaugeLongBuilder(String name);
/**
* Returns a builder for a {@link GaugeDouble} to be added to the registry.
*
* @param name the name of the metric.
* @return a {@code GaugeDouble.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/
GaugeDouble.Builder gaugeDoubleBuilder(String name);
/**
* Returns a builder for a {@link CounterDouble} to be added to the registry.
*
* @param name the name of the metric.
* @return a {@code CounterDouble.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/
CounterDouble.Builder counterDoubleBuilder(String name);
/**
* Returns a builder for a {@link CounterLong} to be added to the registry.
*
* @param name the name of the metric.
* @return a {@code CounterLong.Builder}.
* @throws NullPointerException if {@code name} is null.
* @throws IllegalArgumentException if different metric with the same name already registered.
* @since 0.1.0
*/
CounterLong.Builder counterLongBuilder(String name);
/** Builder class for the {@link MetricRegistry}. */
interface Builder {
/**
* Sets the name of the component that reports these metrics.
*
* <p>The final name of the reported metric will be <code>component + "_" + name</code> if the
* component is not empty.
*
* @param component the name of the component that reports these metrics.
* @return this.
*/
Builder setComponent(String component);
/**
* Sets the {@code Resource} associated with the new {@code MetricRegistry}.
*
* <p>This should be set only when reporting out-of-band metrics, otherwise the implementation
* will set the {@code Resource} for in-process metrics.
*
* @param resource the {@code Resource} associated with the new {@code MetricRegistry}.
* @return this.
*/
Builder setResource(Resource resource);
/**
* Builds and returns a {@link MetricRegistry} with the desired options.
*
* @return a {@link MetricRegistry} with the desired options.
*/
MetricRegistry build();
}
}

View File

@ -40,14 +40,6 @@ public final class NoopMetrics {
} }
private static final class NoopMeter implements Meter { private static final class NoopMeter implements Meter {
@Override
public MetricRegistry.Builder metricRegistryBuilder() {
return new NoopMetricCollection.Builder();
}
}
private static final class NoopMetricCollection implements MetricRegistry {
@Override @Override
public GaugeLong.Builder gaugeLongBuilder(String name) { public GaugeLong.Builder gaugeLongBuilder(String name) {
Utils.checkNotNull(name, "name"); Utils.checkNotNull(name, "name");
@ -71,27 +63,6 @@ public final class NoopMetrics {
Utils.checkNotNull(name, "name"); Utils.checkNotNull(name, "name");
return new NoopCounterLong.NoopBuilder(); return new NoopCounterLong.NoopBuilder();
} }
private static final class Builder implements MetricRegistry.Builder {
private static final MetricRegistry METRIC_COLLECTION = new NoopMetricCollection();
@Override
public MetricRegistry.Builder setComponent(String component) {
Utils.checkNotNull(component, "component");
return this;
}
@Override
public MetricRegistry.Builder setResource(Resource resource) {
Utils.checkNotNull(resource, "resource");
return this;
}
@Override
public MetricRegistry build() {
return METRIC_COLLECTION;
}
}
} }
/** No-op implementations of GaugeLong class. */ /** No-op implementations of GaugeLong class. */
@ -169,6 +140,18 @@ public final class NoopMetrics {
return this; return this;
} }
@Override
public Builder setComponent(String component) {
Utils.checkNotNull(component, "component");
return this;
}
@Override
public Builder setResource(Resource resource) {
Utils.checkNotNull(resource, "resource");
return this;
}
@Override @Override
public GaugeLong build() { public GaugeLong build() {
return new NoopGaugeLong(labelKeysSize); return new NoopGaugeLong(labelKeysSize);
@ -251,6 +234,18 @@ public final class NoopMetrics {
return this; return this;
} }
@Override
public Builder setComponent(String component) {
Utils.checkNotNull(component, "component");
return this;
}
@Override
public Builder setResource(Resource resource) {
Utils.checkNotNull(resource, "resource");
return this;
}
@Override @Override
public GaugeDouble build() { public GaugeDouble build() {
return new NoopGaugeDouble(labelKeysSize); return new NoopGaugeDouble(labelKeysSize);
@ -335,6 +330,18 @@ public final class NoopMetrics {
return this; return this;
} }
@Override
public Builder setComponent(String component) {
Utils.checkNotNull(component, "component");
return this;
}
@Override
public Builder setResource(Resource resource) {
Utils.checkNotNull(resource, "resource");
return this;
}
@Override @Override
public CounterDouble build() { public CounterDouble build() {
return new NoopCounterDouble(labelKeysSize); return new NoopCounterDouble(labelKeysSize);
@ -419,6 +426,18 @@ public final class NoopMetrics {
return this; return this;
} }
@Override
public Builder setComponent(String component) {
Utils.checkNotNull(component, "component");
return this;
}
@Override
public Builder setResource(Resource resource) {
Utils.checkNotNull(resource, "resource");
return this;
}
@Override @Override
public CounterLong build() { public CounterLong build() {
return new NoopCounterLong(labelKeysSize); return new NoopCounterLong(labelKeysSize);

View File

@ -37,13 +37,12 @@ public class CounterDoubleTest {
Collections.singletonList(LabelKey.create("key", "key description")); Collections.singletonList(LabelKey.create("key", "key description"));
private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>(); private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>();
private final MetricRegistry metricRegistry = private final Meter meter = Metrics.getMeter();
NoopMetrics.newNoopMeter().metricRegistryBuilder().build();
@Test @Test
public void noopGetOrCreateTimeSeries_WithNullLabelValues() { public void noopGetOrCreateTimeSeries_WithNullLabelValues() {
CounterDouble counterDouble = CounterDouble counterDouble =
metricRegistry meter
.counterDoubleBuilder(NAME) .counterDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -58,7 +57,7 @@ public class CounterDoubleTest {
public void noopGetOrCreateTimeSeries_WithNullElement() { public void noopGetOrCreateTimeSeries_WithNullElement() {
List<LabelValue> labelValues = Collections.singletonList(null); List<LabelValue> labelValues = Collections.singletonList(null);
CounterDouble counterDouble = CounterDouble counterDouble =
metricRegistry meter
.counterDoubleBuilder(NAME) .counterDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -72,7 +71,7 @@ public class CounterDoubleTest {
@Test @Test
public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() { public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() {
CounterDouble counterDouble = CounterDouble counterDouble =
metricRegistry meter
.counterDoubleBuilder(NAME) .counterDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -86,7 +85,7 @@ public class CounterDoubleTest {
@Test @Test
public void noopRemoveTimeSeries_WithNullLabelValues() { public void noopRemoveTimeSeries_WithNullLabelValues() {
CounterDouble counterDouble = CounterDouble counterDouble =
metricRegistry meter
.counterDoubleBuilder(NAME) .counterDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)

View File

@ -37,13 +37,12 @@ public class CounterLongTest {
Collections.singletonList(LabelKey.create("key", "key description")); Collections.singletonList(LabelKey.create("key", "key description"));
private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>(); private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>();
private final MetricRegistry metricRegistry = private final Meter meter = Metrics.getMeter();
NoopMetrics.newNoopMeter().metricRegistryBuilder().build();
@Test @Test
public void noopGetOrCreateTimeSeries_WithNullLabelValues() { public void noopGetOrCreateTimeSeries_WithNullLabelValues() {
CounterLong counterLong = CounterLong counterLong =
metricRegistry meter
.counterLongBuilder(NAME) .counterLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -58,7 +57,7 @@ public class CounterLongTest {
public void noopGetOrCreateTimeSeries_WithNullElement() { public void noopGetOrCreateTimeSeries_WithNullElement() {
List<LabelValue> labelValues = Collections.singletonList(null); List<LabelValue> labelValues = Collections.singletonList(null);
CounterLong counterLong = CounterLong counterLong =
metricRegistry meter
.counterLongBuilder(NAME) .counterLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -72,7 +71,7 @@ public class CounterLongTest {
@Test @Test
public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() { public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() {
CounterLong counterLong = CounterLong counterLong =
metricRegistry meter
.counterLongBuilder(NAME) .counterLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -86,7 +85,7 @@ public class CounterLongTest {
@Test @Test
public void noopRemoveTimeSeries_WithNullLabelValues() { public void noopRemoveTimeSeries_WithNullLabelValues() {
CounterLong counterLong = CounterLong counterLong =
metricRegistry meter
.counterLongBuilder(NAME) .counterLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)

View File

@ -37,13 +37,12 @@ public class GaugeDoubleTest {
Collections.singletonList(LabelKey.create("key", "key description")); Collections.singletonList(LabelKey.create("key", "key description"));
private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>(); private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>();
private final MetricRegistry metricRegistry = private final Meter meter = Metrics.getMeter();
NoopMetrics.newNoopMeter().metricRegistryBuilder().build();
@Test @Test
public void noopGetOrCreateTimeSeries_WithNullLabelValues() { public void noopGetOrCreateTimeSeries_WithNullLabelValues() {
GaugeDouble gaugeDouble = GaugeDouble gaugeDouble =
metricRegistry meter
.gaugeDoubleBuilder(NAME) .gaugeDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -58,7 +57,7 @@ public class GaugeDoubleTest {
public void noopGetOrCreateTimeSeries_WithNullElement() { public void noopGetOrCreateTimeSeries_WithNullElement() {
List<LabelValue> labelValues = Collections.singletonList(null); List<LabelValue> labelValues = Collections.singletonList(null);
GaugeDouble gaugeDouble = GaugeDouble gaugeDouble =
metricRegistry meter
.gaugeDoubleBuilder(NAME) .gaugeDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -72,7 +71,7 @@ public class GaugeDoubleTest {
@Test @Test
public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() { public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() {
GaugeDouble gaugeDouble = GaugeDouble gaugeDouble =
metricRegistry meter
.gaugeDoubleBuilder(NAME) .gaugeDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -86,7 +85,7 @@ public class GaugeDoubleTest {
@Test @Test
public void noopRemoveTimeSeries_WithNullLabelValues() { public void noopRemoveTimeSeries_WithNullLabelValues() {
GaugeDouble gaugeDouble = GaugeDouble gaugeDouble =
metricRegistry meter
.gaugeDoubleBuilder(NAME) .gaugeDoubleBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)

View File

@ -37,13 +37,12 @@ public class GaugeLongTest {
Collections.singletonList(LabelKey.create("key", "key description")); Collections.singletonList(LabelKey.create("key", "key description"));
private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>(); private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<>();
private final MetricRegistry metricRegistry = private final Meter meter = Metrics.getMeter();
NoopMetrics.newNoopMeter().metricRegistryBuilder().build();
@Test @Test
public void noopGetOrCreateTimeSeries_WithNullLabelValues() { public void noopGetOrCreateTimeSeries_WithNullLabelValues() {
GaugeLong gaugeLong = GaugeLong gaugeLong =
metricRegistry meter
.gaugeLongBuilder(NAME) .gaugeLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -58,7 +57,7 @@ public class GaugeLongTest {
public void noopGetOrCreateTimeSeries_WithNullElement() { public void noopGetOrCreateTimeSeries_WithNullElement() {
List<LabelValue> labelValues = Collections.singletonList(null); List<LabelValue> labelValues = Collections.singletonList(null);
GaugeLong gaugeLong = GaugeLong gaugeLong =
metricRegistry meter
.gaugeLongBuilder(NAME) .gaugeLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -72,7 +71,7 @@ public class GaugeLongTest {
@Test @Test
public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() { public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() {
GaugeLong gaugeLong = GaugeLong gaugeLong =
metricRegistry meter
.gaugeLongBuilder(NAME) .gaugeLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)
@ -86,7 +85,7 @@ public class GaugeLongTest {
@Test @Test
public void noopRemoveTimeSeries_WithNullLabelValues() { public void noopRemoveTimeSeries_WithNullLabelValues() {
GaugeLong gaugeLong = GaugeLong gaugeLong =
metricRegistry meter
.gaugeLongBuilder(NAME) .gaugeLongBuilder(NAME)
.setDescription(DESCRIPTION) .setDescription(DESCRIPTION)
.setLabelKeys(LABEL_KEY) .setLabelKeys(LABEL_KEY)

View File

@ -22,39 +22,37 @@ import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
/** Unit tests for {@link MetricRegistry}. */ /** Unit tests for {@link Meter}. */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class MetricRegistryTest { public class MeterTest {
@Rule public ExpectedException thrown = ExpectedException.none(); @Rule public ExpectedException thrown = ExpectedException.none();
private final Meter meter = Metrics.getMeter();
private final MetricRegistry metricRegistry =
NoopMetrics.newNoopMeter().metricRegistryBuilder().build();
@Test @Test
public void noopAddLongGauge_NullName() { public void noopAddLongGauge_NullName() {
thrown.expect(NullPointerException.class); thrown.expect(NullPointerException.class);
thrown.expectMessage("name"); thrown.expectMessage("name");
metricRegistry.gaugeLongBuilder(null); meter.gaugeLongBuilder(null);
} }
@Test @Test
public void noopAddDoubleGauge_NullName() { public void noopAddDoubleGauge_NullName() {
thrown.expect(NullPointerException.class); thrown.expect(NullPointerException.class);
thrown.expectMessage("name"); thrown.expectMessage("name");
metricRegistry.gaugeDoubleBuilder(null); meter.gaugeDoubleBuilder(null);
} }
@Test @Test
public void noopAddDoubleCumulative_NullName() { public void noopAddDoubleCumulative_NullName() {
thrown.expect(NullPointerException.class); thrown.expect(NullPointerException.class);
thrown.expectMessage("name"); thrown.expectMessage("name");
metricRegistry.counterDoubleBuilder(null); meter.counterDoubleBuilder(null);
} }
@Test @Test
public void noopAddLongCumulative_NullName() { public void noopAddLongCumulative_NullName() {
thrown.expect(NullPointerException.class); thrown.expect(NullPointerException.class);
thrown.expectMessage("name"); thrown.expectMessage("name");
metricRegistry.counterLongBuilder(null); meter.counterLongBuilder(null);
} }
} }

View File

@ -19,7 +19,7 @@ package io.opentelemetry.contrib.metrics.runtime;
import io.opentelemetry.metrics.CounterLong; import io.opentelemetry.metrics.CounterLong;
import io.opentelemetry.metrics.LabelKey; import io.opentelemetry.metrics.LabelKey;
import io.opentelemetry.metrics.LabelValue; import io.opentelemetry.metrics.LabelValue;
import io.opentelemetry.metrics.MetricRegistry; import io.opentelemetry.metrics.Meter;
import io.opentelemetry.metrics.Metrics; import io.opentelemetry.metrics.Metrics;
import java.lang.management.GarbageCollectorMXBean; import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
@ -45,12 +45,12 @@ public final class GarbageCollector {
private static final LabelKey GC = LabelKey.create("gc", ""); private static final LabelKey GC = LabelKey.create("gc", "");
private final List<GarbageCollectorMXBean> garbageCollectors; private final List<GarbageCollectorMXBean> garbageCollectors;
private final MetricRegistry metricRegistry; private final Meter meter;
/** Constructs a new module that is capable to export metrics about "jvm_gc". */ /** Constructs a new module that is capable to export metrics about "jvm_gc". */
public GarbageCollector() { public GarbageCollector() {
this.garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans(); this.garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
this.metricRegistry = Metrics.getMeter().metricRegistryBuilder().setComponent("jvm_gc").build(); this.meter = Metrics.getMeter();
} }
/** Export all metrics generated by this module. */ /** Export all metrics generated by this module. */
@ -58,11 +58,12 @@ public final class GarbageCollector {
// TODO: This should probably be a cumulative Histogram without buckets (or Summary without // TODO: This should probably be a cumulative Histogram without buckets (or Summary without
// percentiles) to allow count/sum. // percentiles) to allow count/sum.
final CounterLong collectionMetric = final CounterLong collectionMetric =
metricRegistry meter
.counterLongBuilder("collection") .counterLongBuilder("collection")
.setDescription("Time spent in a given JVM garbage collector in milliseconds.") .setDescription("Time spent in a given JVM garbage collector in milliseconds.")
.setUnit("ms") .setUnit("ms")
.setLabelKeys(Collections.singletonList(GC)) .setLabelKeys(Collections.singletonList(GC))
.setComponent("jvm_gc")
.build(); .build();
collectionMetric.setCallback( collectionMetric.setCallback(
new Runnable() { new Runnable() {

View File

@ -19,7 +19,7 @@ package io.opentelemetry.contrib.metrics.runtime;
import io.opentelemetry.metrics.GaugeLong; import io.opentelemetry.metrics.GaugeLong;
import io.opentelemetry.metrics.LabelKey; import io.opentelemetry.metrics.LabelKey;
import io.opentelemetry.metrics.LabelValue; import io.opentelemetry.metrics.LabelValue;
import io.opentelemetry.metrics.MetricRegistry; import io.opentelemetry.metrics.Meter;
import io.opentelemetry.metrics.Metrics; import io.opentelemetry.metrics.Metrics;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean; import java.lang.management.MemoryMXBean;
@ -57,14 +57,13 @@ public final class MemoryPools {
private final MemoryMXBean memoryBean; private final MemoryMXBean memoryBean;
private final List<MemoryPoolMXBean> poolBeans; private final List<MemoryPoolMXBean> poolBeans;
private final MetricRegistry metricRegistry; private final Meter meter;
/** Constructs a new module that is capable to export metrics about "jvm_memory". */ /** Constructs a new module that is capable to export metrics about "jvm_memory". */
public MemoryPools() { public MemoryPools() {
this.memoryBean = ManagementFactory.getMemoryMXBean(); this.memoryBean = ManagementFactory.getMemoryMXBean();
this.poolBeans = ManagementFactory.getMemoryPoolMXBeans(); this.poolBeans = ManagementFactory.getMemoryPoolMXBeans();
this.metricRegistry = this.meter = Metrics.getMeter();
Metrics.getMeter().metricRegistryBuilder().setComponent("jvm_memory").build();
} }
/** Export only the "area" metric. */ /** Export only the "area" metric. */
@ -74,11 +73,12 @@ public final class MemoryPools {
// memory is committed (this can also be achieved by displaying the two metrics in the same // memory is committed (this can also be achieved by displaying the two metrics in the same
// chart). // chart).
final GaugeLong areaMetric = final GaugeLong areaMetric =
this.metricRegistry this.meter
.gaugeLongBuilder("area") .gaugeLongBuilder("area")
.setDescription("Bytes of a given JVM memory area.") .setDescription("Bytes of a given JVM memory area.")
.setUnit("By") .setUnit("By")
.setLabelKeys(Arrays.asList(TYPE, AREA)) .setLabelKeys(Arrays.asList(TYPE, AREA))
.setComponent("jvm_memory")
.build(); .build();
final GaugeLong.TimeSeries usedHeap = final GaugeLong.TimeSeries usedHeap =
areaMetric.getOrCreateTimeSeries(Arrays.asList(USED, HEAP)); areaMetric.getOrCreateTimeSeries(Arrays.asList(USED, HEAP));
@ -111,11 +111,12 @@ public final class MemoryPools {
/** Export only the "pool" metric. */ /** Export only the "pool" metric. */
public void exportMemoryPoolMetric() { public void exportMemoryPoolMetric() {
final GaugeLong poolMetric = final GaugeLong poolMetric =
this.metricRegistry this.meter
.gaugeLongBuilder("pool") .gaugeLongBuilder("pool")
.setDescription("Bytes of a given JVM memory pool.") .setDescription("Bytes of a given JVM memory pool.")
.setUnit("By") .setUnit("By")
.setLabelKeys(Arrays.asList(TYPE, POOL)) .setLabelKeys(Arrays.asList(TYPE, POOL))
.setComponent("jvm_memory")
.build(); .build();
poolMetric.setCallback( poolMetric.setCallback(
new Runnable() { new Runnable() {