Skip to content

Commit 05b755b

Browse files
authored
Add PrometheusPushExporter (#213)
1 parent c44186c commit 05b755b

File tree

13 files changed

+1168
-0
lines changed

13 files changed

+1168
-0
lines changed

.github/workflows/prometheus.yml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
name: prometheus instrumentation CI
2+
3+
on:
4+
push:
5+
branches: "*"
6+
paths:
7+
- "exporters/prometheus/**"
8+
- ".github/workflows/prometheus.yml"
9+
pull_request:
10+
branches: [main]
11+
paths:
12+
- "exporters/prometheus/**"
13+
- ".github/workflows/prometheus.yml"
14+
15+
jobs:
16+
prometheus_bazel_linux:
17+
name: Bazel on Linux
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: checkout otel contrib prometheus
21+
uses: actions/checkout@v3
22+
with:
23+
path: "otel_cpp_contrib"
24+
- name: Mount Bazel Cache
25+
uses: actions/cache@v3
26+
env:
27+
cache-name: bazel_cache
28+
with:
29+
path: /home/runner/.cache/bazel
30+
key: bazel_test
31+
- name: run tests
32+
run: |
33+
cd otel_cpp_contrib/exporters/prometheus
34+
bazel --output_user_root=$HOME/.cache/bazel build --copt=-DENABLE_TEST --@io_opentelemetry_cpp//api:with_abseil //...
35+
bazel --output_user_root=$HOME/.cache/bazel test --copt=-DENABLE_TEST --@io_opentelemetry_cpp//api:with_abseil //...
36+
prometheus_bazel_osx:
37+
name: Bazel on MacOS
38+
runs-on: macos-latest
39+
steps:
40+
- name: checkout otel contrib prometheus
41+
uses: actions/checkout@v3
42+
with:
43+
path: "otel_cpp_contrib"
44+
- name: Mount Bazel Cache
45+
uses: actions/cache@v3
46+
env:
47+
cache-name: bazel_cache
48+
with:
49+
path: /Users/runner/.cache/bazel
50+
key: bazel_osx
51+
- name: run tests
52+
run: |
53+
cd otel_cpp_contrib/exporters/prometheus
54+
bazel --output_user_root=$HOME/.cache/bazel build --copt=-DENABLE_TEST --@io_opentelemetry_cpp//api:with_abseil //...
55+
bazel --output_user_root=$HOME/.cache/bazel test --copt=-DENABLE_TEST --@io_opentelemetry_cpp//api:with_abseil //...
56+
prometheus_bazel_window:
57+
name: Bazel on Windows
58+
runs-on: windows-latest
59+
steps:
60+
- name: checkout otel contrib prometheus
61+
uses: actions/checkout@v3
62+
with:
63+
path: "otel_cpp_contrib"
64+
- name: run tests
65+
shell: pwsh
66+
run: |
67+
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
68+
-Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force
69+
$RUNNER_ROOT_PATH = Split-Path (Split-Path (Get-Location))
70+
Set-Location otel_cpp_contrib/exporters/prometheus
71+
& bazel --windows_enable_symlinks "--output_user_root=$RUNNER_ROOT_PATH/bazel_build" `
72+
build --copt=-DENABLE_TEST --copt=/utf-8 --sandbox_debug --@io_opentelemetry_cpp//api:with_abseil //...
73+
& bazel --windows_enable_symlinks "--output_user_root=$RUNNER_ROOT_PATH/bazel_build" `
74+
test --copt=-DENABLE_TEST --copt=/utf-8 --sandbox_debug --@io_opentelemetry_cpp//api:with_abseil //...
75+
prometheus_cmake_linux:
76+
name: CMake on Linux
77+
runs-on: ubuntu-latest
78+
steps:
79+
- name: checkout otel contrib prometheus
80+
uses: actions/checkout@v3
81+
with:
82+
path: "otel_cpp_contrib"
83+
- name: checkout otel cpp
84+
uses: actions/checkout@v3
85+
with:
86+
repository: "open-telemetry/opentelemetry-cpp"
87+
ref: "v1.6.1"
88+
path: "otel_cpp"
89+
submodules: "recursive"
90+
- name: setup
91+
run: |
92+
sudo apt update -y
93+
sudo apt install -y --no-install-recommends --no-install-suggests build-essential\
94+
ca-certificates wget git valgrind lcov
95+
- name: run tests
96+
run: |
97+
vcpkg install prometheus-cpp[pull,push] nlohmann-json GTest Benchmark --triplet=x64-linux
98+
mkdir -p "$GITHUB_WORKSPACE/otel_cpp/build"
99+
cd "$GITHUB_WORKSPACE/otel_cpp/build"
100+
cmake .. -DWITH_PROMETHEUS=ON -DWITH_METRICS_PREVIEW=OFF -DCMAKE_BUILD_TYPE=Debug \
101+
-DVCPKG_TARGET_TRIPLET=x64-linux \
102+
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake"
103+
cmake --build . -j --config Debug
104+
cmake --install . --prefix "$HOME/prebuilt-otel" --config Debug
105+
mkdir -p "$GITHUB_WORKSPACE/otel_cpp_contrib/exporters/prometheus/build"
106+
cd "$GITHUB_WORKSPACE/otel_cpp_contrib/exporters/prometheus/build"
107+
cmake .. "-DCMAKE_PREFIX_PATH=$HOME/prebuilt-otel" -DCMAKE_BUILD_TYPE=Debug \
108+
-DVCPKG_TARGET_TRIPLET=x64-linux \
109+
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake"
110+
cmake --build . -j --config Debug
111+
ctest -VV -R "opentelemetry_cpp_ecosystem_test.*" .

exporters/prometheus/.bazelrc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# bazel configurations for running tests under sanitizers.
2+
# Based on https://github.com/bazelment/trunk/blob/master/tools/bazel.rc
3+
4+
# --config=asan : Address Sanitizer.
5+
common:asan --copt -DADDRESS_SANITIZER
6+
common:asan --copt -fsanitize=address,bool,float-cast-overflow,integer-divide-by-zero,null,return,returns-nonnull-attribute,shift-exponent,signed-integer-overflow,unreachable,vla-bound
7+
common:asan --copt -fsanitize-address-use-after-scope
8+
common:asan --copt -fno-sanitize-recover=all
9+
common:asan --linkopt -fsanitize=address,bool,float-cast-overflow,integer-divide-by-zero,null,return,returns-nonnull-attribute,shift-exponent,signed-integer-overflow,unreachable,vla-bound
10+
common:asan --linkopt -fsanitize-address-use-after-scope
11+
common:asan --linkopt -fno-sanitize-recover=all
12+
common:asan --cc_output_directory_tag=asan
13+
14+
# --config=tsan : Thread Sanitizer.
15+
common:tsan --copt -fsanitize=thread
16+
common:tsan --copt -DTHREAD_SANITIZER
17+
common:tsan --linkopt -fsanitize=thread
18+
common:tsan --cc_output_directory_tag=tsan
19+
# This is needed to address false positive problem with abseil.The same setting as gRPC
20+
# https://github.com/google/sanitizers/issues/953
21+
common:tsan --test_env=TSAN_OPTIONS=report_atomic_races=0
22+
23+
# --config=legacy : GCC 4.8
24+
common:legacy --copt=-D__STDC_NO_ATOMICS__ --copt=-Wno-error=shadow --protocopt=--experimental_allow_proto3_optional

exporters/prometheus/BUILD

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
cc_library(
4+
name = "prometheus_push_exporter",
5+
srcs = [
6+
"src/push_exporter.cc",
7+
],
8+
hdrs = [
9+
"include/opentelemetry/exporters/prometheus/push_exporter.h",
10+
],
11+
strip_include_prefix = "include",
12+
tags = ["prometheus"],
13+
deps = [
14+
"@com_github_jupp0r_prometheus_cpp//core",
15+
"@com_github_jupp0r_prometheus_cpp//push",
16+
"@io_opentelemetry_cpp//api",
17+
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_collector",
18+
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_exporter_utils",
19+
"@io_opentelemetry_cpp//sdk:headers",
20+
],
21+
)
22+
23+
cc_library(
24+
name = "prometheus_test_helper",
25+
hdrs = [
26+
"test/prometheus_test_helper.h",
27+
],
28+
tags = ["prometheus"],
29+
deps = [
30+
"@io_opentelemetry_cpp//api",
31+
"@io_opentelemetry_cpp//sdk:headers",
32+
"@io_opentelemetry_cpp//sdk/src/trace",
33+
],
34+
)
35+
36+
cc_test(
37+
name = "prometheus_push_exporter_test",
38+
srcs = [
39+
"test/push_exporter_test.cc",
40+
],
41+
tags = [
42+
"prometheus",
43+
"test",
44+
],
45+
deps = [
46+
":prometheus_push_exporter",
47+
":prometheus_test_helper",
48+
"@com_google_googletest//:gtest_main",
49+
"@io_opentelemetry_cpp//api",
50+
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_collector",
51+
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_exporter_utils",
52+
"@io_opentelemetry_cpp//sdk:headers",
53+
],
54+
)
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# Copyright 2022, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
# use this file except in compliance with the License. You may obtain a copy of
5+
# the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations under
13+
# the License.
14+
15+
cmake_minimum_required(VERSION 3.12)
16+
17+
project(
18+
opentelemetry-cpp-contrib
19+
VERSION "1.6.1" # opentelemetry-cpp-contrib 1.6.1
20+
HOMEPAGE_URL "https://github.com/open-telemetry/opentelemetry-cpp-contrib"
21+
LANGUAGES CXX)
22+
23+
if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
24+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
25+
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
26+
endif()
27+
28+
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
29+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
30+
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
31+
endif()
32+
33+
if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
34+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
35+
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
36+
endif()
37+
38+
link_directories(${CMAKE_ARCHIVE_OUTPUT_DIRECTORY})
39+
40+
find_package(opentelemetry-cpp REQUIRED)
41+
find_package(prometheus-cpp CONFIG REQUIRED)
42+
43+
get_target_property(OPENTELEMETRY_CPP_API_DEFINITIONS opentelemetry-cpp::api
44+
INTERFACE_COMPILE_DEFINITIONS)
45+
if(HAVE_ABSEIL IN_LIST OPENTELEMETRY_CPP_API_DEFINITIONS)
46+
find_package(absl REQUIRED)
47+
endif()
48+
49+
if(HAVE_GSL IN_LIST OPENTELEMETRY_CPP_API_DEFINITIONS)
50+
find_package(Microsoft.GSL REQUIRED)
51+
endif()
52+
53+
include_directories(include)
54+
include(CMakeDependentOption)
55+
56+
cmake_dependent_option(BUILD_TESTING "Enable tests" ON
57+
"NOT CMAKE_CROSSCOMPILING" OFF)
58+
59+
if(BUILD_TESTING)
60+
enable_testing()
61+
add_definitions(-DENABLE_TEST)
62+
find_package(GTest)
63+
include(GoogleTest)
64+
65+
if(TARGET GTest::gtest)
66+
set(OPENTELEMETRY_CPP_CONTRIB_GTEST_LINK_LIB_NAME GTest::gtest)
67+
elseif(TARGET GTest::GTest)
68+
set(OPENTELEMETRY_CPP_CONTRIB_GTEST_LINK_LIB_NAME GTest::GTest)
69+
else()
70+
set(OPENTELEMETRY_CPP_CONTRIB_GTEST_LINK_LIB_NAME ${GTEST_LIBRARIES})
71+
endif()
72+
73+
if(TARGET GTest::gtest_main)
74+
set(OPENTELEMETRY_CPP_CONTRIB_GTEST_LINK_MAIN_NAME GTest::gtest_main)
75+
elseif(TARGET GTest::Main)
76+
set(OPENTELEMETRY_CPP_CONTRIB_GTEST_LINK_MAIN_NAME GTest::Main)
77+
else()
78+
set(OPENTELEMETRY_CPP_CONTRIB_GTEST_LINK_MAIN_NAME ${GTEST_MAIN_LIBRARIES})
79+
endif()
80+
endif()
81+
82+
add_library(opentelemetry_prometheus_push_exporter src/push_exporter.cc)
83+
set_target_properties(opentelemetry_prometheus_push_exporter
84+
PROPERTIES EXPORT_NAME prometheus_push_exporter)
85+
86+
target_include_directories(
87+
opentelemetry_prometheus_push_exporter
88+
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
89+
"$<INSTALL_INTERFACE:include>")
90+
91+
target_link_libraries(
92+
opentelemetry_prometheus_push_exporter
93+
PUBLIC opentelemetry-cpp::prometheus_exporter opentelemetry-cpp::metrics
94+
opentelemetry-cpp::resources prometheus-cpp::push prometheus-cpp::core)
95+
96+
if(DEFINED VCPKG_CMAKE_SYSTEM_NAME AND VCPKG_CMAKE_SYSTEM_NAME STREQUAL
97+
"WindowsStore")
98+
set(OPENTELEMETRY_CPP_CONTRIB_TARGET_IS_WINDOWS ON)
99+
elseif(MINGW OR CMAKE_SYSTEM_NAME STREQUAL "MinGW")
100+
set(OPENTELEMETRY_CPP_CONTRIB_TARGET_IS_WINDOWS ON)
101+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
102+
set(OPENTELEMETRY_CPP_CONTRIB_TARGET_IS_WINDOWS ON)
103+
endif()
104+
if(OPENTELEMETRY_CPP_CONTRIB_TARGET_IS_WINDOWS)
105+
target_link_libraries(
106+
opentelemetry_prometheus_push_exporter
107+
PUBLIC advapi32
108+
bcrypt
109+
iphlpapi
110+
psapi
111+
user32
112+
wsock32
113+
userenv
114+
Ws2_32
115+
wldap32
116+
crypt32)
117+
elseif(UNIX OR MINGW)
118+
set(OPENTELEMETRY_CPP_CONTRIB_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES
119+
${CMAKE_REQUIRED_LIBRARIES})
120+
set(OPENTELEMETRY_CPP_CONTRIB_TEST_SYSTEM_LIBRARIES m dl pthread rt gcc gcc_s)
121+
include(CheckCXXSourceCompiles)
122+
123+
foreach(LIBNAME IN LISTS OPENTELEMETRY_CPP_CONTRIB_TEST_SYSTEM_LIBRARIES)
124+
if(OPENTELEMETRY_CPP_CONTRIB_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES)
125+
set(CMAKE_REQUIRED_LIBRARIES
126+
"${OPENTELEMETRY_CPP_CONTRIB_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES};${LIBNAME}"
127+
)
128+
else()
129+
set(CMAKE_REQUIRED_LIBRARIES "${LIBNAME}")
130+
endif()
131+
string(TOUPPER "${LIBNAME}" OPENTELEMETRY_CPP_CONTRIB_TEST_NAME)
132+
set(OPENTELEMETRY_CPP_CONTRIB_TEST_NAME
133+
"OPENTELEMETRY_CPP_CONTRIB_TEST_LINK_${OPENTELEMETRY_CPP_CONTRIB_TEST_NAME}"
134+
)
135+
check_cxx_source_compiles("#include <cstdio>
136+
int main() { return 0; }" ${OPENTELEMETRY_CPP_CONTRIB_TEST_NAME})
137+
if(${OPENTELEMETRY_CPP_CONTRIB_TEST_NAME})
138+
list(APPEND OPENTELEMETRY_CPP_CONTRIB_SYSTEM_LIBRARIES "${LIBNAME}")
139+
endif()
140+
endforeach()
141+
142+
unset(OPENTELEMETRY_CPP_CONTRIB_TEST_NAME)
143+
if(OPENTELEMETRY_CPP_CONTRIB_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES)
144+
set(CMAKE_REQUIRED_LIBRARIES
145+
"${OPENTELEMETRY_CPP_CONTRIB_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES}")
146+
else()
147+
unset(CMAKE_REQUIRED_LIBRARIES)
148+
endif()
149+
150+
if(OPENTELEMETRY_CPP_CONTRIB_SYSTEM_LIBRARIES)
151+
target_link_libraries(opentelemetry_prometheus_push_exporter
152+
PUBLIC ${OPENTELEMETRY_CPP_CONTRIB_SYSTEM_LIBRARIES})
153+
endif()
154+
endif()
155+
156+
install(
157+
TARGETS opentelemetry_prometheus_push_exporter
158+
EXPORT "${PROJECT_NAME}-target"
159+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
160+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
161+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
162+
163+
install(
164+
DIRECTORY include/opentelemetry/exporters/prometheus
165+
DESTINATION include/opentelemetry/exporters/
166+
FILES_MATCHING
167+
PATTERN "*.h")
168+
169+
if(BUILD_TESTING)
170+
add_subdirectory(test)
171+
endif()
172+
173+
include(CMakePackageConfigHelpers)
174+
file(MAKE_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}")
175+
176+
configure_package_config_file(
177+
"${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}-config.cmake.in"
178+
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake"
179+
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
180+
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
181+
182+
write_basic_package_version_file(
183+
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake"
184+
VERSION "${PROJECT_VERSION}${OPENTELEMETRY_CPP_ECOSYSTEM_VERSION_TWEAK}"
185+
COMPATIBILITY SameMajorVersion)
186+
187+
install(
188+
FILES
189+
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config.cmake"
190+
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-config-version.cmake"
191+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
192+
193+
export(
194+
EXPORT "${PROJECT_NAME}-target"
195+
NAMESPACE ${PROJECT_NAME}::
196+
FILE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_NAME}/${PROJECT_NAME}-target.cmake"
197+
)
198+
install(
199+
EXPORT "${PROJECT_NAME}-target"
200+
NAMESPACE ${PROJECT_NAME}::
201+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

0 commit comments

Comments
 (0)