Skip to content

[CODE HEALTH] Move SDK common and logs test helpers into anonymous namespaces#4302

Draft
thc1006 wants to merge 3 commits into
open-telemetry:mainfrom
thc1006:codehealth/sdk-test-internal-linkage-4196
Draft

[CODE HEALTH] Move SDK common and logs test helpers into anonymous namespaces#4302
thc1006 wants to merge 3 commits into
open-telemetry:mainfrom
thc1006:codehealth/sdk-test-internal-linkage-4196

Conversation

@thc1006

@thc1006 thc1006 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Part of #4196

Draft while CI runs. Ready for review from my side.

Three misc-use-internal-linkage sites in the SDK tests, the second of three batches after #4301.

file classes
sdk/test/common/global_log_handle_singleton_lifetime_test.cc GlobalLogHandlerChecker, CustomLogHandler
sdk/test/logs/logger_provider_set_test.cc TestProvider

The one that needed care

global_log_handle_singleton_lifetime_test.cc is a destruction-order test: GlobalLogHandlerChecker's destructor must run after the global log handler and aborts otherwise.

Review noted that the existing assertion could not actually catch a reversed order: handle && custom_handler_destroyed only aborts when the handler destructor has run yet a handle is still reachable, so a checker destroyed first (the case the test is for) slipped through. That is a pre-existing weakness, not something the namespace move caused, but since this PR leans on the test I tightened the predicate to !custom_handler_destroyed || handle so a reversed order now aborts. The correct order still passes.

It already had an anonymous namespace holding only ConstructChecker(), with one flagged class above it and the other below, so I extended that namespace over both instead of adding a second one.

I built and ran the test rather than reasoning about it:

[ RUN      ] GlobalLogHandleSingletonTest.Lifetime
GlobalLogHandlerChecker constructed
[       OK ] GlobalLogHandleSingletonTest.Lifetime (4 ms)
[  PASSED  ] 1 test.
Custom Gobal Log Handle destroyed
GlobalLogHandlerChecker destroyed and check pass.

The handler is destroyed first and the checker's check passes, which is the invariant the test exists for. That is what I expected, since an anonymous namespace changes linkage rather than storage duration and the function-local static is still constructed on its first call from the test body, but the test is cheap to run and reasoning about destruction order is a bad place to be confident.

logger_provider_set_test.cc is the ordinary case: a namespace around the test body.

Warning limit

151 to 148 and 161 to 158. Three on each preset, since none of these are ABI gated, unlike the TestEventLoggerProvider site in #4301.

This branch is cut from main rather than stacked on #4301, so if that one merges first this needs a rebase and the limit recomputed to 142 and 153. Happy to do that whenever it is useful; I kept them independent so they can be reviewed in either order.

Verification

  • Both classes and TestProvider end up inside an anonymous namespace, checked by walking namespace depth, and both files have balanced open and close.
  • Both files compile with -DOPENTELEMETRY_ABI_VERSION_NO=1 and =2.
  • The lifetime test builds and runs green, with the destruction order shown above.
  • clang-format 18 clean.
  • Neither sdk/test/common nor sdk/test/logs shares a TEST suite name across files, so this should not repeat the all_tests redefinition from [CODE HEALTH] Move api/test and sdk/test classes into anonymous namespace #4217.

Same honest gap as #4301: I could not re-run misc-use-internal-linkage locally, because it only reports classes from llvm-22 onward and clang-tidy 18, 20 and 21 report functions and variables only, so a local run returns zero regardless. The site list comes from the CI log artifact for main, and CI is the authority on the resulting count.

thc1006 added 2 commits July 25, 2026 02:59
…mespaces

Three misc-use-internal-linkage sites, continuing from open-telemetry#4217 and open-telemetry#4286.

global_log_handle_singleton_lifetime_test.cc had an anonymous namespace
holding only ConstructChecker(), with GlobalLogHandlerChecker above it
and CustomLogHandler below, so the existing namespace is extended over
both rather than joined by a second one.

That file is a destruction-order test whose checker aborts if the global
log handler outlives it, so I built and ran it rather than reasoning
about it. The order is unchanged: the handler is destroyed first and the
checker's check passes. An anonymous namespace changes linkage, not
storage duration, and the function-local static is still constructed on
its first call from the test body.

logger_provider_set_test.cc gets a namespace around the test body.

The warning limit drops by three on both presets, 151 to 148 and 161 to
158. None of these three are ABI gated.

Part of open-telemetry#4196

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.31%. Comparing base (d3c7a47) to head (a1699c0).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #4302   +/-   ##
=======================================
  Coverage   81.31%   81.31%           
=======================================
  Files         445      445           
  Lines       18859    18859           
=======================================
  Hits        15333    15333           
  Misses       3526     3526           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Review of open-telemetry#4302 pointed out the checker's assertion could pass for the
wrong reason. The predicate was `handle && custom_handler_destroyed`,
which only aborts when the handler destructor has already run yet a
handle is still reachable. If the checker were destroyed first, the case
the test exists to catch, custom_handler_destroyed is still false and
the whole predicate is false, so it would not abort.

It is now `!custom_handler_destroyed || handle`: abort if the checker
outruns the handler destructor, or if the handler is still reachable.
This is a pre-existing weakness rather than something the namespace move
introduced, but the PR described the test as failing loudly on a
reversed order, so the test should actually do that.

The correct order still passes: the handler is destroyed first and the
checker's check passes.

Part of open-telemetry#4196

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Jul 25, 2026
…namespaces

Nine misc-use-internal-linkage sites, the last of three batches after
open-telemetry#4301 and open-telemetry#4302.

tracer_test.cc had an anonymous namespace holding only initTracer(),
with the three mock classes above it, so that namespace is extended over
them rather than joined by a second one. measurements_benchmark.cc is
the same shape, with MockMetricExporter pulled into the existing
namespace. The other three get a namespace around the test body.

batch_span_processor_test.cc already sits inside a named namespace via
OPENTELEMETRY_BEGIN_NAMESPACE; the anonymous namespace nests inside it
and covers every class and TEST in the file. Its
BatchSpanProcessorTestPeer and the metrics file's
AlignedHistogramBucketExemplarReservoirTestPeer are the only TEST_F
suite names here, and each is unique to its file, so this does not
repeat the all_tests redefinition from open-telemetry#4217.

The warning limit drops by nine on both presets, 151 to 142 and 161 to
152. None of these are ABI gated.

This branch is cut from main. If open-telemetry#4301 or open-telemetry#4302 merge first, the limit
needs recomputing down by their counts as well.

Part of open-telemetry#4196

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant