Skip to content

Commit fb746d7

Browse files
authored
Update prometheus to support otel-cpp 1.11.0 and bazel 6 (#295)
* Update prometheus to support otel-cpp 1.9.1 and bazel 6 Signed-off-by: WenTao Ou <[email protected]> * Fix dependency Signed-off-by: WenTao Ou <[email protected]> * Use cmake to check MSVC building. Signed-off-by: WenTao Ou <[email protected]> * Fix windows CI script Signed-off-by: WenTao Ou <[email protected]> * Fix build directory conflict Signed-off-by: WenTao Ou <[email protected]> * Fix OOM in ci jobs. Signed-off-by: WenTao Ou <[email protected]> * Update otel to 1.11.0 for PrometheusPushExporter, add factory for PrometheusPushExporter --------- Signed-off-by: WenTao Ou <[email protected]>
1 parent 6bb1c6b commit fb746d7

File tree

14 files changed

+711
-204
lines changed

14 files changed

+711
-204
lines changed

.github/workflows/prometheus.yml

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,44 @@ jobs:
5353
cd otel_cpp_contrib/exporters/prometheus
5454
bazel --output_user_root=$HOME/.cache/bazel build --copt=-DENABLE_TEST --@io_opentelemetry_cpp//api:with_abseil //...
5555
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
56+
prometheus_cmake_window:
57+
name: CMake on Windows
5858
runs-on: windows-latest
5959
steps:
6060
- name: checkout otel contrib prometheus
6161
uses: actions/checkout@v3
6262
with:
6363
path: "otel_cpp_contrib"
64+
- name: checkout otel cpp
65+
uses: actions/checkout@v3
66+
with:
67+
repository: "open-telemetry/opentelemetry-cpp"
68+
ref: "v1.11.0"
69+
path: "otel_cpp"
70+
submodules: "recursive"
6471
- name: run tests
6572
shell: pwsh
6673
run: |
6774
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
6875
-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 //...
76+
vcpkg install "prometheus-cpp[pull,push]" nlohmann-json gtest benchmark --triplet=x64-windows-static-md
77+
New-Item -Path "$ENV:GITHUB_WORKSPACE/otel_cpp/build_jobs_ci" -ItemType "directory" -Force
78+
Set-Location "$ENV:GITHUB_WORKSPACE/otel_cpp/build_jobs_ci"
79+
cmake .. -DWITH_PROMETHEUS=ON -DWITH_METRICS_PREVIEW=OFF -DCMAKE_BUILD_TYPE=Debug `
80+
-DVCPKG_TARGET_TRIPLET=x64-windows-static-md `
81+
"-DCMAKE_TOOLCHAIN_FILE=$ENV:VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake" `
82+
"-DCMAKE_INSTALL_PREFIX=$HOME/prebuilt-otel" `
83+
"-DBUILD_SHARED_LIBS=OFF" "-DBUILD_TESTING=OFF" "-DWITH_EXAMPLES=OFF" "-DWITH_FUNC_TESTS=OFF"
84+
cmake --build . -j --config Debug || cmake --build . -j2 --config Debug || cmake --build . --config Debug
85+
cmake --install . --prefix "$HOME/prebuilt-otel" --config Debug
86+
New-Item -Path "$ENV:GITHUB_WORKSPACE/otel_cpp_contrib/exporters/prometheus/build_jobs_ci" -ItemType "directory" -Force
87+
Set-Location "$ENV:GITHUB_WORKSPACE/otel_cpp_contrib/exporters/prometheus/build_jobs_ci"
88+
cmake .. "-DCMAKE_PREFIX_PATH=$HOME/prebuilt-otel" -DCMAKE_BUILD_TYPE=Debug `
89+
-DVCPKG_TARGET_TRIPLET=x64-windows-static-md `
90+
"-DCMAKE_TOOLCHAIN_FILE=$ENV:VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake" `
91+
"-DBUILD_SHARED_LIBS=OFF"
92+
cmake --build . -j --config Debug || cmake --build . -j2 --config Debug || cmake --build . --config Debug
93+
ctest -VV -C Debug -R "opentelemetry_cpp_ecosystem_test.*" .
7594
prometheus_cmake_linux:
7695
name: CMake on Linux
7796
runs-on: ubuntu-latest
@@ -84,7 +103,7 @@ jobs:
84103
uses: actions/checkout@v3
85104
with:
86105
repository: "open-telemetry/opentelemetry-cpp"
87-
ref: "v1.6.1"
106+
ref: "v1.11.0"
88107
path: "otel_cpp"
89108
submodules: "recursive"
90109
- name: setup
@@ -94,18 +113,20 @@ jobs:
94113
ca-certificates wget git valgrind lcov
95114
- name: run tests
96115
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"
116+
vcpkg install 'prometheus-cpp[pull,push]' nlohmann-json gtest benchmark --triplet=x64-linux
117+
mkdir -p "$GITHUB_WORKSPACE/otel_cpp/build_jobs_ci"
118+
cd "$GITHUB_WORKSPACE/otel_cpp/build_jobs_ci"
100119
cmake .. -DWITH_PROMETHEUS=ON -DWITH_METRICS_PREVIEW=OFF -DCMAKE_BUILD_TYPE=Debug \
101120
-DVCPKG_TARGET_TRIPLET=x64-linux \
102-
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake"
103-
cmake --build . -j --config Debug
121+
"-DCMAKE_INSTALL_PREFIX=$HOME/prebuilt-otel" \
122+
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake" \
123+
"-DBUILD_TESTING=OFF" "-DWITH_EXAMPLES=OFF" "-DWITH_FUNC_TESTS=OFF"
124+
cmake --build . -j --config Debug || cmake --build . -j2 --config Debug || cmake --build . --config Debug
104125
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"
126+
mkdir -p "$GITHUB_WORKSPACE/otel_cpp_contrib/exporters/prometheus/build_jobs_ci"
127+
cd "$GITHUB_WORKSPACE/otel_cpp_contrib/exporters/prometheus/build_jobs_ci"
107128
cmake .. "-DCMAKE_PREFIX_PATH=$HOME/prebuilt-otel" -DCMAKE_BUILD_TYPE=Debug \
108129
-DVCPKG_TARGET_TRIPLET=x64-linux \
109-
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake"
110-
cmake --build . -j --config Debug
130+
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake"
131+
cmake --build . -j --config Debug || cmake --build . -j2 --config Debug || cmake --build . --config Debug
111132
ctest -VV -R "opentelemetry_cpp_ecosystem_test.*" .

exporters/prometheus/.clang-format

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# See Clang docs: http://clang.llvm.org/docs/ClangFormatStyleOptions.html
5+
BasedOnStyle: Chromium
6+
7+
# Allow double brackets such as std::vector<std::vector<int>>.
8+
Standard: Cpp11
9+
10+
# Indent 2 spaces at a time.
11+
IndentWidth: 2
12+
13+
# Keep lines under 100 columns long.
14+
ColumnLimit: 100
15+
16+
# Always break before braces
17+
BreakBeforeBraces: Custom
18+
BraceWrapping:
19+
# TODO(lujc) wait for clang-format-9 support in Chromium tools
20+
# AfterCaseLabel: true
21+
AfterClass: true
22+
AfterControlStatement: true
23+
AfterEnum: true
24+
AfterFunction: true
25+
AfterNamespace: true
26+
AfterStruct: true
27+
AfterUnion: true
28+
BeforeCatch: true
29+
BeforeElse: true
30+
IndentBraces: false
31+
SplitEmptyFunction: false
32+
SplitEmptyRecord: false
33+
SplitEmptyNamespace: false
34+
35+
# Keeps extern "C" blocks unindented.
36+
AfterExternBlock: false
37+
38+
# Indent case labels.
39+
IndentCaseLabels: true
40+
41+
# Right-align pointers and references
42+
PointerAlignment: Right
43+
44+
# ANGLE likes to align things as much as possible.
45+
AlignOperands: true
46+
AlignConsecutiveAssignments: true
47+
48+
# Use 2 space negative offset for access modifiers
49+
AccessModifierOffset: -2
50+
51+
# TODO(jmadill): Decide if we want this on. Doesn't have an "all or none" mode.
52+
AllowShortCaseLabelsOnASingleLine: false
53+
54+
# Useful for spacing out functions in classes
55+
KeepEmptyLinesAtTheStartOfBlocks: true
56+
57+
# Indent nested PP directives.
58+
IndentPPDirectives: AfterHash
59+
60+
# Include blocks style
61+
IncludeBlocks: Preserve
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# If comment markup is enabled, don't reflow the first comment block in
5+
# eachlistfile. Use this to preserve formatting of your
6+
# copyright/licensestatements.
7+
first_comment_is_literal = True

exporters/prometheus/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ cc_library(
44
name = "prometheus_push_exporter",
55
srcs = [
66
"src/push_exporter.cc",
7+
"src/push_exporter_factory.cc",
78
],
89
hdrs = [
910
"include/opentelemetry/exporters/prometheus/push_exporter.h",
11+
"include/opentelemetry/exporters/prometheus/push_exporter_factory.h",
12+
"include/opentelemetry/exporters/prometheus/push_exporter_options.h",
1013
],
1114
strip_include_prefix = "include",
1215
tags = ["prometheus"],
@@ -17,6 +20,7 @@ cc_library(
1720
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_collector",
1821
"@io_opentelemetry_cpp//exporters/prometheus:prometheus_exporter_utils",
1922
"@io_opentelemetry_cpp//sdk:headers",
23+
"@io_opentelemetry_cpp//sdk/src/metrics",
2024
],
2125
)
2226

exporters/prometheus/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cmake_minimum_required(VERSION 3.12)
1616

1717
project(
1818
opentelemetry-cpp-contrib
19-
VERSION "1.6.1" # opentelemetry-cpp-contrib 1.6.1
19+
VERSION "1.11.0" # opentelemetry-cpp-contrib 1.11.0
2020
HOMEPAGE_URL "https://github.com/open-telemetry/opentelemetry-cpp-contrib"
2121
LANGUAGES CXX)
2222

@@ -79,7 +79,7 @@ if(BUILD_TESTING)
7979
endif()
8080
endif()
8181

82-
add_library(opentelemetry_prometheus_push_exporter src/push_exporter.cc)
82+
add_library(opentelemetry_prometheus_push_exporter src/push_exporter.cc src/push_exporter_factory.cc)
8383
set_target_properties(opentelemetry_prometheus_push_exporter
8484
PROPERTIES EXPORT_NAME prometheus_push_exporter)
8585

@@ -93,6 +93,12 @@ target_link_libraries(
9393
PUBLIC opentelemetry-cpp::prometheus_exporter opentelemetry-cpp::metrics
9494
opentelemetry-cpp::resources prometheus-cpp::push prometheus-cpp::core)
9595

96+
target_compile_definitions(
97+
opentelemetry_prometheus_push_exporter
98+
PUBLIC "OPENTELEMTRY_CPP_MAJOR_VERSION=${opentelemetry-cpp_VERSION_MAJOR}"
99+
"OPENTELEMTRY_CPP_MINOR_VERSION=${opentelemetry-cpp_VERSION_MINOR}"
100+
"OPENTELEMTRY_CPP_PATCH_VERSION=${opentelemetry-cpp_VERSION_PATCH}")
101+
96102
if(DEFINED VCPKG_CMAKE_SYSTEM_NAME AND VCPKG_CMAKE_SYSTEM_NAME STREQUAL
97103
"WindowsStore")
98104
set(OPENTELEMETRY_CPP_CONTRIB_TARGET_IS_WINDOWS ON)

exporters/prometheus/WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ load("@io_opentelemetry_cpp//bazel:repository.bzl", "opentelemetry_cpp_deps")
2424
# Load dependencies of opentelemetry-cpp
2525
opentelemetry_cpp_deps()
2626

27+
load("@io_opentelemetry_cpp//bazel:extra_deps.bzl", "opentelemetry_extra_deps")
28+
29+
opentelemetry_extra_deps()
30+
2731
# Load prometheus C++ dependencies.
2832
load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories")
2933

exporters/prometheus/include/opentelemetry/exporters/prometheus/push_exporter.h

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
1-
// Copyright 2022, OpenTelemetry Authors
1+
// Copyright 2023, OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

44
#pragma once
55

6-
# ifdef _WIN32
7-
# include <io.h> // NOLINT
8-
# include <winsock2.h> // NOLINT
9-
# else
10-
# include <unistd.h> // NOLINT
11-
# endif
6+
#ifndef WIN32_LEAN_AND_MEAN
7+
# define WIN32_LEAN_AND_MEAN
8+
#endif
129

13-
# include <chrono>
14-
# include <memory>
15-
# include <string>
16-
# include <vector>
10+
#ifndef NOMINMAX
11+
# define NOMINMAX
12+
#endif
1713

18-
# include "prometheus/gateway.h"
14+
#ifdef _WIN32
15+
# include <io.h> // NOLINT
16+
# include <winsock2.h> // NOLINT
17+
#else
18+
# include <unistd.h> // NOLINT
19+
#endif
20+
21+
#include <chrono>
22+
#include <memory>
23+
#include <string>
24+
#include <vector>
25+
26+
#include "prometheus/gateway.h"
27+
28+
#include "opentelemetry/exporters/prometheus/exporter_utils.h"
29+
#include "opentelemetry/exporters/prometheus/push_exporter_options.h"
30+
#include "opentelemetry/nostd/span.h"
31+
#include "opentelemetry/sdk/common/env_variables.h"
32+
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
33+
#include "opentelemetry/version.h"
1934

20-
# include "opentelemetry/exporters/prometheus/collector.h"
21-
# include "opentelemetry/nostd/span.h"
22-
# include "opentelemetry/sdk/common/env_variables.h"
23-
# include "opentelemetry/sdk/metrics/metric_exporter.h"
24-
# include "opentelemetry/version.h"
2535

2636
OPENTELEMETRY_BEGIN_NAMESPACE
27-
namespace exporter {
28-
namespace metrics {
29-
/**
30-
* Struct to hold Prometheus exporter options.
31-
*/
32-
struct PrometheusPushExporterOptions {
33-
std::string host;
34-
std::string port;
35-
std::string jobname;
36-
::prometheus::Labels labels;
37-
std::string username;
38-
std::string password;
39-
40-
::opentelemetry::sdk::metrics::AggregationTemporality aggregation_temporality =
41-
::opentelemetry::sdk::metrics::AggregationTemporality::kDelta;
42-
};
37+
namespace exporter
38+
{
39+
namespace metrics
40+
{
41+
42+
class PrometheusPushCollector;
4343

44-
class PrometheusPushExporter : public ::opentelemetry::sdk::metrics::MetricExporter {
45-
public:
44+
class PrometheusPushExporter : public ::opentelemetry::sdk::metrics::PushMetricExporter
45+
{
46+
public:
4647
/**
4748
* Constructor - binds an exposer and collector to the exporter
4849
* @param options: options for an exposer that exposes
@@ -69,7 +70,8 @@ class PrometheusPushExporter : public ::opentelemetry::sdk::metrics::MetricExpor
6970
/**
7071
* Force flush the exporter.
7172
*/
72-
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
73+
bool ForceFlush(
74+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
7375

7476
/**
7577
* Shuts down the exporter and does cleanup.
@@ -81,17 +83,18 @@ class PrometheusPushExporter : public ::opentelemetry::sdk::metrics::MetricExpor
8183
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;
8284

8385
/**
84-
* @return: returns a shared_ptr to
85-
* the PrometheusCollector instance
86+
* Gets the maximum size of the collection.
87+
*
88+
* @return max collection size
8689
*/
87-
std::shared_ptr<::opentelemetry::exporter::metrics::PrometheusCollector> &GetCollector();
90+
std::size_t GetMaxCollectionSize() const noexcept;
8891

8992
/**
9093
* @return: Gets the shutdown status of the exporter
9194
*/
9295
bool IsShutdown() const;
9396

94-
private:
97+
private:
9598
// The configuration options associated with this exporter.
9699
const PrometheusPushExporterOptions options_;
97100
/**
@@ -103,7 +106,7 @@ class PrometheusPushExporter : public ::opentelemetry::sdk::metrics::MetricExpor
103106
* Pointer to a
104107
* PrometheusCollector instance
105108
*/
106-
std::shared_ptr<::opentelemetry::exporter::metrics::PrometheusCollector> collector_;
109+
std::shared_ptr<PrometheusPushCollector> collector_;
107110

108111
/**
109112
* Pointer to an
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2023, OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
8+
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
9+
#include "opentelemetry/version.h"
10+
11+
OPENTELEMETRY_BEGIN_NAMESPACE
12+
namespace exporter
13+
{
14+
namespace metrics
15+
{
16+
17+
struct PrometheusPushExporterOptions;
18+
19+
/**
20+
* Factory class for PrometheusExporter.
21+
*/
22+
class PrometheusPushExporterFactory
23+
{
24+
public:
25+
/**
26+
* Create a PrometheusExporter using the given options.
27+
*/
28+
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create(
29+
const PrometheusPushExporterOptions &options);
30+
};
31+
32+
} // namespace metrics
33+
} // namespace exporter
34+
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)