• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019, 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  * Double Cumulative metric, to report instantaneous measurement of a double value. Cumulative
25  * values can go up or stay the same, but can never go down. Cumulative values cannot be negative.
26  *
27  * <p>Example 1: Create a Cumulative 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  *   DoubleCumulative cumulative = metricRegistry.addDoubleCumulative("processed_jobs",
37  *                       "Processed jobs", "1", labelKeys);
38  *
39  *   // It is recommended to keep a reference of a point for manual operations.
40  *   DoublePoint defaultPoint = cumulative.getDefaultTimeSeries();
41  *
42  *   void doWork() {
43  *      // Your code here.
44  *      defaultPoint.add(10);
45  *   }
46  *
47  * }
48  * }</pre>
49  *
50  * <p>Example 2: You can also use labels(keys and values) to track different types of metric.
51  *
52  * <pre>{@code
53  * class YourClass {
54  *
55  *   private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry();
56  *
57  *   List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
58  *   List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
59  *
60  *   DoubleCumulative cumulative = metricRegistry.addDoubleCumulative("processed_jobs",
61  *                       "Processed jobs", "1", labelKeys);
62  *
63  *   // It is recommended to keep a reference of a point for manual operations.
64  *   DoublePoint inboundPoint = cumulative.getOrCreateTimeSeries(labelValues);
65  *
66  *   void doSomeWork() {
67  *      // Your code here.
68  *      inboundPoint.set(15);
69  *   }
70  *
71  * }
72  * }</pre>
73  *
74  * @since 0.21
75  */
76 @ThreadSafe
77 public abstract class DoubleCumulative {
78 
79   /**
80    * Creates a {@code TimeSeries} and returns a {@code DoublePoint} if the specified {@code
81    * labelValues} is not already associated with this cumulative, else returns an existing {@code
82    * DoublePoint}.
83    *
84    * <p>It is recommended to keep a reference to the DoublePoint instead of always calling this
85    * method for manual operations.
86    *
87    * @param labelValues the list of label values. The number of label values must be the same to
88    *     that of the label keys passed to {@link MetricRegistry#addDoubleCumulative}.
89    * @return a {@code DoublePoint} the value of single cumulative.
90    * @throws NullPointerException if {@code labelValues} is null OR any element of {@code
91    *     labelValues} is null.
92    * @throws IllegalArgumentException if number of {@code labelValues}s are not equal to the label
93    *     keys.
94    * @since 0.21
95    */
getOrCreateTimeSeries(List<LabelValue> labelValues)96   public abstract DoublePoint getOrCreateTimeSeries(List<LabelValue> labelValues);
97 
98   /**
99    * Returns a {@code DoublePoint} for a cumulative with all labels not set, or default labels.
100    *
101    * @return a {@code DoublePoint} for a cumulative with all labels not set, or default labels.
102    * @since 0.21
103    */
getDefaultTimeSeries()104   public abstract DoublePoint getDefaultTimeSeries();
105 
106   /**
107    * Removes the {@code TimeSeries} from the cumulative metric, if it is present. i.e. references to
108    * previous {@code DoublePoint} objects are invalid (not part of the metric).
109    *
110    * @param labelValues the list of label values.
111    * @throws NullPointerException if {@code labelValues} is null or any element of {@code
112    *     labelValues} is null.
113    * @since 0.21
114    */
removeTimeSeries(List<LabelValue> labelValues)115   public abstract void removeTimeSeries(List<LabelValue> labelValues);
116 
117   /**
118    * Removes all {@code TimeSeries} from the cumulative metric. i.e. references to all previous
119    * {@code DoublePoint} objects are invalid (not part of the metric).
120    *
121    * @since 0.21
122    */
clear()123   public abstract void clear();
124 
125   /**
126    * Returns the no-op implementation of the {@code DoubleCumulative}.
127    *
128    * @return the no-op implementation of the {@code DoubleCumulative}.
129    * @since 0.21
130    */
newNoopDoubleCumulative( String name, String description, String unit, List<LabelKey> labelKeys)131   static DoubleCumulative newNoopDoubleCumulative(
132       String name, String description, String unit, List<LabelKey> labelKeys) {
133     return NoopDoubleCumulative.create(name, description, unit, labelKeys);
134   }
135 
136   /**
137    * The value of a single point in the Cumulative.TimeSeries.
138    *
139    * @since 0.21
140    */
141   public abstract static class DoublePoint {
142 
143     /**
144      * Adds the given value to the current value. The values cannot be negative.
145      *
146      * @param delta the value to add
147      * @since 0.21
148      */
add(double delta)149     public abstract void add(double delta);
150   }
151 
152   /** No-op implementations of DoubleCumulative class. */
153   private static final class NoopDoubleCumulative extends DoubleCumulative {
154     private final int labelKeysSize;
155 
create( String name, String description, String unit, List<LabelKey> labelKeys)156     static NoopDoubleCumulative create(
157         String name, String description, String unit, List<LabelKey> labelKeys) {
158       return new NoopDoubleCumulative(name, description, unit, labelKeys);
159     }
160 
161     /** Creates a new {@code NoopDoublePoint}. */
NoopDoubleCumulative(String name, String description, String unit, List<LabelKey> labelKeys)162     NoopDoubleCumulative(String name, String description, String unit, List<LabelKey> labelKeys) {
163       Utils.checkNotNull(name, "name");
164       Utils.checkNotNull(description, "description");
165       Utils.checkNotNull(unit, "unit");
166       Utils.checkListElementNotNull(Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey");
167       labelKeysSize = labelKeys.size();
168     }
169 
170     @Override
getOrCreateTimeSeries(List<LabelValue> labelValues)171     public NoopDoublePoint getOrCreateTimeSeries(List<LabelValue> labelValues) {
172       Utils.checkListElementNotNull(Utils.checkNotNull(labelValues, "labelValues"), "labelValue");
173       Utils.checkArgument(
174           labelKeysSize == labelValues.size(), "Label Keys and Label Values don't have same size.");
175       return NoopDoublePoint.INSTANCE;
176     }
177 
178     @Override
getDefaultTimeSeries()179     public NoopDoublePoint getDefaultTimeSeries() {
180       return NoopDoublePoint.INSTANCE;
181     }
182 
183     @Override
removeTimeSeries(List<LabelValue> labelValues)184     public void removeTimeSeries(List<LabelValue> labelValues) {
185       Utils.checkNotNull(labelValues, "labelValues");
186     }
187 
188     @Override
clear()189     public void clear() {}
190 
191     /** No-op implementations of DoublePoint class. */
192     private static final class NoopDoublePoint extends DoublePoint {
193       private static final NoopDoublePoint INSTANCE = new NoopDoublePoint();
194 
NoopDoublePoint()195       private NoopDoublePoint() {}
196 
197       @Override
add(double delta)198       public void add(double delta) {}
199     }
200   }
201 }
202