Skip to content

Commit 1ad6674

Browse files
authored
[CMAKE] Add CMake script to find or fetch prometheus-cpp (#3522)
1 parent a1e6011 commit 1ad6674

File tree

3 files changed

+68
-65
lines changed

3 files changed

+68
-65
lines changed

CMakeLists.txt

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -301,49 +301,7 @@ endif()
301301
include(GNUInstallDirs)
302302

303303
if(WITH_PROMETHEUS)
304-
find_package(prometheus-cpp CONFIG QUIET)
305-
if(NOT prometheus-cpp_FOUND)
306-
message(STATUS "Trying to use local prometheus-cpp from submodule")
307-
if(EXISTS ${PROJECT_SOURCE_DIR}/third_party/prometheus-cpp/.git)
308-
set(SAVED_ENABLE_TESTING ${ENABLE_TESTING})
309-
set(SAVED_CMAKE_CXX_CLANG_TIDY ${CMAKE_CXX_CLANG_TIDY})
310-
set(SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE
311-
${CMAKE_CXX_INCLUDE_WHAT_YOU_USE})
312-
set(ENABLE_TESTING OFF)
313-
set(CMAKE_CXX_CLANG_TIDY "")
314-
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "")
315-
add_subdirectory(third_party/prometheus-cpp)
316-
set(ENABLE_TESTING ${SAVED_ENABLE_TESTING})
317-
set(CMAKE_CXX_CLANG_TIDY ${SAVED_CMAKE_CXX_CLANG_TIDY})
318-
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
319-
${SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE})
320-
321-
# Get the version of the prometheus-cpp submodule
322-
find_package(Git QUIET)
323-
if(Git_FOUND)
324-
execute_process(
325-
COMMAND ${GIT_EXECUTABLE} describe --tags --always
326-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/third_party/prometheus-cpp
327-
OUTPUT_VARIABLE prometheus-cpp_VERSION
328-
OUTPUT_STRIP_TRAILING_WHITESPACE)
329-
string(REGEX REPLACE "^v" "" prometheus-cpp_VERSION
330-
"${prometheus-cpp_VERSION}")
331-
endif()
332-
333-
message(
334-
STATUS
335-
"Using local prometheus-cpp from submodule. Version = ${prometheus-cpp_VERSION}"
336-
)
337-
else()
338-
message(
339-
FATAL_ERROR
340-
"\nprometheus-cpp package was not found. Please either provide it manually or clone with submodules. "
341-
"To initialize, fetch and checkout any nested submodules, you can use the following command:\n"
342-
"git submodule update --init --recursive")
343-
endif()
344-
else()
345-
message(STATUS "Using external prometheus-cpp")
346-
endif()
304+
include("${opentelemetry-cpp_SOURCE_DIR}/cmake/prometheus-cpp.cmake")
347305
endif()
348306

349307
if(WITH_OTLP_GRPC
@@ -699,8 +657,10 @@ if(USE_NLOHMANN_JSON)
699657
STATUS "nlohmann-json: ${nlohmann_json_VERSION} (${nlohmann_json_PROVIDER})"
700658
)
701659
endif()
702-
if(prometheus-cpp_FOUND)
703-
message(STATUS "prometheus-cpp: ${prometheus-cpp_VERSION}")
660+
if(WITH_PROMETHEUS)
661+
message(
662+
STATUS
663+
"prometheus-cpp: ${prometheus-cpp_VERSION} (${prometheus-cpp_PROVIDER})")
704664
endif()
705665
if(WITH_OPENTRACING)
706666
message(

cmake/prometheus-cpp.cmake

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Import prometheus-cpp targets (prometheus-cpp::core and prometheus-cpp::pull)
5+
# 1. Find an installed prometheus-cpp package
6+
# 2. Use FetchContent to build prometheus-cpp from a git submodule
7+
# 3. Use FetchContent to fetch and build prometheus-cpp from GitHub
8+
9+
find_package(prometheus-cpp CONFIG QUIET)
10+
set(prometheus-cpp_PROVIDER "find_package")
11+
12+
if(NOT prometheus-cpp_FOUND)
13+
set(_PROMETHEUS_SUBMODULE_DIR "${opentelemetry-cpp_SOURCE_DIR}/third_party/prometheus-cpp")
14+
if(EXISTS "${_PROMETHEUS_SUBMODULE_DIR}/.git")
15+
FetchContent_Declare(
16+
"prometheus-cpp"
17+
SOURCE_DIR "${_PROMETHEUS_SUBMODULE_DIR}"
18+
)
19+
set(prometheus-cpp_PROVIDER "fetch_source")
20+
else()
21+
FetchContent_Declare(
22+
"prometheus-cpp"
23+
GIT_REPOSITORY "https://github.com/jupp0r/prometheus-cpp.git"
24+
GIT_TAG "${prometheus-cpp_GIT_TAG}"
25+
GIT_SUBMODULES "3rdparty/civetweb"
26+
)
27+
set(prometheus-cpp_PROVIDER "fetch_repository")
28+
endif()
29+
30+
if(DEFINED ENABLE_TESTING)
31+
set(_SAVED_ENABLE_TESTING ${ENABLE_TESTING})
32+
endif()
33+
34+
set(ENABLE_TESTING OFF CACHE BOOL "" FORCE)
35+
set(ENABLE_PUSH OFF CACHE BOOL "" FORCE)
36+
set(USE_THIRDPARTY_LIBRARIES ON CACHE BOOL "" FORCE)
37+
38+
FetchContent_MakeAvailable(prometheus-cpp)
39+
40+
if(DEFINED _SAVED_ENABLE_TESTING)
41+
set(ENABLE_TESTING ${_SAVED_ENABLE_TESTING} CACHE BOOL "" FORCE)
42+
else()
43+
unset(ENABLE_TESTING CACHE)
44+
endif()
45+
46+
# Set the prometheus-cpp_VERSION variable from the git tag.
47+
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" prometheus-cpp_VERSION "${prometheus-cpp_GIT_TAG}")
48+
49+
# Disable iwyu and clang-tidy
50+
foreach(_prometheus_target core pull civetweb)
51+
set_target_properties(${_prometheus_target} PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ""
52+
CXX_CLANG_TIDY "")
53+
endforeach()
54+
endif()
55+
56+
if(NOT TARGET prometheus-cpp::core OR
57+
NOT TARGET prometheus-cpp::pull)
58+
message(FATAL_ERROR "A required prometheus-cpp target (prometheus-cpp::core or prometheus-cpp::pull) was not imported")
59+
endif()

exporters/prometheus/CMakeLists.txt

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(NOT TARGET prometheus-cpp::core)
5-
find_package(prometheus-cpp CONFIG REQUIRED)
6-
endif()
7-
84
add_library(
95
opentelemetry_exporter_prometheus
106
src/exporter.cc src/exporter_options.cc src/exporter_factory.cc
@@ -19,22 +15,10 @@ target_include_directories(
1915
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
2016
"$<INSTALL_INTERFACE:include>")
2117

22-
set(PROMETHEUS_EXPORTER_TARGETS opentelemetry_exporter_prometheus)
23-
if(TARGET pull)
24-
list(APPEND PROMETHEUS_EXPORTER_TARGETS pull)
25-
endif()
26-
if(TARGET core)
27-
list(APPEND PROMETHEUS_EXPORTER_TARGETS core)
28-
endif()
29-
if(TARGET util)
30-
list(APPEND PROMETHEUS_EXPORTER_TARGETS util)
31-
endif()
32-
set(PROMETHEUS_CPP_TARGETS prometheus-cpp::pull prometheus-cpp::core)
33-
if(TARGET prometheus-cpp::util)
34-
list(APPEND PROMETHEUS_CPP_TARGETS prometheus-cpp::util)
35-
endif()
36-
target_link_libraries(opentelemetry_exporter_prometheus
37-
PUBLIC opentelemetry_metrics ${PROMETHEUS_CPP_TARGETS})
18+
target_link_libraries(
19+
opentelemetry_exporter_prometheus
20+
PUBLIC opentelemetry_metrics prometheus-cpp::core
21+
PRIVATE prometheus-cpp::pull)
3822

3923
otel_add_component(
4024
COMPONENT

0 commit comments

Comments
 (0)