Skip to content

Support CMake find_package for opentelemetry-cpp components #3218

@dbarker

Description

@dbarker

TLDR: feature to update the cmake.config file to support find_package(... COMPONENTS ...) and resolve dependencies by component selected.

The opentelemetry-cpp library is nicely configured into components (opentelemetry-cpp::api, sdk,otlp_grpc_exporter, etc.) and provides a clear list of dependencies. The installed opentelemetry-cpp-config.cmake file includes targets for each component but also finds dependencies globally instead of by component.

Problem:
It is not currently possible to use CMake's COMPONENTS argument to find_package to target a specific opentelemetry-cpp component without also having to add find_package statements for dependencies that were included in the installed package. If the installed package was built with gRPC and Protobuf then projects just needing the api or sdk component will also need to add find_package(gRPC) and find_package(Protobuf) to their CMake project.

This seems due to the current config.cmake files use of find_dependency based on the installed configuration independent of components.

Example:
Two simple CMake targets my_library and my_executable in separate CMake projects. The library needs only the otel-cpp api, while the executable needs the sdk and otlp_grpc_exporter.

# `my_library/CMakeLists.txt` 
project(my_library)

# only include the api target and nothing else
find_package(opentelemetry-cpp REQUIRED COMPONENTS api)

add_library(my_library foo.cpp)
target_link_libraries(my_library PRIVATE opentelemetry-cpp::api)
# `my_executable/CMakeLists.txt`
project(my_executable)

# only include the api, sdk, and otlp grpc targets and nothing else
find_package(opentelemetry-cpp REQUIRED COMPONENTS api sdk exporters_otlp_grpc)

add_executable(my_executable main.cpp)
target_link_libraries(my_executable PRIVATE 
    my_library
    opentelemetry-cpp::api 
    opentelemetry-cpp::sdk 
    opentelemetry-cpp::otlp_grpc_exporter
)

This fails currently as you'd need to add find_package(Protobuf ) and find_package(gRPC) to your my_library cmake file even though it does not target the component that brings in the dependency.

Describe the solution you'd like

Update opentelemetry-cpp-config.cmake to call find_dependency conditionally based on the components requested in find_package(... COMPONENTS ...). This ensures that only the necessary dependencies for the requested components are resolved.

Describe alternatives you've considered

There are pkg-config .pc files for a few packages in opentelemetry-cpp (api, common, resources, version, trace, logs, metrics) but not for all components. This isn't a great interface for modern cmake based projects to find packages/components.

# Find pkg-config
find_package(PkgConfig REQUIRED)

pkg_check_modules(OpenTelemetryAPI REQUIRED opentelemetry_api)
pkg_check_modules(OpenTelemetryTraceSDK REQUIRED opentelemetry_trace)

Additional Context

May be related to #3190

Metadata

Metadata

Assignees

No one assigned

    Labels

    Staletriage/acceptedIndicates an issue or PR is ready to be actively worked on.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions