Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/datadog/otel_process_ctx_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ struct StaticOtelCtxState {
};

StaticOtelCtxState& get_otel_ctx_state() {
static StaticOtelCtxState state;
// The StaticOtelCtxState singleton is, on purpose, never destroyed, because
// the Tracer's destructor needs it.
static StaticOtelCtxState& state = *new StaticOtelCtxState;
return state;
}

Expand Down
24 changes: 17 additions & 7 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ target_include_directories(tests
${CMAKE_SOURCE_DIR}/include/datadog
)

# Enable Catch2 std stringification
# Enable Catch2 std stringification
# <https://github.com/catchorg/Catch2/blob/devel/docs/configuration.md#enabling-stringification>
target_compile_definitions(tests
PUBLIC
Expand All @@ -73,12 +73,22 @@ target_link_libraries(tests

if(DD_TRACE_TRANSPORT STREQUAL "curl")
target_sources(tests PRIVATE test_curl.cpp)
target_link_libraries(tests
PRIVATE
# TODO: Remove dependency on libcurl
CURL::libcurl_static
)
target_link_libraries(tests PRIVATE CURL::libcurl_static)
Comment thread
xlamorlette-datadog marked this conversation as resolved.
Resolved
endif()

catch_discover_tests(tests)
# Dedicated executable run by a test in test_otel_process_ctx.cpp
add_executable(exit_with_live_tracer exit_with_live_tracer.cpp)
target_link_libraries(exit_with_live_tracer
PRIVATE
dd-trace-cpp::static
dd-trace-cpp::specs
)

add_dependencies(tests exit_with_live_tracer)

target_compile_definitions(tests
PRIVATE
EXIT_WITH_LIVE_TRACER_EXECUTABLE="$<TARGET_FILE:exit_with_live_tracer>"
)

catch_discover_tests(tests)
37 changes: 37 additions & 0 deletions test/exit_with_live_tracer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Executable that calls exit() while a Tracer, held by a static wrapper, is
// still alive.

#include <datadog/collector.h>
#include <datadog/tracer.h>
Comment thread
xlamorlette-datadog marked this conversation as resolved.
Resolved

namespace dd = datadog::tracing;

namespace {

class NullCollector : public dd::Collector {
public:
dd::Expected<void> send(std::vector<std::unique_ptr<dd::SpanData>>&&,
const std::shared_ptr<dd::TraceSampler>&) override {
return dd::nullopt;
}

std::string config() const override { return R"({"type": "NullCollector"})"; }
};

// Created empty before main(), so before the library statics.
std::optional<dd::Tracer> tracer;

} // namespace

int main() {
dd::TracerConfig config;
config.collector = std::make_shared<NullCollector>();
config.telemetry.enabled = false;
Comment thread
xlamorlette-datadog marked this conversation as resolved.
Resolved
const dd::Expected<dd::FinalizedTracerConfig> finalized_config =
dd::finalize_config(config);
if (!finalized_config) {
return EXIT_FAILURE;
}
tracer.emplace(*finalized_config);
std::exit(EXIT_SUCCESS);
}
14 changes: 14 additions & 0 deletions test/test_otel_process_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,17 @@ OTEL_CTX_TEST(
}
#endif
}

OTEL_CTX_TEST("exit() with a live Tracer in a static wrapper exits cleanly") {
#ifndef __linux__
SUCCEED("OpenTelemetry process context is Linux-only");
#else
// Run a dedicated process and check that it exits cleanly. It must be
// independent of other tests, to control the order of statics creation.
const std::string command =
std::string("'") + EXIT_WITH_LIVE_TRACER_EXECUTABLE + "'";
const int status = std::system(command.c_str());
Comment thread
xlamorlette-datadog marked this conversation as resolved.
Resolved
REQUIRE(WIFEXITED(status));
REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);
#endif
}
Loading