Skip to content

Commit

Permalink
Apply changes from #221 as one signed patch. (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgoffredo authored Apr 11, 2022
1 parent 662f433 commit 80979a8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 62 deletions.
14 changes: 11 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include(GNUInstallDirs)

option(BUILD_SHARED "Builds shared library" ON)
option(BUILD_STATIC "Builds static library" OFF)
option(BUILD_OBJECT "Builds objects for use in another project" OFF)
option(BUILD_PLUGIN "Builds plugin (requires gcc and not macos)" OFF)
option(BUILD_TESTING "Builds tests, also enables BUILD_SHARED" OFF)
option(BUILD_COVERAGE "Builds code with code coverage profiling instrumentation" OFF)
Expand All @@ -27,7 +28,6 @@ endif()
set(CMAKE_CXX_STANDARD 14)

# Includes
set(CMAKE_INCLUDE_PATH 3rd_party/include deps/include)
include_directories(SYSTEM 3rd_party/include deps/include)
include_directories(include)

Expand All @@ -37,7 +37,6 @@ set(CMAKE_LIBRARY_PATH deps/lib)
# Dependencies
find_path(OPENTRACING_INCLUDE_DIR NAMES opentracing/tracer.h)
find_library(OPENTRACING_LIB opentracing)
find_package(ZLIB REQUIRED)
find_library(MSGPACK_LIB msgpack)
find_package(CURL)
find_package(Threads REQUIRED)
Expand Down Expand Up @@ -65,7 +64,7 @@ endif()
if(BUILD_COVERAGE)
set(COVERAGE_LIBRARIES gcov)
endif()
set(DATADOG_LINK_LIBRARIES ${OPENTRACING_LIB} ${CURL_LIBRARIES} ${ZLIB_LIBRARIES} Threads::Threads ${COVERAGE_LIBRARIES})
set(DATADOG_LINK_LIBRARIES ${OPENTRACING_LIB} ${CURL_LIBRARIES} Threads::Threads ${COVERAGE_LIBRARIES})

## Shared lib
if(BUILD_SHARED)
Expand Down Expand Up @@ -94,6 +93,15 @@ if(BUILD_STATIC)
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

## Object lib
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)
set_property(TARGET dd_opentracing-object PROPERTY POSITION_INDEPENDENT_CODE ON)
target_compile_definitions(dd_opentracing-object PUBLIC DD_OPENTRACING_OBJECT)
endif()

## Plugin
if(BUILD_PLUGIN)
if(CMAKE_SYSTEM_NAME MATCHES "Darwin" OR NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Expand Down
11 changes: 11 additions & 0 deletions include/datadog/opentracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ DD_OPENTRACING_API std::shared_ptr<ot::Tracer> makeTracer(const TracerOptions& o
std::tuple<std::shared_ptr<ot::Tracer>, std::shared_ptr<TraceEncoder>> makeTracerAndEncoder(
const TracerOptions& options);

// Return a JSON representation of the specified `options`. If the specified
// `with_timestamp` is `true`, then include a "date" field whose value is the
// current date and time.
// This function is defined in `tracer_options.cpp`.
std::string toJSON(const TracerOptions& options, bool with_timestamp);

// Return a reference to the options used to configure the specified `tracer`.
// The behavior is undefined unless `tracer` is a Datadog tracer.
// This function is defined in `tracer.cpp`.
const TracerOptions& getOptions(const ot::Tracer& tracer);

} // namespace opentracing
} // namespace datadog

Expand Down
25 changes: 4 additions & 21 deletions scripts/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ fi
OPENTRACING_VERSION=${OPENTRACING_VERSION:-1.6.0}
CURL_VERSION=${CURL_VERSION:-7.70.0}
MSGPACK_VERSION=${MSGPACK_VERSION:-3.2.1}
ZLIB_VERSION=${ZLIB_VERSION:-1.2.12}

MAKE_JOB_COUNT=${MAKE_JOB_COUNT:-$(nproc)}

Expand All @@ -18,7 +17,6 @@ if [[ "$1" == "versions" ]]; then
echo "opentracing:$OPENTRACING_VERSION"
echo "curl:$CURL_VERSION"
echo "msgpack:$MSGPACK_VERSION"
echo "zlib:$ZLIB_VERSION"
exit 0
fi

Expand All @@ -28,7 +26,6 @@ fi
BUILD_OPENTRACING=1
BUILD_CURL=1
BUILD_MSGPACK=1
BUILD_ZLIB=1

while test $# -gt 0
do
Expand All @@ -39,8 +36,6 @@ do
;;
not-curl) BUILD_CURL=0
;;
not-zlib) BUILD_ZLIB=0
;;
*) echo "unknown dependency: $1" && exit 1
;;
esac
Expand All @@ -67,26 +62,13 @@ if [ "$BUILD_OPENTRACING" -eq "1" ]; then
rm opentracing-cpp.tar.gz
fi

# Zlib
if [ "$BUILD_ZLIB" -eq "1" ]; then
wget "https://zlib.net/zlib-${ZLIB_VERSION}.tar.gz"
tar zxf "zlib-${ZLIB_VERSION}.tar.gz"
mkdir -p "zlib-${ZLIB_VERSION}"
cd "zlib-${ZLIB_VERSION}"
CFLAGS="$CFLAGS -fPIC" ./configure --prefix="$install_dir" --static
make --jobs="$MAKE_JOB_COUNT" && make install
cd ..
rm -r "zlib-${ZLIB_VERSION}"
rm "zlib-${ZLIB_VERSION}.tar.gz"
fi

# Msgpack
if [ "$BUILD_MSGPACK" -eq "1" ]; then
wget "https://github.com/msgpack/msgpack-c/releases/download/cpp-${MSGPACK_VERSION}/msgpack-${MSGPACK_VERSION}.tar.gz" -O msgpack.tar.gz
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 ..
cmake -DCMAKE_INSTALL_PREFIX="$install_dir" -DBUILD_SHARED_LIBS=OFF -DMSGPACK_CXX11=ON ..
make --jobs="$MAKE_JOB_COUNT"
make install
cd ../..
Expand All @@ -112,11 +94,12 @@ if [ "$BUILD_CURL" -eq "1" ]; then
--without-ssl \
--disable-crypto-auth \
--without-axtls \
--with-zlib \
--without-zlib \
--disable-rtsp \
--enable-shared=no \
--enable-static=yes \
--with-pic
--with-pic \
--without-brotli
make --jobs="$MAKE_JOB_COUNT" && make install
cd ..
rm -r "curl-${CURL_VERSION}/"
Expand Down
45 changes: 9 additions & 36 deletions src/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,44 +105,10 @@ void startupLog(TracerOptions &options) {
return;
}

json j;
std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::stringstream ss;
ss << std::put_time(std::localtime(&t), "%FT%T%z");
j["date"] = ss.str();
j["version"] = datadog::version::tracer_version;
j["lang"] = "cpp";
j["lang_version"] = datadog::version::cpp_version;
j["env"] = options.environment;
j["enabled"] = true;
j["service"] = options.service;
if (!options.agent_url.empty()) {
j["agent_url"] = options.agent_url;
} else {
j["agent_url"] =
std::string("http://") + options.agent_host + ":" + std::to_string(options.agent_port);
}
j["analytics_enabled"] = options.analytics_enabled;
j["analytics_sample_rate"] = options.analytics_rate;
if (!std::isnan(options.sample_rate)) {
j["sample_rate"] = options.sample_rate;
}
j["sampling_rules"] = options.sampling_rules;
j["sampling_limit_per_second"] = options.sampling_limit_per_second;
if (!options.tags.empty()) {
j["tags"] = options.tags;
}
if (!options.version.empty()) {
j["dd_version"] = options.version;
}
j["report_hostname"] = options.report_hostname;
if (!options.operation_name_override.empty()) {
j["operation_name_override"] = options.operation_name_override;
}

std::string message;
message += "DATADOG TRACER CONFIGURATION - ";
message += j.dump();
const bool with_timestamp = true;
message += toJSON(options, with_timestamp);
options.log_func(LogLevel::info, message);
}

Expand Down Expand Up @@ -370,5 +336,12 @@ ot::expected<std::unique_ptr<ot::SpanContext>> Tracer::Extract(

void Tracer::Close() noexcept { buffer_->flush(std::chrono::seconds(5)); }

const TracerOptions &Tracer::options() const noexcept { return opts_; }

const TracerOptions &getOptions(const ot::Tracer &tracer) {
auto &dd_tracer = static_cast<const Tracer &>(tracer);
return dd_tracer.options();
}

} // namespace opentracing
} // namespace datadog
2 changes: 2 additions & 0 deletions src/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Tracer : public ot::Tracer, public std::enable_shared_from_this<Tracer> {

void Close() noexcept override;

const TracerOptions &options() const noexcept;

private:
void configureRulesSampler(std::shared_ptr<RulesSampler> sampler) noexcept;

Expand Down
2 changes: 2 additions & 0 deletions src/tracer_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

using json = nlohmann::json;

extern "C" char **environ;

namespace datadog {
namespace opentracing {

Expand Down
40 changes: 40 additions & 0 deletions src/tracer_options.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "tracer_options.h"

#include <datadog/tags.h>
#include <datadog/version.h>
#include <opentracing/ext/tags.h>

#include <iomanip>
#include <limits>
#include <nlohmann/json.hpp>
#include <regex>
#include <sstream>

Expand Down Expand Up @@ -273,5 +276,42 @@ ot::expected<TracerOptions, std::string> applyTracerOptionsFromEnvironment(
return opts;
}

std::string toJSON(const TracerOptions &options, bool with_timestamp) {
nlohmann::json j;
if (with_timestamp) {
std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::stringstream ss;
ss << std::put_time(std::localtime(&t), "%FT%T%z");
j["date"] = ss.str();
}
j["version"] = datadog::version::tracer_version;
j["lang"] = "cpp";
j["lang_version"] = datadog::version::cpp_version;
j["env"] = options.environment;
j["enabled"] = true;
j["service"] = options.service;
if (!options.agent_url.empty()) {
j["agent_url"] = options.agent_url;
} else {
j["agent_url"] =
std::string("http://") + options.agent_host + ":" + std::to_string(options.agent_port);
}
j["analytics_enabled"] = options.analytics_enabled;
j["analytics_sample_rate"] = options.analytics_rate;
j["sampling_rules"] = options.sampling_rules;
if (!options.tags.empty()) {
j["tags"] = options.tags;
}
if (!options.version.empty()) {
j["dd_version"] = options.version;
}
j["report_hostname"] = options.report_hostname;
if (!options.operation_name_override.empty()) {
j["operation_name_override"] = options.operation_name_override;
}

return j.dump();
}

} // namespace opentracing
} // namespace datadog
2 changes: 1 addition & 1 deletion test/integration/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ENV LDFLAGS="-fPIC"


RUN apt-get update && \
apt-get install -y git build-essential wget curl tar cmake libpcre3-dev zlib1g-dev
apt-get install -y git build-essential wget curl tar cmake libpcre3-dev

WORKDIR /root

Expand Down
1 change: 0 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"homepage": "https://github.com/DataDog/dd-opentracing-cpp",
"description": "Datadog OpenTracing C++ Client",
"dependencies": [
"zlib",
"msgpack",
"curl",
"opentracing"
Expand Down

0 comments on commit 80979a8

Please sign in to comment.