From e57940a94c0d98212bee38ecdf5696753a1ef460 Mon Sep 17 00:00:00 2001 From: easy Date: Thu, 19 Dec 2019 18:04:08 +1100 Subject: [PATCH 1/8] Initial sketch of span_id, example, and build. --- WORKSPACE | 1 + api/BUILD | 14 ++++++ api/include/opentelemetry/trace/span_id.h | 56 +++++++++++++++++++++++ examples/README.md | 7 +++ examples/TBD | 0 examples/WORKSPACE | 21 +++++++++ examples/helloworld/BUILD | 21 +++++++++ examples/helloworld/helloworld.cc | 25 ++++++++++ tools/setup-buildtools-mac.sh | 2 +- 9 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 api/include/opentelemetry/trace/span_id.h create mode 100644 examples/README.md delete mode 100644 examples/TBD create mode 100644 examples/WORKSPACE create mode 100644 examples/helloworld/BUILD create mode 100644 examples/helloworld/helloworld.cc diff --git a/WORKSPACE b/WORKSPACE index 83ef873ce6..10b3c20457 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -20,6 +20,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # Only needed for tests, not to build the OpenTelemetry library. http_archive( name = "com_google_googletest", + sha256 = "9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb", strip_prefix = "googletest-release-1.10.0", urls = ["https://github.com/google/googletest/archive/release-1.10.0.tar.gz"], ) diff --git a/api/BUILD b/api/BUILD index e2fceae4c1..957ac227bd 100644 --- a/api/BUILD +++ b/api/BUILD @@ -1,3 +1,17 @@ +# Copyright 2019, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + package(default_visibility = ["//visibility:public"]) cc_library( diff --git a/api/include/opentelemetry/trace/span_id.h b/api/include/opentelemetry/trace/span_id.h new file mode 100644 index 0000000000..a4006b9e7e --- /dev/null +++ b/api/include/opentelemetry/trace/span_id.h @@ -0,0 +1,56 @@ +// Copyright 2019, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include +#include + +namespace opentelemetry +{ +namespace trace +{ + +class SpanId final +{ +public: + // The size in bytes of the SpanId. + static constexpr int kSize = 8; + + // An invalid SpanId (all zeros). + SpanId() : rep_{0} {} + + // Creates a SpanId by copying the first kSize bytes from the buffer. + explicit SpanId(const uint8_t *buf) { memcpy(rep_, buf, kSize); } + + // Returns a 16-char hex string of the SpanId value. + std::string ToHex() const + { + constexpr char kHex[] = "0123456789ABCDEF"; + std::string s(kSize * 2, ' '); + for (int i = 0; i < kSize; ++i) + { + s[i * 2 + 0] = kHex[(rep_[i] >> 4) & 0xF]; + s[i * 2 + 1] = kHex[(rep_[i] >> 0) & 0xF]; + } + return s; + } + +private: + uint8_t rep_[kSize]; +}; + +} // namespace trace +} // namespace opentelemetry diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000000..e5e8299265 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,7 @@ +The examples/ directory is a separate bazel workspace, to show how +to build libraries and binaries that use the OpenTelemetry library. + +``` +cd examples/ +bazel build //helloworld +``` diff --git a/examples/TBD b/examples/TBD deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/examples/WORKSPACE b/examples/WORKSPACE new file mode 100644 index 0000000000..6968dca772 --- /dev/null +++ b/examples/WORKSPACE @@ -0,0 +1,21 @@ +# Copyright 2019, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +workspace(name = "io_opentelemetry_cpp_examples") + +# OpenTelemetry should be imported via http_archive. +local_repository( + name = "io_opentelemetry_cpp", + path = "../", +) diff --git a/examples/helloworld/BUILD b/examples/helloworld/BUILD new file mode 100644 index 0000000000..3071841408 --- /dev/null +++ b/examples/helloworld/BUILD @@ -0,0 +1,21 @@ +# Copyright 2019, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cc_binary( + name = "helloworld", + srcs = ["helloworld.cc"], + deps = [ + "@io_opentelemetry_cpp//api", + ], +) diff --git a/examples/helloworld/helloworld.cc b/examples/helloworld/helloworld.cc new file mode 100644 index 0000000000..661f89003f --- /dev/null +++ b/examples/helloworld/helloworld.cc @@ -0,0 +1,25 @@ +// Copyright 2019, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include + +int main() +{ + constexpr uint8_t id[] = {1, 2, 3, 4, 0x50, 0x60, 0x70, 0x80}; + opentelemetry::trace::SpanId span_id(id); + std::cout << "Span ID: '" << span_id.ToHex() << "'\n"; +} diff --git a/tools/setup-buildtools-mac.sh b/tools/setup-buildtools-mac.sh index 335fd8e3e4..b74c727df4 100644 --- a/tools/setup-buildtools-mac.sh +++ b/tools/setup-buildtools-mac.sh @@ -5,7 +5,7 @@ # control of the brew dirs. That causes the brew update to fail. # Temporarily allow the user to take over control of brew files. -echo *** +echo *** echo *** You may need to enter your admin password to update the brew files: echo *** From 9d8c63cf9d33760894ab519412e5affe4ee36bac Mon Sep 17 00:00:00 2001 From: easy Date: Mon, 13 Jan 2020 17:27:27 +1100 Subject: [PATCH 2/8] Use nostd::span. --- api/include/opentelemetry/trace/span_id.h | 32 ++++++++++++++++------- examples/helloworld/helloworld.cc | 14 +++++++++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/api/include/opentelemetry/trace/span_id.h b/api/include/opentelemetry/trace/span_id.h index a4006b9e7e..3f8840cd53 100644 --- a/api/include/opentelemetry/trace/span_id.h +++ b/api/include/opentelemetry/trace/span_id.h @@ -16,7 +16,8 @@ #include #include -#include + +#include "opentelemetry/nostd/span.h" namespace opentelemetry { @@ -30,24 +31,35 @@ class SpanId final static constexpr int kSize = 8; // An invalid SpanId (all zeros). - SpanId() : rep_{0} {} + SpanId() noexcept : rep_{0} {} - // Creates a SpanId by copying the first kSize bytes from the buffer. - explicit SpanId(const uint8_t *buf) { memcpy(rep_, buf, kSize); } + // Creates a SpanId with the given ID. + explicit SpanId(nostd::span id) noexcept { memcpy(rep_, id.data(), kSize); } - // Returns a 16-char hex string of the SpanId value. - std::string ToHex() const + // Populates the buffer with the hex representation of the ID. + void ToHex(nostd::span buffer) const noexcept { constexpr char kHex[] = "0123456789ABCDEF"; - std::string s(kSize * 2, ' '); for (int i = 0; i < kSize; ++i) { - s[i * 2 + 0] = kHex[(rep_[i] >> 4) & 0xF]; - s[i * 2 + 1] = kHex[(rep_[i] >> 0) & 0xF]; + buffer[i * 2 + 0] = kHex[(rep_[i] >> 4) & 0xF]; + buffer[i * 2 + 1] = kHex[(rep_[i] >> 0) & 0xF]; } - return s; } + // Returns a nostd::span of the ID. + nostd::span Id() const noexcept { return rep_; } + + bool operator==(const SpanId &that) const noexcept { return memcmp(rep_, that.rep_, kSize) == 0; } + + bool operator!=(const SpanId &that) const noexcept { return !(*this == that); } + + // Returns false if the SpanId is all zeros. + bool IsValid() const noexcept { return *this != SpanId(); } + + // Copies the opaque SpanId data to a buffer. + void CopyTo(nostd::span dest) const noexcept { memcpy(dest.data(), rep_, kSize); } + private: uint8_t rep_[kSize]; }; diff --git a/examples/helloworld/helloworld.cc b/examples/helloworld/helloworld.cc index 661f89003f..e9791fd8d5 100644 --- a/examples/helloworld/helloworld.cc +++ b/examples/helloworld/helloworld.cc @@ -12,14 +12,26 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include +#include + +namespace +{ +std::string ToHex(const opentelemetry::trace::SpanId &span) +{ + char buf[16]; + span.ToHex(buf); + return std::string(buf, sizeof(buf)); +} +} // namespace int main() { constexpr uint8_t id[] = {1, 2, 3, 4, 0x50, 0x60, 0x70, 0x80}; opentelemetry::trace::SpanId span_id(id); - std::cout << "Span ID: '" << span_id.ToHex() << "'\n"; + std::cout << "Span ID: '" << ToHex(span_id) << "'\n"; } From e3ff806d189b2d8761146df1a5f0975dca4d5c5e Mon Sep 17 00:00:00 2001 From: easy Date: Wed, 15 Jan 2020 16:07:13 +1100 Subject: [PATCH 3/8] Rename to match Java API. --- api/include/opentelemetry/trace/span_id.h | 11 +++++++---- examples/helloworld/helloworld.cc | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/api/include/opentelemetry/trace/span_id.h b/api/include/opentelemetry/trace/span_id.h index 3f8840cd53..508b9eb61c 100644 --- a/api/include/opentelemetry/trace/span_id.h +++ b/api/include/opentelemetry/trace/span_id.h @@ -36,8 +36,8 @@ class SpanId final // Creates a SpanId with the given ID. explicit SpanId(nostd::span id) noexcept { memcpy(rep_, id.data(), kSize); } - // Populates the buffer with the hex representation of the ID. - void ToHex(nostd::span buffer) const noexcept + // Populates the buffer with the lowercase base16 representation of the ID. + void ToLowerBase16(nostd::span buffer) const noexcept { constexpr char kHex[] = "0123456789ABCDEF"; for (int i = 0; i < kSize; ++i) @@ -57,8 +57,11 @@ class SpanId final // Returns false if the SpanId is all zeros. bool IsValid() const noexcept { return *this != SpanId(); } - // Copies the opaque SpanId data to a buffer. - void CopyTo(nostd::span dest) const noexcept { memcpy(dest.data(), rep_, kSize); } + // Copies the opaque SpanId data to dest. + void CopyBytesTo(nostd::span dest) const noexcept + { + memcpy(dest.data(), rep_, kSize); + } private: uint8_t rep_[kSize]; diff --git a/examples/helloworld/helloworld.cc b/examples/helloworld/helloworld.cc index e9791fd8d5..5d025d8466 100644 --- a/examples/helloworld/helloworld.cc +++ b/examples/helloworld/helloworld.cc @@ -21,10 +21,10 @@ namespace { -std::string ToHex(const opentelemetry::trace::SpanId &span) +std::string Hex(const opentelemetry::trace::SpanId &span) { char buf[16]; - span.ToHex(buf); + span.ToLowerBase16(buf); return std::string(buf, sizeof(buf)); } } // namespace @@ -33,5 +33,5 @@ int main() { constexpr uint8_t id[] = {1, 2, 3, 4, 0x50, 0x60, 0x70, 0x80}; opentelemetry::trace::SpanId span_id(id); - std::cout << "Span ID: '" << ToHex(span_id) << "'\n"; + std::cout << "Span ID: '" << Hex(span_id) << "'\n"; } From 2e98d791f2c2654bdcb21063f4244374ccb076d0 Mon Sep 17 00:00:00 2001 From: easy Date: Wed, 15 Jan 2020 16:13:31 +1100 Subject: [PATCH 4/8] rm TBD --- api/include/opentelemetry/trace/TBD | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 api/include/opentelemetry/trace/TBD diff --git a/api/include/opentelemetry/trace/TBD b/api/include/opentelemetry/trace/TBD deleted file mode 100644 index e69de29bb2..0000000000 From c826330d7672c0542629ea95aa6c7b5546f6767e Mon Sep 17 00:00:00 2001 From: easy Date: Wed, 15 Jan 2020 16:18:44 +1100 Subject: [PATCH 5/8] Add span_id_test. --- api/test/CMakeLists.txt | 1 + api/test/trace/BUILD | 8 ++++++ api/test/trace/CMakeLists.txt | 6 +++++ api/test/trace/span_id_test.cc | 45 ++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 api/test/trace/BUILD create mode 100644 api/test/trace/CMakeLists.txt create mode 100644 api/test/trace/span_id_test.cc diff --git a/api/test/CMakeLists.txt b/api/test/CMakeLists.txt index 988b463c36..ec50ce7d66 100644 --- a/api/test/CMakeLists.txt +++ b/api/test/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(nostd) +add_subdirectory(trace) diff --git a/api/test/trace/BUILD b/api/test/trace/BUILD new file mode 100644 index 0000000000..84b50f55c2 --- /dev/null +++ b/api/test/trace/BUILD @@ -0,0 +1,8 @@ +cc_test( + name = "span_id_test", + srcs = ["span_id_test.cc"], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/api/test/trace/CMakeLists.txt b/api/test/trace/CMakeLists.txt new file mode 100644 index 0000000000..4b3a44a62c --- /dev/null +++ b/api/test/trace/CMakeLists.txt @@ -0,0 +1,6 @@ +include(GoogleTest) + +add_executable(span_id_test span_id_test.cc) +target_link_libraries(span_id_test ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) +gtest_add_tests(TARGET span_id_test TEST_PREFIX trace. TEST_LIST span_id_test) diff --git a/api/test/trace/span_id_test.cc b/api/test/trace/span_id_test.cc new file mode 100644 index 0000000000..c25c086cc5 --- /dev/null +++ b/api/test/trace/span_id_test.cc @@ -0,0 +1,45 @@ +#include "opentelemetry/trace/span_id.h" + +#include +#include + +#include + +namespace +{ + +using opentelemetry::trace::SpanId; + +std::string Hex(const opentelemetry::trace::SpanId &span) +{ + char buf[16]; + span.ToLowerBase16(buf); + return std::string(buf, sizeof(buf)); +} + +TEST(SpanIdTest, DefaultConstruction) +{ + SpanId id; + EXPECT_FALSE(id.IsValid()); + EXPECT_EQ("0000000000000000", Hex(id)); +} + +TEST(SpanIdTest, ValidId) +{ + constexpr uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8}; + SpanId id(buf); + EXPECT_TRUE(id.IsValid()); + EXPECT_EQ("0102030405060708", Hex(id)); + EXPECT_NE(SpanId(), id); + EXPECT_EQ(SpanId(buf), id); +} + +TEST(SpanIdTest, CopyBytesTo) +{ + constexpr uint8_t src[] = {1, 2, 3, 4, 5, 6, 7, 8}; + SpanId id(src); + uint8_t buf[8]; + id.CopyBytesTo(buf); + EXPECT_TRUE(memcmp(src, buf, 8) == 0); +} +} // namespace From 873cbcf758e2b9e7e3a9485732ff85195e768dfe Mon Sep 17 00:00:00 2001 From: easy Date: Wed, 15 Jan 2020 16:19:50 +1100 Subject: [PATCH 6/8] rm examples/, not for this PR. --- examples/README.md | 7 ------ examples/WORKSPACE | 21 ------------------ examples/helloworld/BUILD | 21 ------------------ examples/helloworld/helloworld.cc | 37 ------------------------------- 4 files changed, 86 deletions(-) delete mode 100644 examples/README.md delete mode 100644 examples/WORKSPACE delete mode 100644 examples/helloworld/BUILD delete mode 100644 examples/helloworld/helloworld.cc diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index e5e8299265..0000000000 --- a/examples/README.md +++ /dev/null @@ -1,7 +0,0 @@ -The examples/ directory is a separate bazel workspace, to show how -to build libraries and binaries that use the OpenTelemetry library. - -``` -cd examples/ -bazel build //helloworld -``` diff --git a/examples/WORKSPACE b/examples/WORKSPACE deleted file mode 100644 index 6968dca772..0000000000 --- a/examples/WORKSPACE +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2019, OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -workspace(name = "io_opentelemetry_cpp_examples") - -# OpenTelemetry should be imported via http_archive. -local_repository( - name = "io_opentelemetry_cpp", - path = "../", -) diff --git a/examples/helloworld/BUILD b/examples/helloworld/BUILD deleted file mode 100644 index 3071841408..0000000000 --- a/examples/helloworld/BUILD +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2019, OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -cc_binary( - name = "helloworld", - srcs = ["helloworld.cc"], - deps = [ - "@io_opentelemetry_cpp//api", - ], -) diff --git a/examples/helloworld/helloworld.cc b/examples/helloworld/helloworld.cc deleted file mode 100644 index 5d025d8466..0000000000 --- a/examples/helloworld/helloworld.cc +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2019, OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include -#include -#include - -namespace -{ -std::string Hex(const opentelemetry::trace::SpanId &span) -{ - char buf[16]; - span.ToLowerBase16(buf); - return std::string(buf, sizeof(buf)); -} -} // namespace - -int main() -{ - constexpr uint8_t id[] = {1, 2, 3, 4, 0x50, 0x60, 0x70, 0x80}; - opentelemetry::trace::SpanId span_id(id); - std::cout << "Span ID: '" << Hex(span_id) << "'\n"; -} From a1536935445a0c59495677f18feab7fba22694ff Mon Sep 17 00:00:00 2001 From: easy Date: Wed, 15 Jan 2020 16:25:21 +1100 Subject: [PATCH 7/8] Restore examples/TBD --- examples/TBD | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 examples/TBD diff --git a/examples/TBD b/examples/TBD new file mode 100644 index 0000000000..e69de29bb2 From 0ebf537334341a7ff1fbfea359652ef4cf46733e Mon Sep 17 00:00:00 2001 From: easy Date: Wed, 15 Jan 2020 16:42:51 +1100 Subject: [PATCH 8/8] Fix gcc4.8 build. --- api/include/opentelemetry/trace/span_id.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/include/opentelemetry/trace/span_id.h b/api/include/opentelemetry/trace/span_id.h index 508b9eb61c..74fc7393e6 100644 --- a/api/include/opentelemetry/trace/span_id.h +++ b/api/include/opentelemetry/trace/span_id.h @@ -48,7 +48,10 @@ class SpanId final } // Returns a nostd::span of the ID. - nostd::span Id() const noexcept { return rep_; } + nostd::span Id() const noexcept + { + return nostd::span(rep_); + } bool operator==(const SpanId &that) const noexcept { return memcmp(rep_, that.rep_, kSize) == 0; }