-
Notifications
You must be signed in to change notification settings - Fork 374
/
tracing_connection_generator.cc
382 lines (334 loc) · 12.2 KB
/
tracing_connection_generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "generator/internal/tracing_connection_generator.h"
#include "generator/internal/codegen_utils.h"
#include "generator/internal/longrunning.h"
#include "generator/internal/pagination.h"
#include "generator/internal/predicate_utils.h"
#include "generator/internal/printer.h"
#include "google/cloud/internal/absl_str_cat_quiet.h"
#include <google/protobuf/descriptor.h>
namespace google {
namespace cloud {
namespace generator_internal {
TracingConnectionGenerator::TracingConnectionGenerator(
google::protobuf::ServiceDescriptor const* service_descriptor,
VarsDictionary service_vars,
std::map<std::string, VarsDictionary> service_method_vars,
google::protobuf::compiler::GeneratorContext* context)
: ServiceCodeGenerator("tracing_connection_header_path",
"tracing_connection_cc_path", service_descriptor,
std::move(service_vars),
std::move(service_method_vars), context) {}
Status TracingConnectionGenerator::GenerateHeader() {
HeaderPrint(CopyrightLicenseFileHeader());
HeaderPrint(R"""(
// Generated by the Codegen C++ plugin.
// If you make any local changes, they will be lost.
// source: $proto_file_name$
#ifndef $header_include_guard$
#define $header_include_guard$
)""");
// includes
HeaderPrint("\n");
HeaderLocalIncludes(
{vars("connection_header_path"), "google/cloud/version.h"});
HeaderSystemIncludes({"memory"});
auto result = HeaderOpenNamespaces(NamespaceType::kInternal);
if (!result.ok()) return result;
HeaderPrint(R"""(
#ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY
class $tracing_connection_class_name$
: public $product_namespace$::$connection_class_name$ {
public:
~$tracing_connection_class_name$() override = default;
explicit $tracing_connection_class_name$(
std::shared_ptr<$product_namespace$::$connection_class_name$> child);
)""");
HeaderPrint(R"""(
Options options() override { return child_->options(); }
)""");
for (auto const& method : methods()) {
HeaderPrintMethod(method, __FILE__, __LINE__, MethodDeclaration(method));
}
for (auto const& method : async_methods()) {
if (IsStreamingRead(method)) continue;
if (IsStreamingWrite(method)) continue;
HeaderPrintMethod(method, __FILE__, __LINE__,
AsyncMethodDeclaration(method));
}
HeaderPrint(R"""(
private:
std::shared_ptr<$product_namespace$::$connection_class_name$> child_;
};
#endif // GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY
/**
* Conditionally applies the tracing decorator to the given connection.
*
* The connection is only decorated if tracing is enabled (as determined by the
* connection's options).
*/
std::shared_ptr<$product_namespace$::$connection_class_name$>
Make$tracing_connection_class_name$(
std::shared_ptr<$product_namespace$::$connection_class_name$> conn);
)""");
HeaderCloseNamespaces();
// close header guard
HeaderPrint("\n#endif // $header_include_guard$\n");
return {};
}
Status TracingConnectionGenerator::GenerateCc() {
CcPrint(CopyrightLicenseFileHeader());
CcPrint(R"""(
// Generated by the Codegen C++ plugin.
// If you make any local changes, they will be lost.
// source: $proto_file_name$
)""");
// includes
CcLocalIncludes({
vars("tracing_connection_header_path"),
"google/cloud/internal/opentelemetry.h",
HasPaginatedMethod() || HasStreamingReadMethod()
? "google/cloud/internal/traced_stream_range.h"
: "",
});
CcSystemIncludes({"memory", "utility"});
auto result = CcOpenNamespaces(NamespaceType::kInternal);
if (!result.ok()) return result;
CcPrint(R"""(
#ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY
$tracing_connection_class_name$::$tracing_connection_class_name$(
std::shared_ptr<$product_namespace$::$connection_class_name$> child)
: child_(std::move(child)) {}
)""");
for (auto const& method : methods()) {
CcPrintMethod(method, __FILE__, __LINE__, MethodDefinition(method));
}
for (auto const& method : async_methods()) {
if (IsStreamingRead(method)) continue;
if (IsStreamingWrite(method)) continue;
CcPrintMethod(method, __FILE__, __LINE__, AsyncMethodDefinition(method));
}
CcPrint(R"""(
#endif // GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY
std::shared_ptr<$product_namespace$::$connection_class_name$>
Make$tracing_connection_class_name$(
std::shared_ptr<$product_namespace$::$connection_class_name$> conn) {
#ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY
if (internal::TracingEnabled(conn->options())) {
conn = std::make_shared<$tracing_connection_class_name$>(std::move(conn));
}
#endif // GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY
return conn;
}
)""");
CcCloseNamespaces();
return {};
}
std::string TracingConnectionGenerator::MethodDeclaration(
google::protobuf::MethodDescriptor const& method) {
if (IsBidirStreaming(method)) {
return R"""(
std::unique_ptr<::google::cloud::AsyncStreamingReadWriteRpc<
$request_type$,
$response_type$>>
Async$method_name$() override;
)""";
}
if (IsStreamingRead(method)) {
return R"""(
StreamRange<$response_type$>
$method_name$($request_type$ const& request) override;
)""";
}
if (IsStreamingWrite(method)) {
// Streaming writes are only used in services that need hand-crafted
// connection and client classes, they are not supported.
return {};
}
if (IsPaginated(method)) {
return R"""(
StreamRange<$range_output_type$>
$method_name$($request_type$ request) override;
)""";
}
if (IsLongrunningOperation(method)) {
if (IsResponseTypeEmpty(method)) {
// TODO(#14344): Remove experimental tag.
return R"""(
future<Status>
$method_name$($request_type$ const& request) override;
StatusOr<$longrunning_operation_type$>
$method_name$(google::cloud::ExperimentalTag, google::cloud::NoAwaitTag,
$request_type$ const& request) override;
future<Status>
$method_name$(google::cloud::ExperimentalTag,
$longrunning_operation_type$ const& operation) override;
)""";
}
// TODO(#14344): Remove experimental tag.
return R"""(
future<StatusOr<$longrunning_deduced_response_type$>>
$method_name$($request_type$ const& request) override;
StatusOr<$longrunning_operation_type$>
$method_name$(google::cloud::ExperimentalTag, google::cloud::NoAwaitTag,
$request_type$ const& request) override;
future<StatusOr<$longrunning_deduced_response_type$>>
$method_name$(google::cloud::ExperimentalTag,
$longrunning_operation_type$ const& operation) override;
)""";
}
if (IsResponseTypeEmpty(method)) {
return R"""(
Status
$method_name$($request_type$ const& request) override;
)""";
}
return R"""(
StatusOr<$response_type$>
$method_name$($request_type$ const& request) override;
)""";
}
std::string TracingConnectionGenerator::AsyncMethodDeclaration(
google::protobuf::MethodDescriptor const& method) {
if (IsResponseTypeEmpty(method)) {
return R"""(
future<Status>
Async$method_name$($request_type$ const& request) override;
)""";
}
return R"""(
future<StatusOr<$response_type$>>
Async$method_name$($request_type$ const& request) override;
)""";
}
std::string TracingConnectionGenerator::MethodDefinition(
google::protobuf::MethodDescriptor const& method) {
if (IsBidirStreaming(method)) {
return R"""(
std::unique_ptr<::google::cloud::AsyncStreamingReadWriteRpc<
$request_type$,
$response_type$>>
$tracing_connection_class_name$::Async$method_name$() {
return child_->Async$method_name$();
}
)""";
}
if (IsStreamingRead(method)) {
return R"""(
StreamRange<$response_type$>
$tracing_connection_class_name$::$method_name$($request_type$ const& request) {
auto span = internal::MakeSpan("$product_namespace$::$connection_class_name$::$method_name$");
internal::OTelScope scope(span);
auto sr = child_->$method_name$(request);
return internal::MakeTracedStreamRange<$response_type$>(
std::move(span), std::move(sr));
})""";
}
if (IsStreamingWrite(method)) {
// Streaming writes are only used in services that need hand-crafted
// connection and client classes, they are not supported.
return {};
}
if (IsPaginated(method)) {
return R"""(
StreamRange<$range_output_type$>
$tracing_connection_class_name$::$method_name$($request_type$ request) {
auto span = internal::MakeSpan("$product_namespace$::$connection_class_name$::$method_name$");
internal::OTelScope scope(span);
auto sr = child_->$method_name$(std::move(request));
return internal::MakeTracedStreamRange<$range_output_type$>(
std::move(span), std::move(sr));
}
)""";
}
if (IsLongrunningOperation(method)) {
return absl::StrCat(
// The return type may be a simple `Status` or the
// computed type of the long-running operation
IsResponseTypeEmpty(method) ?
R"""(
future<Status>)"""
:
R"""(
future<StatusOr<$longrunning_deduced_response_type$>>)""",
R"""(
$tracing_connection_class_name$::$method_name$($request_type$ const& request) {
auto span = internal::MakeSpan(
"$product_namespace$::$connection_class_name$::$method_name$");
internal::OTelScope scope(span);
return internal::EndSpan(std::move(span), child_->$method_name$(request));
}
)""",
R"""(
StatusOr<$longrunning_operation_type$>)""",
// TODO(#14344): Remove experimental tag.
R"""(
$tracing_connection_class_name$::$method_name$(google::cloud::ExperimentalTag,
google::cloud::NoAwaitTag, $request_type$ const& request) {
auto span = internal::MakeSpan(
"$product_namespace$::$connection_class_name$::$method_name$");
internal::OTelScope scope(span);
return internal::EndSpan(*span, child_->$method_name$(
google::cloud::ExperimentalTag{}, google::cloud::NoAwaitTag{}, request));
}
)""",
IsResponseTypeEmpty(method) ?
R"""(
future<Status>)"""
:
R"""(
future<StatusOr<$longrunning_deduced_response_type$>>)""",
// TODO(#14344): Remove experimental tag.
R"""(
$tracing_connection_class_name$::$method_name$(google::cloud::ExperimentalTag,
$longrunning_operation_type$ const& operation) {
auto span = internal::MakeSpan(
"$product_namespace$::$connection_class_name$::$method_name$");
internal::OTelScope scope(span);
return internal::EndSpan(std::move(span),
child_->$method_name$(google::cloud::ExperimentalTag{}, operation));
}
)""");
}
return absl::StrCat(IsResponseTypeEmpty(method) ? R"""(
Status)"""
: R"""(
StatusOr<$response_type$>)""",
R"""(
$tracing_connection_class_name$::$method_name$($request_type$ const& request) {
auto span = internal::MakeSpan("$product_namespace$::$connection_class_name$::$method_name$");
auto scope = opentelemetry::trace::Scope(span);
return internal::EndSpan(*span, child_->$method_name$(request));
}
)""");
}
std::string TracingConnectionGenerator::AsyncMethodDefinition(
google::protobuf::MethodDescriptor const& method) {
return absl::StrCat(IsResponseTypeEmpty(method) ? R"""(
future<Status>)"""
: R"""(
future<StatusOr<$response_type$>>)""",
R"""(
$tracing_connection_class_name$::Async$method_name$($request_type$ const& request) {
auto span = internal::MakeSpan(
"$product_namespace$::$connection_class_name$::Async$method_name$");
internal::OTelScope scope(span);
return internal::EndSpan(std::move(span), child_->Async$method_name$(request));
}
)""");
}
} // namespace generator_internal
} // namespace cloud
} // namespace google