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

Commit e3f39cd

Browse files
authored
Initial revision of opencensus/trace/. (#2)
1 parent c89ac4a commit e3f39cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+7729
-3
lines changed

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Google Inc.
1+
Google Inc.

CONTRIBUTING.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# How to Contribute
2+
3+
We'd love to accept your patches and contributions to this project. There are
4+
just a few small guidelines you need to follow.
5+
6+
## Contributor License Agreement
7+
8+
Contributions to this project must be accompanied by a Contributor License
9+
Agreement. You (or your employer) retain the copyright to your contribution,
10+
this simply gives us permission to use and redistribute your contributions as
11+
part of the project. Head over to <https://cla.developers.google.com/> to see
12+
your current agreements on file or to sign a new one.
13+
14+
You generally only need to submit a CLA once, so if you've already submitted one
15+
(even if it was for a different project), you probably don't need to do it
16+
again.
17+
18+
## Code reviews
19+
20+
All submissions, including submissions by project members, require review. We
21+
use GitHub pull requests for this purpose. Consult
22+
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
23+
information on using pull requests.

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# OpenCensus - A stats collection and distributed tracing framework
22
[![Gitter chat][gitter-image]][gitter-url]
33

4-
OpenCensus is a toolkit for collecting application performance and behavior data. It currently
5-
includes 3 apis: stats, tracing and tags.
4+
OpenCensus is a toolkit for collecting application performance and behavior data. It currently
5+
includes an API for tracing.
66

77
The library is in alpha stage and the API is subject to change.
88

@@ -11,3 +11,37 @@ project.
1111

1212
[gitter-image]: https://badges.gitter.im/census-instrumentation/lobby.svg
1313
[gitter-url]: https://gitter.im/census-instrumentation/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
14+
15+
This is not an officially supported Google product.
16+
17+
## Quickstart
18+
19+
Please refer to
20+
[`trace/examples/span_example.cc`](opencensus/trace/examples/span_example.cc)
21+
22+
## Directory structure
23+
24+
* [`opencensus/`](opencensus) prefix to get `#include` paths like `opencensus/trace/span.h`
25+
* [`common/`](opencensus/common) - Provides common libraries and components for OpenCensus.
26+
* [`doc/`](opencensus/doc) - Documentation for our APIs, coding style, etc.
27+
* [`trace/`](opencensus/trace) - OpenCensus tracing API.
28+
29+
## Language support
30+
31+
* `libstdc++` is required.
32+
* C++11 is required.
33+
* [`absl`](https://github.com/abseil/abseil-cpp/) is used for its building blocks.
34+
* [`googletest`](https://github.com/google/googletest/) is used for tests.
35+
* [`benchmark`](https://github.com/google/benchmark/) is used for benchmarking.
36+
* We do not depend on:
37+
* Boost
38+
* Exception handling
39+
* RTTI
40+
41+
## Compiler support
42+
43+
We are targeting the following compilers:
44+
45+
* gcc 4.8.1
46+
* clang 3.4
47+
* MSVC 19

WORKSPACE

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2017, 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+
workspace(name = "io_opencensus_cpp")
16+
17+
# We depend on Abseil.
18+
http_archive(
19+
name = "com_google_absl",
20+
strip_prefix = "abseil-cpp-master",
21+
urls = ["https://github.com/abseil/abseil-cpp/archive/master.zip"],
22+
)
23+
24+
# CCTZ (Time-zone framework). Used by absl.
25+
http_archive(
26+
name = "com_googlesource_code_cctz",
27+
strip_prefix = "cctz-master",
28+
urls = ["https://github.com/google/cctz/archive/master.zip"],
29+
)
30+
31+
# GoogleTest/GoogleMock framework.
32+
http_archive(
33+
name = "com_google_googletest",
34+
strip_prefix = "googletest-master",
35+
urls = ["https://github.com/google/googletest/archive/master.zip"],
36+
)
37+
38+
# Google Benchmark library.
39+
# Adapted from cctz's WORKSPACE.
40+
# Upstream support for bazel is tracked in
41+
# - https://github.com/google/benchmark/pull/329
42+
# - https://github.com/google/benchmark/issues/191
43+
new_http_archive(
44+
name = "com_google_benchmark",
45+
urls = ["https://github.com/google/benchmark/archive/master.zip"],
46+
strip_prefix = "benchmark-master",
47+
build_file_content =
48+
"""
49+
cc_library(
50+
name = "benchmark",
51+
srcs = glob([
52+
"src/*.h",
53+
"src/*.cc",
54+
]),
55+
hdrs = glob(["include/benchmark/*.h"]),
56+
copts = ["-DHAVE_POSIX_REGEX"], # HAVE_STD_REGEX didn't work.
57+
includes = ["include"],
58+
visibility = ["//visibility:public"],
59+
)
60+
"""
61+
)

opencensus/common/internal/BUILD

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# OpenCensus C++ internal libraries.
2+
#
3+
# Copyright 2017, OpenCensus Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
licenses(["notice"]) # Apache 2.0
18+
19+
package(default_visibility = ["//opencensus:__subpackages__"])
20+
21+
cc_library(
22+
name = "random_lib",
23+
srcs = ["random.cc"],
24+
hdrs = ["random.h"],
25+
deps = [
26+
"@com_google_absl//absl/synchronization",
27+
"@com_google_absl//absl/time",
28+
],
29+
)
30+
31+
cc_library(
32+
name = "stats_object",
33+
hdrs = ["stats_object.h"],
34+
deps = [
35+
"@com_google_absl//absl/base:core_headers",
36+
"@com_google_absl//absl/memory",
37+
"@com_google_absl//absl/strings",
38+
"@com_google_absl//absl/time",
39+
"@com_google_absl//absl/types:span",
40+
],
41+
)
42+
43+
cc_library(
44+
name = "string_vector_hash",
45+
hdrs = ["string_vector_hash.h"],
46+
)
47+
48+
# Tests
49+
# ========================================================================= #
50+
51+
cc_test(
52+
name = "random_test",
53+
srcs = ["random_test.cc"],
54+
deps = [
55+
":random_lib",
56+
"@com_google_googletest//:gtest_main",
57+
],
58+
)
59+
60+
cc_binary(
61+
name = "random_benchmark",
62+
testonly = 1,
63+
srcs = ["random_benchmark.cc"],
64+
linkopts = ["-pthread"], # Required for absl/synchronization bits.
65+
linkstatic = 1,
66+
deps = [
67+
":random_lib",
68+
"@com_google_benchmark//:benchmark",
69+
],
70+
)
71+
72+
cc_test(
73+
name = "stats_object_test",
74+
srcs = ["stats_object_test.cc"],
75+
deps = [
76+
":stats_object",
77+
"@com_google_absl//absl/strings",
78+
"@com_google_absl//absl/types:span",
79+
"@com_google_googletest//:gtest_main",
80+
],
81+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2017, 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 "opencensus/common/internal/random.h"
16+
17+
#include <cstring>
18+
19+
#include "absl/synchronization/mutex.h"
20+
21+
namespace opencensus {
22+
namespace common {
23+
24+
uint64_t Generator::Random64() {
25+
absl::MutexLock l(&mu_);
26+
return rng_();
27+
}
28+
29+
Random* Random::GetRandom() {
30+
static Random* global_random = new Random;
31+
return global_random;
32+
}
33+
34+
uint32_t Random::GenerateRandom32() { return gen_.Random64(); }
35+
36+
uint64_t Random::GenerateRandom64() { return gen_.Random64(); }
37+
38+
float Random::GenerateRandomFloat() {
39+
return static_cast<float>(gen_.Random64()) / static_cast<float>(UINT64_MAX);
40+
}
41+
42+
double Random::GenerateRandomDouble() {
43+
return static_cast<double>(gen_.Random64()) / static_cast<double>(UINT64_MAX);
44+
}
45+
46+
void Random::GenerateRandomBuffer(uint8_t* buf, size_t buf_size) {
47+
absl::MutexLock l(&gen_.mu_);
48+
for (size_t i = 0; i < buf_size; i += sizeof(uint64_t)) {
49+
uint64_t value = gen_.rng_();
50+
if (i + sizeof(uint64_t) <= buf_size) {
51+
memcpy(&buf[i], &value, sizeof(uint64_t));
52+
} else {
53+
memcpy(&buf[i], &value, buf_size - i);
54+
}
55+
}
56+
}
57+
58+
} // namespace common
59+
} // namespace opencensus
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2017, 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+
#ifndef OPENCENSUS_COMMON_INTERNAL_RANDOM_H_
16+
#define OPENCENSUS_COMMON_INTERNAL_RANDOM_H_
17+
18+
#include <cstdint>
19+
#include <cstring>
20+
#include <memory>
21+
#include <random>
22+
#include <vector>
23+
24+
#include "absl/synchronization/mutex.h"
25+
#include "absl/time/clock.h"
26+
27+
namespace opencensus {
28+
namespace common {
29+
30+
class Generator {
31+
public:
32+
Generator() : rng_(absl::GetCurrentTimeNanos()) {}
33+
explicit Generator(uint64_t seed) : rng_(seed) {}
34+
35+
uint64_t Random64() LOCKS_EXCLUDED(mu_);
36+
37+
private:
38+
friend class Random;
39+
40+
absl::Mutex mu_;
41+
std::mt19937_64 rng_ GUARDED_BY(mu_);
42+
};
43+
44+
class Random {
45+
public:
46+
// Initializes and returns a singleton Random generator.
47+
static Random* GetRandom();
48+
49+
// Generating functions.
50+
// Generates a random uint32_t
51+
uint32_t GenerateRandom32();
52+
// Generates a random uint64_t
53+
uint64_t GenerateRandom64();
54+
// Generates a random float between [0.0, 1.0]
55+
float GenerateRandomFloat();
56+
// Generates a random double between [0.0, 1.0]
57+
double GenerateRandomDouble();
58+
// Fills the given buffer with uniformly random bits.
59+
void GenerateRandomBuffer(uint8_t* buf, size_t buf_size);
60+
61+
private:
62+
friend class RandomTest;
63+
64+
Random() = default;
65+
66+
Random(const Random&) = delete;
67+
Random(Random&&) = delete;
68+
Random& operator=(const Random&) = delete;
69+
Random& operator=(Random&&) = delete;
70+
71+
uint64_t GenerateValue();
72+
Generator gen_;
73+
};
74+
75+
} // namespace common
76+
} // namespace opencensus
77+
78+
#endif // OPENCENSUS_COMMON_INTERNAL_RANDOM_H_
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2017, 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 "benchmark/benchmark.h"
16+
#include "opencensus/common/internal/random.h"
17+
18+
namespace {
19+
20+
void BM_GetRandom(benchmark::State& state) {
21+
while (state.KeepRunning()) {
22+
::opencensus::common::Random::GetRandom();
23+
}
24+
}
25+
BENCHMARK(BM_GetRandom);
26+
27+
void BM_Random64(benchmark::State& state) {
28+
while (state.KeepRunning()) {
29+
::opencensus::common::Random::GetRandom()->GenerateRandom64();
30+
}
31+
}
32+
BENCHMARK(BM_Random64);
33+
34+
} // namespace
35+
BENCHMARK_MAIN()

0 commit comments

Comments
 (0)