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.common;
18 
19 import com.google.auto.value.AutoValue;
20 import javax.annotation.concurrent.Immutable;
21 
22 /**
23  * A representation of stats measured on the server side.
24  *
25  * @since 0.16
26  */
27 @Immutable
28 @AutoValue
29 public abstract class ServerStats {
30 
ServerStats()31   ServerStats() {}
32 
33   /**
34    * Returns Load Balancer latency, a latency observed at Load Balancer.
35    *
36    * @return Load Balancer latency in nanoseconds.
37    * @since 0.16
38    */
getLbLatencyNs()39   public abstract long getLbLatencyNs();
40 
41   /**
42    * Returns Service latency, a latency observed at Server.
43    *
44    * @return Service latency in nanoseconds.
45    * @since 0.16
46    */
getServiceLatencyNs()47   public abstract long getServiceLatencyNs();
48 
49   /**
50    * Returns Trace options, a set of bits indicating properties of trace.
51    *
52    * @return Trace options a set of bits indicating properties of trace.
53    * @since 0.16
54    */
getTraceOption()55   public abstract byte getTraceOption();
56 
57   /**
58    * Creates new {@link ServerStats} from specified parameters.
59    *
60    * @param lbLatencyNs Represents request processing latency observed on Load Balancer. It is
61    *     measured in nanoseconds. Must not be less than 0. Value of 0 represents that the latency is
62    *     not measured.
63    * @param serviceLatencyNs Represents request processing latency observed on Server. It is
64    *     measured in nanoseconds. Must not be less than 0. Value of 0 represents that the latency is
65    *     not measured.
66    * @param traceOption Represents set of bits to indicate properties of trace. Currently it used
67    *     only the least signification bit to represent sampling of the request on the server side.
68    *     Other bits are ignored.
69    * @return new {@code ServerStats} with specified fields.
70    * @throws IllegalArgumentException if the arguments are out of range.
71    * @since 0.16
72    */
create(long lbLatencyNs, long serviceLatencyNs, byte traceOption)73   public static ServerStats create(long lbLatencyNs, long serviceLatencyNs, byte traceOption) {
74 
75     if (lbLatencyNs < 0) {
76       throw new IllegalArgumentException("'getLbLatencyNs' is less than zero: " + lbLatencyNs);
77     }
78 
79     if (serviceLatencyNs < 0) {
80       throw new IllegalArgumentException(
81           "'getServiceLatencyNs' is less than zero: " + serviceLatencyNs);
82     }
83 
84     return new AutoValue_ServerStats(lbLatencyNs, serviceLatencyNs, traceOption);
85   }
86 }
87