• 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  * Long Cumulative metric, to report instantaneous measurement of an int64 value. Cumulative values
25  * 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  *   LongCumulative cumulative = metricRegistry.addLongCumulative(
37  *     "processed_jobs", "Processed jobs", "1", labelKeys);
38  *
39  *   // It is recommended to keep a reference of a point for manual operations.
40  *   LongPoint 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  *   LongCumulative cumulative = metricRegistry.addLongCumulative(
61  *     "processed_jobs", "Processed jobs", "1", labelKeys);
62  *
63  *   // It is recommended to keep a reference of a point for manual operations.
64  *   LongPoint 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 LongCumulative {
78 
79   /**
80    * Creates a {@code TimeSeries} and returns a {@code LongPoint} if the specified {@code
81    * labelValues} is not already associated with this cumulative, else returns an existing {@code
82    * LongPoint}.
83    *
84    * <p>It is recommended to keep a reference to the LongPoint instead of always calling this method
85    * 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#addLongCumulative}.
89    * @return a {@code LongPoint} 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 passed to {@link MetricRegistry#addLongCumulative}.
94    * @since 0.21
95    */
getOrCreateTimeSeries(List<LabelValue> labelValues)96   public abstract LongPoint getOrCreateTimeSeries(List<LabelValue> labelValues);
97 
98   /**
99    * Returns a {@code LongPoint} for a cumulative with all labels not set, or default labels.
100    *
101    * @return a {@code LongPoint} for a cumulative with all labels not set, or default labels.
102    * @since 0.21
103    */
getDefaultTimeSeries()104   public abstract LongPoint getDefaultTimeSeries();
105 
106   /**
107    * Removes the {@code TimeSeries} from the cumulative metric, if it is present. i.e. references to
108    * previous {@code LongPoint} 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.
112    * @since 0.21
113    */
removeTimeSeries(List<LabelValue> labelValues)114   public abstract void removeTimeSeries(List<LabelValue> labelValues);
115 
116   /**
117    * Removes all {@code TimeSeries} from the cumulative metric. i.e. references to all previous
118    * {@code LongPoint} objects are invalid (not part of the metric).
119    *
120    * @since 0.21
121    */
clear()122   public abstract void clear();
123 
124   /**
125    * Returns the no-op implementation of the {@code LongCumulative}.
126    *
127    * @return the no-op implementation of the {@code LongCumulative}.
128    * @since 0.21
129    */
newNoopLongCumulative( String name, String description, String unit, List<LabelKey> labelKeys)130   static LongCumulative newNoopLongCumulative(
131       String name, String description, String unit, List<LabelKey> labelKeys) {
132     return NoopLongCumulative.create(name, description, unit, labelKeys);
133   }
134 
135   /**
136    * The value of a single point in the Cumulative.TimeSeries.
137    *
138    * @since 0.21
139    */
140   public abstract static class LongPoint {
141 
142     /**
143      * Adds the given value to the current value. The values cannot be negative.
144      *
145      * @param delta the value to add
146      * @since 0.21
147      */
add(long delta)148     public abstract void add(long delta);
149   }
150 
151   /** No-op implementations of LongCumulative class. */
152   private static final class NoopLongCumulative extends LongCumulative {
153     private final int labelKeysSize;
154 
create( String name, String description, String unit, List<LabelKey> labelKeys)155     static NoopLongCumulative create(
156         String name, String description, String unit, List<LabelKey> labelKeys) {
157       return new NoopLongCumulative(name, description, unit, labelKeys);
158     }
159 
160     /** Creates a new {@code NoopLongPoint}. */
NoopLongCumulative(String name, String description, String unit, List<LabelKey> labelKeys)161     NoopLongCumulative(String name, String description, String unit, List<LabelKey> labelKeys) {
162       labelKeysSize = labelKeys.size();
163     }
164 
165     @Override
getOrCreateTimeSeries(List<LabelValue> labelValues)166     public NoopLongPoint getOrCreateTimeSeries(List<LabelValue> labelValues) {
167       Utils.checkListElementNotNull(Utils.checkNotNull(labelValues, "labelValues"), "labelValue");
168       Utils.checkArgument(
169           labelKeysSize == labelValues.size(), "Label Keys and Label Values don't have same size.");
170       return NoopLongPoint.INSTANCE;
171     }
172 
173     @Override
getDefaultTimeSeries()174     public NoopLongPoint getDefaultTimeSeries() {
175       return NoopLongPoint.INSTANCE;
176     }
177 
178     @Override
removeTimeSeries(List<LabelValue> labelValues)179     public void removeTimeSeries(List<LabelValue> labelValues) {
180       Utils.checkNotNull(labelValues, "labelValues");
181     }
182 
183     @Override
clear()184     public void clear() {}
185 
186     /** No-op implementations of LongPoint class. */
187     private static final class NoopLongPoint extends LongPoint {
188       private static final NoopLongPoint INSTANCE = new NoopLongPoint();
189 
NoopLongPoint()190       private NoopLongPoint() {}
191 
192       @Override
add(long delta)193       public void add(long delta) {}
194     }
195   }
196 }
197