|
21 | 21 | #include "absl/memory/memory.h" |
22 | 22 | #include "absl/strings/str_cat.h" |
23 | 23 | #include "absl/time/clock.h" |
| 24 | +#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h" |
| 25 | +#include "include/grpc++/grpc++.h" |
| 26 | +#include "opencensus/trace/exporter/span_data.h" |
| 27 | +#include "opencensus/trace/exporter/span_exporter.h" |
24 | 28 |
|
25 | 29 | using grpc::ClientContext; |
26 | 30 | using grpc::Status; |
27 | 31 |
|
28 | 32 | namespace opencensus { |
29 | 33 | namespace exporters { |
30 | 34 | namespace trace { |
31 | | - |
32 | | -static constexpr size_t kAttributeStringLen = 256; |
33 | | -static constexpr size_t kAnnotationStringLen = 256; |
34 | | -static constexpr size_t kDisplayNameStringLen = 128; |
35 | | -static constexpr char kGoogleStackDriverTraceAddress[] = |
36 | | - "cloudtrace.googleapis.com"; |
37 | | - |
38 | 35 | namespace { |
39 | 36 |
|
| 37 | +constexpr size_t kAttributeStringLen = 256; |
| 38 | +constexpr size_t kAnnotationStringLen = 256; |
| 39 | +constexpr size_t kDisplayNameStringLen = 128; |
| 40 | +constexpr char kGoogleStackDriverTraceAddress[] = "cloudtrace.googleapis.com"; |
| 41 | + |
40 | 42 | constexpr char kAgentKey[] = "g.co/agent"; |
41 | 43 | constexpr char kAgentValue[] = "opencensus-cpp"; |
42 | 44 |
|
| 45 | +std::string ToString(const grpc::Status& status) { |
| 46 | + return absl::StrCat("status code ", status.error_code(), " details \"", |
| 47 | + status.error_message(), "\""); |
| 48 | +} |
| 49 | + |
43 | 50 | gpr_timespec ConvertToTimespec(absl::Time time) { |
44 | 51 | gpr_timespec g_time; |
45 | 52 | int64_t secs = absl::ToUnixSeconds(time); |
@@ -243,64 +250,44 @@ void ConvertSpans( |
243 | 250 | } |
244 | 251 | } |
245 | 252 |
|
246 | | -} // namespace |
| 253 | +class Handler : public ::opencensus::trace::exporter::SpanExporter::Handler { |
| 254 | + public: |
| 255 | + Handler(absl::string_view project_id, |
| 256 | + const std::shared_ptr<grpc::Channel>& channel) |
| 257 | + : project_id_(project_id), |
| 258 | + stub_(::google::devtools::cloudtrace::v2::TraceService::NewStub( |
| 259 | + channel)) {} |
247 | 260 |
|
248 | | -Status StackdriverExporter::TraceClient::BatchWriteSpans( |
249 | | - const ::google::devtools::cloudtrace::v2::BatchWriteSpansRequest& request) { |
250 | | - ::google::protobuf::Empty response; |
| 261 | + void Export(const std::vector<::opencensus::trace::exporter::SpanData>& spans) |
| 262 | + override; |
251 | 263 |
|
252 | | - // Context for the client. It could be used to convey extra information to |
253 | | - // the server and/or tweak certain RPC behaviors. Deadline set for 3000 |
254 | | - // milliseconds. |
255 | | - ClientContext context; |
256 | | - context.set_deadline( |
257 | | - ConvertToTimespec(absl::Now() + absl::Milliseconds(3000))); |
258 | | - |
259 | | - // The actual RPC that sends the span information to Stackdriver. |
260 | | - return stub_->BatchWriteSpans(&context, request, &response); |
261 | | -} |
262 | | - |
263 | | -void StackdriverExporter::Register(absl::string_view project_id) { |
264 | | - StackdriverExporter* exporter = new StackdriverExporter(project_id); |
265 | | - auto creds = grpc::GoogleDefaultCredentials(); |
266 | | - auto channel = ::grpc::CreateChannel(kGoogleStackDriverTraceAddress, creds); |
267 | | - exporter->trace_client_ = absl::make_unique<TraceClient>(channel); |
268 | | - ::opencensus::trace::exporter::SpanExporter::RegisterHandler( |
269 | | - absl::WrapUnique<::opencensus::trace::exporter::SpanExporter::Handler>( |
270 | | - exporter)); |
271 | | -} |
| 264 | + private: |
| 265 | + const std::string project_id_; |
| 266 | + std::unique_ptr<google::devtools::cloudtrace::v2::TraceService::Stub> stub_; |
| 267 | +}; |
272 | 268 |
|
273 | | -void StackdriverExporter::Export( |
| 269 | +void Handler::Export( |
274 | 270 | const std::vector<::opencensus::trace::exporter::SpanData>& spans) { |
275 | 271 | ::google::devtools::cloudtrace::v2::BatchWriteSpansRequest request; |
276 | 272 | request.set_name(absl::StrCat("projects/", project_id_)); |
277 | 273 | ConvertSpans(spans, project_id_, &request); |
278 | | - |
279 | | - Status status = trace_client_->BatchWriteSpans(request); |
280 | | - // Act upon its status. |
| 274 | + ::google::protobuf::Empty response; |
| 275 | + ClientContext context; |
| 276 | + context.set_deadline( |
| 277 | + ConvertToTimespec(absl::Now() + absl::Milliseconds(3000))); |
| 278 | + Status status = stub_->BatchWriteSpans(&context, request, &response); |
281 | 279 | if (!status.ok()) { |
282 | | - // TODO: log error. |
| 280 | + std::cerr << "BatchWriteSpans failed: " << ToString(status) << "\n"; |
283 | 281 | } |
284 | 282 | } |
285 | 283 |
|
286 | | -void StackdriverExporter::ExportForTesting( |
287 | | - absl::string_view project_id, |
288 | | - const std::vector<::opencensus::trace::exporter::SpanData>& spans) { |
| 284 | +} // namespace |
| 285 | + |
| 286 | +void StackdriverExporter::Register(absl::string_view project_id) { |
289 | 287 | auto creds = grpc::GoogleDefaultCredentials(); |
290 | 288 | auto channel = ::grpc::CreateChannel(kGoogleStackDriverTraceAddress, creds); |
291 | | - std::unique_ptr<StackdriverExporter::TraceClient> trace_client = |
292 | | - absl::make_unique<TraceClient>(channel); |
293 | | - |
294 | | - ::google::devtools::cloudtrace::v2::BatchWriteSpansRequest request; |
295 | | - request.set_name(absl::StrCat("projects/", project_id)); |
296 | | - ConvertSpans(spans, project_id, &request); |
297 | | - |
298 | | - Status status = trace_client->BatchWriteSpans(request); |
299 | | - // Act upon its status. |
300 | | - if (!status.ok()) { |
301 | | - std::cerr << "BatchWriteSpans failed with code " << status.error_code() |
302 | | - << ": " << status.error_message() << "\n"; |
303 | | - } |
| 289 | + ::opencensus::trace::exporter::SpanExporter::RegisterHandler( |
| 290 | + absl::make_unique<Handler>(project_id, channel)); |
304 | 291 | } |
305 | 292 |
|
306 | 293 | } // namespace trace |
|
0 commit comments