Skip to content

Commit d6c6d21

Browse files
committed
find or fetch prometheus-cpp
1 parent 711625f commit d6c6d21

File tree

3 files changed

+62
-65
lines changed

3 files changed

+62
-65
lines changed

CMakeLists.txt

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

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

358316
if(WITH_OTLP_GRPC
@@ -698,8 +656,10 @@ endif()
698656
if(USE_NLOHMANN_JSON)
699657
message(STATUS "nlohmann-json: ${nlohmann_json_VERSION}")
700658
endif()
701-
if(prometheus-cpp_FOUND)
702-
message(STATUS "prometheus-cpp: ${prometheus-cpp_VERSION}")
659+
if(WITH_PROMETHEUS)
660+
message(
661+
STATUS
662+
"prometheus-cpp: ${prometheus-cpp_VERSION} (${prometheus-cpp_PROVIDER})")
703663
endif()
704664
message(STATUS "---------------------------------------------")
705665

cmake/prometheus-cpp.cmake

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
endif()
49+
50+
if(NOT TARGET prometheus-cpp::core OR
51+
NOT TARGET prometheus-cpp::pull)
52+
message(FATAL_ERROR "A required prometheus-cpp target (prometheus-cpp::core or prometheus-cpp::pull) was not imported")
53+
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)