Skip to content

Commit

Permalink
Use a simple header for embedding tracer version. (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgilmour authored Jul 23, 2019
1 parent 2c888d6 commit 8cf643b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 90 deletions.
22 changes: 1 addition & 21 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand All @@ -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"],
Expand Down
32 changes: 0 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions include/datadog/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef DD_INCLUDE_VERSION_H
#define DD_INCLUDE_VERSION_H

#include <string>

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
1 change: 0 additions & 1 deletion src/agent_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "sample.h"
#include "span.h"
#include "transport.h"
#include "version_number.h"

namespace datadog {
namespace opentracing {
Expand Down
6 changes: 3 additions & 3 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "encoder.h"
#include <datadog/version.h>
#include <nlohmann/json.hpp>
#include "sample.h"
#include "span.h"
#include "version_number.h"

using json = nlohmann::json;

Expand All @@ -26,8 +26,8 @@ AgentHttpEncoder::AgentHttpEncoder(std::shared_ptr<SampleProvider> 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";
Expand Down
19 changes: 0 additions & 19 deletions src/version_number.h.in

This file was deleted.

1 change: 0 additions & 1 deletion src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <iostream>
#include "encoder.h"
#include "span.h"
#include "version_number.h"

namespace datadog {
namespace opentracing {
Expand Down
28 changes: 15 additions & 13 deletions test/agent_writer_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../src/agent_writer.h"
#include <datadog/version.h>
#include "../src/agent_writer.cpp" // Otherwise the compiler won't generate AgentWriter for us.
#include "mocks.h"
#include "version_number.h"

#include <ctime>

Expand Down Expand Up @@ -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<std::string, std::string>{
{"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<std::string, std::string>{
{"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") {
Expand Down Expand Up @@ -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<std::string, std::string>{
{"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<std::string, std::string>{
{"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"}});
}
}
}
Expand Down

0 comments on commit 8cf643b

Please sign in to comment.