From ae4c6d1e0c5603387a46c8938d876726eac25780 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Sat, 25 Jul 2026 03:58:12 +0800 Subject: [PATCH 1/2] [CODE HEALTH] Move SDK trace and metrics test helpers into anonymous namespaces Nine misc-use-internal-linkage sites, the last of three batches after #4301 and #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 #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 #4301 or #4302 merge first, the limit needs recomputing down by their counts as well. Part of #4196 Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- .github/workflows/clang-tidy.yaml | 4 ++-- .../aligned_histogram_bucket_exemplar_reservoir_test.cc | 5 +++++ sdk/test/metrics/measurements_benchmark.cc | 6 +++--- sdk/test/trace/batch_span_processor_test.cc | 5 +++++ sdk/test/trace/tracer_provider_set_test.cc | 5 +++++ sdk/test/trace/tracer_test.cc | 5 +++-- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.github/workflows/clang-tidy.yaml b/.github/workflows/clang-tidy.yaml index 48a3cc8d60..3d296602b8 100644 --- a/.github/workflows/clang-tidy.yaml +++ b/.github/workflows/clang-tidy.yaml @@ -17,9 +17,9 @@ jobs: matrix: include: - cmake_options: all-options-abiv1-preview - warning_limit: 151 + warning_limit: 142 - cmake_options: all-options-abiv2-preview - warning_limit: 161 + warning_limit: 152 env: CC: /usr/bin/clang-22 CXX: /usr/bin/clang++-22 diff --git a/sdk/test/metrics/exemplar/aligned_histogram_bucket_exemplar_reservoir_test.cc b/sdk/test/metrics/exemplar/aligned_histogram_bucket_exemplar_reservoir_test.cc index ee1eb72cce..d6effcef3d 100644 --- a/sdk/test/metrics/exemplar/aligned_histogram_bucket_exemplar_reservoir_test.cc +++ b/sdk/test/metrics/exemplar/aligned_histogram_bucket_exemplar_reservoir_test.cc @@ -23,6 +23,9 @@ namespace sdk { namespace metrics { +namespace +{ + class AlignedHistogramBucketExemplarReservoirTestPeer : public ::testing::Test { public: @@ -59,6 +62,8 @@ TEST_F(AlignedHistogramBucketExemplarReservoirTestPeer, OfferMeasurementWithNonE ASSERT_TRUE(!exemplar_data.empty()); } +} // namespace + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/metrics/measurements_benchmark.cc b/sdk/test/metrics/measurements_benchmark.cc index 75a5c34d14..0dee405448 100644 --- a/sdk/test/metrics/measurements_benchmark.cc +++ b/sdk/test/metrics/measurements_benchmark.cc @@ -35,6 +35,9 @@ using namespace opentelemetry::sdk::instrumentationscope; using namespace opentelemetry::sdk::metrics; using namespace opentelemetry::sdk::common; +namespace +{ + class MockMetricExporter : public MetricReader { public: @@ -54,9 +57,6 @@ class MockMetricExporter : public MetricReader void OnInitialized() noexcept override {} }; -namespace -{ - size_t GetBenchmarkThreads() { const char *env = std::getenv("BENCHMARK_THREADS"); diff --git a/sdk/test/trace/batch_span_processor_test.cc b/sdk/test/trace/batch_span_processor_test.cc index 49c8d8b4e2..ba4551b03f 100644 --- a/sdk/test/trace/batch_span_processor_test.cc +++ b/sdk/test/trace/batch_span_processor_test.cc @@ -32,6 +32,9 @@ using opentelemetry::sdk::common::unsetenv; OPENTELEMETRY_BEGIN_NAMESPACE +namespace +{ + /** * Returns a mock span exporter meant exclusively for testing only */ @@ -499,4 +502,6 @@ TEST(BatchSpanProcessorOptionsEnvTest, TestDesignatedInitializers) } #endif +} // namespace + OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/trace/tracer_provider_set_test.cc b/sdk/test/trace/tracer_provider_set_test.cc index 3991582264..d6d6828d26 100644 --- a/sdk/test/trace/tracer_provider_set_test.cc +++ b/sdk/test/trace/tracer_provider_set_test.cc @@ -24,6 +24,9 @@ namespace nostd = opentelemetry::nostd; namespace trace_api = opentelemetry::trace; namespace trace_sdk = opentelemetry::sdk::trace; +namespace +{ + class TestProvider : public TracerProvider { public: @@ -80,3 +83,5 @@ TEST(Provider, SetTracerProviderDisabled) unsetenv("OTEL_SDK_DISABLED"); } #endif + +} // namespace diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index d3107db47a..86114c44a2 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -67,6 +67,9 @@ using opentelemetry::exporter::memory::InMemorySpanData; using opentelemetry::exporter::memory::InMemorySpanExporter; using opentelemetry::trace::SpanContext; +namespace +{ + /** * A mock sampler with ShouldSample returning: * Decision::RECORD_AND_SAMPLE if trace_id is valid @@ -171,8 +174,6 @@ class MockIdGenerator : public IdGenerator uint8_t buf_trace[16] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}; }; -namespace -{ std::shared_ptr initTracer(std::unique_ptr &&exporter) { auto processor = std::unique_ptr(new SimpleSpanProcessor(std::move(exporter))); From 4a57b6e0349ecfe3e8717f0e3d5de227596dc47c Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Sat, 25 Jul 2026 03:59:37 +0800 Subject: [PATCH 2/2] Add CHANGELOG entry for #4303 Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55405b8d55..8baba4503c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ Increment the: ## [Unreleased] +* [CODE HEALTH] Move SDK trace and metrics test helpers into anonymous + namespaces + [#4303](https://github.com/open-telemetry/opentelemetry-cpp/pull/4303) + * [CODE HEALTH] Move metrics storage test fixtures into anonymous namespace [#4286](https://github.com/open-telemetry/opentelemetry-cpp/pull/4286)