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

Commit cad0d03

Browse files
authored
OcAgent: report errors, add to example, add docs. (#368)
* Install all exporters other than prometheus in exporters.cc. * Emit clearer errors. * Add example config.yaml.
1 parent f91d5ed commit cad0d03

10 files changed

Lines changed: 123 additions & 90 deletions

File tree

WORKSPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ switched_rules_by_language(
151151
grpc = True,
152152
)
153153

154-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
155-
154+
# Needed by opencensus-proto.
156155
http_archive(
157156
name = "io_bazel_rules_go",
158157
sha256 = "9fb16af4d4836c8222142e54c9efa0bb5fc562ffc893ce2abeac3e25daead144",
@@ -182,6 +181,7 @@ grpc_java_repositories(
182181
omit_com_google_protobuf = True,
183182
)
184183

184+
# OpenCensus proto - used by OcAgent exporter.
185185
http_archive(
186186
name = "io_opencensus_proto",
187187
strip_prefix = "opencensus-proto-master/src",

examples/grpc/BUILD

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ cc_grpc_library(
3737
)
3838

3939
cc_library(
40-
name = "stackdriver",
41-
srcs = ["stackdriver.cc"],
42-
hdrs = ["stackdriver.h"],
40+
name = "exporters",
41+
srcs = ["exporters.cc"],
42+
hdrs = ["exporters.h"],
4343
copts = DEFAULT_COPTS,
4444
deps = [
4545
"//opencensus/exporters/stats/stackdriver:stackdriver_exporter",
46+
"//opencensus/exporters/stats/stdout:stdout_exporter",
47+
"//opencensus/exporters/trace/ocagent:ocagent_exporter",
4648
"//opencensus/exporters/trace/stackdriver:stackdriver_exporter",
49+
"//opencensus/exporters/trace/stdout:stdout_exporter",
4750
"@com_google_absl//absl/strings",
4851
],
4952
)
@@ -53,13 +56,9 @@ cc_binary(
5356
srcs = ["hello_client.cc"],
5457
copts = DEFAULT_COPTS,
5558
deps = [
59+
":exporters",
5660
":hello_cc_grpc",
5761
":hello_cc_proto",
58-
":stackdriver",
59-
"//opencensus/exporters/stats/stackdriver:stackdriver_exporter",
60-
"//opencensus/exporters/stats/stdout:stdout_exporter",
61-
"//opencensus/exporters/trace/stackdriver:stackdriver_exporter",
62-
"//opencensus/exporters/trace/stdout:stdout_exporter",
6362
"//opencensus/trace",
6463
"@com_github_grpc_grpc//:grpc++",
6564
"@com_github_grpc_grpc//:grpc_opencensus_plugin",
@@ -73,14 +72,10 @@ cc_binary(
7372
srcs = ["hello_server.cc"],
7473
copts = DEFAULT_COPTS,
7574
deps = [
75+
":exporters",
7676
":hello_cc_grpc",
7777
":hello_cc_proto",
78-
":stackdriver",
7978
"//opencensus/exporters/stats/prometheus:prometheus_exporter",
80-
"//opencensus/exporters/stats/stackdriver:stackdriver_exporter",
81-
"//opencensus/exporters/stats/stdout:stdout_exporter",
82-
"//opencensus/exporters/trace/stackdriver:stackdriver_exporter",
83-
"//opencensus/exporters/trace/stdout:stdout_exporter",
8479
"//opencensus/trace",
8580
"@com_github_grpc_grpc//:grpc++",
8681
"@com_github_grpc_grpc//:grpc_opencensus_plugin",

examples/grpc/exporters.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "examples/grpc/exporters.h"
16+
17+
#include <cstdlib>
18+
#include <iostream>
19+
20+
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
21+
#include "opencensus/exporters/stats/stdout/stdout_exporter.h"
22+
#include "opencensus/exporters/trace/ocagent/ocagent_exporter.h"
23+
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
24+
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
25+
26+
void RegisterExporters() {
27+
// For debugging, register exporters that just write to stdout.
28+
opencensus::exporters::stats::StdoutExporter::Register();
29+
opencensus::exporters::trace::StdoutExporter::Register();
30+
31+
const char* project_id = getenv("STACKDRIVER_PROJECT_ID");
32+
if (project_id == nullptr) {
33+
std::cerr << "The STACKDRIVER_PROJECT_ID environment variable is not set: "
34+
"not exporting to Stackdriver.\n";
35+
} else {
36+
std::cout << "RegisterStackdriverExporters:\n";
37+
std::cout << " project_id = \"" << project_id << "\"\n";
38+
39+
opencensus::exporters::stats::StackdriverOptions stats_opts;
40+
stats_opts.project_id = project_id;
41+
opencensus::exporters::stats::StackdriverExporter::Register(
42+
std::move(stats_opts));
43+
44+
opencensus::exporters::trace::StackdriverOptions trace_opts;
45+
trace_opts.project_id = project_id;
46+
opencensus::exporters::trace::StackdriverExporter::Register(
47+
std::move(trace_opts));
48+
}
49+
50+
const char* ocagent_address = getenv("OCAGENT_ADDRESS");
51+
if (ocagent_address == nullptr) {
52+
std::cerr << "The OCAGENT_ADDRESS environment variable is not set: not "
53+
"exporting to OpenCensus Agent. (e.g. localhost:55678)\n";
54+
} else {
55+
opencensus::exporters::trace::OcAgentOptions opts;
56+
opts.address = ocagent_address;
57+
opencensus::exporters::trace::OcAgentExporter::Register(std::move(opts));
58+
}
59+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Register Stackdriver exporters for stats and trace.
16-
void RegisterStackdriverExporters();
15+
void RegisterExporters();

examples/grpc/hello_client.cc

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@
2121
#include <grpcpp/opencensus.h>
2222

2323
#include "absl/time/clock.h"
24+
#include "examples/grpc/exporters.h"
2425
#include "examples/grpc/hello.grpc.pb.h"
2526
#include "examples/grpc/hello.pb.h"
26-
#include "examples/grpc/stackdriver.h"
27-
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
28-
#include "opencensus/exporters/stats/stdout/stdout_exporter.h"
29-
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
30-
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
3127
#include "opencensus/trace/sampler.h"
3228
#include "opencensus/trace/trace_config.h"
3329

@@ -53,17 +49,12 @@ int main(int argc, char **argv) {
5349
// Register the OpenCensus gRPC plugin to enable stats and tracing in gRPC.
5450
grpc::RegisterOpenCensusPlugin();
5551

56-
// Register exporters for Stackdriver.
57-
RegisterStackdriverExporters();
52+
RegisterExporters();
5853

5954
// Trace all outgoing RPCs.
6055
opencensus::trace::TraceConfig::SetCurrentTraceParams(
6156
{128, 128, 128, 128, opencensus::trace::ProbabilitySampler(1.0)});
6257

63-
// For debugging, register exporters that just write to stdout.
64-
opencensus::exporters::stats::StdoutExporter::Register();
65-
opencensus::exporters::trace::StdoutExporter::Register();
66-
6758
// Create a Channel to send RPCs over.
6859
std::shared_ptr<grpc::Channel> channel =
6960
grpc::CreateChannel(hostport, grpc::InsecureChannelCredentials());

examples/grpc/hello_server.cc

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@
2525
#include "absl/strings/escaping.h"
2626
#include "absl/strings/numbers.h"
2727
#include "absl/strings/str_cat.h"
28+
#include "examples/grpc/exporters.h"
2829
#include "examples/grpc/hello.grpc.pb.h"
2930
#include "examples/grpc/hello.pb.h"
30-
#include "examples/grpc/stackdriver.h"
3131
#include "opencensus/exporters/stats/prometheus/prometheus_exporter.h"
32-
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
33-
#include "opencensus/exporters/stats/stdout/stdout_exporter.h"
34-
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
35-
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
3632
#include "opencensus/stats/stats.h"
3733
#include "opencensus/trace/sampler.h"
3834
#include "opencensus/trace/span.h"
@@ -124,12 +120,7 @@ int main(int argc, char **argv) {
124120
// Register the gRPC views (latency, error count, etc).
125121
grpc::RegisterOpenCensusViewsForExport();
126122

127-
// Register exporters for Stackdriver.
128-
RegisterStackdriverExporters();
129-
130-
// For debugging, register exporters that just write to stdout.
131-
opencensus::exporters::stats::StdoutExporter::Register();
132-
opencensus::exporters::trace::StdoutExporter::Register();
123+
RegisterExporters();
133124

134125
// Keep a shared pointer to the Prometheus exporter.
135126
auto exporter =

examples/grpc/stackdriver.cc

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

opencensus/exporters/trace/ocagent/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ tracing to [OcAgent](https://opencensus.io/service/components/agent/).
99

1010
Install OcAgent as the [doc](https://opencensus.io/service/components/agent/install). You can deploy an agent in the same Pod with service. Or deploy it on a remote machine.
1111

12+
Make sure the `opencensus` receiver so configured. Example `config.yaml` file:
13+
14+
```yaml
15+
receivers:
16+
opencensus:
17+
address: "127.0.0.1:55678"
18+
19+
exporters:
20+
zipkin:
21+
endpoint: "http://127.0.0.1:9411/api/v2/spans"
22+
```
23+
24+
Start the agent:
25+
26+
```shell
27+
ocagent_linux -c config.yaml
28+
```
29+
1230
### Register the exporter
1331

1432
Include:
@@ -31,7 +49,7 @@ opts.address = "localhost:55678";
3149
OcAgentExporter::Register(std::move(opts));
3250
```
3351
34-
#### Adding Spans to a Trace
52+
### Adding Spans to a Trace
3553
3654
A trace consists of a tree of spans. You should build an `::opencensus::trace::StartSpanOptions` and then start an span.
3755
@@ -54,4 +72,4 @@ When the Span is finish, call the `End()` function and after that the span will
5472
span1.End();
5573
```
5674

57-
For more usage details, you can find out in the test file `ocagent_exporter_test.cc`.
75+
For more usage details, you can find out in the test file `ocagent_exporter_test.cc`.

opencensus/exporters/trace/ocagent/internal/ocagent_exporter.cc

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
#include "opencensus/exporters/trace/ocagent/ocagent_exporter.h"
1616

17+
#include <grpcpp/grpcpp.h>
1718
#include <unistd.h>
19+
1820
#include <cstdint>
1921
#include <iostream>
2022

@@ -28,8 +30,6 @@
2830
#include "opencensus/trace/exporter/span_data.h"
2931
#include "opencensus/trace/exporter/span_exporter.h"
3032

31-
#include <grpcpp/grpcpp.h>
32-
3333
namespace opencensus {
3434
namespace exporters {
3535
namespace trace {
@@ -268,17 +268,25 @@ void Handler::Export(
268268
void Handler::ExportRpcRequest(
269269
const ::opencensus::proto::agent::trace::v1::ExportTraceServiceRequest
270270
&request) {
271+
// TODO: Re-work this to have a single long-running streaming RPC.
271272
grpc::ClientContext context;
272273
context.set_deadline(absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
273274

274275
auto stream = opts_.trace_service_stub->Export(&context);
275276
if (stream == nullptr) {
276-
std::cerr << "Export() got an NULL stream.\n";
277+
std::cerr << "OcAgent trace exporter: Export() got a NULL stream.\n";
277278
return;
278279
}
279280

280281
if (!stream->Write(request)) {
281-
std::cerr << "Export stream Write() failed.\n";
282+
std::cerr << "OcAgent trace exporter: Export() stream broken.\n";
283+
}
284+
285+
stream->WritesDone();
286+
grpc::Status status = stream->Finish();
287+
if (!status.ok()) {
288+
std::cerr << "OcAgent trace exporter: Export() failed: "
289+
<< opencensus::common::ToString(status) << "\n";
282290
}
283291
}
284292

@@ -305,6 +313,13 @@ void Handler::ConnectAgent() {
305313
*request.mutable_node() = nodeInfo_;
306314
ExportRpcRequest(request);
307315

316+
#if 0
317+
// Config is unimplemented as of opencensus-service v0.1.9:
318+
//
319+
// https://github.com/census-instrumentation/opencensus-service/blob/0747a8305a08390e9eb7f8b6e2baa5143fc18c1d/receiver/opencensusreceiver/octrace/opencensus.go#L85
320+
//
321+
// Resurrect this code when the service implements the handler.
322+
308323
::opencensus::proto::agent::trace::v1::CurrentLibraryConfig cur_lib_cfg;
309324
*cur_lib_cfg.mutable_node() = nodeInfo_;
310325

@@ -319,13 +334,21 @@ void Handler::ConnectAgent() {
319334

320335
auto stream = opts_.trace_service_stub->Config(&context);
321336
if (stream == nullptr) {
322-
std::cerr << "Config() got an NULL stream.\n";
337+
std::cerr << "OcAgent trace exporter: Config() got a NULL stream.\n";
323338
return;
324339
}
325340

326341
if (!stream->Write(cur_lib_cfg)) {
327-
std::cerr << "Config stream Write() failed.\n";
342+
std::cerr << "OcAgent trace exporter: Config() stream broken.\n";
343+
}
344+
345+
stream->WritesDone();
346+
grpc::Status status = stream->Finish();
347+
if (!status.ok()) {
348+
std::cerr << "OcAgent trace exporter: Config() failed: "
349+
<< opencensus::common::ToString(status) << "\n";
328350
}
351+
#endif
329352
}
330353

331354
} // namespace

opencensus/exporters/trace/ocagent/ocagent_exporter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include <string>
1919

20-
#include "absl/strings/string_view.h"
2120
#include "absl/time/time.h"
2221

2322
#include "opencensus/proto/agent/trace/v1/trace_service.grpc.pb.h"

0 commit comments

Comments
 (0)