diff options
author | Julien Desprez <jdesprez@google.com> | 2018-10-22 11:37:22 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2018-10-22 11:37:22 -0700 |
commit | 13217871fefa43f6d16fbb31b04e9904996d87d5 (patch) | |
tree | ede84fcf0a9687d4907ae5f8a4788271d62e0922 /api/src/main/java/io/opencensus/metrics | |
parent | cfbefd32336596ea63784607e4106dc37ce0567f (diff) | |
parent | 6fbc3cf5a1a3369fd354c1e5d9f90c86e4bce0a4 (diff) | |
download | opencensus-java-13217871fefa43f6d16fbb31b04e9904996d87d5.tar.gz |
Merge remote-tracking branch 'aosp/upstream-master' into merge am: dd3cabeacc
am: 6fbc3cf5a1
Change-Id: I11b0ec1cf561d2a14da78e444b1594f167787fe6
Diffstat (limited to 'api/src/main/java/io/opencensus/metrics')
20 files changed, 2660 insertions, 0 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java new file mode 100644 index 00000000..3aaca153 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java @@ -0,0 +1,152 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import io.opencensus.common.ToDoubleFunction; +import io.opencensus.internal.Utils; +import java.lang.ref.WeakReference; +import java.util.List; +import javax.annotation.concurrent.ThreadSafe; + +/*>>> +import org.checkerframework.checker.nullness.qual.Nullable; +*/ + +/** + * Derived Double Gauge metric, to report instantaneous measurement of a double value. Gauges can go + * both up and down. The gauges values can be negative. + * + * <p>Example: Create a Gauge with an object and a callback function. + * + * <pre>{@code + * class YourClass { + * + * private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); + * + * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); + * List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound")); + * + * DerivedDoubleGauge gauge = metricRegistry.addDerivedDoubleGauge( + * "queue_size", "Pending jobs in a queue", "1", labelKeys); + * + * QueueManager queueManager = new QueueManager(); + * gauge.createTimeSeries(labelValues, queueManager, + * new ToDoubleFunction<QueueManager>() { + * {@literal @}Override + * public double applyAsDouble(QueueManager queue) { + * return queue.size(); + * } + * }); + * + * void doWork() { + * // Your code here. + * } + * } + * + * }</pre> + * + * @since 0.17 + */ +@ThreadSafe +public abstract class DerivedDoubleGauge { + /** + * Creates a {@code TimeSeries}. The value of a single point in the TimeSeries is observed from a + * callback function. This function is invoked whenever metrics are collected, meaning the + * reported value is up-to-date. It keeps a {@link WeakReference} to the object and it is the + * user's responsibility to manage the lifetime of the object. + * + * @param labelValues the list of label values. + * @param obj the state object from which the function derives a measurement. + * @param function the function to be called. + * @param <T> the type of the object upon which the function derives a measurement. + * @throws NullPointerException if {@code labelValues} is null OR any element of {@code + * labelValues} is null OR {@code function} is null. + * @throws IllegalArgumentException if different time series with the same labels already exists + * OR if number of {@code labelValues}s are not equal to the label keys. + * @since 0.17 + */ + public abstract <T> void createTimeSeries( + List<LabelValue> labelValues, + /*@Nullable*/ T obj, + ToDoubleFunction</*@Nullable*/ T> function); + + /** + * Removes the {@code TimeSeries} from the gauge metric, if it is present. + * + * @param labelValues the list of label values. + * @throws NullPointerException if {@code labelValues} is null. + * @since 0.17 + */ + public abstract void removeTimeSeries(List<LabelValue> labelValues); + + /** + * Removes all {@code TimeSeries} from the gauge metric. + * + * @since 0.17 + */ + public abstract void clear(); + + /** + * Returns the no-op implementation of the {@code DerivedDoubleGauge}. + * + * @return the no-op implementation of the {@code DerivedDoubleGauge}. + * @since 0.17 + */ + static DerivedDoubleGauge newNoopDerivedDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + return NoopDerivedDoubleGauge.create(name, description, unit, labelKeys); + } + + /** No-op implementations of DerivedDoubleGauge class. */ + private static final class NoopDerivedDoubleGauge extends DerivedDoubleGauge { + private final int labelKeysSize; + + static NoopDerivedDoubleGauge create( + String name, String description, String unit, List<LabelKey> labelKeys) { + return new NoopDerivedDoubleGauge(name, description, unit, labelKeys); + } + + /** Creates a new {@code NoopDerivedDoubleGauge}. */ + NoopDerivedDoubleGauge(String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkNotNull(name, "name"); + Utils.checkNotNull(description, "description"); + Utils.checkNotNull(unit, "unit"); + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + labelKeysSize = labelKeys.size(); + } + + @Override + public <T> void createTimeSeries( + List<LabelValue> labelValues, + /*@Nullable*/ T obj, + ToDoubleFunction</*@Nullable*/ T> function) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelValues, "labelValues"), "labelValue element should not be null."); + Utils.checkArgument(labelKeysSize == labelValues.size(), "Incorrect number of labels."); + Utils.checkNotNull(function, "function"); + } + + @Override + public void removeTimeSeries(List<LabelValue> labelValues) { + Utils.checkNotNull(labelValues, "labelValues"); + } + + @Override + public void clear() {} + } +} diff --git a/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java new file mode 100644 index 00000000..621873f9 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java @@ -0,0 +1,150 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import io.opencensus.common.ToLongFunction; +import io.opencensus.internal.Utils; +import java.lang.ref.WeakReference; +import java.util.List; +import javax.annotation.concurrent.ThreadSafe; + +/*>>> +import org.checkerframework.checker.nullness.qual.Nullable; +*/ + +/** + * Derived Long Gauge metric, to report instantaneous measurement of an int64 value. Gauges can go + * both up and down. The gauges values can be negative. + * + * <p>Example: Create a Gauge with an object and a callback function. + * + * <pre>{@code + * class YourClass { + * + * private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); + * + * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); + * List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound")); + * + * DerivedLongGauge gauge = metricRegistry.addDerivedLongGauge( + * "queue_size", "Pending jobs in a queue", "1", labelKeys); + * + * QueueManager queueManager = new QueueManager(); + * gauge.createTimeSeries(labelValues, queueManager, + * new ToLongFunction<QueueManager>() { + * {@literal @}Override + * public long applyAsLong(QueueManager queue) { + * return queue.size(); + * } + * }); + * + * void doWork() { + * // Your code here. + * } + * } + * + * }</pre> + * + * @since 0.17 + */ +@ThreadSafe +public abstract class DerivedLongGauge { + /** + * Creates a {@code TimeSeries}. The value of a single point in the TimeSeries is observed from a + * callback function. This function is invoked whenever metrics are collected, meaning the + * reported value is up-to-date. It keeps a {@link WeakReference} to the object and it is the + * user's responsibility to manage the lifetime of the object. + * + * @param labelValues the list of label values. + * @param obj the state object from which the function derives a measurement. + * @param function the function to be called. + * @param <T> the type of the object upon which the function derives a measurement. + * @throws NullPointerException if {@code labelValues} is null OR any element of {@code + * labelValues} is null OR {@code function} is null. + * @throws IllegalArgumentException if different time series with the same labels already exists + * OR if number of {@code labelValues}s are not equal to the label keys. + * @since 0.17 + */ + public abstract <T> void createTimeSeries( + List<LabelValue> labelValues, /*@Nullable*/ T obj, ToLongFunction</*@Nullable*/ T> function); + + /** + * Removes the {@code TimeSeries} from the gauge metric, if it is present. + * + * @param labelValues the list of label values. + * @throws NullPointerException if {@code labelValues} is null. + * @since 0.17 + */ + public abstract void removeTimeSeries(List<LabelValue> labelValues); + + /** + * Removes all {@code TimeSeries} from the gauge metric. + * + * @since 0.17 + */ + public abstract void clear(); + + /** + * Returns the no-op implementation of the {@code DerivedLongGauge}. + * + * @return the no-op implementation of the {@code DerivedLongGauge}. + * @since 0.17 + */ + static DerivedLongGauge newNoopDerivedLongGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + return NoopDerivedLongGauge.create(name, description, unit, labelKeys); + } + + /** No-op implementations of DerivedLongGauge class. */ + private static final class NoopDerivedLongGauge extends DerivedLongGauge { + private final int labelKeysSize; + + static NoopDerivedLongGauge create( + String name, String description, String unit, List<LabelKey> labelKeys) { + return new NoopDerivedLongGauge(name, description, unit, labelKeys); + } + + /** Creates a new {@code NoopDerivedLongGauge}. */ + NoopDerivedLongGauge(String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkNotNull(name, "name"); + Utils.checkNotNull(description, "description"); + Utils.checkNotNull(unit, "unit"); + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + labelKeysSize = labelKeys.size(); + } + + @Override + public <T> void createTimeSeries( + List<LabelValue> labelValues, + /*@Nullable*/ T obj, + ToLongFunction</*@Nullable*/ T> function) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelValues, "labelValues"), "labelValue element should not be null."); + Utils.checkArgument(labelKeysSize == labelValues.size(), "Incorrect number of labels."); + Utils.checkNotNull(function, "function"); + } + + @Override + public void removeTimeSeries(List<LabelValue> labelValues) { + Utils.checkNotNull(labelValues, "labelValues"); + } + + @Override + public void clear() {} + } +} diff --git a/api/src/main/java/io/opencensus/metrics/DoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DoubleGauge.java new file mode 100644 index 00000000..32759973 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/DoubleGauge.java @@ -0,0 +1,213 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import io.opencensus.internal.Utils; +import java.util.List; +import javax.annotation.concurrent.ThreadSafe; + +/** + * Double Gauge metric, to report instantaneous measurement of a double value. Gauges can go both up + * and down. The gauges values can be negative. + * + * <p>Example 1: Create a Gauge with default labels. + * + * <pre>{@code + * class YourClass { + * + * private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); + * + * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); + * + * DoubleGauge gauge = metricRegistry.addDoubleGauge("queue_size", + * "Pending jobs", "1", labelKeys); + * + * // It is recommended to keep a reference of a point for manual operations. + * DoublePoint defaultPoint = gauge.getDefaultTimeSeries(); + * + * void doWork() { + * // Your code here. + * defaultPoint.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 MetricRegistry metricRegistry = Metrics.getMetricRegistry(); + * + * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); + * List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound")); + * + * DoubleGauge gauge = metricRegistry.addDoubleGauge("queue_size", + * "Pending jobs", "1", labelKeys); + * + * // It is recommended to keep a reference of a point for manual operations. + * DoublePoint inboundPoint = gauge.getOrCreateTimeSeries(labelValues); + * + * void doSomeWork() { + * // Your code here. + * inboundPoint.set(15); + * } + * + * } + * }</pre> + * + * @since 0.17 + */ +@ThreadSafe +public abstract class DoubleGauge { + + /** + * Creates a {@code TimeSeries} and returns a {@code DoublePoint} if the specified {@code + * labelValues} is not already associated with this gauge, else returns an existing {@code + * DoublePoint}. + * + * <p>It is recommended to keep a reference to the DoublePoint instead of always calling this + * method for manual operations. + * + * @param labelValues the list of label values. The number of label values must be the same to + * that of the label keys passed to {@link MetricRegistry#addDoubleGauge}. + * @return a {@code DoublePoint} the value of single gauge. + * @throws NullPointerException if {@code labelValues} is null OR any element of {@code + * labelValues} is null. + * @throws IllegalArgumentException if number of {@code labelValues}s are not equal to the label + * keys. + * @since 0.17 + */ + public abstract DoublePoint getOrCreateTimeSeries(List<LabelValue> labelValues); + + /** + * Returns a {@code DoublePoint} for a gauge with all labels not set, or default labels. + * + * @return a {@code DoublePoint} for a gauge with all labels not set, or default labels. + * @since 0.17 + */ + public abstract DoublePoint getDefaultTimeSeries(); + + /** + * Removes the {@code TimeSeries} from the gauge metric, if it is present. i.e. references to + * previous {@code DoublePoint} objects are invalid (not part of the metric). + * + * @param labelValues the list of label values. + * @throws NullPointerException if {@code labelValues} is null or any element of {@code + * labelValues} is null. + * @since 0.17 + */ + public abstract void removeTimeSeries(List<LabelValue> labelValues); + + /** + * Removes all {@code TimeSeries} from the gauge metric. i.e. references to all previous {@code + * DoublePoint} objects are invalid (not part of the metric). + * + * @since 0.17 + */ + public abstract void clear(); + + /** + * Returns the no-op implementation of the {@code DoubleGauge}. + * + * @return the no-op implementation of the {@code DoubleGauge}. + * @since 0.17 + */ + static DoubleGauge newNoopDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + return NoopDoubleGauge.create(name, description, unit, labelKeys); + } + + /** + * The value of a single point in the Gauge.TimeSeries. + * + * @since 0.17 + */ + public abstract static class DoublePoint { + + /** + * Adds the given value to the current value. The values can be negative. + * + * @param amt the value to add + * @since 0.17 + */ + public abstract void add(double amt); + + /** + * Sets the given value. + * + * @param val the new value. + * @since 0.17 + */ + public abstract void set(double val); + } + + /** No-op implementations of DoubleGauge class. */ + private static final class NoopDoubleGauge extends DoubleGauge { + private final int labelKeysSize; + + static NoopDoubleGauge create( + String name, String description, String unit, List<LabelKey> labelKeys) { + return new NoopDoubleGauge(name, description, unit, labelKeys); + } + + /** Creates a new {@code NoopDoublePoint}. */ + NoopDoubleGauge(String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkNotNull(name, "name"); + Utils.checkNotNull(description, "description"); + Utils.checkNotNull(unit, "unit"); + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + labelKeysSize = labelKeys.size(); + } + + @Override + public NoopDoublePoint getOrCreateTimeSeries(List<LabelValue> labelValues) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelValues, "labelValues"), "labelValue element should not be null."); + Utils.checkArgument(labelKeysSize == labelValues.size(), "Incorrect number of labels."); + return NoopDoublePoint.INSTANCE; + } + + @Override + public NoopDoublePoint getDefaultTimeSeries() { + return NoopDoublePoint.INSTANCE; + } + + @Override + public void removeTimeSeries(List<LabelValue> labelValues) { + Utils.checkNotNull(labelValues, "labelValues"); + } + + @Override + public void clear() {} + + /** No-op implementations of DoublePoint class. */ + private static final class NoopDoublePoint extends DoublePoint { + private static final NoopDoublePoint INSTANCE = new NoopDoublePoint(); + + private NoopDoublePoint() {} + + @Override + public void add(double amt) {} + + @Override + public void set(double val) {} + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/LabelKey.java b/api/src/main/java/io/opencensus/metrics/LabelKey.java new file mode 100644 index 00000000..efc51e64 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/LabelKey.java @@ -0,0 +1,62 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import javax.annotation.concurrent.Immutable; + +/** + * The key of a {@code Label} associated with a {@code MetricDescriptor}. + * + * @since 0.15 + */ +@ExperimentalApi +@Immutable +@AutoValue +public abstract class LabelKey { + + LabelKey() {} + + /** + * Creates a {@link LabelKey}. + * + * @param key the key of a {@code Label}. + * @param description a human-readable description of what this label key represents. + * @return a {@code LabelKey}. + * @since 0.17 + */ + public static LabelKey create(String key, String description) { + return new AutoValue_LabelKey(key, description); + } + + /** + * Returns the key of this {@link LabelKey}. + * + * @return the key. + * @since 0.17 + */ + public abstract String getKey(); + + /** + * Returns the description of this {@link LabelKey}. + * + * @return the description. + * @since 0.17 + */ + public abstract String getDescription(); +} diff --git a/api/src/main/java/io/opencensus/metrics/LabelValue.java b/api/src/main/java/io/opencensus/metrics/LabelValue.java new file mode 100644 index 00000000..e5708655 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/LabelValue.java @@ -0,0 +1,57 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +/** + * The value of a {@code Label} associated with a {@code TimeSeries}. + * + * @since 0.15 + */ +@ExperimentalApi +@Immutable +@AutoValue +public abstract class LabelValue { + + LabelValue() {} + + /** + * Creates a {@link LabelValue}. + * + * @param value the value of a {@code Label}. {@code null} value indicates an unset {@code + * LabelValue}. + * @return a {@code LabelValue}. + * @since 0.17 + */ + public static LabelValue create(@Nullable String value) { + return new AutoValue_LabelValue(value); + } + + /** + * Returns the value of this {@link LabelValue}. Returns {@code null} if the value is unset and + * supposed to be ignored. + * + * @return the value. + * @since 0.17 + */ + @Nullable + public abstract String getValue(); +} diff --git a/api/src/main/java/io/opencensus/metrics/LongGauge.java b/api/src/main/java/io/opencensus/metrics/LongGauge.java new file mode 100644 index 00000000..1d4489c9 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/LongGauge.java @@ -0,0 +1,205 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import io.opencensus.internal.Utils; +import java.util.List; +import javax.annotation.concurrent.ThreadSafe; + +/** + * Long Gauge metric, to report instantaneous measurement of an int64 value. Gauges can go both up + * and down. The gauges values can be negative. + * + * <p>Example 1: Create a Gauge with default labels. + * + * <pre>{@code + * class YourClass { + * + * private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); + * + * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); + * + * LongGauge gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys); + * + * // It is recommended to keep a reference of a point for manual operations. + * LongPoint defaultPoint = gauge.getDefaultTimeSeries(); + * + * void doWork() { + * // Your code here. + * defaultPoint.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 MetricRegistry metricRegistry = Metrics.getMetricRegistry(); + * + * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); + * List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound")); + * + * LongGauge gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys); + * + * // It is recommended to keep a reference of a point for manual operations. + * LongPoint inboundPoint = gauge.getOrCreateTimeSeries(labelValues); + * + * void doSomeWork() { + * // Your code here. + * inboundPoint.set(15); + * } + * + * } + * }</pre> + * + * @since 0.17 + */ +@ThreadSafe +public abstract class LongGauge { + + /** + * Creates a {@code TimeSeries} and returns a {@code LongPoint} if the specified {@code + * labelValues} is not already associated with this gauge, else returns an existing {@code + * LongPoint}. + * + * <p>It is recommended to keep a reference to the LongPoint instead of always calling this method + * for manual operations. + * + * @param labelValues the list of label values. The number of label values must be the same to + * that of the label keys passed to {@link MetricRegistry#addLongGauge}. + * @return a {@code LongPoint} the value of single gauge. + * @throws NullPointerException if {@code labelValues} is null OR any element of {@code + * labelValues} is null. + * @throws IllegalArgumentException if number of {@code labelValues}s are not equal to the label + * keys passed to {@link MetricRegistry#addLongGauge}. + * @since 0.17 + */ + public abstract LongPoint getOrCreateTimeSeries(List<LabelValue> labelValues); + + /** + * Returns a {@code LongPoint} for a gauge with all labels not set, or default labels. + * + * @return a {@code LongPoint} for a gauge with all labels not set, or default labels. + * @since 0.17 + */ + public abstract LongPoint getDefaultTimeSeries(); + + /** + * Removes the {@code TimeSeries} from the gauge metric, if it is present. i.e. references to + * previous {@code LongPoint} objects are invalid (not part of the metric). + * + * @param labelValues the list of label values. + * @throws NullPointerException if {@code labelValues} is null. + * @since 0.17 + */ + public abstract void removeTimeSeries(List<LabelValue> labelValues); + + /** + * Removes all {@code TimeSeries} from the gauge metric. i.e. references to all previous {@code + * LongPoint} objects are invalid (not part of the metric). + * + * @since 0.17 + */ + public abstract void clear(); + + /** + * Returns the no-op implementation of the {@code LongGauge}. + * + * @return the no-op implementation of the {@code LongGauge}. + * @since 0.17 + */ + static LongGauge newNoopLongGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + return NoopLongGauge.create(name, description, unit, labelKeys); + } + + /** + * The value of a single point in the Gauge.TimeSeries. + * + * @since 0.17 + */ + public abstract static class LongPoint { + + /** + * Adds the given value to the current value. The values can be negative. + * + * @param amt the value to add + * @since 0.17 + */ + public abstract void add(long amt); + + /** + * Sets the given value. + * + * @param val the new value. + * @since 0.17 + */ + public abstract void set(long val); + } + + /** No-op implementations of LongGauge class. */ + private static final class NoopLongGauge extends LongGauge { + private final int labelKeysSize; + + static NoopLongGauge create( + String name, String description, String unit, List<LabelKey> labelKeys) { + return new NoopLongGauge(name, description, unit, labelKeys); + } + + /** Creates a new {@code NoopLongPoint}. */ + NoopLongGauge(String name, String description, String unit, List<LabelKey> labelKeys) { + labelKeysSize = labelKeys.size(); + } + + @Override + public NoopLongPoint getOrCreateTimeSeries(List<LabelValue> labelValues) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelValues, "labelValues"), "labelValue element should not be null."); + Utils.checkArgument(labelKeysSize == labelValues.size(), "Incorrect number of labels."); + return NoopLongPoint.INSTANCE; + } + + @Override + public NoopLongPoint getDefaultTimeSeries() { + return NoopLongPoint.INSTANCE; + } + + @Override + public void removeTimeSeries(List<LabelValue> labelValues) { + Utils.checkNotNull(labelValues, "labelValues"); + } + + @Override + public void clear() {} + + /** No-op implementations of LongPoint class. */ + private static final class NoopLongPoint extends LongPoint { + private static final NoopLongPoint INSTANCE = new NoopLongPoint(); + + private NoopLongPoint() {} + + @Override + public void add(long amt) {} + + @Override + public void set(long val) {} + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java new file mode 100644 index 00000000..5be15594 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java @@ -0,0 +1,156 @@ +/* + * Copyright 2018, OpenCensus 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.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; + +/** + * Creates and manages your application's set of metrics. The default implementation of this creates + * a {@link io.opencensus.metrics.export.MetricProducer} and registers it to the global {@link + * io.opencensus.metrics.export.MetricProducerManager}. + * + * @since 0.17 + */ +@ExperimentalApi +public abstract class MetricRegistry { + /** + * Builds a new long gauge to be added to the registry. This is more convenient form when you want + * to manually increase and decrease values as per your service requirements. + * + * @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 LongGauge addLongGauge( + String name, String description, String unit, List<LabelKey> labelKeys); + + /** + * Builds a new double gauge to be added to the registry. This is more convenient form when you + * want to manually increase and decrease values as per your service requirements. + * + * @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 DoubleGauge addDoubleGauge( + String name, String description, String unit, List<LabelKey> 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<LabelKey> 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<LabelKey> labelKeys); + + static MetricRegistry newNoopMetricRegistry() { + return new NoopMetricRegistry(); + } + + private static final class NoopMetricRegistry extends MetricRegistry { + + @Override + public LongGauge addLongGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + return LongGauge.newNoopLongGauge( + Utils.checkNotNull(name, "name"), + Utils.checkNotNull(description, "description"), + Utils.checkNotNull(unit, "unit"), + labelKeys); + } + + @Override + public DoubleGauge addDoubleGauge( + String name, String description, String unit, List<LabelKey> labelKeys) { + Utils.checkListElementNotNull( + Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null."); + return DoubleGauge.newNoopDoubleGauge( + Utils.checkNotNull(name, "name"), + Utils.checkNotNull(description, "description"), + Utils.checkNotNull(unit, "unit"), + labelKeys); + } + + @Override + public DerivedLongGauge addDerivedLongGauge( + String name, String description, String unit, List<LabelKey> 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<LabelKey> 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); + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/Metrics.java b/api/src/main/java/io/opencensus/metrics/Metrics.java new file mode 100644 index 00000000..920a4a88 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/Metrics.java @@ -0,0 +1,96 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import io.opencensus.common.ExperimentalApi; +import io.opencensus.internal.DefaultVisibilityForTesting; +import io.opencensus.internal.Provider; +import io.opencensus.metrics.export.ExportComponent; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.Nullable; + +/** + * Class for accessing the default {@link MetricsComponent}. + * + * @since 0.17 + */ +@ExperimentalApi +public final class Metrics { + private static final Logger logger = Logger.getLogger(Metrics.class.getName()); + private static final MetricsComponent metricsComponent = + loadMetricsComponent(MetricsComponent.class.getClassLoader()); + + /** + * Returns the global {@link ExportComponent}. + * + * @return the global {@code ExportComponent}. + * @since 0.17 + */ + public static ExportComponent getExportComponent() { + return metricsComponent.getExportComponent(); + } + + /** + * Returns the global {@link MetricRegistry}. + * + * <p>This {@code MetricRegistry} is already added to the global {@link + * io.opencensus.metrics.export.MetricProducerManager}. + * + * @return the global {@code MetricRegistry}. + * @since 0.17 + */ + public static MetricRegistry getMetricRegistry() { + return metricsComponent.getMetricRegistry(); + } + + // Any provider that may be used for MetricsComponent can be added here. + @DefaultVisibilityForTesting + static MetricsComponent loadMetricsComponent(@Nullable ClassLoader classLoader) { + try { + // Call Class.forName with literal string name of the class to help shading tools. + return Provider.createInstance( + Class.forName( + "io.opencensus.impl.metrics.MetricsComponentImpl", /*initialize=*/ true, classLoader), + MetricsComponent.class); + } catch (ClassNotFoundException e) { + logger.log( + Level.FINE, + "Couldn't load full implementation for MetricsComponent, now trying to load lite " + + "implementation.", + e); + } + try { + // Call Class.forName with literal string name of the class to help shading tools. + return Provider.createInstance( + Class.forName( + "io.opencensus.impllite.metrics.MetricsComponentImplLite", + /*initialize=*/ true, + classLoader), + MetricsComponent.class); + } catch (ClassNotFoundException e) { + logger.log( + Level.FINE, + "Couldn't load lite implementation for MetricsComponent, now using default " + + "implementation for MetricsComponent.", + e); + } + return MetricsComponent.newNoopMetricsComponent(); + } + + private Metrics() {} +} diff --git a/api/src/main/java/io/opencensus/metrics/MetricsComponent.java b/api/src/main/java/io/opencensus/metrics/MetricsComponent.java new file mode 100644 index 00000000..3a992306 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/MetricsComponent.java @@ -0,0 +1,71 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics; + +import io.opencensus.common.ExperimentalApi; +import io.opencensus.metrics.export.ExportComponent; + +/** + * Class that holds the implementation instance for {@link ExportComponent}. + * + * @since 0.17 + */ +@ExperimentalApi +public abstract class MetricsComponent { + + /** + * Returns the {@link ExportComponent} with the provided implementation. If no implementation is + * provided then no-op implementations will be used. + * + * @return the {@link ExportComponent} implementation. + * @since 0.17 + */ + public abstract ExportComponent getExportComponent(); + + /** + * Returns the {@link MetricRegistry} with the provided implementation. + * + * @return the {@link MetricRegistry} implementation. + * @since 0.17 + */ + public abstract MetricRegistry getMetricRegistry(); + + /** + * Returns an instance that contains no-op implementations for all the instances. + * + * @return an instance that contains no-op implementations for all the instances. + */ + static MetricsComponent newNoopMetricsComponent() { + return new NoopMetricsComponent(); + } + + private static final class NoopMetricsComponent extends MetricsComponent { + private static final ExportComponent EXPORT_COMPONENT = + ExportComponent.newNoopExportComponent(); + private static final MetricRegistry METRIC_REGISTRY = MetricRegistry.newNoopMetricRegistry(); + + @Override + public ExportComponent getExportComponent() { + return EXPORT_COMPONENT; + } + + @Override + public MetricRegistry getMetricRegistry() { + return METRIC_REGISTRY; + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/export/Distribution.java b/api/src/main/java/io/opencensus/metrics/export/Distribution.java new file mode 100644 index 00000000..d55f101c --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/Distribution.java @@ -0,0 +1,345 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.common.Function; +import io.opencensus.common.Timestamp; +import io.opencensus.internal.Utils; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +/** + * {@link Distribution} contains summary statistics for a population of values. It optionally + * contains a histogram representing the distribution of those values across a set of buckets. + * + * @since 0.17 + */ +@ExperimentalApi +@AutoValue +@Immutable +public abstract class Distribution { + + Distribution() {} + + /** + * Creates a {@link Distribution}. + * + * @param count the count of the population values. + * @param sum the sum of the population values. + * @param sumOfSquaredDeviations the sum of squared deviations of the population values. + * @param bucketOptions the bucket options used to create a histogram for the distribution. + * @param buckets {@link Bucket}s of a histogram. + * @return a {@code Distribution}. + * @since 0.17 + */ + public static Distribution create( + long count, + double sum, + double sumOfSquaredDeviations, + BucketOptions bucketOptions, + List<Bucket> buckets) { + Utils.checkArgument(count >= 0, "count should be non-negative."); + Utils.checkArgument( + sumOfSquaredDeviations >= 0, "sum of squared deviations should be non-negative."); + if (count == 0) { + Utils.checkArgument(sum == 0, "sum should be 0 if count is 0."); + Utils.checkArgument( + sumOfSquaredDeviations == 0, "sum of squared deviations should be 0 if count is 0."); + } + Utils.checkNotNull(bucketOptions, "bucketOptions"); + List<Bucket> bucketsCopy = + Collections.unmodifiableList(new ArrayList<Bucket>(Utils.checkNotNull(buckets, "buckets"))); + Utils.checkListElementNotNull(bucketsCopy, "bucket"); + return new AutoValue_Distribution( + count, sum, sumOfSquaredDeviations, bucketOptions, bucketsCopy); + } + + /** + * Returns the aggregated count. + * + * @return the aggregated count. + * @since 0.17 + */ + public abstract long getCount(); + + /** + * Returns the aggregated sum. + * + * @return the aggregated sum. + * @since 0.17 + */ + public abstract double getSum(); + + /** + * Returns the aggregated sum of squared deviations. + * + * <p>The sum of squared deviations from the mean of the values in the population. For values x_i + * this is: + * + * <p>Sum[i=1..n]((x_i - mean)^2) + * + * <p>If count is zero then this field must be zero. + * + * @return the aggregated sum of squared deviations. + * @since 0.17 + */ + public abstract double getSumOfSquaredDeviations(); + + /** + * Returns bucket options used to create a histogram for the distribution. + * + * @return the {@code BucketOptions} associated with the {@code Distribution}, or {@code null} if + * there isn't one. + * @since 0.17 + */ + @Nullable + public abstract BucketOptions getBucketOptions(); + + /** + * Returns the aggregated histogram {@link Bucket}s. + * + * @return the aggregated histogram buckets. + * @since 0.17 + */ + public abstract List<Bucket> getBuckets(); + + /** + * The bucket options used to create a histogram for the distribution. + * + * @since 0.17 + */ + @Immutable + public abstract static class BucketOptions { + + private BucketOptions() {} + + /** + * Returns a {@link ExplicitOptions}. + * + * <p>The bucket boundaries for that histogram are described by bucket_bounds. This defines + * size(bucket_bounds) + 1 (= N) buckets. The boundaries for bucket index i are: + * + * <ul> + * <li>{@code [0, bucket_bounds[i]) for i == 0} + * <li>{@code [bucket_bounds[i-1], bucket_bounds[i]) for 0 < i < N-1} + * <li>{@code [bucket_bounds[i-1], +infinity) for i == N-1} + * </ul> + * + * <p>If bucket_bounds has no elements (zero size), then there is no histogram associated with + * the Distribution. If bucket_bounds has only one element, there are no finite buckets, and + * that single element is the common boundary of the overflow and underflow buckets. The values + * must be monotonically increasing. + * + * @param bucketBoundaries the bucket boundaries of a distribution (given explicitly). The + * values must be strictly increasing and should be positive values. + * @return a {@code ExplicitOptions} {@code BucketOptions}. + * @since 0.17 + */ + public static BucketOptions explicitOptions(List<Double> bucketBoundaries) { + return ExplicitOptions.create(bucketBoundaries); + } + + /** + * Applies the given match function to the underlying BucketOptions. + * + * @param explicitFunction the function that should be applied if the BucketOptions has type + * {@code ExplicitOptions}. + * @param defaultFunction the function that should be applied if the BucketOptions has a type + * that was added after this {@code match} method was added to the API. See {@link + * io.opencensus.common.Functions} for some common functions for handling unknown types. + * @return the result of the function applied to the underlying BucketOptions. + * @since 0.17 + */ + public abstract <T> T match( + Function<? super ExplicitOptions, T> explicitFunction, + Function<? super BucketOptions, T> defaultFunction); + + /** A Bucket with explicit bounds {@link BucketOptions}. */ + @AutoValue + @Immutable + public abstract static class ExplicitOptions extends BucketOptions { + + ExplicitOptions() {} + + @Override + public final <T> T match( + Function<? super ExplicitOptions, T> explicitFunction, + Function<? super BucketOptions, T> defaultFunction) { + return explicitFunction.apply(this); + } + + /** + * Creates a {@link ExplicitOptions}. + * + * @param bucketBoundaries the bucket boundaries of a distribution (given explicitly). The + * values must be strictly increasing and should be positive. + * @return a {@code ExplicitOptions}. + * @since 0.17 + */ + private static ExplicitOptions create(List<Double> bucketBoundaries) { + Utils.checkNotNull(bucketBoundaries, "bucketBoundaries"); + List<Double> bucketBoundariesCopy = + Collections.unmodifiableList(new ArrayList<Double>(bucketBoundaries)); + checkBucketBoundsAreSorted(bucketBoundariesCopy); + return new AutoValue_Distribution_BucketOptions_ExplicitOptions(bucketBoundariesCopy); + } + + private static void checkBucketBoundsAreSorted(List<Double> bucketBoundaries) { + if (bucketBoundaries.size() >= 1) { + double previous = Utils.checkNotNull(bucketBoundaries.get(0), "bucketBoundary"); + Utils.checkArgument(previous > 0, "bucket boundary should be > 0"); + for (int i = 1; i < bucketBoundaries.size(); i++) { + double next = Utils.checkNotNull(bucketBoundaries.get(i), "bucketBoundary"); + Utils.checkArgument(previous < next, "bucket boundaries not sorted."); + previous = next; + } + } + } + + /** + * Returns the bucket boundaries of this distribution. + * + * @return the bucket boundaries of this distribution. + * @since 0.17 + */ + public abstract List<Double> getBucketBoundaries(); + } + } + + /** + * The histogram bucket of the population values. + * + * @since 0.17 + */ + @AutoValue + @Immutable + public abstract static class Bucket { + + Bucket() {} + + /** + * Creates a {@link Bucket}. + * + * @param count the number of values in each bucket of the histogram. + * @return a {@code Bucket}. + * @since 0.17 + */ + public static Bucket create(long count) { + Utils.checkArgument(count >= 0, "bucket count should be non-negative."); + return new AutoValue_Distribution_Bucket(count, null); + } + + /** + * Creates a {@link Bucket} with an {@link Exemplar}. + * + * @param count the number of values in each bucket of the histogram. + * @param exemplar the {@code Exemplar} of this {@code Bucket}. + * @return a {@code Bucket}. + * @since 0.17 + */ + public static Bucket create(long count, Exemplar exemplar) { + Utils.checkArgument(count >= 0, "bucket count should be non-negative."); + Utils.checkNotNull(exemplar, "exemplar"); + return new AutoValue_Distribution_Bucket(count, exemplar); + } + + /** + * Returns the number of values in each bucket of the histogram. + * + * @return the number of values in each bucket of the histogram. + * @since 0.17 + */ + public abstract long getCount(); + + /** + * Returns the {@link Exemplar} associated with the {@link Bucket}, or {@code null} if there + * isn't one. + * + * @return the {@code Exemplar} associated with the {@code Bucket}, or {@code null} if there + * isn't one. + * @since 0.17 + */ + @Nullable + public abstract Exemplar getExemplar(); + } + + /** + * An example point that may be used to annotate aggregated distribution values, associated with a + * histogram bucket. + * + * @since 0.17 + */ + @Immutable + @AutoValue + public abstract static class Exemplar { + + Exemplar() {} + + /** + * Returns value of the {@link Exemplar} point. + * + * @return value of the {@code Exemplar} point. + * @since 0.17 + */ + public abstract double getValue(); + + /** + * Returns the time that this {@link Exemplar}'s value was recorded. + * + * @return the time that this {@code Exemplar}'s value was recorded. + * @since 0.17 + */ + public abstract Timestamp getTimestamp(); + + /** + * Returns the contextual information about the example value, represented as a string map. + * + * @return the contextual information about the example value. + * @since 0.17 + */ + public abstract Map<String, String> getAttachments(); + + /** + * Creates an {@link Exemplar}. + * + * @param value value of the {@link Exemplar} point. + * @param timestamp the time that this {@code Exemplar}'s value was recorded. + * @param attachments the contextual information about the example value. + * @return an {@code Exemplar}. + * @since 0.17 + */ + public static Exemplar create( + double value, Timestamp timestamp, Map<String, String> attachments) { + Utils.checkNotNull(attachments, "attachments"); + Map<String, String> attachmentsCopy = + Collections.unmodifiableMap(new HashMap<String, String>(attachments)); + for (Entry<String, String> entry : attachmentsCopy.entrySet()) { + Utils.checkNotNull(entry.getKey(), "key of attachments"); + Utils.checkNotNull(entry.getValue(), "value of attachments"); + } + return new AutoValue_Distribution_Exemplar(value, timestamp, attachmentsCopy); + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/export/ExportComponent.java b/api/src/main/java/io/opencensus/metrics/export/ExportComponent.java new file mode 100644 index 00000000..11e1fdbd --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/ExportComponent.java @@ -0,0 +1,60 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import io.opencensus.common.ExperimentalApi; + +/** + * Class that holds the implementation instance for {@link MetricProducerManager}. + * + * <p>Unless otherwise noted all methods (on component) results are cacheable. + * + * @since 0.17 + */ +@ExperimentalApi +public abstract class ExportComponent { + /** + * Returns the no-op implementation of the {@code ExportComponent}. + * + * @return the no-op implementation of the {@code ExportComponent}. + * @since 0.17 + */ + public static ExportComponent newNoopExportComponent() { + return new NoopExportComponent(); + } + + /** + * Returns the global {@link MetricProducerManager} which can be used to register handlers to + * export all the recorded metrics. + * + * @return the implementation of the {@code MetricExporter} or no-op if no implementation linked + * in the binary. + * @since 0.17 + */ + public abstract MetricProducerManager getMetricProducerManager(); + + private static final class NoopExportComponent extends ExportComponent { + + private static final MetricProducerManager METRIC_PRODUCER_MANAGER = + MetricProducerManager.newNoopMetricProducerManager(); + + @Override + public MetricProducerManager getMetricProducerManager() { + return METRIC_PRODUCER_MANAGER; + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/export/Metric.java b/api/src/main/java/io/opencensus/metrics/export/Metric.java new file mode 100644 index 00000000..7b93fc86 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/Metric.java @@ -0,0 +1,137 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.internal.Utils; +import io.opencensus.metrics.export.Value.ValueDistribution; +import io.opencensus.metrics.export.Value.ValueDouble; +import io.opencensus.metrics.export.Value.ValueLong; +import io.opencensus.metrics.export.Value.ValueSummary; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.concurrent.Immutable; + +/** + * A {@link Metric} with one or more {@link TimeSeries}. + * + * @since 0.17 + */ +@ExperimentalApi +@Immutable +@AutoValue +public abstract class Metric { + + Metric() {} + + /** + * Creates a {@link Metric}. + * + * @param metricDescriptor the {@link MetricDescriptor}. + * @param timeSeriesList the {@link TimeSeries} list for this metric. + * @return a {@code Metric}. + * @since 0.17 + */ + public static Metric create(MetricDescriptor metricDescriptor, List<TimeSeries> timeSeriesList) { + Utils.checkListElementNotNull( + Utils.checkNotNull(timeSeriesList, "timeSeriesList"), "timeSeries"); + return createInternal( + metricDescriptor, Collections.unmodifiableList(new ArrayList<TimeSeries>(timeSeriesList))); + } + + /** + * Creates a {@link Metric}. + * + * @param metricDescriptor the {@link MetricDescriptor}. + * @param timeSeries the single {@link TimeSeries} for this metric. + * @return a {@code Metric}. + * @since 0.17 + */ + public static Metric createWithOneTimeSeries( + MetricDescriptor metricDescriptor, TimeSeries timeSeries) { + return createInternal( + metricDescriptor, Collections.singletonList(Utils.checkNotNull(timeSeries, "timeSeries"))); + } + + /** + * Creates a {@link Metric}. + * + * @param metricDescriptor the {@link MetricDescriptor}. + * @param timeSeriesList the {@link TimeSeries} list for this metric. + * @return a {@code Metric}. + * @since 0.17 + */ + private static Metric createInternal( + MetricDescriptor metricDescriptor, List<TimeSeries> timeSeriesList) { + Utils.checkNotNull(metricDescriptor, "metricDescriptor"); + checkTypeMatch(metricDescriptor.getType(), timeSeriesList); + return new AutoValue_Metric(metricDescriptor, timeSeriesList); + } + + /** + * Returns the {@link MetricDescriptor} of this metric. + * + * @return the {@code MetricDescriptor} of this metric. + * @since 0.17 + */ + public abstract MetricDescriptor getMetricDescriptor(); + + /** + * Returns the {@link TimeSeries} list for this metric. + * + * <p>The type of the {@link TimeSeries#getPoints()} must match {@link MetricDescriptor.Type}. + * + * @return the {@code TimeSeriesList} for this metric. + * @since 0.17 + */ + public abstract List<TimeSeries> getTimeSeriesList(); + + private static void checkTypeMatch(MetricDescriptor.Type type, List<TimeSeries> timeSeriesList) { + for (TimeSeries timeSeries : timeSeriesList) { + for (Point point : timeSeries.getPoints()) { + Value value = point.getValue(); + String valueClassName = ""; + if (value.getClass().getSuperclass() != null) { // work around nullness check + // AutoValue classes should always have a super class. + valueClassName = value.getClass().getSuperclass().getSimpleName(); + } + switch (type) { + case GAUGE_INT64: + case CUMULATIVE_INT64: + Utils.checkArgument( + value instanceof ValueLong, "Type mismatch: %s, %s.", type, valueClassName); + break; + case CUMULATIVE_DOUBLE: + case GAUGE_DOUBLE: + Utils.checkArgument( + value instanceof ValueDouble, "Type mismatch: %s, %s.", type, valueClassName); + break; + case GAUGE_DISTRIBUTION: + case CUMULATIVE_DISTRIBUTION: + Utils.checkArgument( + value instanceof ValueDistribution, "Type mismatch: %s, %s.", type, valueClassName); + break; + case SUMMARY: + Utils.checkArgument( + value instanceof ValueSummary, "Type mismatch: %s, %s.", type, valueClassName); + } + } + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/export/MetricDescriptor.java b/api/src/main/java/io/opencensus/metrics/export/MetricDescriptor.java new file mode 100644 index 00000000..a4629f8e --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/MetricDescriptor.java @@ -0,0 +1,173 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.internal.Utils; +import io.opencensus.metrics.LabelKey; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.concurrent.Immutable; + +/** + * {@link MetricDescriptor} defines a {@code Metric} type and its schema. + * + * @since 0.17 + */ +@ExperimentalApi +@Immutable +@AutoValue +public abstract class MetricDescriptor { + + MetricDescriptor() {} + + /** + * Creates a {@link MetricDescriptor}. + * + * @param name name of {@code MetricDescriptor}. + * @param description description of {@code MetricDescriptor}. + * @param unit the metric unit. + * @param type type of {@code MetricDescriptor}. + * @param labelKeys the label keys associated with the {@code MetricDescriptor}. + * @return a {@code MetricDescriptor}. + * @since 0.17 + */ + public static MetricDescriptor create( + String name, String description, String unit, Type type, List<LabelKey> labelKeys) { + Utils.checkNotNull(labelKeys, "labelKeys"); + Utils.checkListElementNotNull(labelKeys, "labelKey"); + return new AutoValue_MetricDescriptor( + name, + description, + unit, + type, + Collections.unmodifiableList(new ArrayList<LabelKey>(labelKeys))); + } + + /** + * Returns the metric descriptor name. + * + * @return the metric descriptor name. + * @since 0.17 + */ + public abstract String getName(); + + /** + * Returns the description of this metric descriptor. + * + * @return the description of this metric descriptor. + * @since 0.17 + */ + public abstract String getDescription(); + + /** + * Returns the unit of this metric descriptor. + * + * @return the unit of this metric descriptor. + * @since 0.17 + */ + public abstract String getUnit(); + + /** + * Returns the type of this metric descriptor. + * + * @return the type of this metric descriptor. + * @since 0.17 + */ + public abstract Type getType(); + + /** + * Returns the label keys associated with this metric descriptor. + * + * @return the label keys associated with this metric descriptor. + * @since 0.17 + */ + public abstract List<LabelKey> getLabelKeys(); + + /** + * The kind of metric. It describes how the data is reported. + * + * <p>A gauge is an instantaneous measurement of a value. + * + * <p>A cumulative measurement is a value accumulated over a time interval. In a time series, + * cumulative measurements should have the same start time and increasing end times, until an + * event resets the cumulative value to zero and sets a new start time for the following points. + * + * @since 0.17 + */ + public enum Type { + + /** + * An instantaneous measurement of an int64 value. + * + * @since 0.17 + */ + GAUGE_INT64, + + /** + * An instantaneous measurement of a double value. + * + * @since 0.17 + */ + GAUGE_DOUBLE, + + /** + * An instantaneous measurement of a distribution value. The count and sum can go both up and + * down. Used in scenarios like a snapshot of time the current items in a queue have spent + * there. + * + * @since 0.17 + */ + GAUGE_DISTRIBUTION, + + /** + * An cumulative measurement of an int64 value. + * + * @since 0.17 + */ + CUMULATIVE_INT64, + + /** + * An cumulative measurement of a double value. + * + * @since 0.17 + */ + CUMULATIVE_DOUBLE, + + /** + * An cumulative measurement of a distribution value. The count and sum can only go up, if + * resets then the start_time should also be reset. + * + * @since 0.17 + */ + CUMULATIVE_DISTRIBUTION, + + /** + * Some frameworks implemented DISTRIBUTION as a summary of observations (usually things like + * request durations and response sizes). While it also provides a total count of observations + * and a sum of all observed values, it calculates configurable quantiles over a sliding time + * window. + * + * <p>This is not recommended, since it cannot be aggregated. + * + * @since 0.17 + */ + SUMMARY, + } +} diff --git a/api/src/main/java/io/opencensus/metrics/export/MetricProducer.java b/api/src/main/java/io/opencensus/metrics/export/MetricProducer.java new file mode 100644 index 00000000..739a0a9f --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/MetricProducer.java @@ -0,0 +1,40 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import io.opencensus.common.ExperimentalApi; +import java.util.Collection; + +/** + * A {@link Metric} producer that can be registered for exporting using {@link + * MetricProducerManager}. + * + * <p>All implementation MUST be thread-safe. + * + * @since 0.17 + */ +@ExperimentalApi +public abstract class MetricProducer { + + /** + * Returns a collection of produced {@link Metric}s to be exported. + * + * @return a collection of produced {@link Metric}s to be exported. + * @since 0.17 + */ + public abstract Collection<Metric> getMetrics(); +} diff --git a/api/src/main/java/io/opencensus/metrics/export/MetricProducerManager.java b/api/src/main/java/io/opencensus/metrics/export/MetricProducerManager.java new file mode 100644 index 00000000..304d9294 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/MetricProducerManager.java @@ -0,0 +1,88 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import io.opencensus.common.ExperimentalApi; +import io.opencensus.internal.Utils; +import java.util.Collections; +import java.util.Set; +import javax.annotation.concurrent.ThreadSafe; + +/** + * Keeps a set of {@link MetricProducer} that is used by exporters to determine the metrics that + * need to be exported. + * + * @since 0.17 + */ +@ExperimentalApi +@ThreadSafe +public abstract class MetricProducerManager { + + /** + * Adds the {@link MetricProducer} to the manager if it is not already present. + * + * @param metricProducer the {@code MetricProducer} to be added to the manager. + * @since 0.17 + */ + public abstract void add(MetricProducer metricProducer); + + /** + * Removes the {@link MetricProducer} to the manager if it is present. + * + * @param metricProducer the {@code MetricProducer} to be removed from the manager. + * @since 0.17 + */ + public abstract void remove(MetricProducer metricProducer); + + /** + * Returns all registered {@link MetricProducer}s that should be exported. + * + * <p>This method should be used by any metrics exporter that automatically exports data for + * {@code MetricProducer} registered with the {@code MetricProducerManager}. + * + * @return all registered {@code MetricProducer}s that should be exported. + * @since 0.17 + */ + public abstract Set<MetricProducer> getAllMetricProducer(); + + /** + * Returns a no-op implementation for {@link MetricProducerManager}. + * + * @return a no-op implementation for {@code MetricProducerManager}. + */ + static MetricProducerManager newNoopMetricProducerManager() { + return new NoopMetricProducerManager(); + } + + private static final class NoopMetricProducerManager extends MetricProducerManager { + + @Override + public void add(MetricProducer metricProducer) { + Utils.checkNotNull(metricProducer, "metricProducer"); + } + + @Override + public void remove(MetricProducer metricProducer) { + Utils.checkNotNull(metricProducer, "metricProducer"); + } + + @Override + public Set<MetricProducer> getAllMetricProducer() { + return Collections.emptySet(); + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/export/Point.java b/api/src/main/java/io/opencensus/metrics/export/Point.java new file mode 100644 index 00000000..1f382f9b --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/Point.java @@ -0,0 +1,63 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.common.Timestamp; +import javax.annotation.concurrent.Immutable; + +/** + * A timestamped measurement of a {@code TimeSeries}. + * + * @since 0.17 + */ +@ExperimentalApi +@AutoValue +@Immutable +public abstract class Point { + + Point() {} + + /** + * Creates a {@link Point}. + * + * @param value the {@link Value} of this {@link Point}. + * @param timestamp the {@link Timestamp} when this {@link Point} was recorded. + * @return a {@code Point}. + * @since 0.17 + */ + public static Point create(Value value, Timestamp timestamp) { + return new AutoValue_Point(value, timestamp); + } + + /** + * Returns the {@link Value}. + * + * @return the {@code Value}. + * @since 0.17 + */ + public abstract Value getValue(); + + /** + * Returns the {@link Timestamp} when this {@link Point} was recorded. + * + * @return the {@code Timestamp}. + * @since 0.17 + */ + public abstract Timestamp getTimestamp(); +} diff --git a/api/src/main/java/io/opencensus/metrics/export/Summary.java b/api/src/main/java/io/opencensus/metrics/export/Summary.java new file mode 100644 index 00000000..c82ca961 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/Summary.java @@ -0,0 +1,187 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.internal.Utils; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +/** + * Implementation of the {@link Distribution} as a summary of observations. + * + * <p>This is not recommended, since it cannot be aggregated. + * + * @since 0.17 + */ +@ExperimentalApi +@AutoValue +@Immutable +public abstract class Summary { + Summary() {} + + /** + * Creates a {@link Summary}. + * + * @param count the count of the population values. + * @param sum the sum of the population values. + * @param snapshot bucket boundaries of a histogram. + * @return a {@code Summary} with the given values. + * @since 0.17 + */ + public static Summary create(@Nullable Long count, @Nullable Double sum, Snapshot snapshot) { + checkCountAndSum(count, sum); + Utils.checkNotNull(snapshot, "snapshot"); + return new AutoValue_Summary(count, sum, snapshot); + } + + /** + * Returns the aggregated count. If not available returns {@code null}. + * + * @return the aggregated count. + * @since 0.17 + */ + @Nullable + public abstract Long getCount(); + + /** + * Returns the aggregated sum. If not available returns {@code null}. + * + * @return the aggregated sum. + * @since 0.17 + */ + @Nullable + public abstract Double getSum(); + + /** + * Returns the {@link Snapshot}. + * + * @return the {@code Snapshot}. + * @since 0.17 + */ + public abstract Snapshot getSnapshot(); + + /** + * Represents the summary observation of the recorded events over a sliding time window. + * + * @since 0.17 + */ + @Immutable + @AutoValue + public abstract static class Snapshot { + /** + * Returns the number of values in this {@code Snapshot}. If not available returns {@code null}. + * + * @return the number of values in this {@code Snapshot}. + * @since 0.17 + */ + @Nullable + public abstract Long getCount(); + + /** + * Returns the sum of values in this {@code Snapshot}. If not available returns {@code null}. + * + * @return the sum of values in this {@code Snapshot}. + * @since 0.17 + */ + @Nullable + public abstract Double getSum(); + + /** + * Returns the list of {@code ValueAtPercentile}s in this {@code Snapshot}. + * + * @return the list of {@code ValueAtPercentile}s in this {@code Snapshot}. + * @since 0.17 + */ + public abstract List<ValueAtPercentile> getValueAtPercentiles(); + + /** + * Creates a {@link Snapshot}. + * + * @param count the number of values in this {@code Snapshot}. + * @param sum the number of values in this {@code Snapshot}. + * @param valueAtPercentiles the list of {@code ValueAtPercentile}. + * @return a {@code Snapshot} with the given values. + * @since 0.17 + */ + public static Snapshot create( + @Nullable Long count, @Nullable Double sum, List<ValueAtPercentile> valueAtPercentiles) { + checkCountAndSum(count, sum); + Utils.checkNotNull(valueAtPercentiles, "valueAtPercentiles"); + Utils.checkListElementNotNull(valueAtPercentiles, "value in valueAtPercentiles"); + return new AutoValue_Summary_Snapshot( + count, + sum, + Collections.unmodifiableList(new ArrayList<ValueAtPercentile>(valueAtPercentiles))); + } + + /** + * Represents the value at a given percentile of a distribution. + * + * @since 0.17 + */ + @Immutable + @AutoValue + public abstract static class ValueAtPercentile { + /** + * Returns the percentile in this {@code ValueAtPercentile}. + * + * <p>Must be in the interval (0.0, 100.0]. + * + * @return the percentile in this {@code ValueAtPercentile}. + * @since 0.17 + */ + public abstract double getPercentile(); + + /** + * Returns the value in this {@code ValueAtPercentile}. + * + * @return the value in this {@code ValueAtPercentile}. + * @since 0.17 + */ + public abstract double getValue(); + + /** + * Creates a {@link ValueAtPercentile}. + * + * @param percentile the percentile in this {@code ValueAtPercentile}. + * @param value the value in this {@code ValueAtPercentile}. + * @return a {@code ValueAtPercentile} with the given values. + * @since 0.17 + */ + public static ValueAtPercentile create(double percentile, double value) { + Utils.checkArgument( + 0 < percentile && percentile <= 100.0, + "percentile must be in the interval (0.0, 100.0]"); + Utils.checkArgument(value >= 0, "value must be non-negative"); + return new AutoValue_Summary_Snapshot_ValueAtPercentile(percentile, value); + } + } + } + + private static void checkCountAndSum(@Nullable Long count, @Nullable Double sum) { + Utils.checkArgument(count == null || count >= 0, "count must be non-negative."); + Utils.checkArgument(sum == null || sum >= 0, "sum must be non-negative."); + if (count != null && count == 0) { + Utils.checkArgument(sum == null || sum == 0, "sum must be 0 if count is 0."); + } + } +} diff --git a/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java b/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java new file mode 100644 index 00000000..bfaeae98 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/TimeSeries.java @@ -0,0 +1,127 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.common.Timestamp; +import io.opencensus.internal.Utils; +import io.opencensus.metrics.LabelKey; +import io.opencensus.metrics.LabelValue; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +/** + * A collection of data points that describes the time-varying values of a {@code Metric}. + * + * @since 0.17 + */ +@ExperimentalApi +@Immutable +@AutoValue +public abstract class TimeSeries { + + TimeSeries() {} + + /** + * Creates a {@link TimeSeries}. + * + * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}. + * @param points the data {@code Point}s of this {@code TimeSeries}. + * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null + * for cumulative {@code Point}s. + * @return a {@code TimeSeries}. + * @since 0.17 + */ + public static TimeSeries create( + List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp) { + Utils.checkNotNull(points, "points"); + Utils.checkListElementNotNull(points, "point"); + return createInternal( + labelValues, Collections.unmodifiableList(new ArrayList<Point>(points)), startTimestamp); + } + + /** + * Creates a {@link TimeSeries}. + * + * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}. + * @param point the single data {@code Point} of this {@code TimeSeries}. + * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null + * for cumulative {@code Point}s. + * @return a {@code TimeSeries}. + * @since 0.17 + */ + public static TimeSeries createWithOnePoint( + List<LabelValue> labelValues, Point point, @Nullable Timestamp startTimestamp) { + Utils.checkNotNull(point, "point"); + return createInternal(labelValues, Collections.singletonList(point), startTimestamp); + } + + /** + * Creates a {@link TimeSeries}. + * + * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}. + * @param points the data {@code Point}s of this {@code TimeSeries}. + * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null + * for cumulative {@code Point}s. + * @return a {@code TimeSeries}. + */ + private static TimeSeries createInternal( + List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp) { + // Fail fast on null lists to prevent NullPointerException when copying the lists. + Utils.checkNotNull(labelValues, "labelValues"); + Utils.checkListElementNotNull(labelValues, "labelValue"); + return new AutoValue_TimeSeries( + Collections.unmodifiableList(new ArrayList<LabelValue>(labelValues)), + points, + startTimestamp); + } + + /** + * Returns the set of {@link LabelValue}s that uniquely identify this {@link TimeSeries}. + * + * <p>Apply to all {@link Point}s. + * + * <p>The order of {@link LabelValue}s must match that of {@link LabelKey}s in the {@code + * MetricDescriptor}. + * + * @return the {@code LabelValue}s. + * @since 0.17 + */ + public abstract List<LabelValue> getLabelValues(); + + /** + * Returns the data {@link Point}s of this {@link TimeSeries}. + * + * @return the data {@code Point}s. + * @since 0.17 + */ + public abstract List<Point> getPoints(); + + /** + * Returns the start {@link Timestamp} of this {@link TimeSeries} if the {@link Point}s are + * cumulative, or {@code null} if the {@link Point}s are gauge. + * + * @return the start {@code Timestamp} or {@code null}. + * @since 0.17 + */ + @Nullable + public abstract Timestamp getStartTimestamp(); +} diff --git a/api/src/main/java/io/opencensus/metrics/export/Value.java b/api/src/main/java/io/opencensus/metrics/export/Value.java new file mode 100644 index 00000000..00a939c0 --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/export/Value.java @@ -0,0 +1,246 @@ +/* + * Copyright 2018, OpenCensus 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.opencensus.metrics.export; + +import com.google.auto.value.AutoValue; +import io.opencensus.common.ExperimentalApi; +import io.opencensus.common.Function; +import javax.annotation.concurrent.Immutable; + +/** + * The actual point value for a {@link Point}. + * + * <p>Currently there are three types of {@link Value}: + * + * <ul> + * <li>{@code double} + * <li>{@code long} + * <li>{@link Distribution} + * </ul> + * + * <p>Each {@link Point} contains exactly one of the three {@link Value} types. + * + * @since 0.17 + */ +@ExperimentalApi +@Immutable +public abstract class Value { + + Value() {} + + /** + * Returns a double {@link Value}. + * + * @param value value in double. + * @return a double {@code Value}. + * @since 0.17 + */ + public static Value doubleValue(double value) { + return ValueDouble.create(value); + } + + /** + * Returns a long {@link Value}. + * + * @param value value in long. + * @return a long {@code Value}. + * @since 0.17 + */ + public static Value longValue(long value) { + return ValueLong.create(value); + } + + /** + * Returns a {@link Distribution} {@link Value}. + * + * @param value value in {@link Distribution}. + * @return a {@code Distribution} {@code Value}. + * @since 0.17 + */ + public static Value distributionValue(Distribution value) { + return ValueDistribution.create(value); + } + + /** + * Returns a {@link Summary} {@link Value}. + * + * @param value value in {@link Summary}. + * @return a {@code Summary} {@code Value}. + * @since 0.17 + */ + public static Value summaryValue(Summary value) { + return ValueSummary.create(value); + } + + /** + * Applies the given match function to the underlying data type. + * + * @since 0.17 + */ + public abstract <T> T match( + Function<? super Double, T> doubleFunction, + Function<? super Long, T> longFunction, + Function<? super Distribution, T> distributionFunction, + Function<? super Summary, T> summaryFunction, + Function<? super Value, T> defaultFunction); + + /** A 64-bit double-precision floating-point {@link Value}. */ + @AutoValue + @Immutable + abstract static class ValueDouble extends Value { + + ValueDouble() {} + + @Override + public final <T> T match( + Function<? super Double, T> doubleFunction, + Function<? super Long, T> longFunction, + Function<? super Distribution, T> distributionFunction, + Function<? super Summary, T> summaryFunction, + Function<? super Value, T> defaultFunction) { + return doubleFunction.apply(getValue()); + } + + /** + * Creates a {@link ValueDouble}. + * + * @param value the value in double. + * @return a {@code ValueDouble}. + */ + static ValueDouble create(double value) { + return new AutoValue_Value_ValueDouble(value); + } + + /** + * Returns the double value. + * + * @return the double value. + */ + abstract double getValue(); + } + + /** A 64-bit integer {@link Value}. */ + @AutoValue + @Immutable + abstract static class ValueLong extends Value { + + ValueLong() {} + + @Override + public final <T> T match( + Function<? super Double, T> doubleFunction, + Function<? super Long, T> longFunction, + Function<? super Distribution, T> distributionFunction, + Function<? super Summary, T> summaryFunction, + Function<? super Value, T> defaultFunction) { + return longFunction.apply(getValue()); + } + + /** + * Creates a {@link ValueLong}. + * + * @param value the value in long. + * @return a {@code ValueLong}. + */ + static ValueLong create(long value) { + return new AutoValue_Value_ValueLong(value); + } + + /** + * Returns the long value. + * + * @return the long value. + */ + abstract long getValue(); + } + + /** + * {@link ValueDistribution} contains summary statistics for a population of values. It optionally + * contains a histogram representing the distribution of those values across a set of buckets. + */ + @AutoValue + @Immutable + abstract static class ValueDistribution extends Value { + + ValueDistribution() {} + + @Override + public final <T> T match( + Function<? super Double, T> doubleFunction, + Function<? super Long, T> longFunction, + Function<? super Distribution, T> distributionFunction, + Function<? super Summary, T> summaryFunction, + Function<? super Value, T> defaultFunction) { + return distributionFunction.apply(getValue()); + } + + /** + * Creates a {@link ValueDistribution}. + * + * @param value the {@link Distribution} value. + * @return a {@code ValueDistribution}. + */ + static ValueDistribution create(Distribution value) { + return new AutoValue_Value_ValueDistribution(value); + } + + /** + * Returns the {@link Distribution} value. + * + * @return the {@code Distribution} value. + */ + abstract Distribution getValue(); + } + + /** + * {@link ValueSummary} contains a snapshot representing values calculated over an arbitrary time + * window. + */ + @AutoValue + @Immutable + abstract static class ValueSummary extends Value { + + ValueSummary() {} + + @Override + public final <T> T match( + Function<? super Double, T> doubleFunction, + Function<? super Long, T> longFunction, + Function<? super Distribution, T> distributionFunction, + Function<? super Summary, T> summaryFunction, + Function<? super Value, T> defaultFunction) { + return summaryFunction.apply(getValue()); + } + + /** + * Creates a {@link ValueSummary}. + * + * @param value the {@link Summary} value. + * @return a {@code ValueSummary}. + */ + static ValueSummary create(Summary value) { + return new AutoValue_Value_ValueSummary(value); + } + + /** + * Returns the {@link Summary} value. + * + * @return the {@code Summary} value. + */ + abstract Summary getValue(); + } +} diff --git a/api/src/main/java/io/opencensus/metrics/package-info.java b/api/src/main/java/io/opencensus/metrics/package-info.java new file mode 100644 index 00000000..33eadf0c --- /dev/null +++ b/api/src/main/java/io/opencensus/metrics/package-info.java @@ -0,0 +1,32 @@ +/* + * Copyright 2018, OpenCensus 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. + */ + +/** + * This package describes the Metrics data model. Metrics are a data model for what stats exporters + * take as input. This data model may eventually become the wire format for metrics. + * + * <p>WARNING: Currently all the public classes under this package are marked as {@link + * io.opencensus.common.ExperimentalApi}. The classes and APIs under {@link io.opencensus.metrics} + * are likely to get backwards-incompatible updates in the future. DO NOT USE except for + * experimental purposes. + * + * <p>Please see + * https://github.com/census-instrumentation/opencensus-specs/blob/master/stats/Metrics.md and + * https://github.com/census-instrumentation/opencensus-proto/blob/master/opencensus/proto/stats/metrics/metrics.proto + * for more details. + */ +@io.opencensus.common.ExperimentalApi +package io.opencensus.metrics; |