Skip to content

fix(testing_util): link opentelemetry-cpp SDK as PUBLIC#16101

Open
GaetanLepage wants to merge 1 commit into
googleapis:mainfrom
GaetanLepage:fix-testing-util-otel-public
Open

fix(testing_util): link opentelemetry-cpp SDK as PUBLIC#16101
GaetanLepage wants to merge 1 commit into
googleapis:mainfrom
GaetanLepage:fix-testing-util-otel-public

Conversation

@GaetanLepage
Copy link
Copy Markdown

Problem

google/cloud/testing_util/opentelemetry_matchers.h is a public header of the google_cloud_cpp_testing target, and it includes and uses opentelemetry-cpp SDK symbols inline (e.g. opentelemetry::sdk::trace::SpanData::GetAttributes()).

Any test that links google_cloud_cpp_testing and includes that header (e.g. google/cloud/internal/opentelemetry_test.cc) therefore references those SDK symbols directly from its own translation unit.

The CMake build, however, links the SDK targets as PRIVATE:

target_link_libraries(
    google_cloud_cpp_testing
    PUBLIC absl::symbolize absl::failure_signal_handler google-cloud-cpp::common
           GTest::gmock
    PRIVATE opentelemetry-cpp::in_memory_span_exporter opentelemetry-cpp::trace)

So they are not propagated to the test executables. When building with shared libraries (BUILD_SHARED_LIBS=ON), linking the test fails:

undefined reference to symbol
  '...opentelemetry::v1::sdk::trace::SpanData::GetAttributes[abi:cxx11]() const'
libopentelemetry_trace.so: error adding symbols: DSO missing from command line

because libopentelemetry_trace.so only arrives as an indirect DT_NEEDED of libgoogle_cloud_cpp_testing.so, which modern binutils refuses to resolve symbols from.

Fix

Move opentelemetry-cpp::in_memory_span_exporter and opentelemetry-cpp::trace to the PUBLIC link interface, since they are part of the public (header) interface of google_cloud_cpp_testing.

Consistency with Bazel

The Bazel build already exposes these dependencies transitively: :google_cloud_cpp_testing depends on :google_cloud_cpp_testing_private, which lists @opentelemetry-cpp//exporters/memory:in_memory_span_exporter and @opentelemetry-cpp//sdk/src/trace in deps. This change aligns the CMake build with the existing Bazel behavior.

Discovered while packaging google-cloud-cpp for nixpkgs (shared-library build with tests enabled).

@GaetanLepage GaetanLepage requested a review from a team as a code owner May 17, 2026 17:09
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the google_cloud_cpp_testing library in google/cloud/testing_util/CMakeLists.txt by moving the opentelemetry-cpp dependencies from PRIVATE to PUBLIC visibility. The reviewer suggested formatting the target_link_libraries list to have one dependency per line to improve readability and simplify future version control diffs.

Comment on lines +61 to +62
GTest::gmock opentelemetry-cpp::in_memory_span_exporter
opentelemetry-cpp::trace)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and to minimize future diffs, consider listing each dependency on its own line in target_link_libraries. This is a common practice in this repository for maintaining long lists of dependencies.

           GTest::gmock
           opentelemetry-cpp::in_memory_span_exporter
           opentelemetry-cpp::trace

The `opentelemetry_matchers.h` public header of `google_cloud_cpp_testing`
includes and uses opentelemetry-cpp SDK symbols inline (e.g.
`sdk::trace::SpanData::GetAttributes()`). Tests that link
`google_cloud_cpp_testing` therefore reference these SDK symbols directly
from their own translation units.

However, the CMake build links `opentelemetry-cpp::in_memory_span_exporter`
and `opentelemetry-cpp::trace` as PRIVATE, so they are not propagated to
the test executables. When building with shared libraries this fails to
link, e.g.:

    undefined reference to symbol
      '...sdk::trace::SpanData::GetAttributes[abi:cxx11]() const'
    libopentelemetry_trace.so: error adding symbols:
      DSO missing from command line

The Bazel build already exposes these dependencies transitively (via
`google_cloud_cpp_testing_private`), so making the CMake link PUBLIC also
aligns the two build systems.
@GaetanLepage GaetanLepage force-pushed the fix-testing-util-otel-public branch from 9fe0333 to 4912061 Compare May 17, 2026 17:23
@GaetanLepage
Copy link
Copy Markdown
Author

Thanks. I kept the dependencies packed rather than one-per-line because that's what this repository's .cmake-format.py config produces (and cmake-format is enforced in CI). The pre-existing PUBLIC absl::symbolize absl::failure_signal_handler google-cloud-cpp::common ... line was already packed for the same reason. I ran cmake-format -c .cmake-format.py so the block now matches the canonical formatting; switching to one dependency per line would fail the format check.

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