From 2ee49576284019a168a9ee5113992059501ffb49 Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sat, 5 Jul 2025 13:03:04 -0600 Subject: [PATCH 1/6] Find or fetch opentracing-cpp. Support the shared or static library targets. Test the opentracing-static target built with conan in ci --- CMakeLists.txt | 36 ++++++----------- cmake/opentracing-cpp.cmake | 65 ++++++++++++++++++++++++++++++ install/conan/conanfile_latest.txt | 2 +- opentracing-shim/CMakeLists.txt | 55 ++++++++++--------------- 4 files changed, 100 insertions(+), 58 deletions(-) create mode 100644 cmake/opentracing-cpp.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index cbbe2798e3..756a804592 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -497,6 +497,14 @@ if((NOT WITH_API_ONLY) AND USE_NLOHMANN_JSON) include("${opentelemetry-cpp_SOURCE_DIR}/cmake/nlohmann-json.cmake") endif() +# +# Do we need OpenTracing ? +# + +if(WITH_OPENTRACING) + include("${opentelemetry-cpp_SOURCE_DIR}/cmake/opentracing-cpp.cmake") +endif() + if(OTELCPP_MAINTAINER_MODE) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") message(STATUS "Building with gcc in maintainer mode.") @@ -701,6 +709,10 @@ endif() if(prometheus-cpp_FOUND) message(STATUS "prometheus-cpp: ${prometheus-cpp_VERSION}") endif() +if(WITH_OPENTRACING) + message( + STATUS "opentracing-cpp: ${OpenTracing_VERSION} (${OpenTracing_PROVIDER})") +endif() message(STATUS "---------------------------------------------") include("${opentelemetry-cpp_SOURCE_DIR}/cmake/otel-install-functions.cmake") @@ -724,30 +736,6 @@ endif() add_subdirectory(api) if(WITH_OPENTRACING) - find_package(OpenTracing CONFIG QUIET) - if(NOT OpenTracing_FOUND) - set(OPENTRACING_DIR "third_party/opentracing-cpp") - message(STATUS "Trying to use local ${OPENTRACING_DIR} from submodule") - if(EXISTS "${PROJECT_SOURCE_DIR}/${OPENTRACING_DIR}/.git") - set(SAVED_BUILD_TESTING ${BUILD_TESTING}) - set(BUILD_TESTING OFF) - set(SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE - ${CMAKE_CXX_INCLUDE_WHAT_YOU_USE}) - set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "") - add_subdirectory(${OPENTRACING_DIR}) - set(BUILD_TESTING ${SAVED_BUILD_TESTING}) - set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE - ${SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}) - else() - message( - FATAL_ERROR - "\nopentracing-cpp package was not found. Please either provide it manually or clone with submodules. " - "To initialize, fetch and checkout any nested submodules, you can use the following command:\n" - "git submodule update --init --recursive") - endif() - else() - message(STATUS "Using external opentracing-cpp") - endif() add_subdirectory(opentracing-shim) endif() diff --git a/cmake/opentracing-cpp.cmake b/cmake/opentracing-cpp.cmake new file mode 100644 index 0000000000..ea92aa71da --- /dev/null +++ b/cmake/opentracing-cpp.cmake @@ -0,0 +1,65 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +find_package(OpenTracing CONFIG QUIET) +set(OpenTracing_PROVIDER "find_package") + +if(NOT OpenTracing_FOUND) + set(_OPENTRACING_SUBMODULE_DIR "${opentelemetry-cpp_SOURCE_DIR}/third_party/opentracing-cpp") + if(EXISTS "${_OPENTRACING_SUBMODULE_DIR}/.git") + FetchContent_Declare( + "opentracing" + SOURCE_DIR "${_OPENTRACING_SUBMODULE_DIR}" + ) + set(OpenTracing_PROVIDER "fetch_source") + else() + FetchContent_Declare( + "opentracing" + GIT_REPOSITORY "https://github.com/opentracing/opentracing-cpp.git" + GIT_TAG "${opentracing-cpp_GIT_TAG}" + ) + set(OpenTracing_PROVIDER "fetch_repository") + endif() + + # OpenTracing uses the BUILD_TESTING variable directly and we must force the cached value to OFF. + # save the current state of BUILD_TESTING + if(DEFINED BUILD_TESTING) + set(_SAVED_BUILD_TESTING ${BUILD_TESTING}) + endif() + + set(BUILD_TESTING OFF CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(opentracing) + + # Restore the saved state of BUILD_TESTING + if(DEFINED _SAVED_BUILD_TESTING) + set(BUILD_TESTING ${_SAVED_BUILD_TESTING} CACHE BOOL "" FORCE) + else() + unset(BUILD_TESTING) + endif() + + # Patch the opentracing targets to set missing includes, add namespaced alias targets, disable iwyu and clang-tidy. + foreach(_target opentracing opentracing-static) + if(TARGET ${_target}) + # Add missing include directories + target_include_directories(${_target} PUBLIC + "$" + "$" + "$" + ) + # Disable CXX_INCLUDE_WHAT_YOU_USE and CXX_CLANG_TIDY + set_target_properties(${_target} + PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "" CXX_CLANG_TIDY "") + # Create alias targets + if(NOT TARGET OpenTracing::${_target}) + add_library(OpenTracing::${_target} ALIAS ${_target}) + endif() + endif() + endforeach() + + # Set the OpenTracing_VERSION variable from the git tag. + string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" OpenTracing_VERSION "${opentracing-cpp_GIT_TAG}") +endif(NOT OpenTracing_FOUND) + +if(NOT TARGET OpenTracing::opentracing AND NOT TARGET OpenTracing::opentracing-static) + message(FATAL_ERROR "No OpenTracing targets (OpenTracing::opentracing or OpenTracing::opentracing-static) were imported") +endif() diff --git a/install/conan/conanfile_latest.txt b/install/conan/conanfile_latest.txt index 6db1d7b130..bece732038 100644 --- a/install/conan/conanfile_latest.txt +++ b/install/conan/conanfile_latest.txt @@ -25,7 +25,7 @@ protobuf/*:shared=False abseil/*:fPIC=True abseil/*:shared=False opentracing-cpp/*:fPIC=True -opentracing-cpp/*:shared=True +opentracing-cpp/*:shared=False pcre2/*:with_bzip2=False [test_requires] diff --git a/opentracing-shim/CMakeLists.txt b/opentracing-shim/CMakeLists.txt index 5e3b89c5b7..0d23dcf555 100644 --- a/opentracing-shim/CMakeLists.txt +++ b/opentracing-shim/CMakeLists.txt @@ -1,37 +1,36 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -set(this_target opentelemetry_opentracing_shim) +add_library( + opentelemetry_opentracing_shim src/shim_utils.cc src/span_shim.cc + src/span_context_shim.cc src/tracer_shim.cc) -add_library(${this_target} src/shim_utils.cc src/span_shim.cc - src/span_context_shim.cc src/tracer_shim.cc) - -set_target_properties(${this_target} PROPERTIES EXPORT_NAME opentracing_shim) -set_target_version(${this_target}) +set_target_properties(opentelemetry_opentracing_shim + PROPERTIES EXPORT_NAME opentracing_shim) +set_target_version(opentelemetry_opentracing_shim) target_include_directories( - ${this_target} PUBLIC "$" - "$") + opentelemetry_opentracing_shim + PUBLIC "$" + "$") + +target_link_libraries(opentelemetry_opentracing_shim PUBLIC opentelemetry_api) -if(OPENTRACING_DIR) - target_include_directories( - ${this_target} - PUBLIC - "$" - "$" - "$" - ) - target_link_libraries(${this_target} opentelemetry_api opentracing) +# link to the shared library target (OpenTracing::opentracing) if it exists +# otherwise use the static library target. +if(TARGET OpenTracing::opentracing) + target_link_libraries(opentelemetry_opentracing_shim + PUBLIC OpenTracing::opentracing) else() - target_link_libraries(${this_target} opentelemetry_api - OpenTracing::opentracing) + target_link_libraries(opentelemetry_opentracing_shim + PUBLIC OpenTracing::opentracing-static) endif() otel_add_component( COMPONENT shims_opentracing TARGETS - ${this_target} + opentelemetry_opentracing_shim FILES_DIRECTORY "include/opentelemetry/opentracingshim" FILES_DESTINATION @@ -43,20 +42,10 @@ otel_add_component( if(BUILD_TESTING) foreach(testname propagation_test shim_utils_test span_shim_test span_context_shim_test tracer_shim_test) - add_executable(${testname} "test/${testname}.cc") - - if(OPENTRACING_DIR) - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - opentelemetry_api opentelemetry_opentracing_shim opentracing) - else() - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - opentelemetry_api opentelemetry_opentracing_shim - OpenTracing::opentracing) - endif() - + target_link_libraries( + ${testname} PRIVATE ${GTEST_BOTH_LIBRARIES} Threads::Threads + opentelemetry_opentracing_shim) gtest_add_tests( TARGET ${testname} TEST_PREFIX opentracing_shim. From ce3b5c07fd9337da7655e64ceb664565b6548d57 Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sat, 5 Jul 2025 13:04:36 -0600 Subject: [PATCH 2/6] Don't force cmake to prefer CONFIG searches. Don't include the vcpkg chainload toolchain file --- CMakeLists.txt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 756a804592..05aa3032d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,11 +36,6 @@ project(opentelemetry-cpp) # Mark variables as used so cmake doesn't complain about them mark_as_advanced(CMAKE_TOOLCHAIN_FILE) -# Note: CMAKE_FIND_PACKAGE_PREFER_CONFIG requires cmake 3.15. Prefer cmake -# CONFIG search mode to find dependencies. This is important to properly find -# protobuf versions 3.22.0 and above due to the abseil-cpp dependency. -set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE) - # Don't use customized cmake modules if vcpkg is used to resolve dependence. if(NOT DEFINED CMAKE_TOOLCHAIN_FILE) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules/") @@ -79,10 +74,6 @@ if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) CACHE STRING "") endif() -if(VCPKG_CHAINLOAD_TOOLCHAIN_FILE) - include("${VCPKG_CHAINLOAD_TOOLCHAIN_FILE}") -endif() - option(WITH_ABI_VERSION_1 "ABI version 1" ON) option(WITH_ABI_VERSION_2 "EXPERIMENTAL: ABI version 2 preview" OFF) From 23ffbfd1c9654b8b11549e610105633a8b729a3c Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sat, 5 Jul 2025 13:14:42 -0600 Subject: [PATCH 3/6] unset BUILD_TESTING from the cache if it was not set before --- cmake/opentracing-cpp.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/opentracing-cpp.cmake b/cmake/opentracing-cpp.cmake index ea92aa71da..bf9632b032 100644 --- a/cmake/opentracing-cpp.cmake +++ b/cmake/opentracing-cpp.cmake @@ -34,7 +34,7 @@ if(NOT OpenTracing_FOUND) if(DEFINED _SAVED_BUILD_TESTING) set(BUILD_TESTING ${_SAVED_BUILD_TESTING} CACHE BOOL "" FORCE) else() - unset(BUILD_TESTING) + unset(BUILD_TESTING CACHE) endif() # Patch the opentracing targets to set missing includes, add namespaced alias targets, disable iwyu and clang-tidy. From 396f1b566a5add34ace0c3e368eadb75eb27dcee Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sat, 5 Jul 2025 14:17:40 -0600 Subject: [PATCH 4/6] Test builting the opentracing-shim against the static opentracing library through conan. --- .github/workflows/cmake_install.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cmake_install.yml b/.github/workflows/cmake_install.yml index 6c56632763..5173744b37 100644 --- a/.github/workflows/cmake_install.yml +++ b/.github/workflows/cmake_install.yml @@ -215,7 +215,7 @@ jobs: run: ./ci/do_ci.sh cmake.opentracing_shim.install.test ubuntu_2404_conan_latest: - name: Ubuntu 24.04 conan latest versions cxx17 (static libs) + name: Ubuntu 24.04 conan latest versions cxx17 (static libs - opentracing shim) runs-on: ubuntu-24.04 env: INSTALL_TEST_DIR: '/home/runner/install_test' @@ -248,6 +248,8 @@ jobs: run: | export PKG_CONFIG_PATH=$INSTALL_TEST_DIR/lib/pkgconfig:$PKG_CONFIG_PATH ./ci/verify_packages.sh + - name: Run OpenTracing Shim Test + run: ./ci/do_ci.sh cmake.opentracing_shim.install.test macos_14_conan_stable: name: macOS 14 conan stable versions cxx17 (static libs) From e2ea98676d981068a3450bf6d6cb1a98ef886bd2 Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sat, 5 Jul 2025 14:18:07 -0600 Subject: [PATCH 5/6] Fix iwyu failure by disabling the unneeded MockTracer build from OpenTracing --- cmake/opentracing-cpp.cmake | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmake/opentracing-cpp.cmake b/cmake/opentracing-cpp.cmake index bf9632b032..f014ecd0cd 100644 --- a/cmake/opentracing-cpp.cmake +++ b/cmake/opentracing-cpp.cmake @@ -27,7 +27,10 @@ if(NOT OpenTracing_FOUND) set(_SAVED_BUILD_TESTING ${BUILD_TESTING}) endif() + # Set the cache variables for the opentracing build set(BUILD_TESTING OFF CACHE BOOL "" FORCE) + set(BUILD_MOCKTRACER OFF CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(opentracing) # Restore the saved state of BUILD_TESTING From ac57c9e3f79043ab35a375b20f6feacd6d01012c Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Sat, 5 Jul 2025 15:23:40 -0600 Subject: [PATCH 6/6] update install test for opentracing-shim to also check for the opentracing-static target --- .../component_tests/shims_opentracing/CMakeLists.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/install/test/cmake/component_tests/shims_opentracing/CMakeLists.txt b/install/test/cmake/component_tests/shims_opentracing/CMakeLists.txt index f541f15dd2..2b45e2528e 100644 --- a/install/test/cmake/component_tests/shims_opentracing/CMakeLists.txt +++ b/install/test/cmake/component_tests/shims_opentracing/CMakeLists.txt @@ -6,8 +6,12 @@ project(opentelemetry-cpp-shims_opentracing-install-test LANGUAGES CXX) find_package(opentelemetry-cpp REQUIRED COMPONENTS shims_opentracing) -if(NOT TARGET OpenTracing::opentracing) - message(FATAL_ERROR "OpenTracing::opentracing target not found") +if(NOT TARGET OpenTracing::opentracing AND NOT TARGET + OpenTracing::opentracing-static) + message( + FATAL_ERROR + "A required OpenTracing target (OpenTracing::opentracing or OpenTracing::opentracing-static) was not imported" + ) endif() find_package(GTest CONFIG REQUIRED)