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

prometheus exporter #1331

Merged
merged 6 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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)
Copy link
Member

Choose a reason for hiding this comment

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

We also need to modify the root CMakeLists.txt to build prometheus even if WITH_METRICS_PREVIEW is not enabled?

Also need to modify test CMakeLists.txt to build the tests only if WITH_METRICS_PREVIEW is enabled.

Also can we raise a github issue to add unit-tests so that we don't miss adding them?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed cmake and added a new issue #1349 for unit tests.

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