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

Commit 3846d21

Browse files
authored
Introduce StackdriverOptions for stats and trace exporters. (#182)
1 parent 0d436fd commit 3846d21

9 files changed

Lines changed: 100 additions & 34 deletions

File tree

examples/grpc/stackdriver.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ void RegisterStackdriverExporters() {
3030
}
3131
const char *hostname = getenv("HOSTNAME");
3232
if (hostname == nullptr) hostname = "hostname";
33-
const std::string opencensus_task =
34-
absl::StrCat("cpp-", getpid(), "@", hostname);
35-
opencensus::exporters::stats::StackdriverExporter::Register(project_id,
36-
opencensus_task);
37-
opencensus::exporters::trace::StackdriverExporter::Register(project_id);
33+
34+
opencensus::exporters::stats::StackdriverOptions stats_opts;
35+
stats_opts.project_id = project_id;
36+
stats_opts.opencensus_task = absl::StrCat("cpp-", getpid(), "@", hostname);
37+
38+
opencensus::exporters::trace::StackdriverOptions trace_opts;
39+
trace_opts.project_id = project_id;
40+
41+
opencensus::exporters::stats::StackdriverExporter::Register(stats_opts);
42+
opencensus::exporters::trace::StackdriverExporter::Register(trace_opts);
3843
}

opencensus/exporters/stats/stackdriver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cc_library(
3030
"//opencensus/common/internal/grpc:status",
3131
"//opencensus/stats",
3232
"@com_github_grpc_grpc//:grpc++",
33+
"@com_google_absl//absl/base",
3334
"@com_google_absl//absl/strings",
3435
"@com_google_absl//absl/synchronization",
3536
"@com_google_absl//absl/time",

opencensus/exporters/stats/stackdriver/README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,33 @@ documentation for configuring roles. The "Monitoring Editor" role is required.
3939

4040
### Register the exporter
4141

42-
`#include opencensus/exporters/stats/stackdriver/stackdriver_exporter.h` (if
43-
using Bazel, this requires a dependency on
44-
`"@io_opencensus_cpp//exporters/stats/stackdriver:stackdriver_exporter"`).
42+
Include:
43+
44+
```c++
45+
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
46+
```
47+
48+
Add a BUILD dependency on:
49+
50+
```
51+
"@io_opencensus_cpp//exporters/stats/stackdriver:stackdriver_exporter",
52+
```
53+
4554
In your application's initialization code, register the exporter:
55+
4656
```c++
4757
const char* hostname = getenv("HOSTNAME");
4858
if (hostname == nullptr) hostname = "hostname";
49-
const std::string opencensus_task =
50-
absl::StrCat("cpp-", getpid(), "@", hostname);
51-
opencensus::exporters::stats::StackdriverExporter::Register(
52-
"my-stackdriver-project-id", opencensus_task);
59+
60+
opencensus::exporters::stats::StackdriverOptions opts;
61+
opts.project_id = "my-stackdriver-project-id";
62+
opts.opencensus_task = absl::StrCat("cpp-", getpid(), "@", hostname);
63+
64+
opencensus::exporters::stats::StackdriverExporter::Register(opts);
5365
```
54-
The `opencensus_task` may be anything, but must be unique among all exporters
55-
simultaneously exporting to Stackdriver concurrently; the format
66+
67+
The `opencensus_task` may be anything, but must be unique among all processes
68+
simultaneously exporting to Stackdriver. The format
5669
`"cpp-${PROCESS_ID}@${HOSTNAME}"` is recommended.
5770
5871
### Register views and record stats

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace exporters {
3535
namespace stats {
3636
namespace {
3737

38-
// This is a true end-to-end test for the stackdriver exporter, connecting to
38+
// This is a true end-to-end test for the Stackdriver Exporter, connecting to
3939
// production Stackdriver. As such, it is subject to failures in networking or
4040
// the Stackdriver backend; it also cannot be run multiple times simultaneously
4141
// under the same Cloud project.
@@ -66,7 +66,10 @@ class StackdriverE2eTest : public ::testing::Test {
6666
"unset.\n";
6767
std::abort();
6868
}
69-
StackdriverExporter::Register(project_id_, "test_task");
69+
StackdriverOptions opts;
70+
opts.project_id = std::string(project_id_);
71+
opts.opencensus_task = "test_task";
72+
StackdriverExporter::Register(opts);
7073
}
7174

7275
// Retrieves data exported under 'descriptor'.

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ constexpr int kTimeSeriesBatchSize = 200;
4343

4444
class Handler : public ::opencensus::stats::StatsExporter::Handler {
4545
public:
46-
Handler(absl::string_view project_id, absl::string_view opencensus_task);
46+
explicit Handler(const StackdriverOptions& opts);
4747

4848
void ExportViewData(
4949
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
@@ -59,18 +59,17 @@ class Handler : public ::opencensus::stats::StatsExporter::Handler {
5959
bool MaybeRegisterView(const opencensus::stats::ViewDescriptor& descriptor)
6060
EXCLUSIVE_LOCKS_REQUIRED(mu_);
6161

62+
const StackdriverOptions opts_;
6263
const std::string project_id_;
63-
const std::string opencensus_task_;
6464
const std::unique_ptr<google::monitoring::v3::MetricService::Stub> stub_;
6565
mutable absl::Mutex mu_;
6666
std::unordered_map<std::string, opencensus::stats::ViewDescriptor>
6767
registered_descriptors_ GUARDED_BY(mu_);
6868
};
6969

70-
Handler::Handler(absl::string_view project_id,
71-
absl::string_view opencensus_task)
72-
: project_id_(absl::StrCat(kProjectIdPrefix, project_id)),
73-
opencensus_task_(opencensus_task),
70+
Handler::Handler(const StackdriverOptions& opts)
71+
: opts_(opts),
72+
project_id_(absl::StrCat(kProjectIdPrefix, opts.project_id)),
7473
stub_(google::monitoring::v3::MetricService::NewStub(
7574
::grpc::CreateChannel(kGoogleStackdriverStatsAddress,
7675
::grpc::GoogleDefaultCredentials()))) {}
@@ -86,7 +85,7 @@ void Handler::ExportViewData(
8685
continue;
8786
}
8887
const auto view_time_series =
89-
MakeTimeSeries(datum.first, datum.second, opencensus_task_);
88+
MakeTimeSeries(datum.first, datum.second, opts_.opencensus_task);
9089
time_series.insert(time_series.end(), view_time_series.begin(),
9190
view_time_series.end());
9291
}
@@ -161,10 +160,18 @@ bool Handler::MaybeRegisterView(
161160
} // namespace
162161

163162
// static
163+
void StackdriverExporter::Register(const StackdriverOptions& opts) {
164+
opencensus::stats::StatsExporter::RegisterPushHandler(
165+
absl::WrapUnique(new Handler(opts)));
166+
}
167+
168+
// static, DEPRECATED
164169
void StackdriverExporter::Register(absl::string_view project_id,
165170
absl::string_view opencensus_task) {
166-
opencensus::stats::StatsExporter::RegisterPushHandler(
167-
absl::WrapUnique(new Handler(project_id, opencensus_task)));
171+
StackdriverOptions opts;
172+
opts.project_id = std::string(project_id);
173+
opts.opencensus_task = std::string(opencensus_task);
174+
Register(opts);
168175
}
169176

170177
} // namespace stats

opencensus/exporters/stats/stackdriver/stackdriver_exporter.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,36 @@
1515
#ifndef OPENCENSUS_EXPORTERS_STATS_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1616
#define OPENCENSUS_EXPORTERS_STATS_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1717

18+
#include <string>
19+
20+
#include "absl/base/macros.h"
1821
#include "absl/strings/string_view.h"
1922

2023
namespace opencensus {
2124
namespace exporters {
2225
namespace stats {
2326

27+
struct StackdriverOptions {
28+
// The Stackdriver project ID to use.
29+
std::string project_id;
30+
31+
// The opencensus_task is used to uniquely identify the task in Stackdriver.
32+
// The recommended format is "{LANGUAGE}-{PID}@{HOSTNAME}". If PID is not
33+
// available, a random number may be used.
34+
std::string opencensus_task;
35+
};
36+
2437
// Exports stats for registered views (see opencensus/stats/stats_exporter.h) to
2538
// Stackdriver. StackdriverExporter is thread-safe.
2639
class StackdriverExporter {
2740
public:
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
32-
// "{LANGUAGE}-{PID}@{HOSTNAME}"; if {PID} is not available a random number
33-
// may be used.
41+
// Registers the exporter.
42+
static void Register(const StackdriverOptions& opts);
43+
44+
// TODO: Retire this:
45+
ABSL_DEPRECATED(
46+
"Register() without StackdriverOptions is deprecated and "
47+
"will be removed on or after 2019-03-20")
3448
static void Register(absl::string_view project_id,
3549
absl::string_view opencensus_task);
3650

opencensus/exporters/trace/stackdriver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ cc_library(
3232
"//opencensus/common/internal/grpc:status",
3333
"//opencensus/trace",
3434
"@com_github_grpc_grpc//:grpc++",
35+
"@com_google_absl//absl/base",
3536
"@com_google_absl//absl/memory",
3637
"@com_google_absl//absl/strings",
3738
],

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,19 @@ void Handler::Export(
277277

278278
} // namespace
279279

280-
void StackdriverExporter::Register(absl::string_view project_id) {
280+
// static
281+
void StackdriverExporter::Register(const StackdriverOptions& opts) {
281282
auto creds = grpc::GoogleDefaultCredentials();
282283
auto channel = ::grpc::CreateChannel(kGoogleStackdriverTraceAddress, creds);
283284
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
284-
absl::make_unique<Handler>(project_id, channel));
285+
absl::make_unique<Handler>(opts.project_id, channel));
286+
}
287+
288+
// static, DEPRECATED
289+
void StackdriverExporter::Register(absl::string_view project_id) {
290+
StackdriverOptions opts;
291+
opts.project_id = std::string(project_id);
292+
Register(opts);
285293
}
286294

287295
} // namespace trace

opencensus/exporters/trace/stackdriver/stackdriver_exporter.h

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

18+
#include <string>
19+
20+
#include "absl/base/macros.h"
1821
#include "absl/strings/string_view.h"
1922

2023
namespace opencensus {
2124
namespace exporters {
2225
namespace trace {
2326

27+
struct StackdriverOptions {
28+
// The Stackdriver project ID to use.
29+
std::string project_id;
30+
};
31+
2432
class StackdriverExporter {
2533
public:
26-
// Registers the exporter and sets the project ID.
34+
// Registers the exporter.
35+
static void Register(const StackdriverOptions& opts);
36+
37+
// TODO: Retire this:
38+
ABSL_DEPRECATED(
39+
"Register() without StackdriverOptions is deprecated and "
40+
"will be removed on or after 2019-03-20")
2741
static void Register(absl::string_view project_id);
2842

2943
private:

0 commit comments

Comments
 (0)