Skip to content

Commit 6dc9ac6

Browse files
author
elsa
committed
feat: add tests for trace-to-profile correlation
* test/test_tracer.cpp * test/test_tracer_config.cpp
1 parent 5b5beaf commit 6dc9ac6

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

test/test_tracer.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,48 @@ TEST_CASE("128-bit trace IDs") {
15661566
REQUIRE(*high == trace_id.high);
15671567
}
15681568

1569+
#ifdef __linux__
1570+
TEST_CASE("correlate full host profiles") {
1571+
TracerConfig config;
1572+
config.service = "testsvc";
1573+
config.collector = std::make_shared<NullCollector>();
1574+
config.logger = std::make_shared<NullLogger>();
1575+
1576+
SECTION("is off by default") {
1577+
auto finalized_config = finalize_config(config);
1578+
REQUIRE(finalized_config);
1579+
Tracer tracer{*finalized_config};
1580+
REQUIRE(elastic_apm_profiling_correlation_process_storage_v1 == nullptr);
1581+
}
1582+
1583+
SECTION("is available when enabled") {
1584+
config.correlate_full_host_profiles = true;
1585+
auto finalized_config = finalize_config(config);
1586+
REQUIRE(finalized_config);
1587+
Tracer tracer{*finalized_config};
1588+
REQUIRE(elastic_apm_profiling_correlation_process_storage_v1 != nullptr);
1589+
{
1590+
auto span = tracer.create_span();
1591+
REQUIRE(elastic_apm_profiling_correlation_tls_v1 != nullptr);
1592+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->trace_present == 1);
1593+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->valid == 1);
1594+
REQUIRE(
1595+
elastic_apm_profiling_correlation_tls_v1->trace_flags ==
1596+
(span.trace_segment().sampling_decision().has_value() &&
1597+
(span.trace_segment().sampling_decision().value().priority >
1598+
0)
1599+
? 1
1600+
: 0));
1601+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->span_id != 0);
1602+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->transaction_id != 0);
1603+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->trace_id_high != 0);
1604+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->trace_id_low != 0);
1605+
}
1606+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->trace_present == 0);
1607+
}
1608+
}
1609+
#endif
1610+
15691611
TEST_CASE(
15701612
"_dd.p.tid invalid or inconsistent with trace ID results in error tag") {
15711613
struct TestCase {

test/test_tracer_config.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,85 @@ TRACER_CONFIG_TEST("configure 128-bit trace IDs") {
13161316
}
13171317
}
13181318

1319+
TRACER_CONFIG_TEST("TracerConfig::correlate_full_host_profiles") {
1320+
TracerConfig config;
1321+
config.service = "testsvc";
1322+
config.logger = std::make_shared<NullLogger>();
1323+
1324+
SECTION("default is false") {
1325+
{
1326+
auto finalized = finalize_config(config);
1327+
REQUIRE(finalized);
1328+
Tracer tracer{*finalized};
1329+
auto span = tracer.create_span();
1330+
}
1331+
REQUIRE(elastic_apm_profiling_correlation_process_storage_v1 == nullptr);
1332+
REQUIRE(elastic_apm_profiling_correlation_tls_v1 == nullptr);
1333+
}
1334+
1335+
SECTION("true enables correlation") {
1336+
{
1337+
config.correlate_full_host_profiles = true;
1338+
auto finalized = finalize_config(config);
1339+
REQUIRE(finalized);
1340+
Tracer tracer{*finalized};
1341+
auto span = tracer.create_span();
1342+
REQUIRE(elastic_apm_profiling_correlation_tls_v1 != nullptr);
1343+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->trace_present == 1);
1344+
}
1345+
REQUIRE(elastic_apm_profiling_correlation_process_storage_v1 != nullptr);
1346+
REQUIRE(elastic_apm_profiling_correlation_tls_v1 != nullptr);
1347+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->trace_present == 0);
1348+
// reset for next tests
1349+
elastic_apm_profiling_correlation_process_storage_v1 = nullptr;
1350+
elastic_apm_profiling_correlation_tls_v1 = nullptr;
1351+
}
1352+
1353+
SECTION("overridden by DD_TRACE_CORRELATE_FULL_HOST_PROFILES") {
1354+
struct TestCase {
1355+
std::string name;
1356+
std::string dd_trace_correlate_full_host_profiles;
1357+
bool original_value;
1358+
bool correlate;
1359+
};
1360+
1361+
auto test_case = GENERATE(values<TestCase>({
1362+
{"falsy override ('false')", "false", true, false},
1363+
{"falsy override ('0')", "0", true, false},
1364+
{"falsy consistent ('false')", "false", false, false},
1365+
{"falsy consistent ('0')", "0", false, false},
1366+
{"truthy override ('true')", "true", false, true},
1367+
{"truthy override ('1')", "1", false, true},
1368+
{"truthy consistent ('true')", "true", true, true},
1369+
{"truthy consistent ('1')", "1", true, true},
1370+
}));
1371+
1372+
CAPTURE(test_case.name);
1373+
const EnvGuard guard{"DD_TRACE_CORRELATE_FULL_HOST_PROFILES",
1374+
test_case.dd_trace_correlate_full_host_profiles};
1375+
config.report_traces = test_case.original_value;
1376+
{
1377+
auto finalized = finalize_config(config);
1378+
REQUIRE(finalized);
1379+
Tracer tracer{*finalized};
1380+
auto span = tracer.create_span();
1381+
if (test_case.correlate) {
1382+
REQUIRE(elastic_apm_profiling_correlation_process_storage_v1 !=
1383+
nullptr);
1384+
REQUIRE(elastic_apm_profiling_correlation_tls_v1 != nullptr);
1385+
REQUIRE(elastic_apm_profiling_correlation_tls_v1->trace_present == 1);
1386+
// reset for next tests
1387+
elastic_apm_profiling_correlation_process_storage_v1 = nullptr;
1388+
elastic_apm_profiling_correlation_tls_v1 = nullptr;
1389+
} else {
1390+
REQUIRE(elastic_apm_profiling_correlation_process_storage_v1 ==
1391+
nullptr);
1392+
REQUIRE(elastic_apm_profiling_correlation_tls_v1 == nullptr);
1393+
}
1394+
}
1395+
}
1396+
}
1397+
13191398
TRACER_CONFIG_TEST("baggage") {
13201399
TracerConfig config;
13211400

0 commit comments

Comments
 (0)