Skip to content

Commit

Permalink
prometheus exporter (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Apr 25, 2022
1 parent bf8f433 commit e6fb935
Show file tree
Hide file tree
Showing 9 changed files with 991 additions and 41 deletions.
2 changes: 1 addition & 1 deletion exporters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ endif()
add_subdirectory(ostream)
add_subdirectory(memory)

if(WITH_PROMETHEUS AND WITH_METRICS_PREVIEW)
if(WITH_PROMETHEUS)
add_subdirectory(prometheus)
endif()

Expand Down
73 changes: 65 additions & 8 deletions exporters/prometheus/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "prometheus_exporter",
name = "prometheus_exporter_deprecated",
srcs = [
"src/prometheus_exporter.cc",
],
Expand All @@ -25,8 +25,8 @@ cc_library(
strip_include_prefix = "include",
tags = ["prometheus"],
deps = [
":prometheus_collector",
":prometheus_exporter_utils",
":prometheus_collector_deprecated",
":prometheus_exporter_utils_deprecated",
"//api",
"//sdk:headers",
"@com_github_jupp0r_prometheus_cpp//core",
Expand All @@ -35,7 +35,7 @@ cc_library(
)

cc_library(
name = "prometheus_exporter_utils",
name = "prometheus_exporter_utils_deprecated",
srcs = [
"src/prometheus_exporter_utils.cc",
],
Expand All @@ -53,7 +53,7 @@ cc_library(
)

cc_library(
name = "prometheus_collector",
name = "prometheus_collector_deprecated",
srcs = [
"src/prometheus_collector.cc",
],
Expand All @@ -63,7 +63,7 @@ cc_library(
strip_include_prefix = "include",
tags = ["prometheus"],
deps = [
":prometheus_exporter_utils",
":prometheus_exporter_utils_deprecated",
"//api",
"//sdk:headers",
"@com_github_jupp0r_prometheus_cpp//core",
Expand All @@ -72,7 +72,7 @@ cc_library(
)

cc_test(
name = "prometheus_exporter_test",
name = "prometheus_exporter_test_deprecated",
srcs = [
"test/prometheus_exporter_test.cc",
],
Expand All @@ -81,7 +81,64 @@ cc_test(
"test",
],
deps = [
":prometheus_exporter",
":prometheus_exporter_deprecated",
"@com_google_googletest//:gtest_main",
],
)

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

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

cc_library(
name = "prometheus_collector",
srcs = [
"src/collector.cc",
],
hdrs = [
"include/opentelemetry/exporters/prometheus/collector.h",
],
strip_include_prefix = "include",
tags = ["prometheus"],
deps = [
":prometheus_exporter_utils",
"//api",
"//sdk:headers",
"@com_github_jupp0r_prometheus_cpp//core",
"@com_github_jupp0r_prometheus_cpp//pull",
],
)
99 changes: 67 additions & 32 deletions exporters/prometheus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,75 @@ include_directories(include)
if(NOT TARGET prometheus-cpp::core)
find_package(prometheus-cpp CONFIG REQUIRED)
endif()
if(WITH_METRICS_PREVIEW)
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>")

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_DEPRECATED prometheus_exporter_deprecated)
if(TARGET pull)
list(APPEND PROMETHEUS_EXPORTER_TARGETS_DEPRECATED pull)
endif()
if(TARGET core)
list(APPEND PROMETHEUS_EXPORTER_TARGETS_DEPRECATED core)
endif()
target_link_libraries(
prometheus_exporter_deprecated
PUBLIC opentelemetry_metrics_deprecated prometheus-cpp::pull
prometheus-cpp::core)
install(
TARGETS ${PROMETHEUS_EXPORTER_TARGETS_DEPRECATED}
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

set(PROMETHEUS_EXPORTER_TARGETS prometheus_exporter_deprecated)
if(TARGET pull)
list(APPEND PROMETHEUS_EXPORTER_TARGETS pull)
endif()
if(TARGET core)
list(APPEND PROMETHEUS_EXPORTER_TARGETS core)
endif()
target_link_libraries(
prometheus_exporter_deprecated
PUBLIC opentelemetry_metrics_deprecated prometheus-cpp::pull
prometheus-cpp::core)
install(
TARGETS ${PROMETHEUS_EXPORTER_TARGETS}
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(
DIRECTORY include/opentelemetry/exporters/prometheus
DESTINATION include/opentelemetry/exporters/
FILES_MATCHING
PATTERN "*.h")
if(BUILD_TESTING)
add_subdirectory(test)
endif()
else()

add_library(prometheus_exporter src/exporter.cc src/collector.cc
src/exporter_utils.cc)
target_include_directories(
prometheus_exporter
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

set(PROMETHEUS_EXPORTER_TARGETS prometheus_exporter)
if(TARGET pull)
list(APPEND PROMETHEUS_EXPORTER_TARGETS pull)
endif()
if(TARGET core)
list(APPEND PROMETHEUS_EXPORTER_TARGETS core)
endif()
target_link_libraries(
prometheus_exporter PUBLIC opentelemetry_metrics prometheus-cpp::pull
prometheus-cpp::core)
install(
TARGETS ${PROMETHEUS_EXPORTER_TARGETS}
EXPORT "${PROJECT_NAME}-target"
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
DIRECTORY include/opentelemetry/exporters/prometheus
DESTINATION include/opentelemetry/exporters
FILES_MATCHING
PATTERN "*.h")
install(
DIRECTORY include/opentelemetry/exporters/prometheus
DESTINATION include/opentelemetry/exporters/
FILES_MATCHING
PATTERN "*.h")

if(BUILD_TESTING)
add_subdirectory(test)
if(BUILD_TESTING)
add_subdirectory(test)
endif()
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW

# include <memory>
# include <mutex>
# include <vector>

# include <prometheus/collectable.h>
# include <prometheus/metric_family.h>
# include "opentelemetry/exporters/prometheus/exporter_utils.h"

namespace prometheus_client = ::prometheus;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace metrics
{
/**
* The Prometheus Collector maintains the intermediate collection in Prometheus Exporter
*/
class PrometheusCollector : public prometheus_client::Collectable
{
public:
/**
* Default Constructor.
*
* This constructor initializes the collection for metrics to export
* in this class with default capacity
*/
explicit PrometheusCollector(size_t max_collection_size = 2048);

/**
* Collects all metrics data from metricsToCollect collection.
*
* @return all metrics in the metricsToCollect snapshot
*/
std::vector<prometheus_client::MetricFamily> Collect() const override;

/**
* This function is called by export() function and add the collection of
* records to the metricsToCollect collection
*
* @param records a collection of records to add to the metricsToCollect collection
*/
void AddMetricData(const sdk::metrics::ResourceMetrics &data);

/**
* Get the current collection in the collector.
*
* @return the current metricsToCollect collection
*/
std::vector<std::unique_ptr<sdk::metrics::ResourceMetrics>> &GetCollection();

/**
* Gets the maximum size of the collection.
*
* @return max collection size
*/
int GetMaxCollectionSize() const;

private:
/**
* Collection of metrics data from the export() function, and to be export
* to user when they send a pull request. This collection is a pointer
* to a collection so Collect() is able to clear the collection, even
* though it is a const function.
*/
mutable std::vector<std::unique_ptr<sdk::metrics::ResourceMetrics>> metrics_to_collect_;

/**
* Maximum size of the metricsToCollect collection.
*/
size_t max_collection_size_;

/*
* Lock when operating the metricsToCollect collection
*/
mutable std::mutex collection_lock_;
};
} // namespace metrics
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
Loading

1 comment on commit e6fb935

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: e6fb935 Previous: bf8f433 Ratio
BM_BaselineBuffer/4 14097092.151641846 ns/iter 6500868.797302246 ns/iter 2.17

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.