From 38f811aa78db5eaf6421cff2e8d4aae234f6fb3f Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 6 Aug 2020 18:31:41 -0700 Subject: [PATCH 01/73] Add PrometheusExporter header, source, and test files --- .../prometheus/prometheus_exporter.h | 92 +++++++++ .../prometheus/src/prometheus_exporter.cc | 91 +++++++++ .../test/prometheus_exporter_test.cc | 193 ++++++++++++++++++ 3 files changed, 376 insertions(+) create mode 100644 exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h create mode 100644 exporters/prometheus/src/prometheus_exporter.cc create mode 100644 exporters/prometheus/test/prometheus_exporter_test.cc diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h new file mode 100644 index 0000000000..48ef6e7a5c --- /dev/null +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include + +#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 &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 &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 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 diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc new file mode 100644 index 0000000000..ee38925294 --- /dev/null +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -0,0 +1,91 @@ +#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(new PrometheusCollector); + + exposer_->RegisterCollectable(collector_); +} + +/** + * PrometheusExporter constructor with no parameters + * Used for testing only + */ +PrometheusExporter::PrometheusExporter() : is_shutdown_(false) +{ + exposer_ = nullptr; + collector_ = std::unique_ptr(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 &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 &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 diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc new file mode 100644 index 0000000000..0b541308a1 --- /dev/null +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -0,0 +1,193 @@ +#include +#include + +#include "opentelemetry/exporters/prometheus/prometheus_collector.h" +#include "opentelemetry/exporters/prometheus/prometheus_exporter.h" +#include "opentelemetry/sdk/metrics/aggregator/counter_aggregator.h" +#include "opentelemetry/version.h" + +/** + * PrometheusExporterTest is a friend class of PrometheusExporter. + * It has access to a private constructor that does not take in + * an exposer as an argument, and instead takes no arguments; this + * private constructor is only to be used here for testing + */ +class opentelemetry::exporter::prometheus::PrometheusExporterTest +{ +public: + opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() + { + return opentelemetry::exporter::prometheus::PrometheusExporter(); + } +}; + +using opentelemetry::exporter::prometheus::PrometheusCollector; +using opentelemetry::exporter::prometheus::PrometheusExporter; +using opentelemetry::exporter::prometheus::PrometheusExporterTest; +using opentelemetry::sdk::metrics::CounterAggregator; +using opentelemetry::sdk::metrics::Record; +using opentelemetry::sdk::metrics::ExportResult; + +/** + * Helper function to create a collection of records taken from + * a counter aggregator + */ +std::vector CreateRecords(int num) +{ + + std::vector records; + + for (int i = 0; i < num; i++) + { + std::string name = "record-" + std::to_string(i); + std::string description = "record-" + std::to_string(i); + std::string labels = "record-" + std::to_string(i) + "-label-1.0"; + auto aggregator = std::shared_ptr>( + new opentelemetry::sdk::metrics::CounterAggregator( + opentelemetry::metrics::InstrumentKind::Counter)); + aggregator->update(10); + aggregator->checkpoint(); + + Record r{name, description, labels, aggregator}; + records.push_back(r); + } + return records; +} + +/** + * When a PrometheusExporter is initialized, + * isShutdown should be false. + */ +TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // Asserts that the exporter is not shutdown. + ASSERT_TRUE(!exporter.IsShutdown()); +} + +/** + * The shutdown() function should set the isShutdown field to true. + */ +TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // exporter shuold not be shutdown by default + ASSERT_TRUE(!exporter.IsShutdown()); + + exporter.Shutdown(); + + // the exporter shuold be shutdown + ASSERT_TRUE(exporter.IsShutdown()); + + // shutdown function should be idempotent + exporter.Shutdown(); + ASSERT_TRUE(exporter.IsShutdown()); +} + +/** + * The Export() function should return SUCCESS = 0 + * when data is exported successfully. + */ +TEST(PrometheusExporter, ExportSuccessfully) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 2; + + std::vector records = CreateRecords(num_records); + + auto res = exporter.Export(records); + + // result should be SUCCESS = 0 + ExportResult code = ExportResult::kSuccess; + ASSERT_EQ(res, code); +} + +/** + * If the exporter is shutdown, it cannot process + * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. + */ +TEST(PrometheusExporter, ExporterIsShutdown) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 1; + + std::vector records = CreateRecords(num_records); + + exporter.Shutdown(); + + // send export request after shutdown + auto res = exporter.Export(records); + + // result code should be FAILURE_ALREADY_SHUTDOWN = 4 + ExportResult code = ExportResult::kFailure; + ASSERT_EQ(res, code); +} + +/** + * The Export() function should return + * FAILURE_FULL_COLLECTION = 1 when the collection is full, + * or when the collection is not full but does not have enough + * space to hold the batch data. + */ +TEST(PrometheusExporter, CollectionNotEnoughSpace) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 2; + + // prepare two collections of records to export, + // one close to max size and another one that, when added + // to the first, will exceed the size of the collection + + int max_collection_size = exporter.GetCollector()->GetMaxCollectionSize(); + + std::vector full_records = CreateRecords(max_collection_size - 1); + std::vector records = CreateRecords(num_records); + + // send export request to fill the + // collection in the collector + auto res = exporter.Export(full_records); + + // the result code should be SUCCESS = 0 + ExportResult code = ExportResult::kSuccess; + ASSERT_EQ(res, code); + + // send export request that does not complete + // due to not enough space in the collection + res = exporter.Export(records); + + // the result code should be FAILURE_FULL_COLLECTION = 1 + code = ExportResult::kFailureFull; + ASSERT_EQ(res, code); +} + +/** + * The Export() function should return + * FAILURE_INVALID_ARGUMENT = 3 when an empty collection + * of records is passed to the Export() function. + */ +TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // Initializes an empty colelction of records + std::vector records; + + // send export request to fill the + // collection in the collector + auto res = exporter.Export(records); + + // the result code should be FAILURE_INVALID_ARGUMENT = 3 + ExportResult code = ExportResult::kFailureInvalidArgument; + ASSERT_EQ(res, code); +} From 140efcde01008490e2e28961e708e3e5495a093f Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 7 Aug 2020 10:51:36 -0700 Subject: [PATCH 02/73] CI formatting, added copyright headers --- .../prometheus/prometheus_exporter.h | 33 ++++++++++++++----- .../prometheus/src/prometheus_exporter.cc | 20 +++++++++-- .../test/prometheus_exporter_test.cc | 18 +++++++++- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index 48ef6e7a5c..82e6be0d6a 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -1,3 +1,19 @@ +/* + * 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 @@ -36,15 +52,16 @@ class PrometheusExporter : public sdk::metrics::MetricsExporter * @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 &records) noexcept override; + sdk::metrics::ExportResult Export( + const std::vector &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. - */ + /** + * 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; /** diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index ee38925294..116b736029 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -1,3 +1,19 @@ +/* + * 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 @@ -57,9 +73,9 @@ sdk::metrics::ExportResult PrometheusExporter::Export( /** * Shuts down the exporter and does cleanup. - * Since Prometheus is a pull based interface, + * Since Prometheus is a pull based interface, * we cannot serve data remaining in the intermediate - * collection to to client an HTTP request being sent, + * collection to to client an HTTP request being sent, * so we flush the data. */ void PrometheusExporter::Shutdown() noexcept diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0b541308a1..6358d25cf7 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -1,3 +1,19 @@ +/* + * 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 #include @@ -25,8 +41,8 @@ using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; using opentelemetry::exporter::prometheus::PrometheusExporterTest; using opentelemetry::sdk::metrics::CounterAggregator; -using opentelemetry::sdk::metrics::Record; using opentelemetry::sdk::metrics::ExportResult; +using opentelemetry::sdk::metrics::Record; /** * Helper function to create a collection of records taken from From feb7126b514ef97bd155114412aac8e8fe64f29c Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Tue, 18 Aug 2020 10:53:50 -0700 Subject: [PATCH 03/73] Added build files --- exporters/prometheus/BUILD | 48 ++++++++++++++++++++++++ exporters/prometheus/CMakeLists.txt | 23 ++++++++++++ exporters/prometheus/test/CMakeLists.txt | 8 ++++ 3 files changed, 79 insertions(+) create mode 100644 exporters/prometheus/BUILD create mode 100644 exporters/prometheus/CMakeLists.txt create mode 100644 exporters/prometheus/test/CMakeLists.txt diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD new file mode 100644 index 0000000000..f3cf46100a --- /dev/null +++ b/exporters/prometheus/BUILD @@ -0,0 +1,48 @@ +# Copyright 2020, 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. + +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 = [ + "//api", + "//sdk:headers", + "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", + ], +) + +cc_test( + name = "prometheus_exporter_test", + srcs = [ + "test/prometheus_exporter_test.cc", + ], + deps = [ + ":prometheus_collector", + ":prometheus_exporter", + "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", + "@com_google_googletest//:gtest_main", + ], +) \ No newline at end of file diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt new file mode 100644 index 0000000000..3004fd0951 --- /dev/null +++ b/exporters/prometheus/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright 2020, 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_directories(include) + +find_package(prometheus-cpp CONFIG REQUIRED) +add_library( + prometheus_exporter src/prometheus_exporter.cc) + +if (BUILD_TESTING) + add_subdirectory(test) +endif () \ No newline at end of file diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt new file mode 100644 index 0000000000..bfd5e2e2f6 --- /dev/null +++ b/exporters/prometheus/test/CMakeLists.txt @@ -0,0 +1,8 @@ +foreach (testname prometheus_exporter_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + prometheus_exporter prometheus-cpp::pull) + gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST ${testname}) +endforeach () + From 5d6fc7e731553c9f9ee6c61f82b19e837f512a63 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 19 Aug 2020 11:30:50 -0700 Subject: [PATCH 04/73] modified comments to reflect removal of ReturnCodes class, removal of kFailureTimeout --- .../test/prometheus_exporter_test.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 6358d25cf7..0656a16a9b 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -105,7 +105,7 @@ TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) } /** - * The Export() function should return SUCCESS = 0 + * The Export() function should return kSuccess = 0 * when data is exported successfully. */ TEST(PrometheusExporter, ExportSuccessfully) @@ -119,14 +119,14 @@ TEST(PrometheusExporter, ExportSuccessfully) auto res = exporter.Export(records); - // result should be SUCCESS = 0 + // result should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); } /** * If the exporter is shutdown, it cannot process - * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. + * any more export requests and returns kFailure = 1. */ TEST(PrometheusExporter, ExporterIsShutdown) { @@ -142,14 +142,14 @@ TEST(PrometheusExporter, ExporterIsShutdown) // send export request after shutdown auto res = exporter.Export(records); - // result code should be FAILURE_ALREADY_SHUTDOWN = 4 + // result code should be kFailure = 1 ExportResult code = ExportResult::kFailure; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_FULL_COLLECTION = 1 when the collection is full, + * kFailureFull = 2 when the collection is full, * or when the collection is not full but does not have enough * space to hold the batch data. */ @@ -173,7 +173,7 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // collection in the collector auto res = exporter.Export(full_records); - // the result code should be SUCCESS = 0 + // the result code should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); @@ -181,14 +181,14 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // due to not enough space in the collection res = exporter.Export(records); - // the result code should be FAILURE_FULL_COLLECTION = 1 + // the result code should be kFailureFull = 2 code = ExportResult::kFailureFull; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_INVALID_ARGUMENT = 3 when an empty collection + * kFailureInvalidArgument = 3 when an empty collection * of records is passed to the Export() function. */ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) @@ -203,7 +203,7 @@ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) // collection in the collector auto res = exporter.Export(records); - // the result code should be FAILURE_INVALID_ARGUMENT = 3 + // the result code should be kFailureInvalidArgument = 3 ExportResult code = ExportResult::kFailureInvalidArgument; ASSERT_EQ(res, code); } From 32880b0075c236f9b4b6e79f909bb5fdd8483b17 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 28 Aug 2020 17:30:36 -0700 Subject: [PATCH 05/73] minor change to structure of friend class --- .../test/prometheus_exporter_test.cc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0656a16a9b..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -28,14 +28,22 @@ * an exposer as an argument, and instead takes no arguments; this * private constructor is only to be used here for testing */ -class opentelemetry::exporter::prometheus::PrometheusExporterTest +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace prometheus +{ +class PrometheusExporterTest // : public ::testing::Test { public: - opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() - { - return opentelemetry::exporter::prometheus::PrometheusExporter(); - } + PrometheusExporter GetExporter() + { + return PrometheusExporter(); + } }; +} +} +OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; From 94af70dda0e430b654180e65afccdde2397d98a9 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Mon, 31 Aug 2020 09:55:40 -0700 Subject: [PATCH 06/73] removed assignment of exposer_ to nullptr in testing constructor --- exporters/prometheus/src/prometheus_exporter.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 116b736029..32014fb98d 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -40,7 +40,6 @@ PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(fals */ PrometheusExporter::PrometheusExporter() : is_shutdown_(false) { - exposer_ = nullptr; collector_ = std::unique_ptr(new PrometheusCollector); } From 367d6a31c35171bf785f649b854abd7520550395 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 11:35:44 -0700 Subject: [PATCH 07/73] removed usage of release9.0 of Prometheus --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index e97686b0b7..5da359a719 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -49,7 +49,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cd third_party git clone https://github.com/jupp0r/prometheus-cpp cd prometheus-cpp - git checkout v0.9.0 + # git checkout v0.9.0 git submodule init git submodule update mkdir _build && cd _build From a5e2cfb8cee212e10e285155237360f5c7ff1edf Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:11:06 -0700 Subject: [PATCH 08/73] BUILD file changes --- exporters/prometheus/BUILD | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index 850ca2ed48..238c62a6de 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -26,6 +26,7 @@ cc_library( ], strip_include_prefix = "include", deps = [ + ":prometheus_collector", "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", @@ -73,10 +74,7 @@ cc_test( "test/prometheus_exporter_test.cc", ], deps = [ - ":prometheus_collector", ":prometheus_exporter", - "@com_github_jupp0r_prometheus_cpp//core", - "@com_github_jupp0r_prometheus_cpp//pull", "@com_google_googletest//:gtest_main", ], ) From 6bbb0510f3baefda2c9f0cf68ca6e84e8eb0fe86 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:43:57 -0700 Subject: [PATCH 09/73] changed Prometheus client back to 9.0 --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 5da359a719..e97686b0b7 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -49,7 +49,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cd third_party git clone https://github.com/jupp0r/prometheus-cpp cd prometheus-cpp - # git checkout v0.9.0 + git checkout v0.9.0 git submodule init git submodule update mkdir _build && cd _build From d3d44f3c362a05ec22df32e8c7ab52f8853036a7 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 14:47:34 -0700 Subject: [PATCH 10/73] removed a test for debugging --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..5476dba1e9 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - PrometheusExporterTest p; - PrometheusExporter exporter = p.GetExporter(); + // PrometheusExporterTest p; + // PrometheusExporter exporter = p.GetExporter(); - // Asserts that the exporter is not shutdown. - ASSERT_TRUE(!exporter.IsShutdown()); + // // Asserts that the exporter is not shutdown. + // ASSERT_TRUE(!exporter.IsShutdown()); } /** From 8dc5cb11ca7cf5bdb3d08b28dbeb28e7f5fea130 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 15:00:35 -0700 Subject: [PATCH 11/73] Revert "removed a test for debugging" This reverts commit d3d44f3c362a05ec22df32e8c7ab52f8853036a7. --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 5476dba1e9..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - // PrometheusExporterTest p; - // PrometheusExporter exporter = p.GetExporter(); + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); - // // Asserts that the exporter is not shutdown. - // ASSERT_TRUE(!exporter.IsShutdown()); + // Asserts that the exporter is not shutdown. + ASSERT_TRUE(!exporter.IsShutdown()); } /** From 94f78c8f604befc1bb907d5a6447d65675a9d108 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 16:16:31 -0700 Subject: [PATCH 12/73] format --- exporters/prometheus/CMakeLists.txt | 6 +++--- exporters/prometheus/test/CMakeLists.txt | 3 ++- exporters/prometheus/test/prometheus_exporter_test.cc | 11 ++++------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index fa0a3e9a8f..337527de12 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -16,9 +16,9 @@ include_directories(include) find_package(prometheus-cpp CONFIG REQUIRED) -add_library(prometheus_exporter src/prometheus_exporter.cc - 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) if(BUILD_TESTING) add_subdirectory(test) diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index 2b324608f9..a0280b4e15 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,4 +1,5 @@ -foreach(testname prometheus_exporter_test 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} diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..f2e1626603 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -33,16 +33,13 @@ namespace exporter { namespace prometheus { -class PrometheusExporterTest // : public ::testing::Test +class PrometheusExporterTest // : public ::testing::Test { public: - PrometheusExporter GetExporter() - { - return PrometheusExporter(); - } + PrometheusExporter GetExporter() { return PrometheusExporter(); } }; -} -} +} // namespace prometheus +} // namespace exporter OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; From 5938aa8fe68f24e830a3d511456cff18382cca78 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 09:49:57 -0700 Subject: [PATCH 13/73] added MAKE_EXE_LINKER_FLAGS cmake link option --- ci/do_ci.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index e97686b0b7..77611ba8ad 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -63,6 +63,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cmake -DCMAKE_BUILD_TYPE=Debug \ -DWITH_PROMETHEUS=ON \ -DCMAKE_CXX_FLAGS="-Werror" \ + MAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From dc28f848e2052d3433ff159312fa7f8e2ffa4e62 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:04:07 -0700 Subject: [PATCH 14/73] added -D to flag --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 77611ba8ad..87eea78c40 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -63,7 +63,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cmake -DCMAKE_BUILD_TYPE=Debug \ -DWITH_PROMETHEUS=ON \ -DCMAKE_CXX_FLAGS="-Werror" \ - MAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 75e23e56af9b7d6050bbe74d71db8655d30f1c14 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:14:44 -0700 Subject: [PATCH 15/73] changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 87eea78c40..4d68b22125 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -63,7 +63,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cmake -DCMAKE_BUILD_TYPE=Debug \ -DWITH_PROMETHEUS=ON \ -DCMAKE_CXX_FLAGS="-Werror" \ - -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DCMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From f9a4ddaa601474a1a1b907c5f84b684fd0c776a3 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 6 Aug 2020 18:31:41 -0700 Subject: [PATCH 16/73] Add PrometheusExporter header, source, and test files --- .../prometheus/prometheus_exporter.h | 92 +++++++++ .../prometheus/src/prometheus_exporter.cc | 91 +++++++++ .../test/prometheus_exporter_test.cc | 193 ++++++++++++++++++ 3 files changed, 376 insertions(+) create mode 100644 exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h create mode 100644 exporters/prometheus/src/prometheus_exporter.cc create mode 100644 exporters/prometheus/test/prometheus_exporter_test.cc diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h new file mode 100644 index 0000000000..48ef6e7a5c --- /dev/null +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include + +#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 &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 &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 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 diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc new file mode 100644 index 0000000000..ee38925294 --- /dev/null +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -0,0 +1,91 @@ +#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(new PrometheusCollector); + + exposer_->RegisterCollectable(collector_); +} + +/** + * PrometheusExporter constructor with no parameters + * Used for testing only + */ +PrometheusExporter::PrometheusExporter() : is_shutdown_(false) +{ + exposer_ = nullptr; + collector_ = std::unique_ptr(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 &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 &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 diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc new file mode 100644 index 0000000000..0b541308a1 --- /dev/null +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -0,0 +1,193 @@ +#include +#include + +#include "opentelemetry/exporters/prometheus/prometheus_collector.h" +#include "opentelemetry/exporters/prometheus/prometheus_exporter.h" +#include "opentelemetry/sdk/metrics/aggregator/counter_aggregator.h" +#include "opentelemetry/version.h" + +/** + * PrometheusExporterTest is a friend class of PrometheusExporter. + * It has access to a private constructor that does not take in + * an exposer as an argument, and instead takes no arguments; this + * private constructor is only to be used here for testing + */ +class opentelemetry::exporter::prometheus::PrometheusExporterTest +{ +public: + opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() + { + return opentelemetry::exporter::prometheus::PrometheusExporter(); + } +}; + +using opentelemetry::exporter::prometheus::PrometheusCollector; +using opentelemetry::exporter::prometheus::PrometheusExporter; +using opentelemetry::exporter::prometheus::PrometheusExporterTest; +using opentelemetry::sdk::metrics::CounterAggregator; +using opentelemetry::sdk::metrics::Record; +using opentelemetry::sdk::metrics::ExportResult; + +/** + * Helper function to create a collection of records taken from + * a counter aggregator + */ +std::vector CreateRecords(int num) +{ + + std::vector records; + + for (int i = 0; i < num; i++) + { + std::string name = "record-" + std::to_string(i); + std::string description = "record-" + std::to_string(i); + std::string labels = "record-" + std::to_string(i) + "-label-1.0"; + auto aggregator = std::shared_ptr>( + new opentelemetry::sdk::metrics::CounterAggregator( + opentelemetry::metrics::InstrumentKind::Counter)); + aggregator->update(10); + aggregator->checkpoint(); + + Record r{name, description, labels, aggregator}; + records.push_back(r); + } + return records; +} + +/** + * When a PrometheusExporter is initialized, + * isShutdown should be false. + */ +TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // Asserts that the exporter is not shutdown. + ASSERT_TRUE(!exporter.IsShutdown()); +} + +/** + * The shutdown() function should set the isShutdown field to true. + */ +TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // exporter shuold not be shutdown by default + ASSERT_TRUE(!exporter.IsShutdown()); + + exporter.Shutdown(); + + // the exporter shuold be shutdown + ASSERT_TRUE(exporter.IsShutdown()); + + // shutdown function should be idempotent + exporter.Shutdown(); + ASSERT_TRUE(exporter.IsShutdown()); +} + +/** + * The Export() function should return SUCCESS = 0 + * when data is exported successfully. + */ +TEST(PrometheusExporter, ExportSuccessfully) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 2; + + std::vector records = CreateRecords(num_records); + + auto res = exporter.Export(records); + + // result should be SUCCESS = 0 + ExportResult code = ExportResult::kSuccess; + ASSERT_EQ(res, code); +} + +/** + * If the exporter is shutdown, it cannot process + * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. + */ +TEST(PrometheusExporter, ExporterIsShutdown) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 1; + + std::vector records = CreateRecords(num_records); + + exporter.Shutdown(); + + // send export request after shutdown + auto res = exporter.Export(records); + + // result code should be FAILURE_ALREADY_SHUTDOWN = 4 + ExportResult code = ExportResult::kFailure; + ASSERT_EQ(res, code); +} + +/** + * The Export() function should return + * FAILURE_FULL_COLLECTION = 1 when the collection is full, + * or when the collection is not full but does not have enough + * space to hold the batch data. + */ +TEST(PrometheusExporter, CollectionNotEnoughSpace) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 2; + + // prepare two collections of records to export, + // one close to max size and another one that, when added + // to the first, will exceed the size of the collection + + int max_collection_size = exporter.GetCollector()->GetMaxCollectionSize(); + + std::vector full_records = CreateRecords(max_collection_size - 1); + std::vector records = CreateRecords(num_records); + + // send export request to fill the + // collection in the collector + auto res = exporter.Export(full_records); + + // the result code should be SUCCESS = 0 + ExportResult code = ExportResult::kSuccess; + ASSERT_EQ(res, code); + + // send export request that does not complete + // due to not enough space in the collection + res = exporter.Export(records); + + // the result code should be FAILURE_FULL_COLLECTION = 1 + code = ExportResult::kFailureFull; + ASSERT_EQ(res, code); +} + +/** + * The Export() function should return + * FAILURE_INVALID_ARGUMENT = 3 when an empty collection + * of records is passed to the Export() function. + */ +TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // Initializes an empty colelction of records + std::vector records; + + // send export request to fill the + // collection in the collector + auto res = exporter.Export(records); + + // the result code should be FAILURE_INVALID_ARGUMENT = 3 + ExportResult code = ExportResult::kFailureInvalidArgument; + ASSERT_EQ(res, code); +} From 0a5fb813232c54693806d347dd21f0691199e402 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 7 Aug 2020 10:51:36 -0700 Subject: [PATCH 17/73] CI formatting, added copyright headers --- .../prometheus/prometheus_exporter.h | 33 ++++++++++++++----- .../prometheus/src/prometheus_exporter.cc | 20 +++++++++-- .../test/prometheus_exporter_test.cc | 18 +++++++++- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index 48ef6e7a5c..82e6be0d6a 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -1,3 +1,19 @@ +/* + * 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 @@ -36,15 +52,16 @@ class PrometheusExporter : public sdk::metrics::MetricsExporter * @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 &records) noexcept override; + sdk::metrics::ExportResult Export( + const std::vector &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. - */ + /** + * 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; /** diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index ee38925294..116b736029 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -1,3 +1,19 @@ +/* + * 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 @@ -57,9 +73,9 @@ sdk::metrics::ExportResult PrometheusExporter::Export( /** * Shuts down the exporter and does cleanup. - * Since Prometheus is a pull based interface, + * Since Prometheus is a pull based interface, * we cannot serve data remaining in the intermediate - * collection to to client an HTTP request being sent, + * collection to to client an HTTP request being sent, * so we flush the data. */ void PrometheusExporter::Shutdown() noexcept diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0b541308a1..6358d25cf7 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -1,3 +1,19 @@ +/* + * 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 #include @@ -25,8 +41,8 @@ using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; using opentelemetry::exporter::prometheus::PrometheusExporterTest; using opentelemetry::sdk::metrics::CounterAggregator; -using opentelemetry::sdk::metrics::Record; using opentelemetry::sdk::metrics::ExportResult; +using opentelemetry::sdk::metrics::Record; /** * Helper function to create a collection of records taken from From 2251d49736973dd6bfc0010bb254f87437112d8d Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Tue, 18 Aug 2020 10:53:50 -0700 Subject: [PATCH 18/73] Added build files --- exporters/prometheus/BUILD | 41 ++++++------------------ exporters/prometheus/CMakeLists.txt | 34 ++++---------------- exporters/prometheus/test/CMakeLists.txt | 18 +++++------ 3 files changed, 24 insertions(+), 69 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index 01a2fe14f3..a1ca4f1b18 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -15,29 +15,13 @@ 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", - ], - strip_include_prefix = "include", - deps = [ - ":prometheus_utils", - "//api", - "//sdk:headers", - "@com_github_jupp0r_prometheus_cpp//core", - ], -) - -cc_library( - name = "prometheus_utils", - srcs = [ - "src/prometheus_exporter_utils.cc", - ], - hdrs = [ + "include/opentelemetry/exporters/prometheus/prometheus_exporter.h", "include/opentelemetry/exporters/prometheus/prometheus_exporter_utils.h", ], strip_include_prefix = "include", @@ -45,27 +29,20 @@ cc_library( "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", ], ) cc_test( - name = "prometheus_collector_test", + name = "prometheus_exporter_test", srcs = [ - "test/prometheus_collector_test.cc", + "test/prometheus_exporter_test.cc", ], deps = [ ":prometheus_collector", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "prometheus_exporter_utils_test", - srcs = [ - "test/prometheus_exporter_utils_test.cc", - ], - deps = [ - ":prometheus_utils", + ":prometheus_exporter", + "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", "@com_google_googletest//:gtest_main", ], ) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index d6b8eefea5..6a5bdf4ced 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -12,32 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -find_package(prometheus-cpp CONFIG REQUIRED) - -add_library(prometheus_exporter src/prometheus_collector.cc - src/prometheus_exporter_utils.cc) - -target_include_directories( - prometheus_exporter - PUBLIC "$" - "$") +include_directories(include) -target_link_libraries(prometheus_exporter PUBLIC opentelemetry_metrics - prometheus-cpp::core) - -install( - TARGETS prometheus_exporter - 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") +find_package(prometheus-cpp CONFIG REQUIRED) +add_library( + prometheus_exporter src/prometheus_exporter.cc) -if(BUILD_TESTING) - add_subdirectory(test) -endif() +if (BUILD_TESTING) + add_subdirectory(test) +endif () diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index 4168c6d3e4..bfd5e2e2f6 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,10 +1,8 @@ -foreach(testname prometheus_collector_test prometheus_exporter_utils_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - prometheus_exporter prometheus-cpp::pull) - gtest_add_tests( - TARGET ${testname} - TEST_PREFIX exporter. - TEST_LIST ${testname}) -endforeach() +foreach (testname prometheus_exporter_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + prometheus_exporter prometheus-cpp::pull) + gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST ${testname}) +endforeach () + From 81d1c1c21e5c918881c5526565869829c086d57e Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 19 Aug 2020 11:30:50 -0700 Subject: [PATCH 19/73] modified comments to reflect removal of ReturnCodes class, removal of kFailureTimeout --- .../test/prometheus_exporter_test.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 6358d25cf7..0656a16a9b 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -105,7 +105,7 @@ TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) } /** - * The Export() function should return SUCCESS = 0 + * The Export() function should return kSuccess = 0 * when data is exported successfully. */ TEST(PrometheusExporter, ExportSuccessfully) @@ -119,14 +119,14 @@ TEST(PrometheusExporter, ExportSuccessfully) auto res = exporter.Export(records); - // result should be SUCCESS = 0 + // result should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); } /** * If the exporter is shutdown, it cannot process - * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. + * any more export requests and returns kFailure = 1. */ TEST(PrometheusExporter, ExporterIsShutdown) { @@ -142,14 +142,14 @@ TEST(PrometheusExporter, ExporterIsShutdown) // send export request after shutdown auto res = exporter.Export(records); - // result code should be FAILURE_ALREADY_SHUTDOWN = 4 + // result code should be kFailure = 1 ExportResult code = ExportResult::kFailure; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_FULL_COLLECTION = 1 when the collection is full, + * kFailureFull = 2 when the collection is full, * or when the collection is not full but does not have enough * space to hold the batch data. */ @@ -173,7 +173,7 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // collection in the collector auto res = exporter.Export(full_records); - // the result code should be SUCCESS = 0 + // the result code should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); @@ -181,14 +181,14 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // due to not enough space in the collection res = exporter.Export(records); - // the result code should be FAILURE_FULL_COLLECTION = 1 + // the result code should be kFailureFull = 2 code = ExportResult::kFailureFull; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_INVALID_ARGUMENT = 3 when an empty collection + * kFailureInvalidArgument = 3 when an empty collection * of records is passed to the Export() function. */ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) @@ -203,7 +203,7 @@ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) // collection in the collector auto res = exporter.Export(records); - // the result code should be FAILURE_INVALID_ARGUMENT = 3 + // the result code should be kFailureInvalidArgument = 3 ExportResult code = ExportResult::kFailureInvalidArgument; ASSERT_EQ(res, code); } From b85f648730e09987f2642218acc1f1bd729cbfcc Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 28 Aug 2020 17:30:36 -0700 Subject: [PATCH 20/73] minor change to structure of friend class --- .../test/prometheus_exporter_test.cc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0656a16a9b..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -28,14 +28,22 @@ * an exposer as an argument, and instead takes no arguments; this * private constructor is only to be used here for testing */ -class opentelemetry::exporter::prometheus::PrometheusExporterTest +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace prometheus +{ +class PrometheusExporterTest // : public ::testing::Test { public: - opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() - { - return opentelemetry::exporter::prometheus::PrometheusExporter(); - } + PrometheusExporter GetExporter() + { + return PrometheusExporter(); + } }; +} +} +OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; From 2f480c055d498ce5f58e8176d6fdc90f458199e5 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Mon, 31 Aug 2020 09:55:40 -0700 Subject: [PATCH 21/73] removed assignment of exposer_ to nullptr in testing constructor --- exporters/prometheus/src/prometheus_exporter.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 116b736029..32014fb98d 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -40,7 +40,6 @@ PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(fals */ PrometheusExporter::PrometheusExporter() : is_shutdown_(false) { - exposer_ = nullptr; collector_ = std::unique_ptr(new PrometheusCollector); } From 99ca914057bdfac30b8c3a92fd6e60fe4ce84431 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 11:35:44 -0700 Subject: [PATCH 22/73] removed usage of release9.0 of Prometheus --- ci/do_ci.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 7dfb2fe983..ccbf523335 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -84,7 +84,23 @@ elif [[ "$1" == "cmake.c++20.stl.test" ]]; then make make test exit 0 -elif [[ "$1" == "cmake.legacy.test" ]]; then +elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then +# export DEBIAN_FRONTEND=noninteractive +# apt-get update +# apt-get install sudo +# apt-get install zlib1g-dev +# apt-get -y install libcurl4-openssl-dev + cd third_party + git clone https://github.com/jupp0r/prometheus-cpp + cd prometheus-cpp + # git checkout v0.9.0 + git submodule init + git submodule update + mkdir _build && cd _build + cmake .. -DBUILD_SHARED_LIBS=ON + make -j 4 + sudo make install + cd "${BUILD_DIR}" rm -rf * export BUILD_ROOT="${BUILD_DIR}" From 605e42f36c18a59d051b0cc11020ccc4bbd46e5b Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:11:06 -0700 Subject: [PATCH 23/73] BUILD file changes --- exporters/prometheus/BUILD | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index a1ca4f1b18..0b08cccbbc 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -26,6 +26,7 @@ cc_library( ], strip_include_prefix = "include", deps = [ + ":prometheus_collector", "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", @@ -39,10 +40,7 @@ cc_test( "test/prometheus_exporter_test.cc", ], deps = [ - ":prometheus_collector", ":prometheus_exporter", - "@com_github_jupp0r_prometheus_cpp//core", - "@com_github_jupp0r_prometheus_cpp//pull", "@com_google_googletest//:gtest_main", ], ) From 87e42230bbe46d402c4b83d4e59c79b5944fbd17 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:43:57 -0700 Subject: [PATCH 24/73] changed Prometheus client back to 9.0 --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index ccbf523335..b7075dca23 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -93,7 +93,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cd third_party git clone https://github.com/jupp0r/prometheus-cpp cd prometheus-cpp - # git checkout v0.9.0 + git checkout v0.9.0 git submodule init git submodule update mkdir _build && cd _build From c83723d3170471ecf7b01103655b0e417f5cbb6f Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 14:47:34 -0700 Subject: [PATCH 25/73] removed a test for debugging --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..5476dba1e9 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - PrometheusExporterTest p; - PrometheusExporter exporter = p.GetExporter(); + // PrometheusExporterTest p; + // PrometheusExporter exporter = p.GetExporter(); - // Asserts that the exporter is not shutdown. - ASSERT_TRUE(!exporter.IsShutdown()); + // // Asserts that the exporter is not shutdown. + // ASSERT_TRUE(!exporter.IsShutdown()); } /** From 4ef1e9021cebd638ea33ed1ed21c4975dd874a9f Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 15:00:35 -0700 Subject: [PATCH 26/73] Revert "removed a test for debugging" This reverts commit d3d44f3c362a05ec22df32e8c7ab52f8853036a7. --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 5476dba1e9..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - // PrometheusExporterTest p; - // PrometheusExporter exporter = p.GetExporter(); + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); - // // Asserts that the exporter is not shutdown. - // ASSERT_TRUE(!exporter.IsShutdown()); + // Asserts that the exporter is not shutdown. + ASSERT_TRUE(!exporter.IsShutdown()); } /** From 9e8dcdbabae9a58cb124844e14689ab31ba43824 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 16:16:31 -0700 Subject: [PATCH 27/73] format --- exporters/prometheus/CMakeLists.txt | 10 +++++++--- exporters/prometheus/test/CMakeLists.txt | 17 +++++++++-------- .../prometheus/test/prometheus_exporter_test.cc | 11 ++++------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 6a5bdf4ced..f6180ee838 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -18,6 +18,10 @@ find_package(prometheus-cpp CONFIG REQUIRED) add_library( prometheus_exporter src/prometheus_exporter.cc) -if (BUILD_TESTING) - add_subdirectory(test) -endif () +add_library( + prometheus_exporter src/prometheus_exporter.cc src/prometheus_collector.cc + src/prometheus_exporter_utils.cc) + +if(BUILD_TESTING) + add_subdirectory(test) +endif() diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index bfd5e2e2f6..a0280b4e15 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,8 +1,9 @@ -foreach (testname prometheus_exporter_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - prometheus_exporter prometheus-cpp::pull) - gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST ${testname}) -endforeach () - +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} + prometheus_exporter prometheus-cpp::pull) + gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST + ${testname}) +endforeach() diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..f2e1626603 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -33,16 +33,13 @@ namespace exporter { namespace prometheus { -class PrometheusExporterTest // : public ::testing::Test +class PrometheusExporterTest // : public ::testing::Test { public: - PrometheusExporter GetExporter() - { - return PrometheusExporter(); - } + PrometheusExporter GetExporter() { return PrometheusExporter(); } }; -} -} +} // namespace prometheus +} // namespace exporter OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; From ba7bb8839ed67122830a1a2f09841dd40eb64bc5 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 09:49:57 -0700 Subject: [PATCH 28/73] added MAKE_EXE_LINKER_FLAGS cmake link option --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index b7075dca23..b7f06d8e34 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - -DCMAKE_CXX_STANDARD=11 \ + MAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 82670946212b3ff4a8c2e32e07c0e5d1eaf0d6da Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:04:07 -0700 Subject: [PATCH 29/73] added -D to flag --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index b7f06d8e34..7b08e6f6bc 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - MAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 81db277931d2dbb42aa2c31c9f87111bae4d338c Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:14:44 -0700 Subject: [PATCH 30/73] changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 7b08e6f6bc..2cef0f826e 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DCMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 99572e0494e346ba4d52334112ab2f7d4457dd5b Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 6 Aug 2020 18:31:41 -0700 Subject: [PATCH 31/73] Add PrometheusExporter header, source, and test files --- .../prometheus/prometheus_exporter.h | 92 +++++++++ .../prometheus/src/prometheus_exporter.cc | 91 +++++++++ .../test/prometheus_exporter_test.cc | 193 ++++++++++++++++++ 3 files changed, 376 insertions(+) create mode 100644 exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h create mode 100644 exporters/prometheus/src/prometheus_exporter.cc create mode 100644 exporters/prometheus/test/prometheus_exporter_test.cc diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h new file mode 100644 index 0000000000..48ef6e7a5c --- /dev/null +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include + +#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 &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 &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 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 diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc new file mode 100644 index 0000000000..ee38925294 --- /dev/null +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -0,0 +1,91 @@ +#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(new PrometheusCollector); + + exposer_->RegisterCollectable(collector_); +} + +/** + * PrometheusExporter constructor with no parameters + * Used for testing only + */ +PrometheusExporter::PrometheusExporter() : is_shutdown_(false) +{ + exposer_ = nullptr; + collector_ = std::unique_ptr(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 &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 &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 diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc new file mode 100644 index 0000000000..0b541308a1 --- /dev/null +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -0,0 +1,193 @@ +#include +#include + +#include "opentelemetry/exporters/prometheus/prometheus_collector.h" +#include "opentelemetry/exporters/prometheus/prometheus_exporter.h" +#include "opentelemetry/sdk/metrics/aggregator/counter_aggregator.h" +#include "opentelemetry/version.h" + +/** + * PrometheusExporterTest is a friend class of PrometheusExporter. + * It has access to a private constructor that does not take in + * an exposer as an argument, and instead takes no arguments; this + * private constructor is only to be used here for testing + */ +class opentelemetry::exporter::prometheus::PrometheusExporterTest +{ +public: + opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() + { + return opentelemetry::exporter::prometheus::PrometheusExporter(); + } +}; + +using opentelemetry::exporter::prometheus::PrometheusCollector; +using opentelemetry::exporter::prometheus::PrometheusExporter; +using opentelemetry::exporter::prometheus::PrometheusExporterTest; +using opentelemetry::sdk::metrics::CounterAggregator; +using opentelemetry::sdk::metrics::Record; +using opentelemetry::sdk::metrics::ExportResult; + +/** + * Helper function to create a collection of records taken from + * a counter aggregator + */ +std::vector CreateRecords(int num) +{ + + std::vector records; + + for (int i = 0; i < num; i++) + { + std::string name = "record-" + std::to_string(i); + std::string description = "record-" + std::to_string(i); + std::string labels = "record-" + std::to_string(i) + "-label-1.0"; + auto aggregator = std::shared_ptr>( + new opentelemetry::sdk::metrics::CounterAggregator( + opentelemetry::metrics::InstrumentKind::Counter)); + aggregator->update(10); + aggregator->checkpoint(); + + Record r{name, description, labels, aggregator}; + records.push_back(r); + } + return records; +} + +/** + * When a PrometheusExporter is initialized, + * isShutdown should be false. + */ +TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // Asserts that the exporter is not shutdown. + ASSERT_TRUE(!exporter.IsShutdown()); +} + +/** + * The shutdown() function should set the isShutdown field to true. + */ +TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // exporter shuold not be shutdown by default + ASSERT_TRUE(!exporter.IsShutdown()); + + exporter.Shutdown(); + + // the exporter shuold be shutdown + ASSERT_TRUE(exporter.IsShutdown()); + + // shutdown function should be idempotent + exporter.Shutdown(); + ASSERT_TRUE(exporter.IsShutdown()); +} + +/** + * The Export() function should return SUCCESS = 0 + * when data is exported successfully. + */ +TEST(PrometheusExporter, ExportSuccessfully) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 2; + + std::vector records = CreateRecords(num_records); + + auto res = exporter.Export(records); + + // result should be SUCCESS = 0 + ExportResult code = ExportResult::kSuccess; + ASSERT_EQ(res, code); +} + +/** + * If the exporter is shutdown, it cannot process + * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. + */ +TEST(PrometheusExporter, ExporterIsShutdown) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 1; + + std::vector records = CreateRecords(num_records); + + exporter.Shutdown(); + + // send export request after shutdown + auto res = exporter.Export(records); + + // result code should be FAILURE_ALREADY_SHUTDOWN = 4 + ExportResult code = ExportResult::kFailure; + ASSERT_EQ(res, code); +} + +/** + * The Export() function should return + * FAILURE_FULL_COLLECTION = 1 when the collection is full, + * or when the collection is not full but does not have enough + * space to hold the batch data. + */ +TEST(PrometheusExporter, CollectionNotEnoughSpace) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + int num_records = 2; + + // prepare two collections of records to export, + // one close to max size and another one that, when added + // to the first, will exceed the size of the collection + + int max_collection_size = exporter.GetCollector()->GetMaxCollectionSize(); + + std::vector full_records = CreateRecords(max_collection_size - 1); + std::vector records = CreateRecords(num_records); + + // send export request to fill the + // collection in the collector + auto res = exporter.Export(full_records); + + // the result code should be SUCCESS = 0 + ExportResult code = ExportResult::kSuccess; + ASSERT_EQ(res, code); + + // send export request that does not complete + // due to not enough space in the collection + res = exporter.Export(records); + + // the result code should be FAILURE_FULL_COLLECTION = 1 + code = ExportResult::kFailureFull; + ASSERT_EQ(res, code); +} + +/** + * The Export() function should return + * FAILURE_INVALID_ARGUMENT = 3 when an empty collection + * of records is passed to the Export() function. + */ +TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) +{ + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); + + // Initializes an empty colelction of records + std::vector records; + + // send export request to fill the + // collection in the collector + auto res = exporter.Export(records); + + // the result code should be FAILURE_INVALID_ARGUMENT = 3 + ExportResult code = ExportResult::kFailureInvalidArgument; + ASSERT_EQ(res, code); +} From a22933ed2f579dbe0607b310e87a05eae8156c35 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 7 Aug 2020 10:51:36 -0700 Subject: [PATCH 32/73] CI formatting, added copyright headers --- .../prometheus/prometheus_exporter.h | 33 ++++++++++++++----- .../prometheus/src/prometheus_exporter.cc | 20 +++++++++-- .../test/prometheus_exporter_test.cc | 18 +++++++++- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index 48ef6e7a5c..82e6be0d6a 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -1,3 +1,19 @@ +/* + * 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 @@ -36,15 +52,16 @@ class PrometheusExporter : public sdk::metrics::MetricsExporter * @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 &records) noexcept override; + sdk::metrics::ExportResult Export( + const std::vector &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. - */ + /** + * 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; /** diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index ee38925294..116b736029 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -1,3 +1,19 @@ +/* + * 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 @@ -57,9 +73,9 @@ sdk::metrics::ExportResult PrometheusExporter::Export( /** * Shuts down the exporter and does cleanup. - * Since Prometheus is a pull based interface, + * Since Prometheus is a pull based interface, * we cannot serve data remaining in the intermediate - * collection to to client an HTTP request being sent, + * collection to to client an HTTP request being sent, * so we flush the data. */ void PrometheusExporter::Shutdown() noexcept diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0b541308a1..6358d25cf7 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -1,3 +1,19 @@ +/* + * 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 #include @@ -25,8 +41,8 @@ using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; using opentelemetry::exporter::prometheus::PrometheusExporterTest; using opentelemetry::sdk::metrics::CounterAggregator; -using opentelemetry::sdk::metrics::Record; using opentelemetry::sdk::metrics::ExportResult; +using opentelemetry::sdk::metrics::Record; /** * Helper function to create a collection of records taken from From 5055c68aed832abe86f719fbb181a544e8711360 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Tue, 18 Aug 2020 10:53:50 -0700 Subject: [PATCH 33/73] Added build files --- exporters/prometheus/BUILD | 41 ++++++------------------ exporters/prometheus/CMakeLists.txt | 34 ++++---------------- exporters/prometheus/test/CMakeLists.txt | 18 +++++------ 3 files changed, 24 insertions(+), 69 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index 01a2fe14f3..a1ca4f1b18 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -15,29 +15,13 @@ 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", - ], - strip_include_prefix = "include", - deps = [ - ":prometheus_utils", - "//api", - "//sdk:headers", - "@com_github_jupp0r_prometheus_cpp//core", - ], -) - -cc_library( - name = "prometheus_utils", - srcs = [ - "src/prometheus_exporter_utils.cc", - ], - hdrs = [ + "include/opentelemetry/exporters/prometheus/prometheus_exporter.h", "include/opentelemetry/exporters/prometheus/prometheus_exporter_utils.h", ], strip_include_prefix = "include", @@ -45,27 +29,20 @@ cc_library( "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", ], ) cc_test( - name = "prometheus_collector_test", + name = "prometheus_exporter_test", srcs = [ - "test/prometheus_collector_test.cc", + "test/prometheus_exporter_test.cc", ], deps = [ ":prometheus_collector", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "prometheus_exporter_utils_test", - srcs = [ - "test/prometheus_exporter_utils_test.cc", - ], - deps = [ - ":prometheus_utils", + ":prometheus_exporter", + "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", "@com_google_googletest//:gtest_main", ], ) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index d6b8eefea5..6a5bdf4ced 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -12,32 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -find_package(prometheus-cpp CONFIG REQUIRED) - -add_library(prometheus_exporter src/prometheus_collector.cc - src/prometheus_exporter_utils.cc) - -target_include_directories( - prometheus_exporter - PUBLIC "$" - "$") +include_directories(include) -target_link_libraries(prometheus_exporter PUBLIC opentelemetry_metrics - prometheus-cpp::core) - -install( - TARGETS prometheus_exporter - 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") +find_package(prometheus-cpp CONFIG REQUIRED) +add_library( + prometheus_exporter src/prometheus_exporter.cc) -if(BUILD_TESTING) - add_subdirectory(test) -endif() +if (BUILD_TESTING) + add_subdirectory(test) +endif () diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index 4168c6d3e4..bfd5e2e2f6 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,10 +1,8 @@ -foreach(testname prometheus_collector_test prometheus_exporter_utils_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - prometheus_exporter prometheus-cpp::pull) - gtest_add_tests( - TARGET ${testname} - TEST_PREFIX exporter. - TEST_LIST ${testname}) -endforeach() +foreach (testname prometheus_exporter_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + prometheus_exporter prometheus-cpp::pull) + gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST ${testname}) +endforeach () + From 5023d4462de47c03e725c7a4f8d0fb1872a12bd9 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 19 Aug 2020 11:30:50 -0700 Subject: [PATCH 34/73] modified comments to reflect removal of ReturnCodes class, removal of kFailureTimeout --- .../test/prometheus_exporter_test.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 6358d25cf7..0656a16a9b 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -105,7 +105,7 @@ TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) } /** - * The Export() function should return SUCCESS = 0 + * The Export() function should return kSuccess = 0 * when data is exported successfully. */ TEST(PrometheusExporter, ExportSuccessfully) @@ -119,14 +119,14 @@ TEST(PrometheusExporter, ExportSuccessfully) auto res = exporter.Export(records); - // result should be SUCCESS = 0 + // result should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); } /** * If the exporter is shutdown, it cannot process - * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. + * any more export requests and returns kFailure = 1. */ TEST(PrometheusExporter, ExporterIsShutdown) { @@ -142,14 +142,14 @@ TEST(PrometheusExporter, ExporterIsShutdown) // send export request after shutdown auto res = exporter.Export(records); - // result code should be FAILURE_ALREADY_SHUTDOWN = 4 + // result code should be kFailure = 1 ExportResult code = ExportResult::kFailure; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_FULL_COLLECTION = 1 when the collection is full, + * kFailureFull = 2 when the collection is full, * or when the collection is not full but does not have enough * space to hold the batch data. */ @@ -173,7 +173,7 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // collection in the collector auto res = exporter.Export(full_records); - // the result code should be SUCCESS = 0 + // the result code should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); @@ -181,14 +181,14 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // due to not enough space in the collection res = exporter.Export(records); - // the result code should be FAILURE_FULL_COLLECTION = 1 + // the result code should be kFailureFull = 2 code = ExportResult::kFailureFull; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_INVALID_ARGUMENT = 3 when an empty collection + * kFailureInvalidArgument = 3 when an empty collection * of records is passed to the Export() function. */ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) @@ -203,7 +203,7 @@ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) // collection in the collector auto res = exporter.Export(records); - // the result code should be FAILURE_INVALID_ARGUMENT = 3 + // the result code should be kFailureInvalidArgument = 3 ExportResult code = ExportResult::kFailureInvalidArgument; ASSERT_EQ(res, code); } From 347caa66cb403235e7cd9e87f9de2d4671fbea91 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 28 Aug 2020 17:30:36 -0700 Subject: [PATCH 35/73] minor change to structure of friend class --- .../test/prometheus_exporter_test.cc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0656a16a9b..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -28,14 +28,22 @@ * an exposer as an argument, and instead takes no arguments; this * private constructor is only to be used here for testing */ -class opentelemetry::exporter::prometheus::PrometheusExporterTest +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace prometheus +{ +class PrometheusExporterTest // : public ::testing::Test { public: - opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() - { - return opentelemetry::exporter::prometheus::PrometheusExporter(); - } + PrometheusExporter GetExporter() + { + return PrometheusExporter(); + } }; +} +} +OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; From 8e7abd3114507432364a36dcc78317e392dffb87 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Mon, 31 Aug 2020 09:55:40 -0700 Subject: [PATCH 36/73] removed assignment of exposer_ to nullptr in testing constructor --- exporters/prometheus/src/prometheus_exporter.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 116b736029..32014fb98d 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -40,7 +40,6 @@ PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(fals */ PrometheusExporter::PrometheusExporter() : is_shutdown_(false) { - exposer_ = nullptr; collector_ = std::unique_ptr(new PrometheusCollector); } From 0195f4be02705c94e3ae3cd2f71406f0c66bdf16 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 11:35:44 -0700 Subject: [PATCH 37/73] removed usage of release9.0 of Prometheus --- ci/do_ci.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 7dfb2fe983..ccbf523335 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -84,7 +84,23 @@ elif [[ "$1" == "cmake.c++20.stl.test" ]]; then make make test exit 0 -elif [[ "$1" == "cmake.legacy.test" ]]; then +elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then +# export DEBIAN_FRONTEND=noninteractive +# apt-get update +# apt-get install sudo +# apt-get install zlib1g-dev +# apt-get -y install libcurl4-openssl-dev + cd third_party + git clone https://github.com/jupp0r/prometheus-cpp + cd prometheus-cpp + # git checkout v0.9.0 + git submodule init + git submodule update + mkdir _build && cd _build + cmake .. -DBUILD_SHARED_LIBS=ON + make -j 4 + sudo make install + cd "${BUILD_DIR}" rm -rf * export BUILD_ROOT="${BUILD_DIR}" From 1cb72fe9a8d87e6f700060257c7384e444ffa17a Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:11:06 -0700 Subject: [PATCH 38/73] BUILD file changes --- exporters/prometheus/BUILD | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index a1ca4f1b18..0b08cccbbc 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -26,6 +26,7 @@ cc_library( ], strip_include_prefix = "include", deps = [ + ":prometheus_collector", "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", @@ -39,10 +40,7 @@ cc_test( "test/prometheus_exporter_test.cc", ], deps = [ - ":prometheus_collector", ":prometheus_exporter", - "@com_github_jupp0r_prometheus_cpp//core", - "@com_github_jupp0r_prometheus_cpp//pull", "@com_google_googletest//:gtest_main", ], ) From 8a463b2cba35ac8608ac02745320f35c74923afe Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:43:57 -0700 Subject: [PATCH 39/73] changed Prometheus client back to 9.0 --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index ccbf523335..b7075dca23 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -93,7 +93,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cd third_party git clone https://github.com/jupp0r/prometheus-cpp cd prometheus-cpp - # git checkout v0.9.0 + git checkout v0.9.0 git submodule init git submodule update mkdir _build && cd _build From d1fc9d400a55dfe2795d585be0f289a5c3494234 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 14:47:34 -0700 Subject: [PATCH 40/73] removed a test for debugging --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..5476dba1e9 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - PrometheusExporterTest p; - PrometheusExporter exporter = p.GetExporter(); + // PrometheusExporterTest p; + // PrometheusExporter exporter = p.GetExporter(); - // Asserts that the exporter is not shutdown. - ASSERT_TRUE(!exporter.IsShutdown()); + // // Asserts that the exporter is not shutdown. + // ASSERT_TRUE(!exporter.IsShutdown()); } /** From 673f1bb9b20194bf27557d64413d176f85f81d0a Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 15:00:35 -0700 Subject: [PATCH 41/73] Revert "removed a test for debugging" This reverts commit d3d44f3c362a05ec22df32e8c7ab52f8853036a7. --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 5476dba1e9..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - // PrometheusExporterTest p; - // PrometheusExporter exporter = p.GetExporter(); + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); - // // Asserts that the exporter is not shutdown. - // ASSERT_TRUE(!exporter.IsShutdown()); + // Asserts that the exporter is not shutdown. + ASSERT_TRUE(!exporter.IsShutdown()); } /** From 825373fd207d57ac80c547548ac5517f282467d2 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 16:16:31 -0700 Subject: [PATCH 42/73] format --- exporters/prometheus/CMakeLists.txt | 10 +++++++--- exporters/prometheus/test/CMakeLists.txt | 17 +++++++++-------- .../prometheus/test/prometheus_exporter_test.cc | 11 ++++------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 6a5bdf4ced..f6180ee838 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -18,6 +18,10 @@ find_package(prometheus-cpp CONFIG REQUIRED) add_library( prometheus_exporter src/prometheus_exporter.cc) -if (BUILD_TESTING) - add_subdirectory(test) -endif () +add_library( + prometheus_exporter src/prometheus_exporter.cc src/prometheus_collector.cc + src/prometheus_exporter_utils.cc) + +if(BUILD_TESTING) + add_subdirectory(test) +endif() diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index bfd5e2e2f6..a0280b4e15 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,8 +1,9 @@ -foreach (testname prometheus_exporter_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - prometheus_exporter prometheus-cpp::pull) - gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST ${testname}) -endforeach () - +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} + prometheus_exporter prometheus-cpp::pull) + gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST + ${testname}) +endforeach() diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..f2e1626603 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -33,16 +33,13 @@ namespace exporter { namespace prometheus { -class PrometheusExporterTest // : public ::testing::Test +class PrometheusExporterTest // : public ::testing::Test { public: - PrometheusExporter GetExporter() - { - return PrometheusExporter(); - } + PrometheusExporter GetExporter() { return PrometheusExporter(); } }; -} -} +} // namespace prometheus +} // namespace exporter OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; From b4df355128f4f213e86f147a1c2b99b124c19708 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 09:49:57 -0700 Subject: [PATCH 43/73] added MAKE_EXE_LINKER_FLAGS cmake link option --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index b7075dca23..b7f06d8e34 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - -DCMAKE_CXX_STANDARD=11 \ + MAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From f96756eba47ab6a107e7a69aa7ea9cd82001d301 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:04:07 -0700 Subject: [PATCH 44/73] added -D to flag --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index b7f06d8e34..7b08e6f6bc 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - MAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 9fccc94938a7aa2f293a477cb875dfe74638edbf Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:14:44 -0700 Subject: [PATCH 45/73] changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 7b08e6f6bc..2cef0f826e 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DCMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From d7d7010b8acea0fcb06026e045692dae252bc48e Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 6 Aug 2020 18:31:41 -0700 Subject: [PATCH 46/73] Add PrometheusExporter header, source, and test files --- .../prometheus/prometheus_exporter.h | 17 +++---- .../prometheus/src/prometheus_exporter.cc | 21 ++------ .../test/prometheus_exporter_test.cc | 51 ++++++------------- 3 files changed, 26 insertions(+), 63 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index 82e6be0d6a..dae2c44dd5 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -52,16 +52,15 @@ class PrometheusExporter : public sdk::metrics::MetricsExporter * @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 &records) noexcept override; + sdk::metrics::ExportResult Export(const std::vector &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. - */ +/** + * 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; /** diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 32014fb98d..ee38925294 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -1,19 +1,3 @@ -/* - * 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 @@ -40,6 +24,7 @@ PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(fals */ PrometheusExporter::PrometheusExporter() : is_shutdown_(false) { + exposer_ = nullptr; collector_ = std::unique_ptr(new PrometheusCollector); } @@ -72,9 +57,9 @@ sdk::metrics::ExportResult PrometheusExporter::Export( /** * Shuts down the exporter and does cleanup. - * Since Prometheus is a pull based interface, + * Since Prometheus is a pull based interface, * we cannot serve data remaining in the intermediate - * collection to to client an HTTP request being sent, + * collection to to client an HTTP request being sent, * so we flush the data. */ void PrometheusExporter::Shutdown() noexcept diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index f2e1626603..0b541308a1 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -1,19 +1,3 @@ -/* - * 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 #include @@ -28,26 +12,21 @@ * an exposer as an argument, and instead takes no arguments; this * private constructor is only to be used here for testing */ -OPENTELEMETRY_BEGIN_NAMESPACE -namespace exporter -{ -namespace prometheus -{ -class PrometheusExporterTest // : public ::testing::Test +class opentelemetry::exporter::prometheus::PrometheusExporterTest { public: - PrometheusExporter GetExporter() { return PrometheusExporter(); } + opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() + { + return opentelemetry::exporter::prometheus::PrometheusExporter(); + } }; -} // namespace prometheus -} // namespace exporter -OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; using opentelemetry::exporter::prometheus::PrometheusExporterTest; using opentelemetry::sdk::metrics::CounterAggregator; -using opentelemetry::sdk::metrics::ExportResult; using opentelemetry::sdk::metrics::Record; +using opentelemetry::sdk::metrics::ExportResult; /** * Helper function to create a collection of records taken from @@ -110,7 +89,7 @@ TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) } /** - * The Export() function should return kSuccess = 0 + * The Export() function should return SUCCESS = 0 * when data is exported successfully. */ TEST(PrometheusExporter, ExportSuccessfully) @@ -124,14 +103,14 @@ TEST(PrometheusExporter, ExportSuccessfully) auto res = exporter.Export(records); - // result should be kSuccess = 0 + // result should be SUCCESS = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); } /** * If the exporter is shutdown, it cannot process - * any more export requests and returns kFailure = 1. + * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. */ TEST(PrometheusExporter, ExporterIsShutdown) { @@ -147,14 +126,14 @@ TEST(PrometheusExporter, ExporterIsShutdown) // send export request after shutdown auto res = exporter.Export(records); - // result code should be kFailure = 1 + // result code should be FAILURE_ALREADY_SHUTDOWN = 4 ExportResult code = ExportResult::kFailure; ASSERT_EQ(res, code); } /** * The Export() function should return - * kFailureFull = 2 when the collection is full, + * FAILURE_FULL_COLLECTION = 1 when the collection is full, * or when the collection is not full but does not have enough * space to hold the batch data. */ @@ -178,7 +157,7 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // collection in the collector auto res = exporter.Export(full_records); - // the result code should be kSuccess = 0 + // the result code should be SUCCESS = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); @@ -186,14 +165,14 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // due to not enough space in the collection res = exporter.Export(records); - // the result code should be kFailureFull = 2 + // the result code should be FAILURE_FULL_COLLECTION = 1 code = ExportResult::kFailureFull; ASSERT_EQ(res, code); } /** * The Export() function should return - * kFailureInvalidArgument = 3 when an empty collection + * FAILURE_INVALID_ARGUMENT = 3 when an empty collection * of records is passed to the Export() function. */ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) @@ -208,7 +187,7 @@ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) // collection in the collector auto res = exporter.Export(records); - // the result code should be kFailureInvalidArgument = 3 + // the result code should be FAILURE_INVALID_ARGUMENT = 3 ExportResult code = ExportResult::kFailureInvalidArgument; ASSERT_EQ(res, code); } From 9085479a25097965ed02f52e5c30ff3e494c31ff Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 7 Aug 2020 10:51:36 -0700 Subject: [PATCH 47/73] CI formatting, added copyright headers --- .../prometheus/prometheus_exporter.h | 17 ++++++++-------- .../prometheus/src/prometheus_exporter.cc | 20 +++++++++++++++++-- .../test/prometheus_exporter_test.cc | 18 ++++++++++++++++- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index dae2c44dd5..82e6be0d6a 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -52,15 +52,16 @@ class PrometheusExporter : public sdk::metrics::MetricsExporter * @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 &records) noexcept override; + sdk::metrics::ExportResult Export( + const std::vector &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. - */ + /** + * 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; /** diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index ee38925294..116b736029 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -1,3 +1,19 @@ +/* + * 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 @@ -57,9 +73,9 @@ sdk::metrics::ExportResult PrometheusExporter::Export( /** * Shuts down the exporter and does cleanup. - * Since Prometheus is a pull based interface, + * Since Prometheus is a pull based interface, * we cannot serve data remaining in the intermediate - * collection to to client an HTTP request being sent, + * collection to to client an HTTP request being sent, * so we flush the data. */ void PrometheusExporter::Shutdown() noexcept diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0b541308a1..6358d25cf7 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -1,3 +1,19 @@ +/* + * 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 #include @@ -25,8 +41,8 @@ using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; using opentelemetry::exporter::prometheus::PrometheusExporterTest; using opentelemetry::sdk::metrics::CounterAggregator; -using opentelemetry::sdk::metrics::Record; using opentelemetry::sdk::metrics::ExportResult; +using opentelemetry::sdk::metrics::Record; /** * Helper function to create a collection of records taken from From e0c974aad4bc713bc846dbefd6a34487032777fd Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Tue, 18 Aug 2020 10:53:50 -0700 Subject: [PATCH 48/73] Added build files --- exporters/prometheus/BUILD | 4 +++- exporters/prometheus/CMakeLists.txt | 10 +++------- exporters/prometheus/test/CMakeLists.txt | 17 ++++++++--------- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index 0b08cccbbc..a1ca4f1b18 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -26,7 +26,6 @@ cc_library( ], strip_include_prefix = "include", deps = [ - ":prometheus_collector", "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", @@ -40,7 +39,10 @@ cc_test( "test/prometheus_exporter_test.cc", ], deps = [ + ":prometheus_collector", ":prometheus_exporter", + "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", "@com_google_googletest//:gtest_main", ], ) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index f6180ee838..6a5bdf4ced 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -18,10 +18,6 @@ find_package(prometheus-cpp CONFIG REQUIRED) add_library( prometheus_exporter src/prometheus_exporter.cc) -add_library( - prometheus_exporter src/prometheus_exporter.cc src/prometheus_collector.cc - src/prometheus_exporter_utils.cc) - -if(BUILD_TESTING) - add_subdirectory(test) -endif() +if (BUILD_TESTING) + add_subdirectory(test) +endif () diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index a0280b4e15..bfd5e2e2f6 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,9 +1,8 @@ -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} - prometheus_exporter prometheus-cpp::pull) - gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST - ${testname}) -endforeach() +foreach (testname prometheus_exporter_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + prometheus_exporter prometheus-cpp::pull) + gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST ${testname}) +endforeach () + From f1aadef9d951810c1a1d90d162fc888a7b4e1793 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 19 Aug 2020 11:30:50 -0700 Subject: [PATCH 49/73] modified comments to reflect removal of ReturnCodes class, removal of kFailureTimeout --- .../test/prometheus_exporter_test.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 6358d25cf7..0656a16a9b 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -105,7 +105,7 @@ TEST(PrometheusExporter, ShutdownSetsIsShutdownToTrue) } /** - * The Export() function should return SUCCESS = 0 + * The Export() function should return kSuccess = 0 * when data is exported successfully. */ TEST(PrometheusExporter, ExportSuccessfully) @@ -119,14 +119,14 @@ TEST(PrometheusExporter, ExportSuccessfully) auto res = exporter.Export(records); - // result should be SUCCESS = 0 + // result should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); } /** * If the exporter is shutdown, it cannot process - * any more export requests and returns FAILURE_ALREADY_SHUTDOWN = 4. + * any more export requests and returns kFailure = 1. */ TEST(PrometheusExporter, ExporterIsShutdown) { @@ -142,14 +142,14 @@ TEST(PrometheusExporter, ExporterIsShutdown) // send export request after shutdown auto res = exporter.Export(records); - // result code should be FAILURE_ALREADY_SHUTDOWN = 4 + // result code should be kFailure = 1 ExportResult code = ExportResult::kFailure; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_FULL_COLLECTION = 1 when the collection is full, + * kFailureFull = 2 when the collection is full, * or when the collection is not full but does not have enough * space to hold the batch data. */ @@ -173,7 +173,7 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // collection in the collector auto res = exporter.Export(full_records); - // the result code should be SUCCESS = 0 + // the result code should be kSuccess = 0 ExportResult code = ExportResult::kSuccess; ASSERT_EQ(res, code); @@ -181,14 +181,14 @@ TEST(PrometheusExporter, CollectionNotEnoughSpace) // due to not enough space in the collection res = exporter.Export(records); - // the result code should be FAILURE_FULL_COLLECTION = 1 + // the result code should be kFailureFull = 2 code = ExportResult::kFailureFull; ASSERT_EQ(res, code); } /** * The Export() function should return - * FAILURE_INVALID_ARGUMENT = 3 when an empty collection + * kFailureInvalidArgument = 3 when an empty collection * of records is passed to the Export() function. */ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) @@ -203,7 +203,7 @@ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) // collection in the collector auto res = exporter.Export(records); - // the result code should be FAILURE_INVALID_ARGUMENT = 3 + // the result code should be kFailureInvalidArgument = 3 ExportResult code = ExportResult::kFailureInvalidArgument; ASSERT_EQ(res, code); } From b33edf99325421ce666e34d38d0957fc5468606b Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Fri, 28 Aug 2020 17:30:36 -0700 Subject: [PATCH 50/73] minor change to structure of friend class --- .../test/prometheus_exporter_test.cc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 0656a16a9b..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -28,14 +28,22 @@ * an exposer as an argument, and instead takes no arguments; this * private constructor is only to be used here for testing */ -class opentelemetry::exporter::prometheus::PrometheusExporterTest +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace prometheus +{ +class PrometheusExporterTest // : public ::testing::Test { public: - opentelemetry::exporter::prometheus::PrometheusExporter GetExporter() - { - return opentelemetry::exporter::prometheus::PrometheusExporter(); - } + PrometheusExporter GetExporter() + { + return PrometheusExporter(); + } }; +} +} +OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; From 69de0e86768a86e82dda800a35802b2c7b67c8b6 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Mon, 31 Aug 2020 09:55:40 -0700 Subject: [PATCH 51/73] removed assignment of exposer_ to nullptr in testing constructor --- exporters/prometheus/src/prometheus_exporter.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 116b736029..32014fb98d 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -40,7 +40,6 @@ PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(fals */ PrometheusExporter::PrometheusExporter() : is_shutdown_(false) { - exposer_ = nullptr; collector_ = std::unique_ptr(new PrometheusCollector); } From fd32df625cbc39bda9fcebe6a7d04f8fba6eb5bb Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 11:35:44 -0700 Subject: [PATCH 52/73] removed usage of release9.0 of Prometheus --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 2cef0f826e..fe32d24a1d 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -93,7 +93,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cd third_party git clone https://github.com/jupp0r/prometheus-cpp cd prometheus-cpp - git checkout v0.9.0 + # git checkout v0.9.0 git submodule init git submodule update mkdir _build && cd _build From f7da6574a1319dccece5a216d1c8c1b3f15847fb Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:11:06 -0700 Subject: [PATCH 53/73] BUILD file changes --- exporters/prometheus/BUILD | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index a1ca4f1b18..0b08cccbbc 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -26,6 +26,7 @@ cc_library( ], strip_include_prefix = "include", deps = [ + ":prometheus_collector", "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", @@ -39,10 +40,7 @@ cc_test( "test/prometheus_exporter_test.cc", ], deps = [ - ":prometheus_collector", ":prometheus_exporter", - "@com_github_jupp0r_prometheus_cpp//core", - "@com_github_jupp0r_prometheus_cpp//pull", "@com_google_googletest//:gtest_main", ], ) From 2986e8cdb3ae8090735e46052dcfe12294d8172e Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 12:43:57 -0700 Subject: [PATCH 54/73] changed Prometheus client back to 9.0 --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index fe32d24a1d..2cef0f826e 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -93,7 +93,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then cd third_party git clone https://github.com/jupp0r/prometheus-cpp cd prometheus-cpp - # git checkout v0.9.0 + git checkout v0.9.0 git submodule init git submodule update mkdir _build && cd _build From bc872edc7a09be86b42d3dd6df5ccd553300af0c Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 14:47:34 -0700 Subject: [PATCH 55/73] removed a test for debugging --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..5476dba1e9 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - PrometheusExporterTest p; - PrometheusExporter exporter = p.GetExporter(); + // PrometheusExporterTest p; + // PrometheusExporter exporter = p.GetExporter(); - // Asserts that the exporter is not shutdown. - ASSERT_TRUE(!exporter.IsShutdown()); + // // Asserts that the exporter is not shutdown. + // ASSERT_TRUE(!exporter.IsShutdown()); } /** From 1859eca05f6b20f93f3307252ac82d5ba83ab830 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 15:00:35 -0700 Subject: [PATCH 56/73] Revert "removed a test for debugging" This reverts commit d3d44f3c362a05ec22df32e8c7ab52f8853036a7. --- exporters/prometheus/test/prometheus_exporter_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 5476dba1e9..9e6c638a94 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -84,11 +84,11 @@ std::vector CreateRecords(int num) */ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) { - // PrometheusExporterTest p; - // PrometheusExporter exporter = p.GetExporter(); + PrometheusExporterTest p; + PrometheusExporter exporter = p.GetExporter(); - // // Asserts that the exporter is not shutdown. - // ASSERT_TRUE(!exporter.IsShutdown()); + // Asserts that the exporter is not shutdown. + ASSERT_TRUE(!exporter.IsShutdown()); } /** From 80c811baa1f3f0ee1d50d9e26283f6f59784d15d Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Wed, 2 Sep 2020 16:16:31 -0700 Subject: [PATCH 57/73] format --- exporters/prometheus/CMakeLists.txt | 10 +++++++--- exporters/prometheus/test/CMakeLists.txt | 17 +++++++++-------- .../prometheus/test/prometheus_exporter_test.cc | 11 ++++------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 6a5bdf4ced..f6180ee838 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -18,6 +18,10 @@ find_package(prometheus-cpp CONFIG REQUIRED) add_library( prometheus_exporter src/prometheus_exporter.cc) -if (BUILD_TESTING) - add_subdirectory(test) -endif () +add_library( + prometheus_exporter src/prometheus_exporter.cc src/prometheus_collector.cc + src/prometheus_exporter_utils.cc) + +if(BUILD_TESTING) + add_subdirectory(test) +endif() diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index bfd5e2e2f6..a0280b4e15 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,8 +1,9 @@ -foreach (testname prometheus_exporter_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries( - ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - prometheus_exporter prometheus-cpp::pull) - gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST ${testname}) -endforeach () - +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} + prometheus_exporter prometheus-cpp::pull) + gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST + ${testname}) +endforeach() diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 9e6c638a94..f2e1626603 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -33,16 +33,13 @@ namespace exporter { namespace prometheus { -class PrometheusExporterTest // : public ::testing::Test +class PrometheusExporterTest // : public ::testing::Test { public: - PrometheusExporter GetExporter() - { - return PrometheusExporter(); - } + PrometheusExporter GetExporter() { return PrometheusExporter(); } }; -} -} +} // namespace prometheus +} // namespace exporter OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; From 398256d461c34a69f03030605841648120faae29 Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 09:49:57 -0700 Subject: [PATCH 58/73] added MAKE_EXE_LINKER_FLAGS cmake link option --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 2cef0f826e..b7f06d8e34 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - -DCMAKE_EXE_LINKER_FLAGS="-lpthread" \ + MAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 3d424d2b848cf9736f604dafa38299393eac4e3f Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:04:07 -0700 Subject: [PATCH 59/73] added -D to flag --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index b7f06d8e34..7b08e6f6bc 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - MAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 5e3b456b23e81649d7dfaa1f9249747ac139cbac Mon Sep 17 00:00:00 2001 From: erichsueh3 Date: Thu, 3 Sep 2020 10:14:44 -0700 Subject: [PATCH 60/73] changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS --- ci/do_ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 7b08e6f6bc..2cef0f826e 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -108,7 +108,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ - -DMAKE_EXE_LINKER_FLAGS="-lpthread" \ + -DCMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From 6ca2e10aa3a0274d7fc024d70e1cd711946da365 Mon Sep 17 00:00:00 2001 From: esigo Date: Sun, 24 Oct 2021 07:45:30 +0200 Subject: [PATCH 61/73] fix compile fail --- exporters/prometheus/BUILD | 27 ++++++++++--------- .../prometheus/prometheus_collector.h | 2 +- .../prometheus/prometheus_exporter.h | 13 +++++---- .../prometheus/src/prometheus_exporter.cc | 17 +++++++----- .../test/prometheus_exporter_test.cc | 2 +- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index 5826fc4a00..4f56f57d8f 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -20,12 +20,11 @@ cc_library( "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_exporter_utils", ":prometheus_collector", "//api", "//sdk:headers", @@ -35,18 +34,15 @@ cc_library( ) cc_library( - name = "prometheus_collector", + name = "prometheus_exporter_utils", srcs = [ - "src/prometheus_exporter.cc", + "src/prometheus_exporter_utils.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", @@ -54,14 +50,21 @@ cc_library( ], ) -cc_test( - name = "prometheus_exporter_test", +cc_library( + name = "prometheus_collector", srcs = [ - "test/prometheus_exporter_test.cc", + "src/prometheus_collector.cc", + ], + hdrs = [ + "include/opentelemetry/exporters/prometheus/prometheus_collector.h", ], + strip_include_prefix = "include", deps = [ - ":prometheus_exporter", - "@com_google_googletest//:gtest_main", + ":prometheus_exporter_utils", + "//api", + "//sdk:headers", + "@com_github_jupp0r_prometheus_cpp//core", + "@com_github_jupp0r_prometheus_cpp//pull", ], ) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h index e1e6e74f4e..b1de7dc16f 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h @@ -11,7 +11,7 @@ # include "opentelemetry/sdk/metrics/record.h" # include "prometheus/collectable.h" # include "prometheus/metric_family.h" -# include "prometheus_exporter_utils.h" +# include "opentelemetry/exporters/prometheus/prometheus_exporter_utils.h" namespace prometheus_client = ::prometheus; namespace metric_sdk = opentelemetry::sdk::metrics; diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index 82e6be0d6a..c76e77f50f 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -15,7 +15,7 @@ */ #pragma once - +#ifdef ENABLE_METRICS_PREVIEW #include #include #include @@ -24,7 +24,7 @@ #include "opentelemetry/sdk/metrics/record.h" #include "opentelemetry/version.h" #include "prometheus/exposer.h" -#include "prometheus_collector.h" +#include "opentelemetry/exporters/prometheus/prometheus_collector.h" /** * This class is an implementation of the MetricsExporter interface and @@ -33,11 +33,13 @@ */ OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdkmetrics = opentelemetry::sdk::metrics; + namespace exporter { namespace prometheus { -class PrometheusExporter : public sdk::metrics::MetricsExporter +class PrometheusExporter : public sdkmetrics::MetricsExporter { public: /** @@ -52,8 +54,8 @@ class PrometheusExporter : public sdk::metrics::MetricsExporter * @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 &records) noexcept override; + sdk::common::ExportResult Export( + const std::vector &records) noexcept override; /** * Shuts down the exporter and does cleanup. @@ -107,3 +109,4 @@ class PrometheusExporter : public sdk::metrics::MetricsExporter } // namespace prometheus } // namespace exporter OPENTELEMETRY_END_NAMESPACE +#endif // ENABLE_METRICS_PREVIEW diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 32014fb98d..79355366df 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -14,9 +14,11 @@ * limitations under the License. */ +#ifdef ENABLE_METRICS_PREVIEW #include "opentelemetry/exporters/prometheus/prometheus_exporter.h" OPENTELEMETRY_BEGIN_NAMESPACE + namespace exporter { namespace prometheus @@ -48,25 +50,25 @@ PrometheusExporter::PrometheusExporter() : is_shutdown_(false) * @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 &records) noexcept +sdk::common::ExportResult PrometheusExporter::Export( + const std::vector &records) noexcept { if (is_shutdown_) { - return sdk::metrics::ExportResult::kFailure; + return sdk::common::ExportResult::kFailure; } else if (records.empty()) { - return sdk::metrics::ExportResult::kFailureInvalidArgument; + return sdk::common::ExportResult::kFailureInvalidArgument; } - else if (collector_->GetCollection().size() + records.size() > collector_->GetMaxCollectionSize()) + else if (collector_->GetCollection().size() + records.size() > (size_t)collector_->GetMaxCollectionSize()) { - return sdk::metrics::ExportResult::kFailureFull; + return sdk::common::ExportResult::kFailureFull; } else { collector_->AddMetricData(records); - return sdk::metrics::ExportResult::kSuccess; + return sdk::common::ExportResult::kSuccess; } } @@ -104,3 +106,4 @@ bool PrometheusExporter::IsShutdown() const } // namespace prometheus } // namespace exporter OPENTELEMETRY_END_NAMESPACE +#endif // ENABLE_METRICS_PREVIEW diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index f2e1626603..c2fc79437e 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -46,7 +46,7 @@ using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; using opentelemetry::exporter::prometheus::PrometheusExporterTest; using opentelemetry::sdk::metrics::CounterAggregator; -using opentelemetry::sdk::metrics::ExportResult; +using opentelemetry::sdk::common::ExportResult; using opentelemetry::sdk::metrics::Record; /** From 032bfb9ffc45cf4f23506ea95640d046138a019e Mon Sep 17 00:00:00 2001 From: esigo Date: Sun, 24 Oct 2021 12:41:41 +0200 Subject: [PATCH 62/73] fix cmake and format --- exporters/prometheus/CMakeLists.txt | 4 ++-- .../prometheus/prometheus_collector.h | 2 +- .../prometheus/prometheus_exporter.h | 18 ++++++++--------- .../prometheus/src/prometheus_exporter.cc | 7 ++++--- exporters/prometheus/test/CMakeLists.txt | 11 +++++----- .../test/prometheus_exporter_test.cc | 20 +++++++++++-------- 6 files changed, 34 insertions(+), 28 deletions(-) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index f6180ee838..859f676cd1 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -15,12 +15,12 @@ include_directories(include) find_package(prometheus-cpp CONFIG REQUIRED) -add_library( - prometheus_exporter src/prometheus_exporter.cc) add_library( prometheus_exporter src/prometheus_exporter.cc src/prometheus_collector.cc src/prometheus_exporter_utils.cc) +target_link_libraries(prometheus_exporter prometheus-cpp::pull + prometheus-cpp::core) if(BUILD_TESTING) add_subdirectory(test) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h index b1de7dc16f..beada58a78 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_collector.h @@ -8,10 +8,10 @@ # include # include +# include "opentelemetry/exporters/prometheus/prometheus_exporter_utils.h" # include "opentelemetry/sdk/metrics/record.h" # include "prometheus/collectable.h" # include "prometheus/metric_family.h" -# include "opentelemetry/exporters/prometheus/prometheus_exporter_utils.h" namespace prometheus_client = ::prometheus; namespace metric_sdk = opentelemetry::sdk::metrics; diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index c76e77f50f..f0008b669b 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -16,15 +16,15 @@ #pragma once #ifdef ENABLE_METRICS_PREVIEW -#include -#include -#include +# include +# include +# include -#include "opentelemetry/sdk/metrics/exporter.h" -#include "opentelemetry/sdk/metrics/record.h" -#include "opentelemetry/version.h" -#include "prometheus/exposer.h" -#include "opentelemetry/exporters/prometheus/prometheus_collector.h" +# 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 @@ -109,4 +109,4 @@ class PrometheusExporter : public sdkmetrics::MetricsExporter } // namespace prometheus } // namespace exporter OPENTELEMETRY_END_NAMESPACE -#endif // ENABLE_METRICS_PREVIEW +#endif // ENABLE_METRICS_PREVIEW diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 79355366df..82cb55c5a8 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -15,7 +15,7 @@ */ #ifdef ENABLE_METRICS_PREVIEW -#include "opentelemetry/exporters/prometheus/prometheus_exporter.h" +# include "opentelemetry/exporters/prometheus/prometheus_exporter.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -61,7 +61,8 @@ sdk::common::ExportResult PrometheusExporter::Export( { return sdk::common::ExportResult::kFailureInvalidArgument; } - else if (collector_->GetCollection().size() + records.size() > (size_t)collector_->GetMaxCollectionSize()) + else if (collector_->GetCollection().size() + records.size() > + (size_t)collector_->GetMaxCollectionSize()) { return sdk::common::ExportResult::kFailureFull; } @@ -106,4 +107,4 @@ bool PrometheusExporter::IsShutdown() const } // namespace prometheus } // namespace exporter OPENTELEMETRY_END_NAMESPACE -#endif // ENABLE_METRICS_PREVIEW +#endif // ENABLE_METRICS_PREVIEW diff --git a/exporters/prometheus/test/CMakeLists.txt b/exporters/prometheus/test/CMakeLists.txt index a0280b4e15..353c218e2f 100644 --- a/exporters/prometheus/test/CMakeLists.txt +++ b/exporters/prometheus/test/CMakeLists.txt @@ -1,9 +1,10 @@ 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} - prometheus_exporter prometheus-cpp::pull) - gtest_add_tests(TARGET ${testname} TEST_PREFIX exporter. TEST_LIST - ${testname}) + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} prometheus_exporter) + gtest_add_tests( + TARGET ${testname} + TEST_PREFIX exporter. + TEST_LIST ${testname}) endforeach() diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index c2fc79437e..1d1c1dfd8a 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -14,13 +14,15 @@ * limitations under the License. */ -#include -#include +#ifdef ENABLE_METRICS_PREVIEW -#include "opentelemetry/exporters/prometheus/prometheus_collector.h" -#include "opentelemetry/exporters/prometheus/prometheus_exporter.h" -#include "opentelemetry/sdk/metrics/aggregator/counter_aggregator.h" -#include "opentelemetry/version.h" +# include +# include + +# include "opentelemetry/exporters/prometheus/prometheus_collector.h" +# include "opentelemetry/exporters/prometheus/prometheus_exporter.h" +# include "opentelemetry/sdk/metrics/aggregator/counter_aggregator.h" +# include "opentelemetry/version.h" /** * PrometheusExporterTest is a friend class of PrometheusExporter. @@ -45,8 +47,8 @@ OPENTELEMETRY_END_NAMESPACE using opentelemetry::exporter::prometheus::PrometheusCollector; using opentelemetry::exporter::prometheus::PrometheusExporter; using opentelemetry::exporter::prometheus::PrometheusExporterTest; -using opentelemetry::sdk::metrics::CounterAggregator; using opentelemetry::sdk::common::ExportResult; +using opentelemetry::sdk::metrics::CounterAggregator; using opentelemetry::sdk::metrics::Record; /** @@ -84,7 +86,7 @@ TEST(PrometheusExporter, InitializeConstructorIsNotShutdown) PrometheusExporterTest p; PrometheusExporter exporter = p.GetExporter(); - // Asserts that the exporter is not shutdown. + // // Asserts that the exporter is not shutdown. ASSERT_TRUE(!exporter.IsShutdown()); } @@ -212,3 +214,5 @@ TEST(PrometheusExporter, InvalidArgumentWhenPassedEmptyRecordCollection) ExportResult code = ExportResult::kFailureInvalidArgument; ASSERT_EQ(res, code); } + +#endif // ENABLE_METRICS_PREVIEW From 4d4f0ef7396a25a4c82dfad579cdb469a4316e65 Mon Sep 17 00:00:00 2001 From: esigo Date: Sun, 24 Oct 2021 12:52:21 +0200 Subject: [PATCH 63/73] bazel format --- exporters/prometheus/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/prometheus/BUILD b/exporters/prometheus/BUILD index 4f56f57d8f..f5ac897a3c 100644 --- a/exporters/prometheus/BUILD +++ b/exporters/prometheus/BUILD @@ -24,8 +24,8 @@ cc_library( ], strip_include_prefix = "include", deps = [ - ":prometheus_exporter_utils", ":prometheus_collector", + ":prometheus_exporter_utils", "//api", "//sdk:headers", "@com_github_jupp0r_prometheus_cpp//core", From ef20e67662648a128e50eca50fe76b90cd912fed Mon Sep 17 00:00:00 2001 From: esigo Date: Sun, 24 Oct 2021 18:39:23 +0200 Subject: [PATCH 64/73] use prometheus subdir --- CMakeLists.txt | 18 ++++++++++++++++++ ci/do_ci.sh | 1 - exporters/prometheus/CMakeLists.txt | 2 -- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6e2585631..9854452019 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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/CMakeLists.txt) + 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) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 2cef0f826e..5669d25b9b 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -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 \ diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 859f676cd1..6731fa6d79 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -14,8 +14,6 @@ include_directories(include) -find_package(prometheus-cpp CONFIG REQUIRED) - add_library( prometheus_exporter src/prometheus_exporter.cc src/prometheus_collector.cc src/prometheus_exporter_utils.cc) From 908e19d903e4d38b783bf950fd0281b54eb75b3a Mon Sep 17 00:00:00 2001 From: Oblivion Date: Wed, 27 Oct 2021 18:16:22 +0000 Subject: [PATCH 65/73] prometheus install handled in cmake --- ci/do_ci.sh | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 5669d25b9b..516514edc5 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -83,22 +83,7 @@ elif [[ "$1" == "cmake.c++20.stl.test" ]]; then make make test exit 0 -elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then -# export DEBIAN_FRONTEND=noninteractive -# apt-get update -# apt-get install sudo -# apt-get install zlib1g-dev -# apt-get -y install libcurl4-openssl-dev - cd third_party - git clone https://github.com/jupp0r/prometheus-cpp - cd prometheus-cpp - git checkout v0.9.0 - git submodule init - git submodule update - mkdir _build && cd _build - cmake .. -DBUILD_SHARED_LIBS=ON - make -j 4 - sudo make install +elif [[ "$1" == "cmake.legacy.test" ]]; then cd "${BUILD_DIR}" rm -rf * @@ -107,6 +92,7 @@ elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; then ${SRC_DIR}/tools/build-benchmark.sh cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ + -DCMAKE_CXX_STANDARD=11 \ -DCMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make From f6f5c8bc47a3d3a38a199a460448024c91da9f15 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Wed, 27 Oct 2021 18:55:55 +0000 Subject: [PATCH 66/73] update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b42529ded..74b3b6ac8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Increment the: ## [1.0.1] 2021-10-21 +* [EXPORTER] Prometheus Exporter ([#1031](https://github.com/open-telemetry/opentelemetry-cpp/pull/1031)) * [EXPORTER] Exports span attributes to ETW ([#1021](https://github.com/open-telemetry/opentelemetry-cpp/pull/1021)) * [BUILD] cmake: add FindThrift.cmake find module method for thrift ([#1020](https://github.com/open-telemetry/opentelemetry-cpp/pull/1020)) * [BUILD] Fix nlohmann_json package dependency ([#1017](https://github.com/open-telemetry/opentelemetry-cpp/pull/1017)) From bdd2a7bf592b46d1d1ca1f2282bc0b66c00d17b2 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Fri, 5 Nov 2021 18:52:06 +0000 Subject: [PATCH 67/73] comments --- .../opentelemetry/exporters/prometheus/prometheus_exporter.h | 5 ++--- exporters/prometheus/src/prometheus_exporter.cc | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index f0008b669b..98caf6e676 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -33,13 +33,12 @@ */ OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdkmetrics = opentelemetry::sdk::metrics; namespace exporter { namespace prometheus { -class PrometheusExporter : public sdkmetrics::MetricsExporter +class PrometheusExporter : public sdk::metrics::MetricsExporter { public: /** @@ -55,7 +54,7 @@ class PrometheusExporter : public sdkmetrics::MetricsExporter * @return: returns a ReturnCode detailing a success, or type of failure */ sdk::common::ExportResult Export( - const std::vector &records) noexcept override; + const std::vector &records) noexcept override; /** * Shuts down the exporter and does cleanup. diff --git a/exporters/prometheus/src/prometheus_exporter.cc b/exporters/prometheus/src/prometheus_exporter.cc index 82cb55c5a8..b64af1e904 100644 --- a/exporters/prometheus/src/prometheus_exporter.cc +++ b/exporters/prometheus/src/prometheus_exporter.cc @@ -51,7 +51,7 @@ PrometheusExporter::PrometheusExporter() : is_shutdown_(false) * @return: returns a ReturnCode detailing a success, or type of failure */ sdk::common::ExportResult PrometheusExporter::Export( - const std::vector &records) noexcept + const std::vector &records) noexcept { if (is_shutdown_) { From 5052fd568e2abd17241523eb5bd3ac8d37274e87 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 6 Nov 2021 12:29:09 +0000 Subject: [PATCH 68/73] fix ci --- exporters/prometheus/CMakeLists.txt | 14 +++++++++++--- .../exporters/prometheus/prometheus_exporter.h | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 2567c53071..8de04f7c1d 100644 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -13,17 +13,25 @@ # limitations under the License. include_directories(include) -find_package(prometheus-cpp CONFIG REQUIRED) +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 "$" "$") target_link_libraries( - prometheus_exporter_deprecated PUBLIC opentelemetry_metrics_deprecated prometheus-cpp::pull - prometheus-cpp::core) + prometheus_exporter_deprecated + PUBLIC opentelemetry_metrics_deprecated prometheus-cpp::pull + prometheus-cpp::core) +message(${prometheus-cpp}) install( TARGETS prometheus_exporter_deprecated EXPORT "${PROJECT_NAME}-target" diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index 98caf6e676..e21f176fbd 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -21,8 +21,8 @@ # include # include "opentelemetry/exporters/prometheus/prometheus_collector.h" -# include "opentelemetry/sdk/metrics/exporter.h" -# include "opentelemetry/sdk/metrics/record.h" +# include "opentelemetry/sdk/_metrics/exporter.h" +# include "opentelemetry/sdk/_metrics/record.h" # include "opentelemetry/version.h" # include "prometheus/exposer.h" From abc06b7b0e30526a4134fcb3f7e07ac9e28a33cb Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 6 Nov 2021 12:33:45 +0000 Subject: [PATCH 69/73] remove debug message --- exporters/prometheus/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) mode change 100644 => 100755 exporters/prometheus/CMakeLists.txt diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt old mode 100644 new mode 100755 index 8de04f7c1d..9531cf2c7b --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -31,7 +31,6 @@ target_link_libraries( PUBLIC opentelemetry_metrics_deprecated prometheus-cpp::pull prometheus-cpp::core) -message(${prometheus-cpp}) install( TARGETS prometheus_exporter_deprecated EXPORT "${PROJECT_NAME}-target" From d1c226e0d3f5bae52a975a8c9185dc44d57c66c3 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 6 Nov 2021 12:49:25 +0000 Subject: [PATCH 70/73] fix bazel ci and comments --- .../exporters/prometheus/prometheus_exporter.h | 17 ++--------------- .../prometheus/test/prometheus_exporter_test.cc | 2 +- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h index e21f176fbd..7c1f99a757 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h @@ -1,18 +1,5 @@ -/* - * 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. - */ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 #pragma once #ifdef ENABLE_METRICS_PREVIEW diff --git a/exporters/prometheus/test/prometheus_exporter_test.cc b/exporters/prometheus/test/prometheus_exporter_test.cc index 1d1c1dfd8a..564880c26d 100644 --- a/exporters/prometheus/test/prometheus_exporter_test.cc +++ b/exporters/prometheus/test/prometheus_exporter_test.cc @@ -21,7 +21,7 @@ # include "opentelemetry/exporters/prometheus/prometheus_collector.h" # include "opentelemetry/exporters/prometheus/prometheus_exporter.h" -# include "opentelemetry/sdk/metrics/aggregator/counter_aggregator.h" +# include "opentelemetry/sdk/_metrics/aggregator/counter_aggregator.h" # include "opentelemetry/version.h" /** From a6f33155d1ba9911b023bc4df2e65f41345908c9 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 6 Nov 2021 14:35:37 +0000 Subject: [PATCH 71/73] fix cmake ci --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9854452019..1bbd0cb77c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -303,8 +303,8 @@ 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/CMakeLists.txt) - add_subdirectory(third_party/prometheus-cpp) + if(EXISTS ${PROJECT_SOURCE_DIR}/third_party/prometheus-cpp/.git) + include(third_party/prometheus-cpp) else() message( FATAL_ERROR From 6c0e4d33e91fce5868dd27caede28696ca270741 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 6 Nov 2021 14:40:44 +0000 Subject: [PATCH 72/73] comments --- CHANGELOG.md | 2 +- ci/do_ci.sh | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 487333d682..216189178d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,11 +15,11 @@ Increment the: ## [Unreleased] +* [EXPORTER] Prometheus Exporter ([#1031](https://github.com/open-telemetry/opentelemetry-cpp/pull/1031)) * [EXPORTER] Add OTLP/HTTP Log Exporter ([#1030](https://github.com/open-telemetry/opentelemetry-cpp/pull/1030)) ## [1.0.1] 2021-10-21 -* [EXPORTER] Prometheus Exporter ([#1031](https://github.com/open-telemetry/opentelemetry-cpp/pull/1031)) * [EXPORTER] Exports span attributes to ETW ([#1021](https://github.com/open-telemetry/opentelemetry-cpp/pull/1021)) * [BUILD] cmake: add FindThrift.cmake find module method for thrift ([#1020](https://github.com/open-telemetry/opentelemetry-cpp/pull/1020)) * [BUILD] Fix nlohmann_json package dependency ([#1017](https://github.com/open-telemetry/opentelemetry-cpp/pull/1017)) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 516514edc5..b70d565b0d 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -84,7 +84,6 @@ elif [[ "$1" == "cmake.c++20.stl.test" ]]; then make test exit 0 elif [[ "$1" == "cmake.legacy.test" ]]; then - cd "${BUILD_DIR}" rm -rf * export BUILD_ROOT="${BUILD_DIR}" @@ -93,7 +92,6 @@ elif [[ "$1" == "cmake.legacy.test" ]]; then cmake -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-Werror" \ -DCMAKE_CXX_STANDARD=11 \ - -DCMAKE_EXE_LINKER_FLAGS="-lpthread" \ "${SRC_DIR}" make make test From c93caf7a3bcdd1e9443c6548c02b3e1238c6142e Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 6 Nov 2021 15:57:46 +0000 Subject: [PATCH 73/73] install missing targets --- CMakeLists.txt | 2 +- exporters/prometheus/CMakeLists.txt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bbd0cb77c..8844a66624 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -304,7 +304,7 @@ if(WITH_PROMETHEUS) if(NOT prometheus-cpp_FOUND) message("Trying to use local prometheus-cpp from submodule") if(EXISTS ${PROJECT_SOURCE_DIR}/third_party/prometheus-cpp/.git) - include(third_party/prometheus-cpp) + add_subdirectory(third_party/prometheus-cpp) else() message( FATAL_ERROR diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 9531cf2c7b..3ef8e035ae 100755 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -26,13 +26,13 @@ target_include_directories( PUBLIC "$" "$") +set(PROMETHEUS_EXPORTER_TARGETS prometheus_exporter_deprecated pull core) target_link_libraries( 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}