diff --git a/.gitignore b/.gitignore index af46e411..6e59f507 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # Build folder .build/ +build/ # Locally installed dependencies deps/ diff --git a/CMakeLists.txt b/CMakeLists.txt index fc121ca5..e8449f65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13) -project(dd-opentracing-cpp) +project(dd-opentracing-cpp CXX C) set(SOVERSION 0) @@ -25,37 +25,33 @@ endif() if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE "RelWithDebInfo") endif() -set(CMAKE_CXX_STANDARD 14) -# Includes -include_directories(SYSTEM 3rd_party/include deps/include) -include_directories(include) +set(CMAKE_CXX_STANDARD 14) -# Libraries -set(CMAKE_LIBRARY_PATH deps/lib) +include(Dependencies.cmake) +add_external_dependencies() # Dependencies -find_path(OPENTRACING_INCLUDE_DIR NAMES opentracing/tracer.h) -find_library(OPENTRACING_LIB opentracing) -find_library(MSGPACK_LIB msgpack) -find_package(CURL) find_package(Threads REQUIRED) # Code Sanitizers -set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/3rd_party/sanitizers-cmake" ${CMAKE_MODULE_PATH}) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rd_party/sanitizers-cmake") find_package(Sanitizers) # Code install(DIRECTORY include/datadog DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) file(GLOB DD_OPENTRACING_SOURCES "src/*.cpp") message(STATUS "Compiler ID: ${CMAKE_CXX_COMPILER_ID}") + +add_library(datadog_project_options INTERFACE) + if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) - add_compile_options(-Wall -Wextra -Werror -pedantic -Wnon-virtual-dtor -Woverloaded-virtual -Wold-style-cast -std=c++14) + target_compile_options(datadog_project_options INTERFACE -Wall -Wextra -pedantic) if(BUILD_COVERAGE) - add_compile_options(-g -O0 -fprofile-arcs -ftest-coverage) + target_compile_options(datadog_project_options INTERFACE -g -O0 -fprofile-arcs -ftest-coverage) endif() elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - add_compile_options(/W3) + target_compile_options(datadog_project_options INTERFACE /W3) else() message(FATAL_ERROR "Unknown compiler ${CMAKE_CXX_COMPILER_ID}") endif() @@ -64,7 +60,9 @@ endif() if(BUILD_COVERAGE) set(COVERAGE_LIBRARIES gcov) endif() -set(DATADOG_LINK_LIBRARIES ${OPENTRACING_LIB} ${CURL_LIBRARIES} Threads::Threads ${COVERAGE_LIBRARIES}) +set(DATADOG_LINK_LIBRARIES + msgpack-cxx + libcurl Threads::Threads nlohmann_json::nlohmann_json CLI11::CLI11 ${COVERAGE_LIBRARIES} datadog_project_options opentelemetry_api) ## Shared lib if(BUILD_SHARED) @@ -73,8 +71,9 @@ if(BUILD_SHARED) if(BUILD_COVERAGE) target_link_options(dd_opentracing PRIVATE -fprofile-arcs -ftest-coverage) endif() - target_link_libraries(dd_opentracing ${DATADOG_LINK_LIBRARIES}) + target_link_libraries(dd_opentracing opentelemetry_opentracing_shim ${DATADOG_LINK_LIBRARIES}) set_target_properties(dd_opentracing PROPERTIES SOVERSION ${SOVERSION}) + target_include_directories(dd_opentracing PUBLIC include) target_compile_definitions(dd_opentracing PUBLIC DD_OPENTRACING_SHARED) install(TARGETS dd_opentracing LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -85,8 +84,9 @@ endif() if(BUILD_STATIC) add_library(dd_opentracing-static STATIC ${DD_OPENTRACING_SOURCES}) add_sanitizers(dd_opentracing-static) - target_link_libraries(dd_opentracing-static ${DATADOG_LINK_LIBRARIES}) + target_link_libraries(dd_opentracing-static opentelemetry_opentracing_shim_static ${DATADOG_LINK_LIBRARIES}) set_target_properties(dd_opentracing-static PROPERTIES OUTPUT_NAME dd_opentracing POSITION_INDEPENDENT_CODE ON) + target_include_directories(dd_opentracing-static PUBLIC include) target_compile_definitions(dd_opentracing-static PUBLIC DD_OPENTRACING_STATIC) install(TARGETS dd_opentracing-static LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -97,7 +97,8 @@ endif() if(BUILD_OBJECT) add_library(dd_opentracing-object OBJECT ${DD_OPENTRACING_SOURCES}) add_sanitizers(dd_opentracing-object) - target_link_libraries(dd_opentracing-object ${CURL_LIBRARIES} Threads::Threads) + target_link_libraries(dd_opentracing-object opentelemetry_opentracing_shim ${DATADOG_LINK_LIBRARIES} ${CURL_LIBRARIES} Threads::Threads) + target_include_directories(dd_opentracing-object PUBLIC include) set_property(TARGET dd_opentracing-object PROPERTY POSITION_INDEPENDENT_CODE ON) target_compile_definitions(dd_opentracing-object PUBLIC DD_OPENTRACING_OBJECT) endif() @@ -114,10 +115,11 @@ if(BUILD_PLUGIN) if(BUILD_COVERAGE) target_link_options(dd_opentracing_plugin PRIVATE -fprofile-arcs -ftest-coverage) endif() - target_link_libraries(dd_opentracing_plugin PUBLIC ${DATADOG_LINK_LIBRARIES} + target_link_libraries(dd_opentracing_plugin PUBLIC opentelemetry_opentracing_shim ${DATADOG_LINK_LIBRARIES} -static-libstdc++ -static-libgcc -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/export.map) + target_include_directories(dd_opentracing_plugin PUBLIC include) install(TARGETS dd_opentracing_plugin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() diff --git a/Dependencies.cmake b/Dependencies.cmake new file mode 100644 index 00000000..296c22b3 --- /dev/null +++ b/Dependencies.cmake @@ -0,0 +1,38 @@ +include(cmake/CPM.cmake) +set(CPM_USE_LOCAL_PACKAGES ON) + +function(add_external_dependencies) + +CPMAddPackage("gh:catchorg/Catch2@3.3.2") +CPMAddPackage("gh:CLIUtils/CLI11@2.3.2") +CPMAddPackage( + NAME opentelemetry-cpp + GITHUB_REPOSITORY maztheman/opentelemetry-cpp + GIT_TAG main + OPTIONS + "WITH_STL OFF" "WITH_OPENTRACING ON" "BUILD_TESTING OFF" "BUILD_STATIC_LIBS ON" "WITH_EXAMPLES OFF" +) +CPMAddPackage( + NAME curl + GITHUB_REPOSITORY curl/curl + GIT_TAG curl-8_2_1 + OPTIONS + "HTTP_ONLY ON" "CURL_ENABLE_SSL OFF" "BUILD_CURL_EXE OFF" "BUILD_SHARED_LIBS OFF" "CMAKE_POSITION_INDEPENDENT_CODE ON" + +) +CPMAddPackage( + NAME msgpack-c + GITHUB_REPOSITORY msgpack/msgpack-c + GIT_TAG cpp-6.1.0 + OPTIONS "MSGPACK_CXX14 ON" "MSGPACK_USE_BOOST OFF" "BUILD_SHARED_LIBS OFF" +) +CPMAddPackage( + NAME nlohmann_json + VERSION 3.11.2 + GITHUB_REPOSITORY nlohmann/json + GITHUB_TAG v3.11.2 + OPTIONS + "JSON_BuildTests OFF" +) + +endfunction() diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 00000000..f49d7434 --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,33 @@ +set(CPM_DOWNLOAD_VERSION 0.38.2) + +if(CPM_SOURCE_CACHE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +else() + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +endif() + +# Expand relative path. This is important if the provided path contains a tilde (~) +get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) + +function(download_cpm) + message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") + file(DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake + ${CPM_DOWNLOAD_LOCATION} + ) +endfunction() + +if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) + download_cpm() +else() + # resume download if it previously failed + file(READ ${CPM_DOWNLOAD_LOCATION} check) + if("${check}" STREQUAL "") + download_cpm() + endif() + unset(check) +endif() + +include(${CPM_DOWNLOAD_LOCATION}) diff --git a/scripts/install_dependencies.sh b/scripts/install_dependencies.sh index 5a36b9fd..7174217e 100755 --- a/scripts/install_dependencies.sh +++ b/scripts/install_dependencies.sh @@ -68,7 +68,7 @@ if [ "$BUILD_MSGPACK" -eq "1" ]; then tar zxf msgpack.tar.gz mkdir -p "msgpack-${MSGPACK_VERSION}/.build" cd "msgpack-${MSGPACK_VERSION}/.build" - cmake -DCMAKE_INSTALL_PREFIX="$install_dir" -DBUILD_SHARED_LIBS=OFF -DMSGPACK_CXX11=ON .. + cmake -DCMAKE_INSTALL_PREFIX="$install_dir" -DBUILD_SHARED_LIBS=OFF -DMSGPACK_CXX14=ON .. make --jobs="$MAKE_JOB_COUNT" make install cd ../.. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 67d0e22b..0c7728d1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,3 @@ -add_library(catch STATIC test_main.cpp) - macro(_datadog_test TEST_NAME) add_executable(${TEST_NAME} ${ARGN}) add_sanitizers(${TEST_NAME}) @@ -7,8 +5,9 @@ macro(_datadog_test TEST_NAME) target_link_options(${TEST_NAME} PRIVATE -fprofile-arcs -ftest-coverage) endif() target_link_libraries(${TEST_NAME} dd_opentracing + Catch2::Catch2WithMain ${DATADOG_LINK_LIBRARIES} - catch) + ) add_test(${TEST_NAME} ${TEST_NAME}) endmacro() diff --git a/test/agent_writer_test.cpp b/test/agent_writer_test.cpp index 6baa6261..96e25dc5 100644 --- a/test/agent_writer_test.cpp +++ b/test/agent_writer_test.cpp @@ -1,8 +1,10 @@ #include "../src/agent_writer.h" +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include -#include +#include #include #include "mocks.h" @@ -185,7 +187,7 @@ TEST_CASE("writer") { } SECTION("handle error responses") { - using Catch::Matchers::Contains; + using Catch::Matchers::ContainsSubstring; // HTTP status zero indicates "no status." handle->response_status = 0; @@ -195,7 +197,8 @@ TEST_CASE("writer") { writer.flush(std::chrono::seconds(10)); REQUIRE(logger->records.size() != 0); // The logged error diagnostic will say that there was no response status. - REQUIRE_THAT(logger->records.back().message, Contains("response without an HTTP status")); + REQUIRE_THAT(logger->records.back().message, + ContainsSubstring("response without an HTTP status")); // HTTP status 200 with an empty body means that the response really should // be 429 "too many requests," but the Agent is not configured to return @@ -207,7 +210,7 @@ TEST_CASE("writer") { writer.flush(std::chrono::seconds(10)); REQUIRE(logger->records.size() != 0); // The logged error diagnostic will mention the lack of response. - REQUIRE_THAT(logger->records.back().message, Contains("response without a body")); + REQUIRE_THAT(logger->records.back().message, ContainsSubstring("response without a body")); // HTTP statuses other than 200 are unexpected. std::vector statuses; @@ -225,7 +228,8 @@ TEST_CASE("writer") { writer.flush(std::chrono::seconds(10)); REQUIRE(logger->records.size() != 0); // The logged error diagnostic will contain the response status. - REQUIRE_THAT(logger->records.back().message, Contains(" " + std::to_string(status) + " ")); + REQUIRE_THAT(logger->records.back().message, + ContainsSubstring(" " + std::to_string(status) + " ")); } SECTION("queue does not grow indefinitely") { diff --git a/test/glob_test.cpp b/test/glob_test.cpp index 973a9322..18774114 100644 --- a/test/glob_test.cpp +++ b/test/glob_test.cpp @@ -3,7 +3,9 @@ #include "../src/glob.h" -#include +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING +#include using namespace datadog::opentracing; diff --git a/test/limiter_test.cpp b/test/limiter_test.cpp index 837e2404..7afb77d9 100644 --- a/test/limiter_test.cpp +++ b/test/limiter_test.cpp @@ -1,6 +1,8 @@ #include "../src/limiter.h" -#include +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING +#include #include "mocks.h" using namespace datadog::opentracing; diff --git a/test/logger_test.cpp b/test/logger_test.cpp index b22f96b5..699bf21c 100644 --- a/test/logger_test.cpp +++ b/test/logger_test.cpp @@ -1,6 +1,9 @@ #include "../src/logger.h" -#include +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING +#include + using namespace datadog::opentracing; TEST_CASE("logger") { diff --git a/test/opentracing_test.cpp b/test/opentracing_test.cpp index a956ced2..c40827aa 100644 --- a/test/opentracing_test.cpp +++ b/test/opentracing_test.cpp @@ -1,6 +1,8 @@ #include -#include +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING +#include #include "mocks.h" using namespace datadog::opentracing; diff --git a/test/propagation_test.cpp b/test/propagation_test.cpp index d9a542d2..0a685527 100644 --- a/test/propagation_test.cpp +++ b/test/propagation_test.cpp @@ -1,9 +1,11 @@ +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include #include #include #include -#include +#include #include #include diff --git a/test/sample_test.cpp b/test/sample_test.cpp index 53c93c03..b323c148 100644 --- a/test/sample_test.cpp +++ b/test/sample_test.cpp @@ -1,7 +1,9 @@ #include "../src/sample.h" +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include -#include +#include #include #include diff --git a/test/span_buffer_test.cpp b/test/span_buffer_test.cpp index 18a63138..d2b56af4 100644 --- a/test/span_buffer_test.cpp +++ b/test/span_buffer_test.cpp @@ -1,6 +1,8 @@ #include "../src/span_buffer.h" -#include +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING +#include #include "../src/sample.h" #include "mocks.h" diff --git a/test/span_test.cpp b/test/span_test.cpp index 843e11ba..1f147da1 100644 --- a/test/span_test.cpp +++ b/test/span_test.cpp @@ -1,9 +1,11 @@ #include "../src/span.h" +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include #include -#include +#include #include #include #include diff --git a/test/tag_propagation_test.cpp b/test/tag_propagation_test.cpp index 5ab2027c..694345c2 100644 --- a/test/tag_propagation_test.cpp +++ b/test/tag_propagation_test.cpp @@ -3,8 +3,10 @@ #include "../src/tag_propagation.h" +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include -#include +#include #include #include #include diff --git a/test/test_main.cpp b/test/test_main.cpp deleted file mode 100644 index 4ed06df1..00000000 --- a/test/test_main.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include diff --git a/test/tracer_factory_test.cpp b/test/tracer_factory_test.cpp index 9ebddc8a..b15595ac 100644 --- a/test/tracer_factory_test.cpp +++ b/test/tracer_factory_test.cpp @@ -1,8 +1,10 @@ #include "../src/tracer_factory.h" +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include -#include +#include #include #include #include diff --git a/test/tracer_options_test.cpp b/test/tracer_options_test.cpp index dbe772a9..6be92422 100644 --- a/test/tracer_options_test.cpp +++ b/test/tracer_options_test.cpp @@ -1,8 +1,10 @@ #include "../src/tracer_options.h" +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include -#include +#include #include using namespace datadog::opentracing; using namespace std::string_literals; diff --git a/test/tracer_test.cpp b/test/tracer_test.cpp index 08669f6e..f2565ab7 100644 --- a/test/tracer_test.cpp +++ b/test/tracer_test.cpp @@ -1,9 +1,11 @@ #include "../src/tracer.h" +#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_ENABLE_BENCHMARKING #include #include -#include +#include #include #include "../src/sample.h"