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

metrics: add prometheus exporter #1

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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: 2 additions & 0 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ elif [[ "$1" == "cmake.legacy.test" ]]; then
${SRC_DIR}/tools/build-gtest.sh
${SRC_DIR}/tools/build-benchmark.sh
cmake -DCMAKE_BUILD_TYPE=Debug \
-DWITH_PROMETHEUS=ON \
-DCMAKE_CXX_FLAGS="-Werror" \
-DCMAKE_CXX_STANDARD=11 \
-DCMAKE_EXE_LINKER_FLAGS="-lpthread" \
"${SRC_DIR}"
make
make test
Expand Down
31 changes: 31 additions & 0 deletions exporters/prometheus/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@

package(default_visibility = ["//visibility:public"])

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

cc_library(
name = "prometheus_collector",
srcs = [
Expand Down Expand Up @@ -48,6 +68,17 @@ cc_library(
],
)

cc_test(
name = "prometheus_exporter_test",
srcs = [
"test/prometheus_exporter_test.cc",
],
deps = [
":prometheus_exporter",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "prometheus_collector_test",
srcs = [
Expand Down
5 changes: 3 additions & 2 deletions exporters/prometheus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

find_package(prometheus-cpp CONFIG REQUIRED)

add_library(prometheus_exporter src/prometheus_collector.cc
src/prometheus_exporter_utils.cc)
add_library(
prometheus_exporter src/prometheus_exporter.cc src/prometheus_collector.cc
src/prometheus_exporter_utils.cc)

target_include_directories(
prometheus_exporter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.
*/

#pragma once

#include <memory>
#include <string>
#include <vector>

#include "opentelemetry/sdk/metrics/exporter.h"
#include "opentelemetry/sdk/metrics/record.h"
#include "opentelemetry/version.h"
#include "prometheus/exposer.h"
#include "prometheus_collector.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::metrics::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
106 changes: 106 additions & 0 deletions exporters/prometheus/src/prometheus_exporter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.
*/

#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::metrics::ExportResult PrometheusExporter::Export(
const std::vector<sdk::metrics::Record> &records) noexcept
{
if (is_shutdown_)
{
return sdk::metrics::ExportResult::kFailure;
}
else if (records.empty())
{
return sdk::metrics::ExportResult::kFailureInvalidArgument;
}
else if (collector_->GetCollection().size() + records.size() > collector_->GetMaxCollectionSize())
{
return sdk::metrics::ExportResult::kFailureFull;
}
else
{
collector_->AddMetricData(records);
return sdk::metrics::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
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