1 /* 2 * Copyright 2018, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.metrics; 18 19 import io.opencensus.internal.Utils; 20 import java.util.List; 21 import javax.annotation.concurrent.ThreadSafe; 22 23 /** 24 * Long Gauge metric, to report instantaneous measurement of an int64 value. Gauges can go both up 25 * and down. The gauges values can be negative. 26 * 27 * <p>Example 1: Create a Gauge with default labels. 28 * 29 * <pre>{@code 30 * class YourClass { 31 * 32 * private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); 33 * 34 * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); 35 * 36 * LongGauge gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys); 37 * 38 * // It is recommended to keep a reference of a point for manual operations. 39 * LongPoint defaultPoint = gauge.getDefaultTimeSeries(); 40 * 41 * void doWork() { 42 * // Your code here. 43 * defaultPoint.add(10); 44 * } 45 * 46 * } 47 * }</pre> 48 * 49 * <p>Example 2: You can also use labels(keys and values) to track different types of metric. 50 * 51 * <pre>{@code 52 * class YourClass { 53 * 54 * private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry(); 55 * 56 * List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc")); 57 * List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound")); 58 * 59 * LongGauge gauge = metricRegistry.addLongGauge("queue_size", "Pending jobs", "1", labelKeys); 60 * 61 * // It is recommended to keep a reference of a point for manual operations. 62 * LongPoint inboundPoint = gauge.getOrCreateTimeSeries(labelValues); 63 * 64 * void doSomeWork() { 65 * // Your code here. 66 * inboundPoint.set(15); 67 * } 68 * 69 * } 70 * }</pre> 71 * 72 * @since 0.17 73 */ 74 @ThreadSafe 75 public abstract class LongGauge { 76 77 /** 78 * Creates a {@code TimeSeries} and returns a {@code LongPoint} if the specified {@code 79 * labelValues} is not already associated with this gauge, else returns an existing {@code 80 * LongPoint}. 81 * 82 * <p>It is recommended to keep a reference to the LongPoint instead of always calling this method 83 * for manual operations. 84 * 85 * @param labelValues the list of label values. The number of label values must be the same to 86 * that of the label keys passed to {@link MetricRegistry#addLongGauge}. 87 * @return a {@code LongPoint} the value of single gauge. 88 * @throws NullPointerException if {@code labelValues} is null OR any element of {@code 89 * labelValues} is null. 90 * @throws IllegalArgumentException if number of {@code labelValues}s are not equal to the label 91 * keys passed to {@link MetricRegistry#addLongGauge}. 92 * @since 0.17 93 */ getOrCreateTimeSeries(List<LabelValue> labelValues)94 public abstract LongPoint getOrCreateTimeSeries(List<LabelValue> labelValues); 95 96 /** 97 * Returns a {@code LongPoint} for a gauge with all labels not set, or default labels. 98 * 99 * @return a {@code LongPoint} for a gauge with all labels not set, or default labels. 100 * @since 0.17 101 */ getDefaultTimeSeries()102 public abstract LongPoint getDefaultTimeSeries(); 103 104 /** 105 * Removes the {@code TimeSeries} from the gauge metric, if it is present. i.e. references to 106 * previous {@code LongPoint} objects are invalid (not part of the metric). 107 * 108 * @param labelValues the list of label values. 109 * @throws NullPointerException if {@code labelValues} is null. 110 * @since 0.17 111 */ removeTimeSeries(List<LabelValue> labelValues)112 public abstract void removeTimeSeries(List<LabelValue> labelValues); 113 114 /** 115 * Removes all {@code TimeSeries} from the gauge metric. i.e. references to all previous {@code 116 * LongPoint} objects are invalid (not part of the metric). 117 * 118 * @since 0.17 119 */ clear()120 public abstract void clear(); 121 122 /** 123 * Returns the no-op implementation of the {@code LongGauge}. 124 * 125 * @return the no-op implementation of the {@code LongGauge}. 126 * @since 0.17 127 */ newNoopLongGauge( String name, String description, String unit, List<LabelKey> labelKeys)128 static LongGauge newNoopLongGauge( 129 String name, String description, String unit, List<LabelKey> labelKeys) { 130 return NoopLongGauge.create(name, description, unit, labelKeys); 131 } 132 133 /** 134 * The value of a single point in the Gauge.TimeSeries. 135 * 136 * @since 0.17 137 */ 138 public abstract static class LongPoint { 139 140 /** 141 * Adds the given value to the current value. The values can be negative. 142 * 143 * @param amt the value to add 144 * @since 0.17 145 */ add(long amt)146 public abstract void add(long amt); 147 148 /** 149 * Sets the given value. 150 * 151 * @param val the new value. 152 * @since 0.17 153 */ set(long val)154 public abstract void set(long val); 155 } 156 157 /** No-op implementations of LongGauge class. */ 158 private static final class NoopLongGauge extends LongGauge { 159 private final int labelKeysSize; 160 create( String name, String description, String unit, List<LabelKey> labelKeys)161 static NoopLongGauge create( 162 String name, String description, String unit, List<LabelKey> labelKeys) { 163 return new NoopLongGauge(name, description, unit, labelKeys); 164 } 165 166 /** Creates a new {@code NoopLongPoint}. */ NoopLongGauge(String name, String description, String unit, List<LabelKey> labelKeys)167 NoopLongGauge(String name, String description, String unit, List<LabelKey> labelKeys) { 168 labelKeysSize = labelKeys.size(); 169 } 170 171 @Override getOrCreateTimeSeries(List<LabelValue> labelValues)172 public NoopLongPoint getOrCreateTimeSeries(List<LabelValue> labelValues) { 173 Utils.checkListElementNotNull( 174 Utils.checkNotNull(labelValues, "labelValues"), "labelValue element should not be null."); 175 Utils.checkArgument(labelKeysSize == labelValues.size(), "Incorrect number of labels."); 176 return NoopLongPoint.INSTANCE; 177 } 178 179 @Override getDefaultTimeSeries()180 public NoopLongPoint getDefaultTimeSeries() { 181 return NoopLongPoint.INSTANCE; 182 } 183 184 @Override removeTimeSeries(List<LabelValue> labelValues)185 public void removeTimeSeries(List<LabelValue> labelValues) { 186 Utils.checkNotNull(labelValues, "labelValues"); 187 } 188 189 @Override clear()190 public void clear() {} 191 192 /** No-op implementations of LongPoint class. */ 193 private static final class NoopLongPoint extends LongPoint { 194 private static final NoopLongPoint INSTANCE = new NoopLongPoint(); 195 NoopLongPoint()196 private NoopLongPoint() {} 197 198 @Override add(long amt)199 public void add(long amt) {} 200 201 @Override set(long val)202 public void set(long val) {} 203 } 204 } 205 } 206