Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prometheus Exporter #1031

Merged
merged 83 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
38f811a
Add PrometheusExporter header, source, and test files
erichsueh3 Aug 7, 2020
140efcd
CI formatting, added copyright headers
erichsueh3 Aug 7, 2020
feb7126
Added build files
erichsueh3 Aug 18, 2020
5d6fc7e
modified comments to reflect removal of ReturnCodes class, removal of…
erichsueh3 Aug 19, 2020
845d6f1
Merge branch 'master' into prom_exporter
erichsueh3 Aug 29, 2020
32880b0
minor change to structure of friend class
erichsueh3 Aug 29, 2020
61783a7
Merge branch 'prom_exporter' of https:/erichsueh3/opentel…
erichsueh3 Aug 29, 2020
b8d8f5a
Merge branch 'master' into prom_exporter
erichsueh3 Aug 29, 2020
94af70d
removed assignment of exposer_ to nullptr in testing constructor
erichsueh3 Aug 31, 2020
550bb7f
Merge branch 'master' into prom_exporter
erichsueh3 Sep 2, 2020
367d6a3
removed usage of release9.0 of Prometheus
erichsueh3 Sep 2, 2020
a5e2cfb
BUILD file changes
erichsueh3 Sep 2, 2020
6bbb051
changed Prometheus client back to 9.0
erichsueh3 Sep 2, 2020
d3d44f3
removed a test for debugging
erichsueh3 Sep 2, 2020
8dc5cb1
Revert "removed a test for debugging"
erichsueh3 Sep 2, 2020
94f78c8
format
erichsueh3 Sep 2, 2020
5938aa8
added MAKE_EXE_LINKER_FLAGS cmake link option
erichsueh3 Sep 3, 2020
dc28f84
added -D to flag
erichsueh3 Sep 3, 2020
75e23e5
changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS
erichsueh3 Sep 3, 2020
f9a4dda
Add PrometheusExporter header, source, and test files
erichsueh3 Aug 7, 2020
0a5fb81
CI formatting, added copyright headers
erichsueh3 Aug 7, 2020
2251d49
Added build files
erichsueh3 Aug 18, 2020
81d1c1c
modified comments to reflect removal of ReturnCodes class, removal of…
erichsueh3 Aug 19, 2020
b85f648
minor change to structure of friend class
erichsueh3 Aug 29, 2020
2f480c0
removed assignment of exposer_ to nullptr in testing constructor
erichsueh3 Aug 31, 2020
99ca914
removed usage of release9.0 of Prometheus
erichsueh3 Sep 2, 2020
605e42f
BUILD file changes
erichsueh3 Sep 2, 2020
87e4223
changed Prometheus client back to 9.0
erichsueh3 Sep 2, 2020
c83723d
removed a test for debugging
erichsueh3 Sep 2, 2020
4ef1e90
Revert "removed a test for debugging"
erichsueh3 Sep 2, 2020
9e8dcdb
format
erichsueh3 Sep 2, 2020
ba7bb88
added MAKE_EXE_LINKER_FLAGS cmake link option
erichsueh3 Sep 3, 2020
8267094
added -D to flag
erichsueh3 Sep 3, 2020
81db277
changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS
erichsueh3 Sep 3, 2020
e3ad09c
Merge branch 'prom_exporter' of https:/erichsueh3/opentel…
esigo Oct 23, 2021
99572e0
Add PrometheusExporter header, source, and test files
erichsueh3 Aug 7, 2020
a22933e
CI formatting, added copyright headers
erichsueh3 Aug 7, 2020
5055c68
Added build files
erichsueh3 Aug 18, 2020
5023d44
modified comments to reflect removal of ReturnCodes class, removal of…
erichsueh3 Aug 19, 2020
347caa6
minor change to structure of friend class
erichsueh3 Aug 29, 2020
8e7abd3
removed assignment of exposer_ to nullptr in testing constructor
erichsueh3 Aug 31, 2020
0195f4b
removed usage of release9.0 of Prometheus
erichsueh3 Sep 2, 2020
1cb72fe
BUILD file changes
erichsueh3 Sep 2, 2020
8a463b2
changed Prometheus client back to 9.0
erichsueh3 Sep 2, 2020
d1fc9d4
removed a test for debugging
erichsueh3 Sep 2, 2020
673f1bb
Revert "removed a test for debugging"
erichsueh3 Sep 2, 2020
825373f
format
erichsueh3 Sep 2, 2020
b4df355
added MAKE_EXE_LINKER_FLAGS cmake link option
erichsueh3 Sep 3, 2020
f96756e
added -D to flag
erichsueh3 Sep 3, 2020
9fccc94
changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS
erichsueh3 Sep 3, 2020
d7d7010
Add PrometheusExporter header, source, and test files
erichsueh3 Aug 7, 2020
9085479
CI formatting, added copyright headers
erichsueh3 Aug 7, 2020
e0c974a
Added build files
erichsueh3 Aug 18, 2020
f1aadef
modified comments to reflect removal of ReturnCodes class, removal of…
erichsueh3 Aug 19, 2020
b33edf9
minor change to structure of friend class
erichsueh3 Aug 29, 2020
69de0e8
removed assignment of exposer_ to nullptr in testing constructor
erichsueh3 Aug 31, 2020
fd32df6
removed usage of release9.0 of Prometheus
erichsueh3 Sep 2, 2020
f7da657
BUILD file changes
erichsueh3 Sep 2, 2020
2986e8c
changed Prometheus client back to 9.0
erichsueh3 Sep 2, 2020
bc872ed
removed a test for debugging
erichsueh3 Sep 2, 2020
1859eca
Revert "removed a test for debugging"
erichsueh3 Sep 2, 2020
80c811b
format
erichsueh3 Sep 2, 2020
398256d
added MAKE_EXE_LINKER_FLAGS cmake link option
erichsueh3 Sep 3, 2020
3d424d2
added -D to flag
erichsueh3 Sep 3, 2020
5e3b456
changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS
erichsueh3 Sep 3, 2020
f54b772
Merge branch 'esigo-prometheus-exporter' of https:/esigo/…
esigo Oct 23, 2021
6ca2e10
fix compile fail
esigo Oct 24, 2021
032bfb9
fix cmake and format
esigo Oct 24, 2021
4d4f0ef
bazel format
esigo Oct 24, 2021
ef20e67
use prometheus subdir
esigo Oct 24, 2021
908e19d
prometheus install handled in cmake
esigo Oct 27, 2021
f6f5c8b
update CHANGELOG
esigo Oct 27, 2021
4648358
Merge branch 'main' into esigo-prometheus-exporter
esigo Oct 30, 2021
5f2744a
Merge branch 'main' into esigo-prometheus-exporter
lalitb Nov 5, 2021
bdd2a7b
comments
esigo Nov 5, 2021
ba8e877
Merge branch 'main' into esigo-prometheus-exporter
esigo Nov 5, 2021
5052fd5
fix ci
esigo Nov 6, 2021
abc06b7
remove debug message
esigo Nov 6, 2021
d1c226e
fix bazel ci and comments
esigo Nov 6, 2021
a6f3315
fix cmake ci
esigo Nov 6, 2021
6c0e4d3
comments
esigo Nov 6, 2021
c93caf7
install missing targets
esigo Nov 6, 2021
8ff4d7a
Merge branch 'main' into esigo-prometheus-exporter
lalitb Nov 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Increment the:

## [Unreleased]

* [EXPORTER] Prometheus Exporter ([#1031](https:/open-telemetry/opentelemetry-cpp/pull/1031))
* [EXPORTER] Add OTLP/HTTP Log Exporter ([#1030](https:/open-telemetry/opentelemetry-cpp/pull/1030))

## [1.0.1] 2021-10-21
Expand Down
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ if((NOT WITH_API_ONLY)
endif()
endif()

if(WITH_PROMETHEUS)
find_package(prometheus-cpp CONFIG QUIET)
if(NOT prometheus-cpp_FOUND)
message("Trying to use local prometheus-cpp from submodule")
if(EXISTS ${PROJECT_SOURCE_DIR}/third_party/prometheus-cpp/.git)
add_subdirectory(third_party/prometheus-cpp)
else()
message(
FATAL_ERROR
"\nprometheus-cpp package was not found. Please either provide it manually or clone with submodules. "
"To initialize, fetch and checkout any nested submodules, you can use the following command:\n"
"git submodule update --init --recursive")
endif()
else()
message("Using external prometheus-cpp")
endif()
endif()

if(WITH_OTLP)
set(protobuf_MODULE_COMPATIBLE ON)
find_package(Protobuf)
Expand Down
1 change: 0 additions & 1 deletion ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ BAZEL_STARTUP_OPTIONS="--output_user_root=$HOME/.cache/bazel"
export CTEST_OUTPUT_ON_FAILURE=1

if [[ "$1" == "cmake.test" ]]; then
install_prometheus_cpp_client
cd "${BUILD_DIR}"
rm -rf *
cmake -DCMAKE_BUILD_TYPE=Debug \
Expand Down
37 changes: 23 additions & 14 deletions exporters/prometheus/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "prometheus_collector",
name = "prometheus_exporter",
srcs = [
"src/prometheus_collector.cc",
"src/prometheus_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/prometheus/prometheus_collector.h",
"include/opentelemetry/exporters/prometheus/prometheus_exporter_utils.h",
"include/opentelemetry/exporters/prometheus/prometheus_exporter.h",
],
strip_include_prefix = "include",
deps = [
":prometheus_utils",
":prometheus_collector",
":prometheus_exporter_utils",
"//api",
"//sdk:headers",
"@com_github_jupp0r_prometheus_cpp//core",
"@com_github_jupp0r_prometheus_cpp//pull",
],
)

cc_library(
name = "prometheus_utils",
name = "prometheus_exporter_utils",
srcs = [
"src/prometheus_exporter_utils.cc",
],
Expand All @@ -45,27 +46,35 @@ cc_library(
"//api",
"//sdk:headers",
"@com_github_jupp0r_prometheus_cpp//core",
"@com_github_jupp0r_prometheus_cpp//pull",
],
)

cc_test(
name = "prometheus_collector_test",
cc_library(
name = "prometheus_collector",
srcs = [
"test/prometheus_collector_test.cc",
"src/prometheus_collector.cc",
],
hdrs = [
"include/opentelemetry/exporters/prometheus/prometheus_collector.h",
],
strip_include_prefix = "include",
deps = [
":prometheus_collector",
"@com_google_googletest//:gtest_main",
":prometheus_exporter_utils",
"//api",
"//sdk:headers",
"@com_github_jupp0r_prometheus_cpp//core",
"@com_github_jupp0r_prometheus_cpp//pull",
],
)

cc_test(
name = "prometheus_exporter_utils_test",
name = "prometheus_exporter_test",
srcs = [
"test/prometheus_exporter_utils_test.cc",
"test/prometheus_exporter_test.cc",
],
deps = [
":prometheus_utils",
":prometheus_exporter",
"@com_google_googletest//:gtest_main",
],
)
20 changes: 12 additions & 8 deletions exporters/prometheus/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@
# limitations under the License.

include_directories(include)
find_package(prometheus-cpp CONFIG REQUIRED)

add_library(prometheus_exporter_deprecated src/prometheus_collector.cc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The target is removed here, but I didn't see it is added back in other places but it is referenced below in target_include_directories.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

src/prometheus_exporter_utils.cc)
if(NOT TARGET prometheus-cpp::core)
find_package(prometheus-cpp CONFIG REQUIRED)
endif()

add_library(
prometheus_exporter_deprecated
src/prometheus_exporter.cc src/prometheus_collector.cc
src/prometheus_exporter_utils.cc)
target_include_directories(
prometheus_exporter_deprecated
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

set(PROMETHEUS_EXPORTER_TARGETS prometheus_exporter_deprecated pull core)
target_link_libraries(
prometheus_exporter_deprecated PUBLIC opentelemetry_metrics_deprecated
prometheus-cpp::core)

prometheus_exporter_deprecated
PUBLIC opentelemetry_metrics_deprecated prometheus-cpp::pull
prometheus-cpp::core)
install(
TARGETS prometheus_exporter_deprecated
TARGETS ${PROMETHEUS_EXPORTER_TARGETS}
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
# include <mutex>
# include <vector>

# include "opentelemetry/exporters/prometheus/prometheus_exporter_utils.h"
# include "opentelemetry/sdk/_metrics/record.h"
# include "prometheus/collectable.h"
# include "prometheus/metric_family.h"
# include "prometheus_exporter_utils.h"

namespace prometheus_client = ::prometheus;
namespace metric_sdk = opentelemetry::sdk::metrics;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef ENABLE_METRICS_PREVIEW
# include <memory>
# include <string>
# include <vector>

# include "opentelemetry/exporters/prometheus/prometheus_collector.h"
# include "opentelemetry/sdk/_metrics/exporter.h"
# include "opentelemetry/sdk/_metrics/record.h"
# include "opentelemetry/version.h"
# include "prometheus/exposer.h"

/**
* This class is an implementation of the MetricsExporter interface and
* exports Prometheus metrics data. Functions in this class should be
* called by the Controller in our data pipeline.
*/

OPENTELEMETRY_BEGIN_NAMESPACE

namespace exporter
{
namespace prometheus
{
class PrometheusExporter : public sdk::metrics::MetricsExporter
{
public:
/**
* Constructor - binds an exposer and collector to the exporter
* @param address: an address for an exposer that exposes
* an HTTP endpoint for the exporter to connect to
*/
PrometheusExporter(std::string &address);

/**
* Exports a batch of Metric Records.
* @param records: a collection of records to export
* @return: returns a ReturnCode detailing a success, or type of failure
*/
sdk::common::ExportResult Export(
const std::vector<sdk::metrics::Record> &records) noexcept override;

/**
* Shuts down the exporter and does cleanup.
* Since Prometheus is a pull based interface,
* we cannot serve data remaining in the intermediate
* collection to to client an HTTP request being sent,
* so we flush the data.
*/
void Shutdown() noexcept;

/**
* @return: returns a shared_ptr to
* the PrometheusCollector instance
*/
std::shared_ptr<PrometheusCollector> &GetCollector();

/**
* @return: Gets the shutdown status of the exporter
*/
bool IsShutdown() const;

private:
/**
* exporter shutdown status
*/
bool is_shutdown_;

/**
* Pointer to a
* PrometheusCollector instance
*/
std::shared_ptr<PrometheusCollector> collector_;

/**
* Pointer to an
* Exposer instance
*/
std::unique_ptr<::prometheus::Exposer> exposer_;

/**
* friend class for testing
*/
friend class PrometheusExporterTest;

/**
* PrometheusExporter constructor with no parameters
* Used for testing only
*/
PrometheusExporter();
};
} // namespace prometheus
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif // ENABLE_METRICS_PREVIEW
110 changes: 110 additions & 0 deletions exporters/prometheus/src/prometheus_exporter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifdef ENABLE_METRICS_PREVIEW
# include "opentelemetry/exporters/prometheus/prometheus_exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE

namespace exporter
{
namespace prometheus
{
/**
* Constructor - binds an exposer and collector to the exporter
* @param address: an address for an exposer that exposes
* an HTTP endpoint for the exporter to connect to
*/
PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(false)
{
exposer_ = std::unique_ptr<::prometheus::Exposer>(new ::prometheus::Exposer{address});
collector_ = std::shared_ptr<PrometheusCollector>(new PrometheusCollector);

exposer_->RegisterCollectable(collector_);
}

/**
* PrometheusExporter constructor with no parameters
* Used for testing only
*/
PrometheusExporter::PrometheusExporter() : is_shutdown_(false)
{
collector_ = std::unique_ptr<PrometheusCollector>(new PrometheusCollector);
}

/**
* Exports a batch of Metric Records.
* @param records: a collection of records to export
* @return: returns a ReturnCode detailing a success, or type of failure
*/
sdk::common::ExportResult PrometheusExporter::Export(
const std::vector<sdk::metrics::Record> &records) noexcept
{
if (is_shutdown_)
{
return sdk::common::ExportResult::kFailure;
}
else if (records.empty())
{
return sdk::common::ExportResult::kFailureInvalidArgument;
}
else if (collector_->GetCollection().size() + records.size() >
(size_t)collector_->GetMaxCollectionSize())
{
return sdk::common::ExportResult::kFailureFull;
}
else
{
collector_->AddMetricData(records);
return sdk::common::ExportResult::kSuccess;
}
}

/**
* Shuts down the exporter and does cleanup.
* Since Prometheus is a pull based interface,
* we cannot serve data remaining in the intermediate
* collection to to client an HTTP request being sent,
* so we flush the data.
*/
void PrometheusExporter::Shutdown() noexcept
{
is_shutdown_ = true;

collector_->GetCollection().clear();
}

/**
* @return: returns a shared_ptr to
* the PrometheusCollector instance
*/
std::shared_ptr<PrometheusCollector> &PrometheusExporter::GetCollector()
{
return collector_;
}

/**
* @return: Gets the shutdown status of the exporter
*/
bool PrometheusExporter::IsShutdown() const
{
return is_shutdown_;
}

} // namespace prometheus
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif // ENABLE_METRICS_PREVIEW
3 changes: 2 additions & 1 deletion exporters/prometheus/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
foreach(testname prometheus_collector_test prometheus_exporter_utils_test)
foreach(testname prometheus_exporter_test prometheus_collector_test
prometheus_exporter_utils_test)
add_executable(${testname} "${testname}.cc")
target_link_libraries(
${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
Expand Down
Loading