• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.export;
18 
19 import com.google.auto.value.AutoValue;
20 import io.opencensus.common.ExperimentalApi;
21 import io.opencensus.common.Timestamp;
22 import io.opencensus.internal.Utils;
23 import io.opencensus.metrics.LabelKey;
24 import io.opencensus.metrics.LabelValue;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import javax.annotation.Nullable;
29 import javax.annotation.concurrent.Immutable;
30 
31 /**
32  * A collection of data points that describes the time-varying values of a {@code Metric}.
33  *
34  * @since 0.17
35  */
36 @ExperimentalApi
37 @Immutable
38 @AutoValue
39 public abstract class TimeSeries {
40 
TimeSeries()41   TimeSeries() {}
42 
43   /**
44    * Creates a {@link TimeSeries}.
45    *
46    * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}.
47    * @param points the data {@code Point}s of this {@code TimeSeries}.
48    * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null
49    *     for cumulative {@code Point}s.
50    * @return a {@code TimeSeries}.
51    * @since 0.17
52    */
create( List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp)53   public static TimeSeries create(
54       List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp) {
55     Utils.checkListElementNotNull(Utils.checkNotNull(points, "points"), "point");
56     return createInternal(
57         labelValues, Collections.unmodifiableList(new ArrayList<Point>(points)), startTimestamp);
58   }
59 
60   /**
61    * Creates a {@link TimeSeries} with empty(or no) points.
62    *
63    * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}.
64    * @return a {@code TimeSeries}.
65    * @since 0.17
66    */
create(List<LabelValue> labelValues)67   public static TimeSeries create(List<LabelValue> labelValues) {
68     return createInternal(labelValues, Collections.<Point>emptyList(), null);
69   }
70 
71   /**
72    * Creates a {@link TimeSeries}.
73    *
74    * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}.
75    * @param point the single data {@code Point} of this {@code TimeSeries}.
76    * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null
77    *     for cumulative {@code Point}s.
78    * @return a {@code TimeSeries}.
79    * @since 0.17
80    */
createWithOnePoint( List<LabelValue> labelValues, Point point, @Nullable Timestamp startTimestamp)81   public static TimeSeries createWithOnePoint(
82       List<LabelValue> labelValues, Point point, @Nullable Timestamp startTimestamp) {
83     Utils.checkNotNull(point, "point");
84     return createInternal(labelValues, Collections.singletonList(point), startTimestamp);
85   }
86 
87   /**
88    * Sets the {@code Point} of the {@link TimeSeries}.
89    *
90    * @param point the single data {@code Point} of this {@code TimeSeries}.
91    * @return a {@code TimeSeries}.
92    * @since 0.17
93    */
setPoint(Point point)94   public TimeSeries setPoint(Point point) {
95     Utils.checkNotNull(point, "point");
96     return new AutoValue_TimeSeries(getLabelValues(), Collections.singletonList(point), null);
97   }
98 
99   /**
100    * Creates a {@link TimeSeries}.
101    *
102    * @param labelValues the {@code LabelValue}s that uniquely identify this {@code TimeSeries}.
103    * @param points the data {@code Point}s of this {@code TimeSeries}.
104    * @param startTimestamp the start {@code Timestamp} of this {@code TimeSeries}. Must be non-null
105    *     for cumulative {@code Point}s.
106    * @return a {@code TimeSeries}.
107    */
createInternal( List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp)108   private static TimeSeries createInternal(
109       List<LabelValue> labelValues, List<Point> points, @Nullable Timestamp startTimestamp) {
110     // Fail fast on null lists to prevent NullPointerException when copying the lists.
111     Utils.checkListElementNotNull(Utils.checkNotNull(labelValues, "labelValues"), "labelValue");
112     return new AutoValue_TimeSeries(
113         Collections.unmodifiableList(new ArrayList<LabelValue>(labelValues)),
114         points,
115         startTimestamp);
116   }
117 
118   /**
119    * Returns the set of {@link LabelValue}s that uniquely identify this {@link TimeSeries}.
120    *
121    * <p>Apply to all {@link Point}s.
122    *
123    * <p>The order of {@link LabelValue}s must match that of {@link LabelKey}s in the {@code
124    * MetricDescriptor}.
125    *
126    * @return the {@code LabelValue}s.
127    * @since 0.17
128    */
getLabelValues()129   public abstract List<LabelValue> getLabelValues();
130 
131   /**
132    * Returns the data {@link Point}s of this {@link TimeSeries}.
133    *
134    * @return the data {@code Point}s.
135    * @since 0.17
136    */
getPoints()137   public abstract List<Point> getPoints();
138 
139   /**
140    * Returns the start {@link Timestamp} of this {@link TimeSeries} if the {@link Point}s are
141    * cumulative, or {@code null} if the {@link Point}s are gauge.
142    *
143    * @return the start {@code Timestamp} or {@code null}.
144    * @since 0.17
145    */
146   @Nullable
getStartTimestamp()147   public abstract Timestamp getStartTimestamp();
148 }
149