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

Add StackdriverExporterOptions to StackdriverExporter::Register(). #177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,20 @@ void ConvertSpans(
class Handler : public ::opencensus::trace::exporter::SpanExporter::Handler {
public:
Handler(absl::string_view project_id,
const std::shared_ptr<grpc::Channel>& channel)
const std::shared_ptr<grpc::Channel>& channel,
const StackdriverExporterOptions& opts)
: project_id_(project_id),
stub_(::google::devtools::cloudtrace::v2::TraceService::NewStub(
channel)) {}
stub_(
::google::devtools::cloudtrace::v2::TraceService::NewStub(channel)),
opts_(opts) {}

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

private:
const std::string project_id_;
std::unique_ptr<google::devtools::cloudtrace::v2::TraceService::Stub> stub_;
const StackdriverExporterOptions opts_;
};

void Handler::Export(
Expand All @@ -267,7 +270,7 @@ void Handler::Export(
::google::protobuf::Empty response;
grpc::ClientContext context;
context.set_deadline(
ConvertToTimespec(absl::Now() + absl::Milliseconds(3000)));
ConvertToTimespec(absl::Now() + absl::Seconds(opts_.rpc_deadline_secs)));
grpc::Status status = stub_->BatchWriteSpans(&context, request, &response);
if (!status.ok()) {
std::cerr << "BatchWriteSpans failed: "
Expand All @@ -277,11 +280,23 @@ void Handler::Export(

} // namespace

void StackdriverExporter::Register(absl::string_view project_id) {
grpc::Status StackdriverExporter::Register(
absl::string_view project_id, const StackdriverExporterOptions& opts) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto creds = grpc::GoogleDefaultCredentials();
auto channel = ::grpc::CreateChannel(kGoogleStackdriverTraceAddress, creds);
if (opts.block_until_connected) {
if (!channel->WaitForStateChange(
GRPC_CHANNEL_IDLE,
ConvertToTimespec(absl::Now() +
absl::Seconds(opts.rpc_deadline_secs)))) {
// channel->close()?
return grpc::Status(grpc::StatusCode::DEADLINE_EXCEEDED,
"initial connection timed out");
}
}
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
absl::make_unique<Handler>(project_id, channel));
absl::make_unique<Handler>(project_id, channel, opts));
return grpc::Status::OK;
}

} // namespace trace
Expand Down
22 changes: 20 additions & 2 deletions opencensus/exporters/trace/stackdriver/stackdriver_exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,33 @@
#define OPENCENSUS_EXPORTERS_TRACE_STACKDRIVER_STACKDRIVER_EXPORTER_H_

#include "absl/strings/string_view.h"
#include "opencensus/common/internal/grpc/status.h"

namespace opencensus {
namespace exporters {
namespace trace {

struct StackdriverExporterOptions {
// If true, Register() blocks until a connection is established and returns a
// meaningful Status object.
bool block_until_connected = false;

// Deadline to set on outgoing RPCs.
double rpc_deadline_secs = 3.0;

static StackdriverExporterOptions Default() {
return StackdriverExporterOptions();
}
};

class StackdriverExporter {
public:
// Registers the exporter and sets the project ID.
static void Register(absl::string_view project_id);
// Registers the exporter and sets the project ID. Returns OK and initializes
// in the background, unless opts.block_until_connected is true, in which case
// it blocks and returns a meaningful Status.
static grpc::Status Register(absl::string_view project_id,
const StackdriverExporterOptions& opts =
StackdriverExporterOptions::Default());

private:
StackdriverExporter() = delete;
Expand Down