Skip to content

Commit a817fa6

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

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

Diff for: test/test_tracer.cpp

+42
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 {

Diff for: test/test_tracer_config.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,87 @@ TRACER_CONFIG_TEST("configure 128-bit trace IDs") {
13161316
}
13171317
}
13181318

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

0 commit comments

Comments
 (0)