• 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 <grpc/support/port_platform.h>
20 
21 #include "src/cpp/ext/filters/census/grpc_plugin.h"
22 
23 #include "absl/time/time.h"
24 #include "opencensus/stats/internal/aggregation_window.h"
25 #include "opencensus/stats/internal/set_aggregation_window.h"
26 #include "opencensus/stats/stats.h"
27 
28 namespace grpc {
29 
30 using ::opencensus::stats::Aggregation;
31 using ::opencensus::stats::AggregationWindow;
32 using ::opencensus::stats::BucketBoundaries;
33 using ::opencensus::stats::ViewDescriptor;
34 
35 // These measure definitions should be kept in sync across opencensus
36 // implementations.
37 
38 namespace {
39 
BytesDistributionAggregation()40 Aggregation BytesDistributionAggregation() {
41   return Aggregation::Distribution(BucketBoundaries::Explicit(
42       {0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216,
43        67108864, 268435456, 1073741824, 4294967296}));
44 }
45 
MillisDistributionAggregation()46 Aggregation MillisDistributionAggregation() {
47   return Aggregation::Distribution(BucketBoundaries::Explicit(
48       {0,   0.01, 0.05, 0.1,  0.3,   0.6,   0.8,   1,     2,   3,   4,
49        5,   6,    8,    10,   13,    16,    20,    25,    30,  40,  50,
50        65,  80,   100,  130,  160,   200,   250,   300,   400, 500, 650,
51        800, 1000, 2000, 5000, 10000, 20000, 50000, 100000}));
52 }
53 
CountDistributionAggregation()54 Aggregation CountDistributionAggregation() {
55   return Aggregation::Distribution(BucketBoundaries::Exponential(17, 1.0, 2.0));
56 }
57 
MinuteDescriptor()58 ViewDescriptor MinuteDescriptor() {
59   auto descriptor = ViewDescriptor();
60   SetAggregationWindow(AggregationWindow::Interval(absl::Minutes(1)),
61                        &descriptor);
62   return descriptor;
63 }
64 
HourDescriptor()65 ViewDescriptor HourDescriptor() {
66   auto descriptor = ViewDescriptor();
67   SetAggregationWindow(AggregationWindow::Interval(absl::Hours(1)),
68                        &descriptor);
69   return descriptor;
70 }
71 
72 }  // namespace
73 
RegisterOpenCensusViewsForExport()74 void RegisterOpenCensusViewsForExport() {
75   ClientSentMessagesPerRpcCumulative().RegisterForExport();
76   ClientSentBytesPerRpcCumulative().RegisterForExport();
77   ClientReceivedMessagesPerRpcCumulative().RegisterForExport();
78   ClientReceivedBytesPerRpcCumulative().RegisterForExport();
79   ClientRoundtripLatencyCumulative().RegisterForExport();
80   ClientServerLatencyCumulative().RegisterForExport();
81 
82   ServerSentMessagesPerRpcCumulative().RegisterForExport();
83   ServerSentBytesPerRpcCumulative().RegisterForExport();
84   ServerReceivedMessagesPerRpcCumulative().RegisterForExport();
85   ServerReceivedBytesPerRpcCumulative().RegisterForExport();
86   ServerServerLatencyCumulative().RegisterForExport();
87 }
88 
89 // client cumulative
ClientSentBytesPerRpcCumulative()90 const ViewDescriptor& ClientSentBytesPerRpcCumulative() {
91   const static ViewDescriptor descriptor =
92       ViewDescriptor()
93           .set_name("grpc.io/client/sent_bytes_per_rpc/cumulative")
94           .set_measure(kRpcClientSentBytesPerRpcMeasureName)
95           .set_aggregation(BytesDistributionAggregation())
96           .add_column(ClientMethodTagKey());
97   return descriptor;
98 }
99 
ClientReceivedBytesPerRpcCumulative()100 const ViewDescriptor& ClientReceivedBytesPerRpcCumulative() {
101   const static ViewDescriptor descriptor =
102       ViewDescriptor()
103           .set_name("grpc.io/client/received_bytes_per_rpc/cumulative")
104           .set_measure(kRpcClientReceivedBytesPerRpcMeasureName)
105           .set_aggregation(BytesDistributionAggregation())
106           .add_column(ClientMethodTagKey());
107   return descriptor;
108 }
109 
ClientRoundtripLatencyCumulative()110 const ViewDescriptor& ClientRoundtripLatencyCumulative() {
111   const static ViewDescriptor descriptor =
112       ViewDescriptor()
113           .set_name("grpc.io/client/roundtrip_latency/cumulative")
114           .set_measure(kRpcClientRoundtripLatencyMeasureName)
115           .set_aggregation(MillisDistributionAggregation())
116           .add_column(ClientMethodTagKey());
117   return descriptor;
118 }
119 
ClientServerLatencyCumulative()120 const ViewDescriptor& ClientServerLatencyCumulative() {
121   const static ViewDescriptor descriptor =
122       ViewDescriptor()
123           .set_name("grpc.io/client/server_latency/cumulative")
124           .set_measure(kRpcClientServerLatencyMeasureName)
125           .set_aggregation(MillisDistributionAggregation())
126           .add_column(ClientMethodTagKey());
127   return descriptor;
128 }
129 
ClientCompletedRpcsCumulative()130 const ViewDescriptor& ClientCompletedRpcsCumulative() {
131   const static ViewDescriptor descriptor =
132       ViewDescriptor()
133           .set_name("grpc.io/client/completed_rpcs/cumulative")
134           .set_measure(kRpcClientRoundtripLatencyMeasureName)
135           .set_aggregation(Aggregation::Count())
136           .add_column(ClientMethodTagKey())
137           .add_column(ClientStatusTagKey());
138   return descriptor;
139 }
140 
ClientSentMessagesPerRpcCumulative()141 const ViewDescriptor& ClientSentMessagesPerRpcCumulative() {
142   const static ViewDescriptor descriptor =
143       ViewDescriptor()
144           .set_name("grpc.io/client/received_messages_per_rpc/cumulative")
145           .set_measure(kRpcClientSentMessagesPerRpcMeasureName)
146           .set_aggregation(CountDistributionAggregation())
147           .add_column(ClientMethodTagKey());
148   return descriptor;
149 }
150 
ClientReceivedMessagesPerRpcCumulative()151 const ViewDescriptor& ClientReceivedMessagesPerRpcCumulative() {
152   const static ViewDescriptor descriptor =
153       ViewDescriptor()
154           .set_name("grpc.io/client/sent_messages_per_rpc/cumulative")
155           .set_measure(kRpcClientReceivedMessagesPerRpcMeasureName)
156           .set_aggregation(CountDistributionAggregation())
157           .add_column(ClientMethodTagKey());
158   return descriptor;
159 }
160 
161 // server cumulative
ServerSentBytesPerRpcCumulative()162 const ViewDescriptor& ServerSentBytesPerRpcCumulative() {
163   const static ViewDescriptor descriptor =
164       ViewDescriptor()
165           .set_name("grpc.io/server/received_bytes_per_rpc/cumulative")
166           .set_measure(kRpcServerSentBytesPerRpcMeasureName)
167           .set_aggregation(BytesDistributionAggregation())
168           .add_column(ServerMethodTagKey());
169   return descriptor;
170 }
171 
ServerReceivedBytesPerRpcCumulative()172 const ViewDescriptor& ServerReceivedBytesPerRpcCumulative() {
173   const static ViewDescriptor descriptor =
174       ViewDescriptor()
175           .set_name("grpc.io/server/sent_bytes_per_rpc/cumulative")
176           .set_measure(kRpcServerReceivedBytesPerRpcMeasureName)
177           .set_aggregation(BytesDistributionAggregation())
178           .add_column(ServerMethodTagKey());
179   return descriptor;
180 }
181 
ServerServerLatencyCumulative()182 const ViewDescriptor& ServerServerLatencyCumulative() {
183   const static ViewDescriptor descriptor =
184       ViewDescriptor()
185           .set_name("grpc.io/server/elapsed_time/cumulative")
186           .set_measure(kRpcServerServerLatencyMeasureName)
187           .set_aggregation(MillisDistributionAggregation())
188           .add_column(ServerMethodTagKey());
189   return descriptor;
190 }
191 
ServerCompletedRpcsCumulative()192 const ViewDescriptor& ServerCompletedRpcsCumulative() {
193   const static ViewDescriptor descriptor =
194       ViewDescriptor()
195           .set_name("grpc.io/server/completed_rpcs/cumulative")
196           .set_measure(kRpcServerServerLatencyMeasureName)
197           .set_aggregation(Aggregation::Count())
198           .add_column(ServerMethodTagKey())
199           .add_column(ServerStatusTagKey());
200   return descriptor;
201 }
202 
ServerSentMessagesPerRpcCumulative()203 const ViewDescriptor& ServerSentMessagesPerRpcCumulative() {
204   const static ViewDescriptor descriptor =
205       ViewDescriptor()
206           .set_name("grpc.io/server/received_messages_per_rpc/cumulative")
207           .set_measure(kRpcServerSentMessagesPerRpcMeasureName)
208           .set_aggregation(CountDistributionAggregation())
209           .add_column(ServerMethodTagKey());
210   return descriptor;
211 }
212 
ServerReceivedMessagesPerRpcCumulative()213 const ViewDescriptor& ServerReceivedMessagesPerRpcCumulative() {
214   const static ViewDescriptor descriptor =
215       ViewDescriptor()
216           .set_name("grpc.io/server/sent_messages_per_rpc/cumulative")
217           .set_measure(kRpcServerReceivedMessagesPerRpcMeasureName)
218           .set_aggregation(CountDistributionAggregation())
219           .add_column(ServerMethodTagKey());
220   return descriptor;
221 }
222 
223 // client minute
ClientSentBytesPerRpcMinute()224 const ViewDescriptor& ClientSentBytesPerRpcMinute() {
225   const static ViewDescriptor descriptor =
226       MinuteDescriptor()
227           .set_name("grpc.io/client/sent_bytes_per_rpc/minute")
228           .set_measure(kRpcClientSentBytesPerRpcMeasureName)
229           .set_aggregation(BytesDistributionAggregation())
230           .add_column(ClientMethodTagKey());
231   return descriptor;
232 }
233 
ClientReceivedBytesPerRpcMinute()234 const ViewDescriptor& ClientReceivedBytesPerRpcMinute() {
235   const static ViewDescriptor descriptor =
236       MinuteDescriptor()
237           .set_name("grpc.io/client/received_bytes_per_rpc/minute")
238           .set_measure(kRpcClientReceivedBytesPerRpcMeasureName)
239           .set_aggregation(BytesDistributionAggregation())
240           .add_column(ClientMethodTagKey());
241   return descriptor;
242 }
243 
ClientRoundtripLatencyMinute()244 const ViewDescriptor& ClientRoundtripLatencyMinute() {
245   const static ViewDescriptor descriptor =
246       MinuteDescriptor()
247           .set_name("grpc.io/client/roundtrip_latency/minute")
248           .set_measure(kRpcClientRoundtripLatencyMeasureName)
249           .set_aggregation(MillisDistributionAggregation())
250           .add_column(ClientMethodTagKey());
251   return descriptor;
252 }
253 
ClientServerLatencyMinute()254 const ViewDescriptor& ClientServerLatencyMinute() {
255   const static ViewDescriptor descriptor =
256       MinuteDescriptor()
257           .set_name("grpc.io/client/server_latency/minute")
258           .set_measure(kRpcClientServerLatencyMeasureName)
259           .set_aggregation(MillisDistributionAggregation())
260           .add_column(ClientMethodTagKey());
261   return descriptor;
262 }
263 
ClientCompletedRpcsMinute()264 const ViewDescriptor& ClientCompletedRpcsMinute() {
265   const static ViewDescriptor descriptor =
266       MinuteDescriptor()
267           .set_name("grpc.io/client/completed_rpcs/minute")
268           .set_measure(kRpcClientRoundtripLatencyMeasureName)
269           .set_aggregation(Aggregation::Count())
270           .add_column(ClientMethodTagKey())
271           .add_column(ClientStatusTagKey());
272   return descriptor;
273 }
274 
ClientSentMessagesPerRpcMinute()275 const ViewDescriptor& ClientSentMessagesPerRpcMinute() {
276   const static ViewDescriptor descriptor =
277       MinuteDescriptor()
278           .set_name("grpc.io/client/sent_messages_per_rpc/minute")
279           .set_measure(kRpcClientSentMessagesPerRpcMeasureName)
280           .set_aggregation(CountDistributionAggregation())
281           .add_column(ClientMethodTagKey());
282   return descriptor;
283 }
284 
ClientReceivedMessagesPerRpcMinute()285 const ViewDescriptor& ClientReceivedMessagesPerRpcMinute() {
286   const static ViewDescriptor descriptor =
287       MinuteDescriptor()
288           .set_name("grpc.io/client/received_messages_per_rpc/minute")
289           .set_measure(kRpcClientReceivedMessagesPerRpcMeasureName)
290           .set_aggregation(CountDistributionAggregation())
291           .add_column(ClientMethodTagKey());
292   return descriptor;
293 }
294 
295 // server minute
ServerSentBytesPerRpcMinute()296 const ViewDescriptor& ServerSentBytesPerRpcMinute() {
297   const static ViewDescriptor descriptor =
298       MinuteDescriptor()
299           .set_name("grpc.io/server/sent_bytes_per_rpc/minute")
300           .set_measure(kRpcServerSentBytesPerRpcMeasureName)
301           .set_aggregation(BytesDistributionAggregation())
302           .add_column(ServerMethodTagKey());
303   return descriptor;
304 }
305 
ServerReceivedBytesPerRpcMinute()306 const ViewDescriptor& ServerReceivedBytesPerRpcMinute() {
307   const static ViewDescriptor descriptor =
308       MinuteDescriptor()
309           .set_name("grpc.io/server/received_bytes_per_rpc/minute")
310           .set_measure(kRpcServerReceivedBytesPerRpcMeasureName)
311           .set_aggregation(BytesDistributionAggregation())
312           .add_column(ServerMethodTagKey());
313   return descriptor;
314 }
315 
ServerServerLatencyMinute()316 const ViewDescriptor& ServerServerLatencyMinute() {
317   const static ViewDescriptor descriptor =
318       MinuteDescriptor()
319           .set_name("grpc.io/server/server_latency/minute")
320           .set_measure(kRpcServerServerLatencyMeasureName)
321           .set_aggregation(MillisDistributionAggregation())
322           .add_column(ServerMethodTagKey());
323   return descriptor;
324 }
325 
ServerCompletedRpcsMinute()326 const ViewDescriptor& ServerCompletedRpcsMinute() {
327   const static ViewDescriptor descriptor =
328       MinuteDescriptor()
329           .set_name("grpc.io/server/completed_rpcs/minute")
330           .set_measure(kRpcServerServerLatencyMeasureName)
331           .set_aggregation(Aggregation::Count())
332           .add_column(ServerMethodTagKey())
333           .add_column(ServerStatusTagKey());
334   return descriptor;
335 }
336 
ServerSentMessagesPerRpcMinute()337 const ViewDescriptor& ServerSentMessagesPerRpcMinute() {
338   const static ViewDescriptor descriptor =
339       MinuteDescriptor()
340           .set_name("grpc.io/server/sent_messages_per_rpc/minute")
341           .set_measure(kRpcServerSentMessagesPerRpcMeasureName)
342           .set_aggregation(CountDistributionAggregation())
343           .add_column(ServerMethodTagKey());
344   return descriptor;
345 }
346 
ServerReceivedMessagesPerRpcMinute()347 const ViewDescriptor& ServerReceivedMessagesPerRpcMinute() {
348   const static ViewDescriptor descriptor =
349       MinuteDescriptor()
350           .set_name("grpc.io/server/received_messages_per_rpc/minute")
351           .set_measure(kRpcServerReceivedMessagesPerRpcMeasureName)
352           .set_aggregation(CountDistributionAggregation())
353           .add_column(ServerMethodTagKey());
354   return descriptor;
355 }
356 
357 // client hour
ClientSentBytesPerRpcHour()358 const ViewDescriptor& ClientSentBytesPerRpcHour() {
359   const static ViewDescriptor descriptor =
360       HourDescriptor()
361           .set_name("grpc.io/client/sent_bytes_per_rpc/hour")
362           .set_measure(kRpcClientSentBytesPerRpcMeasureName)
363           .set_aggregation(BytesDistributionAggregation())
364           .add_column(ClientMethodTagKey());
365   return descriptor;
366 }
367 
ClientReceivedBytesPerRpcHour()368 const ViewDescriptor& ClientReceivedBytesPerRpcHour() {
369   const static ViewDescriptor descriptor =
370       HourDescriptor()
371           .set_name("grpc.io/client/received_bytes_per_rpc/hour")
372           .set_measure(kRpcClientReceivedBytesPerRpcMeasureName)
373           .set_aggregation(BytesDistributionAggregation())
374           .add_column(ClientMethodTagKey());
375   return descriptor;
376 }
377 
ClientRoundtripLatencyHour()378 const ViewDescriptor& ClientRoundtripLatencyHour() {
379   const static ViewDescriptor descriptor =
380       HourDescriptor()
381           .set_name("grpc.io/client/roundtrip_latency/hour")
382           .set_measure(kRpcClientRoundtripLatencyMeasureName)
383           .set_aggregation(MillisDistributionAggregation())
384           .add_column(ClientMethodTagKey());
385   return descriptor;
386 }
387 
ClientServerLatencyHour()388 const ViewDescriptor& ClientServerLatencyHour() {
389   const static ViewDescriptor descriptor =
390       HourDescriptor()
391           .set_name("grpc.io/client/server_latency/hour")
392           .set_measure(kRpcClientServerLatencyMeasureName)
393           .set_aggregation(MillisDistributionAggregation())
394           .add_column(ClientMethodTagKey());
395   return descriptor;
396 }
397 
ClientCompletedRpcsHour()398 const ViewDescriptor& ClientCompletedRpcsHour() {
399   const static ViewDescriptor descriptor =
400       HourDescriptor()
401           .set_name("grpc.io/client/completed_rpcs/hour")
402           .set_measure(kRpcClientRoundtripLatencyMeasureName)
403           .set_aggregation(Aggregation::Count())
404           .add_column(ClientMethodTagKey())
405           .add_column(ClientStatusTagKey());
406   return descriptor;
407 }
408 
ClientSentMessagesPerRpcHour()409 const ViewDescriptor& ClientSentMessagesPerRpcHour() {
410   const static ViewDescriptor descriptor =
411       HourDescriptor()
412           .set_name("grpc.io/client/sent_messages_per_rpc/hour")
413           .set_measure(kRpcClientSentMessagesPerRpcMeasureName)
414           .set_aggregation(CountDistributionAggregation())
415           .add_column(ClientMethodTagKey());
416   return descriptor;
417 }
418 
ClientReceivedMessagesPerRpcHour()419 const ViewDescriptor& ClientReceivedMessagesPerRpcHour() {
420   const static ViewDescriptor descriptor =
421       HourDescriptor()
422           .set_name("grpc.io/client/received_messages_per_rpc/hour")
423           .set_measure(kRpcClientReceivedMessagesPerRpcMeasureName)
424           .set_aggregation(CountDistributionAggregation())
425           .add_column(ClientMethodTagKey());
426   return descriptor;
427 }
428 
429 // server hour
ServerSentBytesPerRpcHour()430 const ViewDescriptor& ServerSentBytesPerRpcHour() {
431   const static ViewDescriptor descriptor =
432       HourDescriptor()
433           .set_name("grpc.io/server/sent_bytes_per_rpc/hour")
434           .set_measure(kRpcServerSentBytesPerRpcMeasureName)
435           .set_aggregation(BytesDistributionAggregation())
436           .add_column(ServerMethodTagKey());
437   return descriptor;
438 }
439 
ServerReceivedBytesPerRpcHour()440 const ViewDescriptor& ServerReceivedBytesPerRpcHour() {
441   const static ViewDescriptor descriptor =
442       HourDescriptor()
443           .set_name("grpc.io/server/received_bytes_per_rpc/hour")
444           .set_measure(kRpcServerReceivedBytesPerRpcMeasureName)
445           .set_aggregation(BytesDistributionAggregation())
446           .add_column(ServerMethodTagKey());
447   return descriptor;
448 }
449 
ServerServerLatencyHour()450 const ViewDescriptor& ServerServerLatencyHour() {
451   const static ViewDescriptor descriptor =
452       HourDescriptor()
453           .set_name("grpc.io/server/server_latency/hour")
454           .set_measure(kRpcServerServerLatencyMeasureName)
455           .set_aggregation(MillisDistributionAggregation())
456           .add_column(ServerMethodTagKey());
457   return descriptor;
458 }
459 
ServerCompletedRpcsHour()460 const ViewDescriptor& ServerCompletedRpcsHour() {
461   const static ViewDescriptor descriptor =
462       HourDescriptor()
463           .set_name("grpc.io/server/completed_rpcs/hour")
464           .set_measure(kRpcServerServerLatencyMeasureName)
465           .set_aggregation(Aggregation::Count())
466           .add_column(ServerMethodTagKey())
467           .add_column(ServerStatusTagKey());
468   return descriptor;
469 }
470 
ServerSentMessagesPerRpcHour()471 const ViewDescriptor& ServerSentMessagesPerRpcHour() {
472   const static ViewDescriptor descriptor =
473       HourDescriptor()
474           .set_name("grpc.io/server/sent_messages_per_rpc/hour")
475           .set_measure(kRpcServerSentMessagesPerRpcMeasureName)
476           .set_aggregation(CountDistributionAggregation())
477           .add_column(ServerMethodTagKey());
478   return descriptor;
479 }
480 
ServerReceivedMessagesPerRpcHour()481 const ViewDescriptor& ServerReceivedMessagesPerRpcHour() {
482   const static ViewDescriptor descriptor =
483       HourDescriptor()
484           .set_name("grpc.io/server/received_messages_per_rpc/hour")
485           .set_measure(kRpcServerReceivedMessagesPerRpcMeasureName)
486           .set_aggregation(CountDistributionAggregation())
487           .add_column(ServerMethodTagKey());
488   return descriptor;
489 }
490 
491 }  // namespace grpc
492