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

Commit fd9ac51

Browse files
authored
Simplify stats::StackdriverExporter. (#180)
This makes it look more like trace::StackdriverExporter, and reduces includes.
1 parent d0718f0 commit fd9ac51

5 files changed

Lines changed: 19 additions & 29 deletions

File tree

examples/grpc/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ cc_binary(
5555
"@com_github_grpc_grpc//:grpc++",
5656
"@com_github_grpc_grpc//:grpc_opencensus_plugin",
5757
"@com_google_absl//absl/strings",
58+
"@com_google_absl//absl/time",
5859
],
5960
)
6061

examples/grpc/hello_client.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <grpcpp/grpcpp.h>
2121
#include <grpcpp/opencensus.h>
2222

23+
#include "absl/time/clock.h"
2324
#include "examples/grpc/hello.grpc.pb.h"
2425
#include "examples/grpc/hello.pb.h"
2526
#include "examples/grpc/stackdriver.h"

opencensus/exporters/stats/stackdriver/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ that key.
3535

3636
Please refer to the [Stats access controls](https://cloud.google.com/monitoring/access-control)
3737
and [Trace access controls](https://cloud.google.com/trace/docs/iam)
38-
documentation for configuring roles.
38+
documentation for configuring roles. The "Monitoring Editor" role is required.
3939

4040
### Register the exporter
4141

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ constexpr char kProjectIdPrefix[] = "projects/";
4141
// Stackdriver limits a single CreateTimeSeries request to 200 series.
4242
constexpr int kTimeSeriesBatchSize = 200;
4343

44-
} // namespace
45-
46-
class StackdriverExporter::Handler
47-
: public ::opencensus::stats::StatsExporter::Handler {
44+
class Handler : public ::opencensus::stats::StatsExporter::Handler {
4845
public:
4946
Handler(absl::string_view project_id, absl::string_view opencensus_task);
5047

@@ -70,15 +67,15 @@ class StackdriverExporter::Handler
7067
registered_descriptors_ GUARDED_BY(mu_);
7168
};
7269

73-
StackdriverExporter::Handler::Handler(absl::string_view project_id,
74-
absl::string_view opencensus_task)
70+
Handler::Handler(absl::string_view project_id,
71+
absl::string_view opencensus_task)
7572
: project_id_(absl::StrCat(kProjectIdPrefix, project_id)),
7673
opencensus_task_(opencensus_task),
7774
stub_(google::monitoring::v3::MetricService::NewStub(
7875
::grpc::CreateChannel(kGoogleStackdriverStatsAddress,
7976
::grpc::GoogleDefaultCredentials()))) {}
8077

81-
void StackdriverExporter::Handler::ExportViewData(
78+
void Handler::ExportViewData(
8279
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
8380
opencensus::stats::ViewData>>& data) {
8481
// TODO: refactor to avoid copying the timeseries.
@@ -131,7 +128,7 @@ void StackdriverExporter::Handler::ExportViewData(
131128
}
132129
}
133130

134-
bool StackdriverExporter::Handler::MaybeRegisterView(
131+
bool Handler::MaybeRegisterView(
135132
const opencensus::stats::ViewDescriptor& descriptor) {
136133
const auto& it = registered_descriptors_.find(descriptor.name());
137134
if (it != registered_descriptors_.end()) {
@@ -161,11 +158,13 @@ bool StackdriverExporter::Handler::MaybeRegisterView(
161158
return true;
162159
}
163160

161+
} // namespace
162+
164163
// static
165164
void StackdriverExporter::Register(absl::string_view project_id,
166165
absl::string_view opencensus_task) {
167-
opencensus::stats::StatsExporter::RegisterPushHandler(absl::WrapUnique(
168-
new StackdriverExporter::Handler(project_id, opencensus_task)));
166+
opencensus::stats::StatsExporter::RegisterPushHandler(
167+
absl::WrapUnique(new Handler(project_id, opencensus_task)));
169168
}
170169

171170
} // namespace stats

opencensus/exporters/stats/stackdriver/stackdriver_exporter.h

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,26 @@
1616
#define OPENCENSUS_EXPORTERS_STATS_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1717

1818
#include "absl/strings/string_view.h"
19-
#include "opencensus/stats/stats.h"
2019

2120
namespace opencensus {
2221
namespace exporters {
2322
namespace stats {
2423

2524
// Exports stats for registered views (see opencensus/stats/stats_exporter.h) to
26-
// Stackdriver.
27-
// StackdriverExporter is thread-safe.
28-
class StackdriverExporter : public ::opencensus::stats::StatsExporter::Handler {
25+
// Stackdriver. StackdriverExporter is thread-safe.
26+
class StackdriverExporter {
2927
public:
30-
// Registers the exporter and sets the project ID and task value. The
31-
// following are required to communicate with stackdriver: a valid project ID,
32-
// a valid authentication key which has been generated for that project (with
33-
// the "Monitoring Editor" role), and the server security credentials. An
34-
// authentication key can be generated for your project in the gcp/stackdriver
35-
// settings for your account. Grpc will look for the key using the path
36-
// defined by GOOGLE_APPLICATION_CREDENTIALS environment variable (export
37-
// GOOGLE_APPLICATION_CREDENTIALS=<path_to_key>). The server security
38-
// credentials located in <grpc_path>/etc/roots.pem needs to be copied to
39-
// /usr/share/grpc/roots.pem
40-
// project_id should be the exact id of the project, as in the GCP console,
41-
// with no prefix--e.g. "sample-project-id". opencensus_task is used to
42-
// uniquely identify the task in Stackdriver. The recommended format is
28+
// Registers the exporter and sets the project ID and task value. project_id
29+
// should be the exact id of the project, as in the GCP console, with no
30+
// prefix--e.g. "sample-project-id". opencensus_task is used to uniquely
31+
// identify the task in Stackdriver. The recommended format is
4332
// "{LANGUAGE}-{PID}@{HOSTNAME}"; if {PID} is not available a random number
4433
// may be used.
4534
static void Register(absl::string_view project_id,
4635
absl::string_view opencensus_task);
4736

4837
private:
49-
class Handler;
38+
StackdriverExporter() = delete;
5039
};
5140

5241
} // namespace stats

0 commit comments

Comments
 (0)