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

Commit 9cde3c9

Browse files
authored
Make exporters/trace/stackdriver more consistent with the stats one. (#105)
- This simplifies the public API. - Remove stackdriver_exporter_test for now, it wasn't testing anything. - Add README pointing at the stats/ README.
1 parent 89a62b1 commit 9cde3c9

File tree

6 files changed

+56
-202
lines changed

6 files changed

+56
-202
lines changed

opencensus/exporters/stats/stackdriver/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ In order to be able to push your stats to [Stackdriver Monitoring](stackdriver-m
1919
These steps enable the API but don't require that your app is hosted on Google Cloud Platform.
2020

2121
### Setup authentication
22+
2223
The Stackdriver exporter uses gRPC, which requires a certificate
2324
(`etc/roots.pem` in the gRPC repository) copied to
2425
to `/usr/share/grpc/roots.pem`.
2526

2627
If your application runs on Google Cloud Platform, it can automatically
2728
determine credentials to authenticate to Stackdriver from the VM environment.
29+
2830
Otherwise, create a
29-
[Google Cloud service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts)
30-
with the "Monitoring Editor" role, create and download a service account key,
31+
[Google Cloud service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts),
32+
create and download a service account key,
3133
and set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the path to
3234
that key.
3335

36+
Please refer to the [Stats access controls](https://cloud.google.com/monitoring/access-control)
37+
and [Trace access controls](https://cloud.google.com/trace/docs/iam)
38+
documentation for configuring roles.
39+
3440
### Register the exporter
3541

3642
`#include opencensus/exporters/stats/stackdriver/stackdriver_exporter.h` (if

opencensus/exporters/trace/stackdriver/BUILD

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ load("//opencensus:copts.bzl", "DEFAULT_COPTS", "TEST_COPTS")
1616

1717
licenses(["notice"]) # Apache License 2.0
1818

19-
package(default_visibility = ["//visibility:public"])
19+
package(default_visibility = ["//visibility:private"])
2020

2121
# Libraries
2222
# ========================================================================= #
@@ -26,27 +26,12 @@ cc_library(
2626
srcs = ["internal/stackdriver_exporter.cc"],
2727
hdrs = ["stackdriver_exporter.h"],
2828
copts = DEFAULT_COPTS,
29+
visibility = ["//visibility:public"],
2930
deps = [
3031
"//google/devtools/cloudtrace/v2:tracing_proto",
3132
"//opencensus/trace",
3233
"@com_github_grpc_grpc//:grpc++",
33-
"@com_google_absl//absl/base:core_headers",
3434
"@com_google_absl//absl/memory",
3535
"@com_google_absl//absl/strings",
3636
],
3737
)
38-
39-
# Tests
40-
# ========================================================================= #
41-
42-
cc_test(
43-
name = "stackdriver_exporter_test",
44-
srcs = ["internal/stackdriver_exporter_test.cc"],
45-
copts = TEST_COPTS,
46-
deps = [
47-
":stackdriver_exporter",
48-
"//opencensus/trace",
49-
"@com_github_grpc_grpc//test/core/util:gpr_test_util",
50-
"@com_google_googletest//:gtest_main",
51-
],
52-
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Please refer to the
2+
[Stackdriver Stats Exporter instructions](../../stats/stackdriver/README.md) for
3+
setup.

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

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,32 @@
2121
#include "absl/memory/memory.h"
2222
#include "absl/strings/str_cat.h"
2323
#include "absl/time/clock.h"
24+
#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h"
25+
#include "include/grpc++/grpc++.h"
26+
#include "opencensus/trace/exporter/span_data.h"
27+
#include "opencensus/trace/exporter/span_exporter.h"
2428

2529
using grpc::ClientContext;
2630
using grpc::Status;
2731

2832
namespace opencensus {
2933
namespace exporters {
3034
namespace trace {
31-
32-
static constexpr size_t kAttributeStringLen = 256;
33-
static constexpr size_t kAnnotationStringLen = 256;
34-
static constexpr size_t kDisplayNameStringLen = 128;
35-
static constexpr char kGoogleStackDriverTraceAddress[] =
36-
"cloudtrace.googleapis.com";
37-
3835
namespace {
3936

37+
constexpr size_t kAttributeStringLen = 256;
38+
constexpr size_t kAnnotationStringLen = 256;
39+
constexpr size_t kDisplayNameStringLen = 128;
40+
constexpr char kGoogleStackDriverTraceAddress[] = "cloudtrace.googleapis.com";
41+
4042
constexpr char kAgentKey[] = "g.co/agent";
4143
constexpr char kAgentValue[] = "opencensus-cpp";
4244

45+
std::string ToString(const grpc::Status& status) {
46+
return absl::StrCat("status code ", status.error_code(), " details \"",
47+
status.error_message(), "\"");
48+
}
49+
4350
gpr_timespec ConvertToTimespec(absl::Time time) {
4451
gpr_timespec g_time;
4552
int64_t secs = absl::ToUnixSeconds(time);
@@ -243,64 +250,44 @@ void ConvertSpans(
243250
}
244251
}
245252

246-
} // namespace
253+
class Handler : public ::opencensus::trace::exporter::SpanExporter::Handler {
254+
public:
255+
Handler(absl::string_view project_id,
256+
const std::shared_ptr<grpc::Channel>& channel)
257+
: project_id_(project_id),
258+
stub_(::google::devtools::cloudtrace::v2::TraceService::NewStub(
259+
channel)) {}
247260

248-
Status StackdriverExporter::TraceClient::BatchWriteSpans(
249-
const ::google::devtools::cloudtrace::v2::BatchWriteSpansRequest& request) {
250-
::google::protobuf::Empty response;
261+
void Export(const std::vector<::opencensus::trace::exporter::SpanData>& spans)
262+
override;
251263

252-
// Context for the client. It could be used to convey extra information to
253-
// the server and/or tweak certain RPC behaviors. Deadline set for 3000
254-
// milliseconds.
255-
ClientContext context;
256-
context.set_deadline(
257-
ConvertToTimespec(absl::Now() + absl::Milliseconds(3000)));
258-
259-
// The actual RPC that sends the span information to Stackdriver.
260-
return stub_->BatchWriteSpans(&context, request, &response);
261-
}
262-
263-
void StackdriverExporter::Register(absl::string_view project_id) {
264-
StackdriverExporter* exporter = new StackdriverExporter(project_id);
265-
auto creds = grpc::GoogleDefaultCredentials();
266-
auto channel = ::grpc::CreateChannel(kGoogleStackDriverTraceAddress, creds);
267-
exporter->trace_client_ = absl::make_unique<TraceClient>(channel);
268-
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
269-
absl::WrapUnique<::opencensus::trace::exporter::SpanExporter::Handler>(
270-
exporter));
271-
}
264+
private:
265+
const std::string project_id_;
266+
std::unique_ptr<google::devtools::cloudtrace::v2::TraceService::Stub> stub_;
267+
};
272268

273-
void StackdriverExporter::Export(
269+
void Handler::Export(
274270
const std::vector<::opencensus::trace::exporter::SpanData>& spans) {
275271
::google::devtools::cloudtrace::v2::BatchWriteSpansRequest request;
276272
request.set_name(absl::StrCat("projects/", project_id_));
277273
ConvertSpans(spans, project_id_, &request);
278-
279-
Status status = trace_client_->BatchWriteSpans(request);
280-
// Act upon its status.
274+
::google::protobuf::Empty response;
275+
ClientContext context;
276+
context.set_deadline(
277+
ConvertToTimespec(absl::Now() + absl::Milliseconds(3000)));
278+
Status status = stub_->BatchWriteSpans(&context, request, &response);
281279
if (!status.ok()) {
282-
// TODO: log error.
280+
std::cerr << "BatchWriteSpans failed: " << ToString(status) << "\n";
283281
}
284282
}
285283

286-
void StackdriverExporter::ExportForTesting(
287-
absl::string_view project_id,
288-
const std::vector<::opencensus::trace::exporter::SpanData>& spans) {
284+
} // namespace
285+
286+
void StackdriverExporter::Register(absl::string_view project_id) {
289287
auto creds = grpc::GoogleDefaultCredentials();
290288
auto channel = ::grpc::CreateChannel(kGoogleStackDriverTraceAddress, creds);
291-
std::unique_ptr<StackdriverExporter::TraceClient> trace_client =
292-
absl::make_unique<TraceClient>(channel);
293-
294-
::google::devtools::cloudtrace::v2::BatchWriteSpansRequest request;
295-
request.set_name(absl::StrCat("projects/", project_id));
296-
ConvertSpans(spans, project_id, &request);
297-
298-
Status status = trace_client->BatchWriteSpans(request);
299-
// Act upon its status.
300-
if (!status.ok()) {
301-
std::cerr << "BatchWriteSpans failed with code " << status.error_code()
302-
<< ": " << status.error_message() << "\n";
303-
}
289+
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
290+
absl::make_unique<Handler>(project_id, channel));
304291
}
305292

306293
} // namespace trace

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

Lines changed: 0 additions & 84 deletions
This file was deleted.

opencensus/exporters/trace/stackdriver/stackdriver_exporter.h

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,19 @@
1515
#ifndef OPENCENSUS_EXPORTERS_TRACE_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1616
#define OPENCENSUS_EXPORTERS_TRACE_STACKDRIVER_STACKDRIVER_EXPORTER_H_
1717

18-
#include <memory>
19-
#include <string>
20-
#include <vector>
21-
22-
#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h"
23-
#include "include/grpc++/grpc++.h"
24-
#include "opencensus/trace/exporter/span_data.h"
25-
#include "opencensus/trace/exporter/span_exporter.h"
18+
#include "absl/strings/string_view.h"
2619

2720
namespace opencensus {
2821
namespace exporters {
2922
namespace trace {
3023

31-
class StackdriverExporter
32-
: public ::opencensus::trace::exporter::SpanExporter::Handler {
24+
class StackdriverExporter {
3325
public:
34-
StackdriverExporter(absl::string_view project_id) : project_id_(project_id) {}
35-
void Export(const std::vector<::opencensus::trace::exporter::SpanData>& spans)
36-
override;
37-
38-
// Registers the exporter and sets the project Id. The following are required
39-
// to communicate with stackdriver: a valid project Id, a valid authentication
40-
// key which has been generated for that project, and the server security
41-
// credentials. An authentication key can be generated for your project in the
42-
// gcp/stackdriver settings for your account. Grpc will look for the key using
43-
// the path defined by GOOGLE_APPLICATION_CREDENTIALS environment variable
44-
// (export GOOGLE_APPLICATION_CREDENTIALS=<path_to_key>). The server security
45-
// credentials located in <grpc_path>/etc/roots.pem needs to be copied to
46-
// /usr/share/grpc/roots.pem
26+
// Registers the exporter and sets the project ID.
4727
static void Register(absl::string_view project_id);
4828

4929
private:
50-
class TraceClient {
51-
public:
52-
TraceClient(const std::shared_ptr<grpc::Channel>& channel)
53-
: stub_(google::devtools::cloudtrace::v2::TraceService::NewStub(
54-
channel)) {}
55-
56-
// Packages a batch of spans into a single request and writes it to
57-
// stackdriver. Returns the status of the operation.
58-
grpc::Status BatchWriteSpans(
59-
const ::google::devtools::cloudtrace::v2::BatchWriteSpansRequest&
60-
request);
61-
62-
private:
63-
std::unique_ptr<google::devtools::cloudtrace::v2::TraceService::Stub> stub_;
64-
};
65-
66-
friend class StackdriverExporterTestPeer;
67-
68-
static void ExportForTesting(
69-
absl::string_view project_id,
70-
const std::vector<::opencensus::trace::exporter::SpanData>& spans);
71-
72-
const std::string project_id_;
73-
std::unique_ptr<TraceClient> trace_client_;
30+
StackdriverExporter() = delete;
7431
};
7532

7633
} // namespace trace

0 commit comments

Comments
 (0)