Skip to content

Commit 9ac719c

Browse files
authored
build dd-trace-cpp as a submodule within nginx-datadog (#24)
* create multiple build target types, one of which (*-objects) nginx-datadog will consume * Tracer::config_json() * include "tags" in to_json(rule) * forbid assignment to Span * factor out parsing of propagation styles * use libstdc++ for fuzzing, even with clang
1 parent 4e490a7 commit 9ac719c

File tree

15 files changed

+111
-72
lines changed

15 files changed

+111
-72
lines changed

CMakeLists.txt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ set(CMAKE_CXX_EXTENSIONS OFF)
1919

2020
# If we're building with clang, then use the libc++ version of the standard
2121
# library instead of libstdc++. Better coverage of build configurations.
22-
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
23-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
22+
#
23+
# But there's one exception: libfuzzer is built with libstdc++ on Ubuntu,
24+
# and so won't link to libc++. So, if any of the FUZZ_* variables are set,
25+
# keep to libstdc++ (the default on most systems).
26+
if ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND (NOT ${FUZZ_W3C_PROPAGATION}))
27+
add_compile_options(-stdlib=libc++)
28+
add_link_options(-stdlib=libc++)
2429
endif()
2530

2631
include(ProcessorCount)
@@ -74,8 +79,8 @@ if(BUILD_COVERAGE)
7479
set(COVERAGE_LIBRARIES gcov)
7580
endif()
7681

77-
add_library(dd_trace_cpp SHARED)
78-
target_sources(dd_trace_cpp PRIVATE
82+
add_library(dd_trace_cpp-objects OBJECT)
83+
target_sources(dd_trace_cpp-objects PRIVATE
7984
src/datadog/cerr_logger.cpp
8085
src/datadog/clock.cpp
8186
src/datadog/collector.cpp
@@ -132,7 +137,7 @@ target_sources(dd_trace_cpp PRIVATE
132137
)
133138

134139
# This library's public headers are just its source headers.
135-
target_sources(dd_trace_cpp PUBLIC
140+
target_sources(dd_trace_cpp-objects PUBLIC
136141
FILE_SET public_headers
137142
TYPE HEADERS
138143
BASE_DIRS src/
@@ -193,20 +198,30 @@ target_sources(dd_trace_cpp PUBLIC
193198
src/datadog/w3c_propagation.h
194199
)
195200

196-
add_dependencies(dd_trace_cpp curl)
201+
add_dependencies(dd_trace_cpp-objects curl)
197202

198-
# Make the build libcurl visible to dd_trace_cpp, and its dependents.
199-
target_include_directories(dd_trace_cpp PUBLIC ${CMAKE_BINARY_DIR}/include)
203+
# Make the build libcurl visible.
204+
include_directories(${CMAKE_BINARY_DIR}/include)
200205

201206
# Linking this library requires libcurl and threads.
202207
find_package(Threads REQUIRED)
203-
target_link_libraries(dd_trace_cpp PUBLIC ${CMAKE_BINARY_DIR}/lib/libcurl.a PUBLIC Threads::Threads ${COVERAGE_LIBRARIES})
208+
target_link_libraries(dd_trace_cpp-objects PUBLIC ${CMAKE_BINARY_DIR}/lib/libcurl.a PUBLIC Threads::Threads ${COVERAGE_LIBRARIES})
204209

205210
# When installing, install the library and its public headers.
206-
207-
install(TARGETS dd_trace_cpp
211+
install(TARGETS dd_trace_cpp-objects
208212
FILE_SET public_headers)
209213

214+
# Produce both shared and static versions of the library.
215+
add_library(dd_trace_cpp-shared SHARED $<TARGET_OBJECTS:dd_trace_cpp-objects>)
216+
set_target_properties(dd_trace_cpp-shared PROPERTIES OUTPUT_NAME "dd_trace_cpp")
217+
add_dependencies(dd_trace_cpp-shared dd_trace_cpp-objects)
218+
target_link_libraries(dd_trace_cpp-shared dd_trace_cpp-objects)
219+
220+
add_library(dd_trace_cpp-static STATIC $<TARGET_OBJECTS:dd_trace_cpp-objects>)
221+
set_target_properties(dd_trace_cpp-static PROPERTIES OUTPUT_NAME "dd_trace_cpp")
222+
add_dependencies(dd_trace_cpp-static dd_trace_cpp-objects)
223+
target_link_libraries(dd_trace_cpp-static dd_trace_cpp-objects)
224+
210225
if(BUILD_TESTING)
211226
add_subdirectory(test)
212227
endif()

example/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
add_executable(example hasher.cpp)
22

3-
target_link_libraries(example dd_trace_cpp)
3+
target_link_libraries(example dd_trace_cpp-static)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_executable(fuzz fuzz.cpp)
22

3-
target_link_libraries(fuzz dd_trace_cpp)
3+
add_dependencies(fuzz dd_trace_cpp-static)
4+
target_link_libraries(fuzz dd_trace_cpp-static)

src/datadog/parse_util.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,10 @@ bool starts_with(StringView subject, StringView prefix) {
108108
prefix.end();
109109
}
110110

111+
void to_lower(std::string& text) {
112+
std::transform(text.begin(), text.end(), text.begin(),
113+
[](unsigned char ch) { return std::tolower(ch); });
114+
}
115+
111116
} // namespace tracing
112117
} // namespace datadog

src/datadog/parse_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@ Expected<double> parse_double(StringView input);
3636
// Return whether the specified `prefix` is a prefix of the specified `subject`.
3737
bool starts_with(StringView subject, StringView prefix);
3838

39+
// Convert the specified `text` to lower case in-place.
40+
void to_lower(std::string& text);
41+
3942
} // namespace tracing
4043
} // namespace datadog

src/datadog/propagation_style.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cassert>
44

55
#include "json.hpp"
6+
#include "parse_util.h"
67

78
namespace datadog {
89
namespace tracing {
@@ -31,5 +32,24 @@ nlohmann::json to_json(const std::vector<PropagationStyle>& styles) {
3132
return styles_json;
3233
}
3334

35+
Optional<PropagationStyle> parse_propagation_style(StringView text) {
36+
auto token = std::string{text};
37+
to_lower(token);
38+
39+
// Note: Make sure that these strings are consistent (modulo case) with
40+
// `to_json`, above.
41+
if (token == "datadog") {
42+
return PropagationStyle::DATADOG;
43+
} else if (token == "b3" || token == "b3multi") {
44+
return PropagationStyle::B3;
45+
} else if (token == "tracecontext") {
46+
return PropagationStyle::W3C;
47+
} else if (token == "none") {
48+
return PropagationStyle::NONE;
49+
}
50+
51+
return nullopt;
52+
}
53+
3454
} // namespace tracing
3555
} // namespace datadog

src/datadog/propagation_style.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <vector>
99

1010
#include "json_fwd.hpp"
11+
#include "optional.h"
12+
#include "string_view.h"
1113

1214
namespace datadog {
1315
namespace tracing {
@@ -28,5 +30,7 @@ enum class PropagationStyle {
2830
nlohmann::json to_json(PropagationStyle style);
2931
nlohmann::json to_json(const std::vector<PropagationStyle>& styles);
3032

33+
Optional<PropagationStyle> parse_propagation_style(StringView text);
34+
3135
} // namespace tracing
3236
} // namespace datadog

src/datadog/span.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class Span {
7676
const Clock& clock);
7777
Span(const Span&) = delete;
7878
Span(Span&&) = default;
79-
Span& operator=(Span&&) = default;
79+
Span& operator=(Span&&) = delete;
80+
Span& operator=(const Span&) = delete;
8081

8182
// Finish this span and submit it to the associated trace segment. If
8283
// `set_end_time` has not been called on this span, then set this span's end

src/datadog/span_sampler_config.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,9 @@ Expected<FinalizedSpanSamplerConfig> finalize_config(
245245
}
246246

247247
nlohmann::json to_json(const FinalizedSpanSamplerConfig::Rule &rule) {
248-
auto result = nlohmann::json::object({
249-
{"service", rule.service},
250-
{"name", rule.name},
251-
{"resource", rule.resource},
252-
{"sample_rate", double(rule.sample_rate)},
253-
});
254-
248+
// Get the base class's fields, then add our own.
249+
auto result = static_cast<const SpanMatcher &>(rule).to_json();
250+
result["sample_rate"] = double(rule.sample_rate);
255251
if (rule.max_per_second) {
256252
result["max_per_second"] = *rule.max_per_second;
257253
}

src/datadog/trace_sampler_config.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,10 @@ Expected<FinalizedTraceSamplerConfig> finalize_config(
184184
}
185185

186186
nlohmann::json to_json(const FinalizedTraceSamplerConfig::Rule &rule) {
187-
return nlohmann::json::object({
188-
{"service", rule.service},
189-
{"name", rule.name},
190-
{"resource", rule.resource},
191-
{"sample_rate", double(rule.sample_rate)},
192-
});
187+
// Get the base class's fields, then add our own.
188+
auto result = static_cast<const SpanMatcher &>(rule).to_json();
189+
result["sample_rate"] = double(rule.sample_rate);
190+
return result;
193191
}
194192

195193
} // namespace tracing

0 commit comments

Comments
 (0)