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