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

Commit f91d5ed

Browse files
panzhongxiang-easy
authored andcommitted
Add OcAgent Trace Exporter. (#355)
Fixes #196.
1 parent ddb869d commit f91d5ed

7 files changed

Lines changed: 628 additions & 0 deletions

File tree

BUILD

Whitespace-only changes.

WORKSPACE

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,40 @@ switched_rules_by_language(
150150
cc = True,
151151
grpc = True,
152152
)
153+
154+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
155+
156+
http_archive(
157+
name = "io_bazel_rules_go",
158+
sha256 = "9fb16af4d4836c8222142e54c9efa0bb5fc562ffc893ce2abeac3e25daead144",
159+
urls = [
160+
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.19.0/rules_go-0.19.0.tar.gz",
161+
"https://github.com/bazelbuild/rules_go/releases/download/0.19.0/rules_go-0.19.0.tar.gz",
162+
],
163+
)
164+
165+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
166+
167+
go_rules_dependencies()
168+
169+
go_register_toolchains()
170+
171+
# Needed by opencensus-proto.
172+
http_archive(
173+
name = "grpc_java",
174+
strip_prefix = "grpc-java-master",
175+
urls = ["https://github.com/grpc/grpc-java/archive/master.zip"],
176+
)
177+
178+
load("@grpc_java//:repositories.bzl", "grpc_java_repositories")
179+
180+
grpc_java_repositories(
181+
# Omit to avoid conflicts.
182+
omit_com_google_protobuf = True,
183+
)
184+
185+
http_archive(
186+
name = "io_opencensus_proto",
187+
strip_prefix = "opencensus-proto-master/src",
188+
urls = ["https://github.com/census-instrumentation/opencensus-proto/archive/master.zip"],
189+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2019, 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+
load("//opencensus:copts.bzl", "DEFAULT_COPTS", "TEST_COPTS")
16+
17+
licenses(["notice"]) # Apache License 2.0
18+
19+
package(default_visibility = ["//visibility:private"])
20+
21+
# Libraries
22+
# ========================================================================= #
23+
24+
cc_library(
25+
name = "ocagent_exporter",
26+
srcs = ["internal/ocagent_exporter.cc"],
27+
hdrs = ["ocagent_exporter.h"],
28+
copts = DEFAULT_COPTS,
29+
visibility = ["//visibility:public"],
30+
deps = [
31+
"//opencensus/common:version",
32+
"//opencensus/common/internal:hostname",
33+
"//opencensus/common/internal/grpc:status",
34+
"//opencensus/common/internal/grpc:with_user_agent",
35+
"//opencensus/trace",
36+
"@com_github_grpc_grpc//:grpc++",
37+
"@com_google_absl//absl/base:core_headers",
38+
"@com_google_absl//absl/memory",
39+
"@com_google_absl//absl/strings",
40+
"@com_google_absl//absl/time",
41+
"@io_opencensus_proto//opencensus/proto/agent/trace/v1:trace_service_grpc_cc",
42+
"@io_opencensus_proto//opencensus/proto/agent/trace/v1:trace_service_proto_cc",
43+
],
44+
)
45+
46+
cc_test(
47+
name = "ocagent_exporter_test",
48+
srcs = ["internal/ocagent_exporter_test.cc"],
49+
copts = TEST_COPTS,
50+
deps = [
51+
":ocagent_exporter",
52+
"//opencensus/trace",
53+
"@com_google_absl//absl/time",
54+
"@com_google_googletest//:gtest_main",
55+
],
56+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# OpenCensus OcAgent Tracing Exporter
2+
3+
The *OpenCensus OcAgent Tracing Exporter* is a tracing exporter that exports
4+
tracing to [OcAgent](https://opencensus.io/service/components/agent/).
5+
6+
## Quickstart
7+
8+
### Prerequisites
9+
10+
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.
11+
12+
### Register the exporter
13+
14+
Include:
15+
16+
```c++
17+
#include "opencensus/exporters/trace/ocagent/ocagent_exporter.h"
18+
```
19+
20+
Add a BUILD dependency on:
21+
22+
```
23+
"@io_opencensus_cpp//exporters/trace/ocagent:ocagent_exporter",
24+
```
25+
26+
In your application's initialization code, register the exporter:
27+
28+
```c++
29+
opencensus::exporters::trace::OcAgentOptions opts;
30+
opts.address = "localhost:55678";
31+
OcAgentExporter::Register(std::move(opts));
32+
```
33+
34+
#### Adding Spans to a Trace
35+
36+
A trace consists of a tree of spans. You should build an `::opencensus::trace::StartSpanOptions` and then start an span.
37+
38+
```c++
39+
::opencensus::trace::AlwaysSampler sampler;
40+
::opencensus::trace::StartSpanOptions opts = {&sampler};
41+
42+
auto span1 = ::opencensus::trace::Span::StartSpan("Span1", nullptr, opts);
43+
```
44+
45+
Set some fields of the span.
46+
47+
```c++
48+
span1.AddAnnotation("Annotation1", {{"TestBool", true}});
49+
```
50+
51+
When the Span is finish, call the `End()` function and after that the span will be exported.
52+
53+
```c++
54+
span1.End();
55+
```
56+
57+
For more usage details, you can find out in the test file `ocagent_exporter_test.cc`.

0 commit comments

Comments
 (0)