Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 5539b5b

Browse files
authored
Stackdriver Trace exporter: allow override of trace_service_stub. (#341)
Add Register(rvalue) and mark Register(ref) deprecated.
1 parent 52fb442 commit 5539b5b

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

opencensus/exporters/trace/stackdriver/internal/stackdriver_exporter.cc

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,13 @@ void ConvertSpans(
239239

240240
class Handler : public ::opencensus::trace::exporter::SpanExporter::Handler {
241241
public:
242-
Handler(const StackdriverOptions& opts,
243-
const std::shared_ptr<grpc::Channel>& channel)
244-
: opts_(opts),
245-
stub_(::google::devtools::cloudtrace::v2::TraceService::NewStub(
246-
channel)) {}
242+
Handler(StackdriverOptions&& opts) : opts_(std::move(opts)) {}
247243

248244
void Export(const std::vector<::opencensus::trace::exporter::SpanData>& spans)
249245
override;
250246

251247
private:
252248
const StackdriverOptions opts_;
253-
std::unique_ptr<google::devtools::cloudtrace::v2::TraceService::Stub> stub_;
254249
};
255250

256251
void Handler::Export(
@@ -261,29 +256,52 @@ void Handler::Export(
261256
::google::protobuf::Empty response;
262257
grpc::ClientContext context;
263258
context.set_deadline(absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
264-
grpc::Status status = stub_->BatchWriteSpans(&context, request, &response);
259+
grpc::Status status =
260+
opts_.trace_service_stub->BatchWriteSpans(&context, request, &response);
265261
if (!status.ok()) {
266262
std::cerr << "BatchWriteSpans failed: "
267263
<< opencensus::common::ToString(status) << "\n";
268264
}
269265
}
270266

271-
} // namespace
272-
273-
// static
274-
void StackdriverExporter::Register(const StackdriverOptions& opts) {
267+
std::unique_ptr<google::devtools::cloudtrace::v2::TraceService::Stub>
268+
MakeStackdriverStub() {
275269
auto channel = ::grpc::CreateCustomChannel(
276270
kGoogleStackdriverTraceAddress, ::grpc::GoogleDefaultCredentials(),
277271
::opencensus::common::WithUserAgent());
272+
return ::google::devtools::cloudtrace::v2::TraceService::NewStub(channel);
273+
}
274+
275+
} // namespace
276+
277+
// static
278+
void StackdriverExporter::Register(StackdriverOptions&& opts) {
279+
if (opts.trace_service_stub == nullptr) {
280+
opts.trace_service_stub = MakeStackdriverStub();
281+
}
282+
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
283+
absl::make_unique<Handler>(std::move(opts)));
284+
}
285+
286+
// static, DEPRECATED
287+
void StackdriverExporter::Register(StackdriverOptions& opts) {
288+
if (opts.trace_service_stub == nullptr) {
289+
opts.trace_service_stub = MakeStackdriverStub();
290+
}
291+
// Copy opts but take ownership of trace_service_stub.
292+
StackdriverOptions copied_opts;
293+
copied_opts.project_id = opts.project_id;
294+
copied_opts.rpc_deadline = opts.rpc_deadline;
295+
copied_opts.trace_service_stub = std::move(opts.trace_service_stub);
278296
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
279-
absl::make_unique<Handler>(opts, channel));
297+
absl::make_unique<Handler>(std::move(copied_opts)));
280298
}
281299

282300
// static, DEPRECATED
283301
void StackdriverExporter::Register(absl::string_view project_id) {
284302
StackdriverOptions opts;
285303
opts.project_id = std::string(project_id);
286-
Register(opts);
304+
Register(std::move(opts));
287305
}
288306

289307
} // namespace trace

opencensus/exporters/trace/stackdriver/stackdriver_exporter.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#ifndef OPENCENSUS_EXPORTERS_TRACE_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1616
#define OPENCENSUS_EXPORTERS_TRACE_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1717

18+
#include <memory>
1819
#include <string>
1920

2021
#include "absl/base/macros.h"
2122
#include "absl/strings/string_view.h"
2223
#include "absl/time/time.h"
24+
#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h"
2325

2426
namespace opencensus {
2527
namespace exporters {
@@ -31,12 +33,25 @@ struct StackdriverOptions {
3133

3234
// The RPC deadline to use when exporting to Stackdriver.
3335
absl::Duration rpc_deadline = absl::Seconds(5);
36+
37+
// (optional) By default, the exporter connects to Stackdriver using gRPC. If
38+
// this stub is non-null, the exporter will use this stub to send gRPC calls
39+
// instead. Useful for testing.
40+
std::unique_ptr<google::devtools::cloudtrace::v2::TraceService::Stub>
41+
trace_service_stub;
3442
};
3543

3644
class StackdriverExporter {
3745
public:
3846
// Registers the exporter.
39-
static void Register(const StackdriverOptions& opts);
47+
static void Register(StackdriverOptions&& opts);
48+
49+
// Registers the exporter. Takes ownership of opts.trace_service_stub
50+
// and resets it to nullptr.
51+
ABSL_DEPRECATED(
52+
"Register() without rvalue StackdriverOptions is deprecated and "
53+
"will be removed on or after 2020-01-18")
54+
static void Register(StackdriverOptions& opts);
4055

4156
// TODO: Retire this:
4257
ABSL_DEPRECATED(

0 commit comments

Comments
 (0)