From 8cf643ba1ce2d038828cecfe6e78444fd7b37f89 Mon Sep 17 00:00:00 2001 From: Caleb Gilmour Date: Wed, 24 Jul 2019 09:49:11 +1200 Subject: [PATCH] Use a simple header for embedding tracer version. (#105) --- BUILD.bazel | 22 +--------------------- CMakeLists.txt | 32 -------------------------------- include/datadog/version.h | 15 +++++++++++++++ src/agent_writer.cpp | 1 - src/encoder.cpp | 6 +++--- src/version_number.h.in | 19 ------------------- src/writer.cpp | 1 - test/agent_writer_test.cpp | 28 +++++++++++++++------------- 8 files changed, 34 insertions(+), 90 deletions(-) create mode 100644 include/datadog/version.h delete mode 100644 src/version_number.h.in diff --git a/BUILD.bazel b/BUILD.bazel index cbae3173..523e8d71 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -21,9 +21,9 @@ cc_library( "src/tracer_options.h", "src/writer.cpp", "src/writer.h", - ":version_number.h", ], hdrs = [ + "include/datadog/version.h", "include/datadog/opentracing.h", "include/datadog/tags.h", ], @@ -45,26 +45,6 @@ cc_library( ], ) -genrule( - name = "generate_version_number_h", - srcs = glob([ - "CMakeLists.txt", - "src/*", - ]), - outs = [ - "version_number.h", - ], - cmd = """ - TEMP_DIR=$$(mktemp -d) - trap -- 'rm -r $$TEMP_DIR' EXIT - VERSION_NUMBER_H_OUT=$${PWD}/$(location :version_number.h) - CMAKE_ROOT=$$(dirname $${PWD}/$(location :CMakeLists.txt)) - cd $$TEMP_DIR - cmake -DHEADERS_ONLY=ON -L $$CMAKE_ROOT - mv version_number.h $$VERSION_NUMBER_H_OUT - """, -) - cc_library( name = "3rd_party_nlohmann", hdrs = ["3rd_party/include/nlohmann/json.hpp"], diff --git a/CMakeLists.txt b/CMakeLists.txt index eabddfad..04854a3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,6 @@ option(BUILD_SHARED "Builds shared library" ON) option(BUILD_STATIC "Builds static library" OFF) option(BUILD_PLUGIN "Builds plugin (requires gcc and not macos)" OFF) option(BUILD_TESTING "Builds tests, also enables BUILD_SHARED" OFF) -option(HEADERS_ONLY "Only generate version_number.h" OFF) if(BUILD_TESTING) set(BUILD_SHARED ON) @@ -23,37 +22,6 @@ endif() # Configure the compiler. set(CMAKE_CXX_STANDARD 14) -# If a git tag is set then use that as the version number, -# otherwise use short commit hash. If git unavailable, -# "unknown" is used. -if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git) - find_package(Git) - if(GIT_FOUND) - execute_process( - COMMAND ${GIT_EXECUTABLE} describe --tags --always - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE "VERSION" - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE) - message(STATUS "Git version: ${VERSION}") - else(GIT_FOUND) - set(VERSION "unknown") - message(WARNING "Git missing, version number for this build is 'unknown'") - endif(GIT_FOUND) -else(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git) - set(VERSION "unknown") - message(WARNING "Not being built from git repo, version number for this build is 'unknown'") -endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git) - -configure_file( - "${PROJECT_SOURCE_DIR}/src/version_number.h.in" - "${PROJECT_BINARY_DIR}/version_number.h") -include_directories("${PROJECT_BINARY_DIR}") - -if(HEADERS_ONLY) - return() -endif() - # Dependencies find_path(OPENTRACING_INCLUDE_DIR NAMES opentracing/tracer.h) find_library(OPENTRACING_LIB opentracing) diff --git a/include/datadog/version.h b/include/datadog/version.h new file mode 100644 index 00000000..76da12a3 --- /dev/null +++ b/include/datadog/version.h @@ -0,0 +1,15 @@ +#ifndef DD_INCLUDE_VERSION_H +#define DD_INCLUDE_VERSION_H + +#include + +namespace datadog { +namespace version { + +const std::string tracer_version = "v1.0.0"; +const std::string cpp_version = std::to_string(__cplusplus); + +} // namespace version +} // namespace datadog + +#endif // DD_INCLUDE_VERSION_H diff --git a/src/agent_writer.cpp b/src/agent_writer.cpp index 9b23897b..7438fce9 100644 --- a/src/agent_writer.cpp +++ b/src/agent_writer.cpp @@ -4,7 +4,6 @@ #include "sample.h" #include "span.h" #include "transport.h" -#include "version_number.h" namespace datadog { namespace opentracing { diff --git a/src/encoder.cpp b/src/encoder.cpp index c80fce15..98ca0cf9 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -1,8 +1,8 @@ #include "encoder.h" +#include #include #include "sample.h" #include "span.h" -#include "version_number.h" using json = nlohmann::json; @@ -26,8 +26,8 @@ AgentHttpEncoder::AgentHttpEncoder(std::shared_ptr sampler) // Set up common headers and default encoder common_headers_ = {{header_content_type, "application/msgpack"}, {header_dd_meta_lang, "cpp"}, - {header_dd_meta_lang_version, config::cpp_version}, - {header_dd_meta_tracer_version, config::tracer_version}}; + {header_dd_meta_lang_version, ::datadog::version::cpp_version}, + {header_dd_meta_tracer_version, ::datadog::version::tracer_version}}; } const std::string agent_api_path = "/v0.4/traces"; diff --git a/src/version_number.h.in b/src/version_number.h.in deleted file mode 100644 index e214ca9d..00000000 --- a/src/version_number.h.in +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef DD_OPENTRACING_VERSION_NUMBER_H -#define DD_OPENTRACING_VERSION_NUMBER_H - -#include - -#define VERSION "@VERSION@" - -namespace datadog { -namespace opentracing { -namespace config { - -const std::string tracer_version = VERSION; -const std::string cpp_version = std::to_string(__cplusplus); - -} // namespace config -} // namespace opentracing -} // namespace datadog - -#endif // DD_OPENTRACING_VERSION_NUMBER_H diff --git a/src/writer.cpp b/src/writer.cpp index 549c9872..dcebb0c0 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -2,7 +2,6 @@ #include #include "encoder.h" #include "span.h" -#include "version_number.h" namespace datadog { namespace opentracing { diff --git a/test/agent_writer_test.cpp b/test/agent_writer_test.cpp index 4bd2f68f..b4cdd82e 100644 --- a/test/agent_writer_test.cpp +++ b/test/agent_writer_test.cpp @@ -1,7 +1,7 @@ #include "../src/agent_writer.h" +#include #include "../src/agent_writer.cpp" // Otherwise the compiler won't generate AgentWriter for us. #include "mocks.h" -#include "version_number.h" #include @@ -67,12 +67,13 @@ TEST_CASE("writer") { {CURLOPT_URL, "http://hostname:6319/v0.4/traces"}, {CURLOPT_TIMEOUT_MS, "2000"}, {CURLOPT_POSTFIELDSIZE, "135"}}); - REQUIRE(handle->headers == std::map{ - {"Content-Type", "application/msgpack"}, - {"Datadog-Meta-Lang", "cpp"}, - {"Datadog-Meta-Tracer-Version", config::tracer_version}, - {"Datadog-Meta-Lang-Version", config::cpp_version}, - {"X-Datadog-Trace-Count", "1"}}); + REQUIRE(handle->headers == + std::map{ + {"Content-Type", "application/msgpack"}, + {"Datadog-Meta-Lang", "cpp"}, + {"Datadog-Meta-Tracer-Version", ::datadog::version::tracer_version}, + {"Datadog-Meta-Lang-Version", ::datadog::version::cpp_version}, + {"X-Datadog-Trace-Count", "1"}}); } SECTION("responses are sent to sampler") { @@ -324,12 +325,13 @@ TEST_CASE("writer") { writer.write(make_trace( {TestSpanData{"web", "service", "resource", "service.name", 3, 1, 1, 69, 420, 0}})); writer.flush(std::chrono::seconds(10)); - REQUIRE(handle->headers == std::map{ - {"Content-Type", "application/msgpack"}, - {"Datadog-Meta-Lang", "cpp"}, - {"Datadog-Meta-Tracer-Version", config::tracer_version}, - {"Datadog-Meta-Lang-Version", config::cpp_version}, - {"X-Datadog-Trace-Count", "3"}}); + REQUIRE(handle->headers == + std::map{ + {"Content-Type", "application/msgpack"}, + {"Datadog-Meta-Lang", "cpp"}, + {"Datadog-Meta-Tracer-Version", ::datadog::version::tracer_version}, + {"Datadog-Meta-Lang-Version", ::datadog::version::cpp_version}, + {"X-Datadog-Trace-Count", "3"}}); } } }