1 //
2 //
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/cpp/ext/filters/census/measures.h"
22 
23 #include "opencensus/stats/stats.h"
24 
25 #include <grpcpp/opencensus.h>
26 
27 #include "src/cpp/ext/filters/census/grpc_plugin.h"
28 
29 namespace grpc {
30 
31 using ::opencensus::stats::MeasureDouble;
32 using ::opencensus::stats::MeasureInt64;
33 
34 // These measure definitions should be kept in sync across opencensus
35 // implementations--see
36 // https://github.com/census-instrumentation/opencensus-java/blob/master/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcMeasureConstants.java.
37 
38 namespace {
39 
40 // Unit constants
41 constexpr char kUnitBytes[] = "By";
42 constexpr char kUnitMilliseconds[] = "ms";
43 constexpr char kCount[] = "1";
44 
45 }  // namespace
46 
47 // Client
RpcClientSentBytesPerRpc()48 MeasureDouble RpcClientSentBytesPerRpc() {
49   static const auto measure = MeasureDouble::Register(
50       experimental::kRpcClientSentBytesPerRpcMeasureName,
51       "Total bytes sent across all request messages per RPC", kUnitBytes);
52   return measure;
53 }
54 
RpcClientReceivedBytesPerRpc()55 MeasureDouble RpcClientReceivedBytesPerRpc() {
56   static const auto measure = MeasureDouble::Register(
57       experimental::kRpcClientReceivedBytesPerRpcMeasureName,
58       "Total bytes received across all response messages per RPC", kUnitBytes);
59   return measure;
60 }
61 
RpcClientRoundtripLatency()62 MeasureDouble RpcClientRoundtripLatency() {
63   static const auto measure = MeasureDouble::Register(
64       experimental::kRpcClientRoundtripLatencyMeasureName,
65       "Time between first byte of request sent to last byte of response "
66       "received, or terminal error",
67       kUnitMilliseconds);
68   return measure;
69 }
70 
RpcClientServerLatency()71 MeasureDouble RpcClientServerLatency() {
72   static const auto measure = MeasureDouble::Register(
73       experimental::kRpcClientServerLatencyMeasureName,
74       "Time between first byte of request received to last byte of response "
75       "sent, or terminal error (propagated from the server)",
76       kUnitMilliseconds);
77   return measure;
78 }
79 
RpcClientSentMessagesPerRpc()80 MeasureInt64 RpcClientSentMessagesPerRpc() {
81   static const auto measure = MeasureInt64::Register(
82       experimental::kRpcClientSentMessagesPerRpcMeasureName,
83       "Number of messages sent per RPC", kCount);
84   return measure;
85 }
86 
RpcClientReceivedMessagesPerRpc()87 MeasureInt64 RpcClientReceivedMessagesPerRpc() {
88   static const auto measure = MeasureInt64::Register(
89       experimental::kRpcClientReceivedMessagesPerRpcMeasureName,
90       "Number of messages received per RPC", kCount);
91   return measure;
92 }
93 
RpcClientStartedRpcs()94 MeasureInt64 RpcClientStartedRpcs() {
95   static const auto measure =
96       MeasureInt64::Register(experimental::kRpcClientStartedRpcsMeasureName,
97                              "The total number of client RPCs ever opened, "
98                              "including those that have not been completed.",
99                              kCount);
100   return measure;
101 }
102 
RpcClientTransportLatency()103 MeasureDouble RpcClientTransportLatency() {
104   static const auto measure = MeasureDouble::Register(
105       experimental::kRpcClientTransportLatencyMeasureName,
106       "Time between first byte of request sent to last byte of response "
107       "received on the transport",
108       kUnitMilliseconds);
109   return measure;
110 }
111 
112 // Client per-overall-client-call measures
RpcClientRetriesPerCall()113 MeasureInt64 RpcClientRetriesPerCall() {
114   static const auto measure =
115       MeasureInt64::Register(experimental::kRpcClientRetriesPerCallMeasureName,
116                              "Number of retry or hedging attempts excluding "
117                              "transparent retries made during the client call",
118                              kCount);
119   return measure;
120 }
121 
RpcClientTransparentRetriesPerCall()122 MeasureInt64 RpcClientTransparentRetriesPerCall() {
123   static const auto measure = MeasureInt64::Register(
124       experimental::kRpcClientTransparentRetriesPerCallMeasureName,
125       "Number of transparent retries made during the client call", kCount);
126   return measure;
127 }
128 
RpcClientRetryDelayPerCall()129 MeasureDouble RpcClientRetryDelayPerCall() {
130   static const auto measure = MeasureDouble::Register(
131       experimental::kRpcClientRetryDelayPerCallMeasureName,
132       "Total time of delay while there is no active "
133       "attempt during the client call",
134       kUnitMilliseconds);
135   return measure;
136 }
137 
138 // Server
RpcServerSentBytesPerRpc()139 MeasureDouble RpcServerSentBytesPerRpc() {
140   static const auto measure = MeasureDouble::Register(
141       experimental::kRpcServerSentBytesPerRpcMeasureName,
142       "Total bytes sent across all messages per RPC", kUnitBytes);
143   return measure;
144 }
145 
RpcServerReceivedBytesPerRpc()146 MeasureDouble RpcServerReceivedBytesPerRpc() {
147   static const auto measure = MeasureDouble::Register(
148       experimental::kRpcServerReceivedBytesPerRpcMeasureName,
149       "Total bytes received across all messages per RPC", kUnitBytes);
150   return measure;
151 }
152 
RpcServerServerLatency()153 MeasureDouble RpcServerServerLatency() {
154   static const auto measure = MeasureDouble::Register(
155       experimental::kRpcServerServerLatencyMeasureName,
156       "Time between first byte of request received to last byte of response "
157       "sent, or terminal error",
158       kUnitMilliseconds);
159   return measure;
160 }
161 
RpcServerStartedRpcs()162 MeasureInt64 RpcServerStartedRpcs() {
163   static const auto measure =
164       MeasureInt64::Register(experimental::kRpcServerStartedRpcsMeasureName,
165                              "The total number of server RPCs ever opened, "
166                              "including those that have not been completed.",
167                              kCount);
168   return measure;
169 }
170 
RpcServerSentMessagesPerRpc()171 MeasureInt64 RpcServerSentMessagesPerRpc() {
172   static const auto measure = MeasureInt64::Register(
173       experimental::kRpcServerSentMessagesPerRpcMeasureName,
174       "Number of messages sent per RPC", kCount);
175   return measure;
176 }
177 
RpcServerReceivedMessagesPerRpc()178 MeasureInt64 RpcServerReceivedMessagesPerRpc() {
179   static const auto measure = MeasureInt64::Register(
180       experimental::kRpcServerReceivedMessagesPerRpcMeasureName,
181       "Number of messages received per RPC", kCount);
182   return measure;
183 }
184 
185 namespace internal {
186 
RpcClientApiLatency()187 MeasureDouble RpcClientApiLatency() {
188   static const auto measure = MeasureDouble::Register(
189       kRpcClientApiLatencyMeasureName,
190       "End-to-end time taken to complete an RPC", kUnitMilliseconds);
191   return measure;
192 }
193 
194 }  // namespace internal
195 
196 }  // namespace grpc
197