Skip to content

POC: Possible implementation of opentelemetry-cpp-contrib cmake project with components #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.14)

project(opentelemetry-cpp-contrib VERSION 0.1.0 LANGUAGES CXX)

# Define options for each component in opentelemetry-cpp-contrib
option(WITH_COMPONENT_GENEVA "Build with Geneva exporters" ON)
option(WITH_COMPONENT_USER_EVENTS "Build with User Events exporters" ON)

# Other options for opentelemetry-cpp-contrib
option(OPENTELEMETRY_CONTRIB_INSTALL "Install the opentelemetry-cpp-contrib package" ON)
option(WITH_EXAMPLES "Build example" ON)
option(WITH_BENCHMARK "Build with benchmark" ON)
option(BUILD_TESTING "Build tests" ON)
option(BUILD_TRACEPOINTS "Build tracepoints library" ON)
option(OTELCPP_VERSIONED_LIBS "Whether to generate the versioned shared libs" OFF)

# Set the opentelemetry-cpp provider to use.
# - "fetch" will use the FetchContent module to download the opentelemetry-cpp repo and build in the same build tree
# - "package" will find_package to import the installed opentelemetry-cpp package
set(opentelemetry-cpp_PROVIDER "fetch" CACHE STRING "Provider for opentelemetry-cpp")
set_property(CACHE opentelemetry-cpp_PROVIDER PROPERTY STRINGS "package" "fetch")

include(FetchContent)

if(BUILD_TESTING)
include(CTest)
include(GoogleTest)
# Find or Fetch googletest
include(${PROJECT_SOURCE_DIR}/cmake/googletest.cmake)
enable_testing()
endif()

if(WITH_BENCHMARK)
# Find or Fetch benchmark
include(${PROJECT_SOURCE_DIR}/cmake/benchmark.cmake)
endif()

# Make Threads available
find_package(Threads REQUIRED)

# Find or Fetch opentelemetry-cpp
include(${PROJECT_SOURCE_DIR}/cmake/opentelemetry-cpp.cmake)

# Include the opentelemetry-cpp cmake functions to support adding and installing components
include(${opentelemetry-cpp_SOURCE_DIR}/cmake/otel-install-functions.cmake)

# Add the contrib exporters components to the build tree
add_subdirectory(exporters)

# install the opentelemetry-cpp-contrib package
if(OPENTELEMETRY_CONTRIB_INSTALL)
otel_install_cmake_config()
otel_install_components()
otel_install_thirdparty_definitions()
endif()
20 changes: 20 additions & 0 deletions cmake/benchmark.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
find_package(benchmark CONFIG QUIET)
if(NOT benchmark_FOUND)
message(STATUS "benchmark not found, fetching...")
FetchContent_Declare(
benchmark
GIT_REPOSITORY
https://github.com/google/benchmark.git
GIT_TAG
299e5928955cc62af9968370293b916f5130916f #v1.9.3
)
FetchContent_MakeAvailable(benchmark)
endif()

if(NOT benchmark_VERSION)
message(FATAL_ERROR "benchmark version not found")
endif()

if(NOT TARGET benchmark::benchmark)
message(FATAL_ERROR "benchmark not found")
endif()
24 changes: 24 additions & 0 deletions cmake/googletest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
find_package(GTest CONFIG QUIET)
if(NOT GTest_FOUND)
message(STATUS "GTest not found, fetching...")
FetchContent_Declare(
googletest
GIT_REPOSITORY
https://github.com/google/googletest.git
GIT_TAG
52eb8108c5bdec04579160ae17225d66034bd723 # v1.17.0
)
FetchContent_MakeAvailable(googletest)
endif()

if(NOT GTest_VERSION)
message(FATAL_ERROR "GTest version not found")
endif()

if(NOT TARGET GTest::gtest)
message(FATAL_ERROR "GTest::gtest not found")
endif()

if(NOT TARGET GTest::gtest_main)
message(FATAL_ERROR "GTest::gtest_main not found")
endif()
38 changes: 38 additions & 0 deletions cmake/opentelemetry-cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Fetch opentelemetry-cpp:
# This is required for the cmake install functions.
FetchContent_Declare(
opentelemetry-cpp
GIT_REPOSITORY
https://github.com/dbarker/opentelemetry-cpp.git
GIT_TAG
poc_otel_cmake_external_repo_support
)

# Alternatively fetch from an opentelemetry-cpp local src directory
# FetchContent_Declare(
# opentelemetry-cpp
# SOURCE_DIR "/workspaces/opentelemetry-cpp"
# )

if(opentelemetry-cpp_PROVIDER STREQUAL "package")
find_package(opentelemetry-cpp CONFIG REQUIRED)
# silence CMP0169 deprecation of one-arg Populate()
cmake_policy(SET CMP0169 OLD)
# Populate the opentelemetry-cpp src to enable using the cmake install functions
FetchContent_Populate(opentelemetry-cpp)
elseif(opentelemetry-cpp_PROVIDER STREQUAL "fetch")
set(OPENTELEMETRY_INSTALL ${OPENTELEMETRY_CONTRIB_INSTALL})
# If user events is enabled, set the otlp file to ON to make the otlp_recordable target available
if(WITH_COMPONENT_USER_EVENTS)
set(WITH_OTLP_FILE ON)
endif()
# Add opetelemetry-cpp src to the build tree
FetchContent_MakeAvailable(opentelemetry-cpp)
get_target_property(opentelemetry-cpp_VERSION opentelemetry-cpp::api INTERFACE_OPENTELEMETRY_VERSION)
get_target_property(OPENTELEMETRY_ABI_VERSION_NO opentelemetry-cpp::api INTERFACE_OPENTELEMETRY_ABI_VERSION_NO)
endif()

message(STATUS "opentelemetry-cpp PROVIDER: ${opentelemetry-cpp_PROVIDER}")
message(STATUS "opentelemetry-cpp SOURCE_DIR: ${opentelemetry-cpp_SOURCE_DIR}")
message(STATUS "opentelemetry-cpp VERSION: ${opentelemetry-cpp_VERSION}")
message(STATUS "opentelemetry-cpp ABI_VERSION_NO: ${OPENTELEMETRY_ABI_VERSION_NO}")
48 changes: 48 additions & 0 deletions cmake/templates/opentelemetry-cpp-contrib-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

#.rst:
# opentelemetry-cpp-contrib-config.cmake
# --------
# Finding opentelemetry-cpp-contrib in CMake projects
# ========================================
#
# - find_package(opentelemetry-cpp-contrib CONFIG REQUIRED) to import all installed targets and dependencies
# - find_package(opentelemetry-cpp-contrib CONFIG COMPONENTS ...) to import specific components' targets and dependencies
#

@PACKAGE_INIT@

# Include the opentelemetry-cpp cmake functions file that includes component defintions, thirdparty definitons and functions to support finding this package and dependencies.
include("${CMAKE_CURRENT_LIST_DIR}/find-package-support-functions.cmake")

set(_INSTALLED_COMPONENTS "")
get_installed_components(_INSTALLED_COMPONENTS)

set(OPENTELEMETRY_CPP_CONTRIB_COMPONENTS_INSTALLED ${_INSTALLED_COMPONENTS} CACHE STRING "opentelemetry-cpp-contrib components installed" FORCE)

set(_REQUESTED_COMPONENTS "")
get_requested_components(_INSTALLED_COMPONENTS _REQUESTED_COMPONENTS)

find_required_dependencies(_REQUESTED_COMPONENTS)

# include the target files selected and set the component found flag
foreach(_COMPONENT IN LISTS _REQUESTED_COMPONENTS)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-${_COMPONENT}-target.cmake")
set(${CMAKE_FIND_PACKAGE_NAME}_${_COMPONENT}_FOUND TRUE
CACHE BOOL "whether ${CMAKE_FIND_PACKAGE_NAME} component ${_COMPONENT} is found" FORCE)
endforeach()

# get installed and requested targets
set(_OPENTELEMETRY_CPP_CONTRIB_TARGETS "")
get_targets(_REQUESTED_COMPONENTS _OPENTELEMETRY_CPP_CONTRIB_TARGETS)
check_targets_imported(_OPENTELEMETRY_CPP_CONTRIB_TARGETS)

# handle the QUIETLY and REQUIRED arguments and set opentelemetry-cpp-contrib_FOUND to
# TRUE
include("FindPackageHandleStandardArgs")
find_package_handle_standard_args(
${CMAKE_FIND_PACKAGE_NAME}
FOUND_VAR ${CMAKE_FIND_PACKAGE_NAME}_FOUND)

check_required_components(${CMAKE_FIND_PACKAGE_NAME})
26 changes: 26 additions & 0 deletions cmake/thirdparty-dependency-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

#-----------------------------------------------------------------------
# Third party dependencies supported by opentelemetry-cpp-contrib
# Dependencies that must be found with find_dependency() when a user calls find_package(opentelemetry-cpp-contrib ...)
# should be included in this list.
#-----------------------------------------------------------------------
set(OTEL_THIRDPARTY_DEPENDENCIES_SUPPORTED
opentelemetry-cpp
tracepoint
)

#-----------------------------------------------------------------------
# Third party dependency target namespaces. Defaults to the dependency's project name if not set.
# Only set if the target namespace is different from the project name (these are case sensitive).
# set(OTEL_<dependency>_TARGET_NAMESPACE "<namespace>")
#-----------------------------------------------------------------------
# set(OTEL_Protobuf_TARGET_NAMESPACE "protobuf")

#-----------------------------------------------------------------------
# Set the find_dependecy search mode - empty is default. Options: cmake default (empty string ""), "MODULE", or "CONFIG"
# # set(OTEL_<dependency>_SEARCH_MODE "<search mode>")
#-----------------------------------------------------------------------
set(OTEL_opentelemetry-cpp_SEARCH_MODE "CONFIG")
set(OTEL_tracepoint_SEARCH_MODE "CONFIG")
12 changes: 12 additions & 0 deletions exporters/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

if(WITH_COMPONENT_GENEVA)
message(STATUS "Building with Geneva exporters")
add_subdirectory(geneva)
endif()

if(WITH_COMPONENT_USER_EVENTS)
message(STATUS "Building with User Events exporters")
add_subdirectory(user_events)
endif()
Loading
Loading