/Users/deen/code/yugabyte-db/src/yb/gen_yrpc/metric_descriptor.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/gen_yrpc/metric_descriptor.h" |
15 | | |
16 | | #include <google/protobuf/descriptor.h> |
17 | | |
18 | | #include "yb/gen_yrpc/model.h" |
19 | | |
20 | | namespace yb { |
21 | | namespace gen_yrpc { |
22 | | |
23 | 0 | Substitutions MetricDescriptor::CreateSubstitutions() const { |
24 | 0 | Substitutions result; |
25 | 0 | result.emplace_back("metric_name", name); |
26 | 0 | result.emplace_back("metric_prefix", prefix); |
27 | 0 | result.emplace_back("metric_kind", kind); |
28 | 0 | result.emplace_back("metric_extra_args", extra_args); |
29 | 0 | result.emplace_back("metric_units", units); |
30 | 0 | result.emplace_back("metric_description", description); |
31 | 0 | return result; |
32 | 0 | } |
33 | | |
34 | | void GenerateMethodIndexesEnum( |
35 | 0 | YBPrinter printer, const google::protobuf::ServiceDescriptor* service) { |
36 | 0 | printer("enum class $service_method_enum$ {\n"); |
37 | 0 | for (int method_idx = 0; method_idx < service->method_count(); ++method_idx) { |
38 | 0 | ScopedSubstituter method_subs(printer, service->method(method_idx), rpc::RpcSides::SERVICE); |
39 | |
|
40 | 0 | printer(" $metric_enum_key$,\n"); |
41 | 0 | } |
42 | |
|
43 | 0 | printer("}; // enum\n"); |
44 | 0 | } |
45 | | |
46 | | void GenerateMetricDefines( |
47 | | YBPrinter printer, const google::protobuf::FileDescriptor* file, |
48 | 0 | const std::vector<MetricDescriptor>& metric_descriptors) { |
49 | 0 | for (int service_idx = 0; service_idx < file->service_count(); ++service_idx) { |
50 | 0 | const auto* service = file->service(service_idx); |
51 | 0 | ScopedSubstituter service_subs(printer, service); |
52 | |
|
53 | 0 | for (int method_idx = 0; method_idx < service->method_count(); ++method_idx) { |
54 | 0 | ScopedSubstituter method_subs(printer, service->method(method_idx), rpc::RpcSides::SERVICE); |
55 | |
|
56 | 0 | for (const auto& desc : metric_descriptors) { |
57 | 0 | ScopedSubstituter metric_subs(printer, desc.CreateSubstitutions()); |
58 | 0 | std::string text = |
59 | 0 | "METRIC_DEFINE_$metric_kind$(\n server," |
60 | 0 | " $metric_prefix$$metric_name$_$rpc_full_name_plainchars$,\n" |
61 | 0 | " \"$metric_description$ $rpc_full_name$() RPC requests\",\n" |
62 | 0 | " $metric_units$,\n" |
63 | 0 | " \"$metric_description$ $rpc_full_name$() RPC requests\"$metric_extra_args$);\n" |
64 | 0 | "\n"; |
65 | 0 | printer(text); |
66 | 0 | } |
67 | 0 | } |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | void GenerateHandlerAssignment( |
72 | 0 | YBPrinter printer, const google::protobuf::MethodDescriptor* method) { |
73 | 0 | printer(".handler = [this](::yb::rpc::InboundCallPtr call) {\n"); |
74 | 0 | ScopedIndent handler_indent(printer); |
75 | 0 | printer( |
76 | 0 | "call->SetRpcMethodMetrics(methods_[" |
77 | 0 | "static_cast<size_t>($service_method_enum$::$metric_enum_key$)].metrics);\n" |
78 | 0 | "::yb::rpc::HandleCall<::yb::rpc::$params$Impl<$request$, $response$>>(\n" |
79 | 0 | " std::move(call), [this](const $request$* req, $response$* resp, " |
80 | 0 | "::yb::rpc::RpcContext rpc_context) {\n"); |
81 | 0 | if (IsTrivialMethod(method)) { |
82 | 0 | printer( |
83 | 0 | " auto result = $rpc_name$(*req, rpc_context.GetClientDeadline());\n" |
84 | 0 | " rpc_context.RespondTrivial(&result, resp);\n" |
85 | 0 | ); |
86 | 0 | } else { |
87 | 0 | printer(" $rpc_name$(req, resp, std::move(rpc_context));\n"); |
88 | 0 | } |
89 | 0 | printer( |
90 | 0 | "});\n" |
91 | 0 | ); |
92 | 0 | handler_indent.Reset("},\n"); |
93 | 0 | } |
94 | | |
95 | | void GenerateMethodAssignments( |
96 | | YBPrinter printer, const google::protobuf::ServiceDescriptor* service, |
97 | | const std::string &mutable_metric_fmt, bool service_side, |
98 | 0 | const std::vector<MetricDescriptor>& metric_descriptors) { |
99 | 0 | ScopedIndent indent(printer); |
100 | |
|
101 | 0 | for (int method_idx = 0; method_idx < service->method_count(); ++method_idx) { |
102 | 0 | auto* method = service->method(method_idx); |
103 | 0 | ScopedSubstituter method_subs(printer, method, rpc::RpcSides::SERVICE); |
104 | |
|
105 | 0 | printer(mutable_metric_fmt + " = {\n"); |
106 | 0 | if (service_side) { |
107 | 0 | ScopedIndent method_indent(printer); |
108 | 0 | printer(".method = ::yb::rpc::RemoteMethod(\"$full_service_name$\", \"$rpc_name$\"),\n"); |
109 | 0 | GenerateHandlerAssignment(printer, method); |
110 | 0 | printer(".metrics = ::yb::rpc::RpcMethodMetrics(\n"); |
111 | 0 | } |
112 | 0 | bool first = true; |
113 | 0 | for (const auto& desc : metric_descriptors) { |
114 | 0 | if (first) { |
115 | 0 | first = false; |
116 | 0 | } else { |
117 | 0 | printer(",\n"); |
118 | 0 | } |
119 | 0 | ScopedSubstituter metric_subs(printer, desc.CreateSubstitutions()); |
120 | 0 | if (service_side) { |
121 | 0 | printer(" "); |
122 | 0 | } |
123 | 0 | printer( |
124 | 0 | " METRIC_$metric_prefix$$metric_name$_$rpc_full_name_plainchars$.Instantiate(entity)"); |
125 | 0 | } |
126 | 0 | printer((service_side ? ")" : std::string()) + "\n};\n\n"); |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | } // namespace gen_yrpc |
131 | | } // namespace yb |