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

Commit c43dcef

Browse files
authored
Stackdriver Stats exporter: allow override of metric_service_stub. (#348)
1 parent 5539b5b commit c43dcef

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

examples/grpc/stackdriver.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ void RegisterStackdriverExporters() {
5858
opencensus::exporters::trace::StackdriverOptions trace_opts;
5959
trace_opts.project_id = project_id;
6060

61-
opencensus::exporters::stats::StackdriverExporter::Register(stats_opts);
62-
opencensus::exporters::trace::StackdriverExporter::Register(trace_opts);
61+
opencensus::exporters::stats::StackdriverExporter::Register(
62+
std::move(stats_opts));
63+
opencensus::exporters::trace::StackdriverExporter::Register(
64+
std::move(trace_opts));
6365
}

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

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ constexpr char kDefaultMetricNamePrefix[] = "custom.googleapis.com/opencensus/";
4747

4848
class Handler : public ::opencensus::stats::StatsExporter::Handler {
4949
public:
50-
explicit Handler(const StackdriverOptions& opts);
50+
explicit Handler(StackdriverOptions&& opts);
5151

5252
void ExportViewData(
5353
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
@@ -64,26 +64,33 @@ class Handler : public ::opencensus::stats::StatsExporter::Handler {
6464
EXCLUSIVE_LOCKS_REQUIRED(mu_);
6565

6666
const StackdriverOptions opts_;
67-
const std::string project_id_;
68-
const std::string metric_name_prefix_;
69-
const std::unique_ptr<google::monitoring::v3::MetricService::Stub> stub_;
7067
mutable absl::Mutex mu_;
7168
std::unordered_map<std::string, opencensus::stats::ViewDescriptor>
7269
registered_descriptors_ GUARDED_BY(mu_);
7370
};
7471

75-
Handler::Handler(const StackdriverOptions& opts)
76-
: opts_(opts),
77-
project_id_(absl::StrCat(kProjectIdPrefix, opts.project_id)),
78-
metric_name_prefix_(opts.metric_name_prefix.empty()
79-
? kDefaultMetricNamePrefix
80-
: opts.metric_name_prefix),
81-
stub_(google::monitoring::v3::MetricService::NewStub(
82-
::grpc::CreateCustomChannel(kGoogleStackdriverStatsAddress,
83-
::grpc::GoogleDefaultCredentials(),
84-
::opencensus::common::WithUserAgent()))) {
72+
StackdriverOptions SetOptionDefaults(StackdriverOptions&& o) {
73+
StackdriverOptions opts(std::move(o));
74+
75+
// Prepend prefix because we always use this with a prefix.
76+
opts.project_id = absl::StrCat(kProjectIdPrefix, opts.project_id);
77+
78+
if (opts.metric_name_prefix.empty()) {
79+
opts.metric_name_prefix = kDefaultMetricNamePrefix;
80+
}
81+
82+
if (opts.metric_service_stub == nullptr) {
83+
opts.metric_service_stub = google::monitoring::v3::MetricService::NewStub(
84+
::grpc::CreateCustomChannel(kGoogleStackdriverStatsAddress,
85+
::grpc::GoogleDefaultCredentials(),
86+
::opencensus::common::WithUserAgent()));
87+
}
88+
return opts;
8589
}
8690

91+
Handler::Handler(StackdriverOptions&& opts)
92+
: opts_(SetOptionDefaults(std::move(opts))) {}
93+
8794
void Handler::ExportViewData(
8895
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
8996
opencensus::stats::ViewData>>& data) {
@@ -95,7 +102,7 @@ void Handler::ExportViewData(
95102
continue;
96103
}
97104
const auto view_time_series =
98-
MakeTimeSeries(metric_name_prefix_, opts_.monitored_resource,
105+
MakeTimeSeries(opts_.metric_name_prefix, opts_.monitored_resource,
99106
opts_.per_metric_monitored_resource, datum.first,
100107
datum.second, opts_.opencensus_task);
101108
time_series.insert(time_series.end(), view_time_series.begin(),
@@ -113,15 +120,16 @@ void Handler::ExportViewData(
113120

114121
for (int rpc_index = 0; rpc_index < num_rpcs; ++rpc_index) {
115122
auto request = google::monitoring::v3::CreateTimeSeriesRequest();
116-
request.set_name(project_id_);
123+
request.set_name(opts_.project_id);
117124
const int batch_end = std::min(static_cast<int>(time_series.size()),
118125
(rpc_index + 1) * kTimeSeriesBatchSize);
119126
for (int i = rpc_index * kTimeSeriesBatchSize; i < batch_end; ++i) {
120127
*request.add_time_series() = time_series[i];
121128
}
122129
ctx[rpc_index].set_deadline(
123130
absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
124-
auto rpc(stub_->AsyncCreateTimeSeries(&ctx[rpc_index], request, &cq));
131+
auto rpc(opts_.metric_service_stub->AsyncCreateTimeSeries(&ctx[rpc_index],
132+
request, &cq));
125133
rpc->Finish(&response, &status[rpc_index], (void*)(uintptr_t)rpc_index);
126134
}
127135

@@ -153,14 +161,14 @@ bool Handler::MaybeRegisterView(
153161
}
154162

155163
auto request = google::monitoring::v3::CreateMetricDescriptorRequest();
156-
request.set_name(project_id_);
157-
SetMetricDescriptor(project_id_, metric_name_prefix_, descriptor,
164+
request.set_name(opts_.project_id);
165+
SetMetricDescriptor(opts_.project_id, opts_.metric_name_prefix, descriptor,
158166
request.mutable_metric_descriptor());
159167
::grpc::ClientContext context;
160168
context.set_deadline(absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
161169
google::api::MetricDescriptor response;
162-
::grpc::Status status =
163-
stub_->CreateMetricDescriptor(&context, request, &response);
170+
::grpc::Status status = opts_.metric_service_stub->CreateMetricDescriptor(
171+
&context, request, &response);
164172
if (!status.ok()) {
165173
std::cerr << "CreateMetricDescriptor request failed: "
166174
<< opencensus::common::ToString(status) << "\n";
@@ -173,9 +181,25 @@ bool Handler::MaybeRegisterView(
173181
} // namespace
174182

175183
// static
176-
void StackdriverExporter::Register(const StackdriverOptions& opts) {
184+
void StackdriverExporter::Register(StackdriverOptions&& opts) {
185+
opencensus::stats::StatsExporter::RegisterPushHandler(
186+
absl::WrapUnique(new Handler(std::move(opts))));
187+
}
188+
189+
// static, DEPRECATED
190+
void StackdriverExporter::Register(StackdriverOptions& opts) {
191+
// Copy opts but take ownership of metric_service_stub.
192+
StackdriverOptions copied_opts;
193+
copied_opts.project_id = opts.project_id;
194+
copied_opts.opencensus_task = opts.opencensus_task;
195+
copied_opts.rpc_deadline = opts.rpc_deadline;
196+
copied_opts.monitored_resource = opts.monitored_resource;
197+
copied_opts.per_metric_monitored_resource =
198+
opts.per_metric_monitored_resource;
199+
copied_opts.metric_name_prefix = opts.metric_name_prefix;
200+
copied_opts.metric_service_stub = std::move(opts.metric_service_stub);
177201
opencensus::stats::StatsExporter::RegisterPushHandler(
178-
absl::WrapUnique(new Handler(opts)));
202+
absl::WrapUnique(new Handler(std::move(opts))));
179203
}
180204

181205
// static, DEPRECATED
@@ -184,7 +208,7 @@ void StackdriverExporter::Register(absl::string_view project_id,
184208
StackdriverOptions opts;
185209
opts.project_id = std::string(project_id);
186210
opts.opencensus_task = std::string(opencensus_task);
187-
Register(opts);
211+
Register(std::move(opts));
188212
}
189213

190214
} // namespace stats

opencensus/exporters/stats/stackdriver/stackdriver_exporter.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
#ifndef OPENCENSUS_EXPORTERS_STATS_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1616
#define OPENCENSUS_EXPORTERS_STATS_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"
2324
#include "google/api/monitored_resource.pb.h"
25+
#include "google/monitoring/v3/metric_service.grpc.pb.h"
2426

2527
namespace opencensus {
2628
namespace exporters {
@@ -71,14 +73,27 @@ struct StackdriverOptions {
7173
// If empty, the exporter will use "custom.googleapis.com/opencensus/" by
7274
// default.
7375
std::string metric_name_prefix;
76+
77+
// Optional: by default, the exporter connects to Stackdriver using gRPC. If
78+
// this stub is non-null, the exporter will use this stub to send gRPC calls
79+
// instead. Useful for testing.
80+
std::unique_ptr<google::monitoring::v3::MetricService::Stub>
81+
metric_service_stub;
7482
};
7583

7684
// Exports stats for registered views (see opencensus/stats/stats_exporter.h) to
7785
// Stackdriver. StackdriverExporter is thread-safe.
7886
class StackdriverExporter {
7987
public:
8088
// Registers the exporter.
81-
static void Register(const StackdriverOptions& opts);
89+
static void Register(StackdriverOptions&& opts);
90+
91+
// Registers the exporter. Takes ownership of opts.metric_service_stub
92+
// and resets it to nullptr.
93+
ABSL_DEPRECATED(
94+
"Register() without rvalue StackdriverOptions is deprecated and "
95+
"will be removed on or after 2020-01-18")
96+
static void Register(StackdriverOptions& opts);
8297

8398
// TODO: Retire this:
8499
ABSL_DEPRECATED(

0 commit comments

Comments
 (0)