Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
15 changes: 15 additions & 0 deletions .bazelrc.absl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# In order to support both Unixen and Windows, different styles of compiler
# flags must be used.
#
# This .bazelrc file specifies different compiler flags for Linux/Darwin versus
# Windows.
#
# This bazelrc defines the `DD_USE_ABSEIL_FOR_ENVOY` preprocessor macro, and so
# the resulting library will use `absl::string_view` and `absl::optional`
# instead of their standard (`std`) equivalents.

build --enable_platform_specific_config

build:linux --cxxopt='-std=c++17' --cxxopt='-Wall' --cxxopt='-Wextra' --cxxopt='-pedantic' --cxxopt='-DDD_USE_ABSEIL_FOR_ENVOY'
build:macos --cxxopt='-std=c++17' --cxxopt='-Wall' --cxxopt='-Wextra' --cxxopt='-pedantic' --cxxopt='-DDD_USE_ABSEIL_FOR_ENVOY'
build:windows --cxxopt='/std:c++17' --cxxopt='/DDD_USE_ABSEIL_FOR_ENVOY' --linkopt='ws2_32.lib'
15 changes: 15 additions & 0 deletions .bazelrc.std
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# In order to support both Unixen and Windows, different styles of compiler
# flags must be used.
#
# This .bazelrc file specifies different compiler flags for Linux/Darwin versus
# Windows.
#
# This bazelrc does _not_ define the `DD_USE_ABSEIL_FOR_ENVOY` preprocessor
# macro, and so the resulting library will use `std::string_view` and
# `std::optional` instead of their Abseil equivalents.

build --enable_platform_specific_config

build:linux --cxxopt='-std=c++17' --cxxopt='-Wall' --cxxopt='-Wextra' --cxxopt='-pedantic'
build:macos --cxxopt='-std=c++17' --cxxopt='-Wall' --cxxopt='-Wextra' --cxxopt='-pedantic'
build:windows --cxxopt='/std:c++17' --linkopt='ws2_32.lib'
36 changes: 32 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: 2.1

orbs:
windows: circleci/windows@5.0

executors:
docker-amd64:
docker:
Expand Down Expand Up @@ -27,18 +30,38 @@ jobs:
- checkout
- run: find bin/ -executable -type f -print0 | xargs -0 shellcheck

build-bazel:
test-windows-bazel:
parameters:
# `bazelrc` is the path to the .bazelrc file to use in the build/test.
# The repository has two flavors: one that uses Abseil types (for use with
# Envoy), and one that uses std types.
bazelrc:
type: string
executor: windows/server-2022
environment:
MAKE_JOB_COUNT: 4
steps:
- checkout
- run: choco install -y bazelisk
- run: bazelisk --bazelrc=<< parameters.bazelrc >> test --jobs $env:MAKE_JOB_COUNT dd_trace_cpp_test

test-bazel:
parameters:
toolchain:
type: string
arch:
type: string
# `bazelrc` is the path to the .bazelrc file to use in the build/test.
# The repository has two flavors: one that uses Abseil types (for use with
# Envoy), and one that uses std types.
bazelrc:
type: string
executor: docker-<< parameters.arch >>
environment:
MAKE_JOB_COUNT: 8
steps:
- checkout
- run: bin/with-toolchain << parameters.toolchain >> bazelisk build --jobs $MAKE_JOB_COUNT dd_trace_cpp
- run: bin/with-toolchain << parameters.toolchain >> bazelisk --bazelrc=<< parameters.bazelrc >> test --jobs $MAKE_JOB_COUNT dd_trace_cpp_test

test-cmake:
parameters:
Expand Down Expand Up @@ -113,7 +136,7 @@ jobs:
DD_TRACE_CPP_COMMIT: << pipeline.git.revision >>
command: |
echo "https://github.com/DataDog/dd-trace-cpp@$DD_TRACE_CPP_COMMIT" > ~/project/system-tests/binaries/cpp-load-from-git
cd system-tests && ./build.sh -i runner && ./run.sh PARAMETRIC --log-cli-level=DEBUG
cd system-tests && ./build.sh -i runner && ./run.sh PARAMETRIC --log-cli-level=DEBUG
- run:
name: Collect artifacts
command: tar -cvzf logs_cpp_parametric_dev.tar.gz -C system-tests logs_parametric
Expand All @@ -127,17 +150,22 @@ workflows:
jobs:
- format
- shellcheck
- test-windows-bazel:
matrix:
parameters:
bazelrc: [".bazelrc.absl", ".bazelrc.std"]
- test-cmake:
matrix:
parameters:
toolchain: ["gnu", "llvm"]
sanitize: ["on", "off"]
arch: ["amd64", "arm64"]
- build-bazel:
- test-bazel:
matrix:
parameters:
arch: ["amd64", "arm64"]
toolchain: ["gnu", "llvm"]
bazelrc: [".bazelrc.absl", ".bazelrc.std"]
- coverage
- system-tests:
filters:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/bazel-*
/.build/
/.coverage/
MODULE.bazel.lock
/MODULE.bazel
/MODULE.bazel.lock
62 changes: 54 additions & 8 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,64 @@ cc_library(
"src/datadog/version.h",
"src/datadog/w3c_propagation.h",
],
copts = [
"-Wall",
"-Wextra",
"-Werror",
"-pedantic",
"-std=c++17",
"-DDD_USE_ABSEIL_FOR_ENVOY",
],
strip_include_prefix = "src/",
visibility = ["//visibility:public"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
],
)


cc_test(
name = "dd_trace_cpp_test",
srcs = [
# test driver
"test/main.cpp",

# wrapper around Catch2
"test/test.cpp",
"test/test.h",
"test/catch.hpp",

# mocks
"test/mocks/collectors.cpp",
"test/mocks/collectors.h",
"test/mocks/dict_readers.cpp",
"test/mocks/dict_readers.h",
"test/mocks/dict_writers.cpp",
"test/mocks/dict_writers.h",
"test/mocks/event_schedulers.cpp",
"test/mocks/event_schedulers.h",
"test/mocks/http_clients.cpp",
"test/mocks/http_clients.h",
"test/mocks/loggers.cpp",
"test/mocks/loggers.h",

# utilities
"test/matchers.cpp",
"test/matchers.h",

# test cases
"test/test_cerr_logger.cpp",
# "test/test_curl.cpp", no curl: bazel build/test is for Envoy.
"test/test_datadog_agent.cpp",
"test/test_glob.cpp",
"test/test_limiter.cpp",
"test/test_metrics.cpp",
"test/test_msgpack.cpp",
"test/test_parse_util.cpp",
"test/test_smoke.cpp",
"test/test_span.cpp",
"test/test_span_sampler.cpp",
"test/test_trace_id.cpp",
"test/test_trace_segment.cpp",
"test/test_tracer_config.cpp",
"test/test_tracer_telemetry.cpp",
"test/test_tracer.cpp",
"test/test_trace_sampler.cpp",
],
deps = [
"dd_trace_cpp",
],
)
2 changes: 2 additions & 0 deletions bin/bazel-build
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ set -e
cd "$(dirname "$0")"/..

bazelisk build dd_trace_cpp

bazelisk "$@" test dd_trace_cpp_test
3 changes: 2 additions & 1 deletion bin/check
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ cd "$(dirname "$0")"/..

bin/format --dry-run -Werror
bin/test
bin/bazel-build
bin/bazel-build --bazelrc=.bazelrc.absl
bin/bazel-build --bazelrc=.bazelrc.std
find bin/ -executable -type f -print0 | xargs -0 shellcheck
22 changes: 14 additions & 8 deletions src/datadog/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@
namespace datadog {
namespace tracing {
namespace environment {
namespace {

Optional<std::string> get_env(const char *name) {
if (const char *value = std::getenv(name)) {
return value;
}
return nullopt;
}

} // namespace

StringView name(Variable variable) { return variable_names[variable]; }

Optional<StringView> lookup(Variable variable) {
Optional<std::string> lookup(Variable variable) {
const char *name = variable_names[variable];
const char *value = std::getenv(name);
if (!value) {
return nullopt;
}
return StringView{value};
return get_env(name);
}

nlohmann::json to_json() {
auto result = nlohmann::json::object({});

for (const char *name : variable_names) {
if (const char *value = std::getenv(name)) {
result[name] = value;
if (auto value = get_env(name)) {
result[name] = std::move(*value);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/datadog/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
//
// `lookup` retrieves the value of `Variable` in the environment.

#include <string>

#include "json_fwd.hpp"
#include "optional.h"
#include "string_view.h"
Expand Down Expand Up @@ -76,7 +78,7 @@ StringView name(Variable variable);

// Return the value of the specified environment `variable`, or return
// `nullopt` if that variable is not set in the environment.
Optional<StringView> lookup(Variable variable);
Optional<std::string> lookup(Variable variable);

nlohmann::json to_json();

Expand Down
55 changes: 28 additions & 27 deletions src/datadog/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
// error then it cannot be "dereferenced" with `operator*`, i.e. it is analogous
// to `Optional<Error>` (and is implemented as such).

#include <utility>
#include <variant>

#include "error.h"
Expand All @@ -52,16 +53,16 @@ class Expected {
public:
Expected() = default;
Expected(const Expected&) = default;
Expected(Expected&) = default;
Expected(Expected&&) = default;

Expected(const Value&);
Expected(Value&&);
Expected(const Error&);
Expected(Error&&);

Expected& operator=(const Expected&) = default;
Expected& operator=(Expected&&) = default;

template <typename Other>
Expected(Other&&);
template <typename Other>
Expected& operator=(Other&&);

// Return whether this object holds a `Value` (as opposed to an `Error`).
bool has_value() const noexcept;
explicit operator bool() const noexcept;
Expand Down Expand Up @@ -99,15 +100,16 @@ class Expected {
};

template <typename Value>
template <typename Other>
Expected<Value>::Expected(Other&& other) : data_(std::forward<Other>(other)) {}
Expected<Value>::Expected(const Value& value) : data_(value) {}

template <typename Value>
template <typename Other>
Expected<Value>& Expected<Value>::operator=(Other&& other) {
data_ = std::forward<Other>(other);
return *this;
}
Expected<Value>::Expected(Value&& value) : data_(std::move(value)) {}

template <typename Value>
Expected<Value>::Expected(const Error& error) : data_(error) {}

template <typename Value>
Expected<Value>::Expected(Error&& error) : data_(std::move(error)) {}

template <typename Value>
bool Expected<Value>::has_value() const noexcept {
Expand Down Expand Up @@ -194,16 +196,17 @@ class Expected<void> {
public:
Expected() = default;
Expected(const Expected&) = default;
Expected(Expected&) = default;
Expected(Expected&&) = default;

Expected(const Error&);
Expected(Error&&);
Expected(decltype(nullopt));
explicit Expected(const Optional<Error>&);
explicit Expected(Optional<Error>&&);

Expected& operator=(const Expected&) = default;
Expected& operator=(Expected&&) = default;

template <typename Other>
Expected(Other&&);
template <typename Other>
Expected& operator=(Other&&);

void swap(Expected& other);

bool has_value() const;
Expand All @@ -221,14 +224,12 @@ class Expected<void> {
const Error* if_error() const&& = delete;
};

template <typename Other>
Expected<void>::Expected(Other&& other) : data_(std::forward<Other>(other)) {}

template <typename Other>
Expected<void>& Expected<void>::operator=(Other&& other) {
data_ = std::forward<Other>(other);
return *this;
}
inline Expected<void>::Expected(const Error& error) : data_(error) {}
inline Expected<void>::Expected(Error&& error) : data_(std::move(error)) {}
inline Expected<void>::Expected(decltype(nullopt)) : data_(nullopt) {}
inline Expected<void>::Expected(const Optional<Error>& data) : data_(data) {}
inline Expected<void>::Expected(Optional<Error>&& data)
: data_(std::move(data)) {}

inline void Expected<void>::swap(Expected& other) { data_.swap(other.data_); }

Expand Down
4 changes: 2 additions & 2 deletions src/datadog/extraction_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Expected<Optional<std::uint64_t>> extract_id_header(const DictReader& headers,
int base) {
auto found = headers.lookup(header);
if (!found) {
return nullopt;
return Optional<std::uint64_t>();
}
auto result = parse_uint64(*found, base);
if (auto* error = result.if_error()) {
Expand All @@ -87,7 +87,7 @@ Expected<Optional<std::uint64_t>> extract_id_header(const DictReader& headers,
prefix += ' ';
return error->with_prefix(prefix);
}
return *result;
return Optional<std::uint64_t>(*result);
}

} // namespace
Expand Down
2 changes: 1 addition & 1 deletion src/datadog/msgpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ inline void pack_integer(std::string& buffer, std::int32_t value) {
}

inline Expected<void> pack_string(std::string& buffer, StringView value) {
return pack_string(buffer, value.begin(), value.size());
return pack_string(buffer, value.data(), value.size());
}

} // namespace msgpack
Expand Down
Loading