-
Notifications
You must be signed in to change notification settings - Fork 410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Prometheus Exporter: Step 1 #280
Changes from all commits
f171647
75df0a8
9c74f41
91f382a
b826383
d98baf5
deb2d4b
c099a76
215a938
eee7fac
eb4957e
c1e1da3
942714e
3a70d63
a13bf69
5a21c58
7f8f2f5
26c735a
019e3f5
c083771
31d5992
140c73d
5e7453f
0383028
645da39
22237b4
1d2930e
09eb665
6e525d9
e5f63c9
17639bc
7c6b308
eaceeb5
0d7b02c
fda7664
6569d3e
781277d
c370218
dc4c1fe
3f81dee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "third_party/prometheus-cpp"] | ||
path = third_party/prometheus-cpp | ||
url = https://github.com/jupp0r/prometheus-cpp.git |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,33 @@ elif [[ "$1" == "cmake.exporter.otprotocol.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 | ||
|
||
cd "${BUILD_DIR}" | ||
rm -rf * | ||
|
||
cmake -DCMAKE_BUILD_TYPE=Debug \ | ||
-DWITH_PROMETHEUS=ON \ | ||
-DCMAKE_CXX_FLAGS="-Werror" \ | ||
"${SRC_DIR}" | ||
make | ||
make test | ||
exit 0 | ||
elif [[ "$1" == "cmake.test_example_plugin" ]]; then | ||
# Build the plugin | ||
cd "${BUILD_DIR}" | ||
|
@@ -83,8 +110,10 @@ elif [[ "$1" == "bazel.legacy.test" ]]; then | |
bazel test $BAZEL_TEST_OPTIONS -- //... -//exporters/otlp/... | ||
exit 0 | ||
elif [[ "$1" == "bazel.noexcept" ]]; then | ||
bazel build --copt=-fno-exceptions $BAZEL_OPTIONS //... | ||
bazel test --copt=-fno-exceptions $BAZEL_TEST_OPTIONS //... | ||
# there are some exceptions and error handling code from the Prometheus Client | ||
# that make this test always fail. ignore Prometheus exporter in the noexcept here. | ||
bazel build --copt=-fno-exceptions $BAZEL_OPTIONS -- //... -//exporters/prometheus/... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we want to take exception here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we run the test, we found some error handling and exceptions from the Prometheus client, which made this test always fail. We discussed this problem with Johannes last week, and he suggested us to consider excluding the Prometheus component in the noexcept test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment here. |
||
bazel test --copt=-fno-exceptions $BAZEL_TEST_OPTIONS -- //... -//exporters/prometheus/... | ||
exit 0 | ||
elif [[ "$1" == "bazel.asan" ]]; then | ||
bazel test --config=asan $BAZEL_TEST_OPTIONS //... | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
if(WITH_OTPROTOCOL) | ||
add_subdirectory(otlp) | ||
endif() | ||
|
||
add_subdirectory(ostream) | ||
|
||
if(WITH_PROMETHEUS) | ||
add_subdirectory(prometheus) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 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_utils", | ||
srcs = [ | ||
"src/prometheus_exporter_utils.cc", | ||
], | ||
hdrs = [ | ||
"include/opentelemetry/exporters/prometheus/prometheus_exporter_utils.h", | ||
], | ||
strip_include_prefix = "include", | ||
deps = [ | ||
"//api", | ||
"//sdk:headers", | ||
"@com_github_jupp0r_prometheus_cpp//core", | ||
], | ||
) | ||
|
||
cc_test( | ||
name = "prometheus_exporter_utils_test", | ||
srcs = [ | ||
"test/prometheus_exporter_utils_test.cc", | ||
], | ||
deps = [ | ||
":prometheus_utils", | ||
"@com_google_googletest//:gtest_main", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 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_utils.cc) | ||
|
||
if(BUILD_TESTING) | ||
add_subdirectory(test) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* 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 <string> | ||
#include <vector> | ||
|
||
#include "opentelemetry/sdk/metrics/record.h" | ||
#include "prometheus/metric_family.h" | ||
|
||
namespace metric_sdk = opentelemetry::sdk::metrics; | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace exporter | ||
{ | ||
namespace prometheus | ||
{ | ||
/** | ||
* The Prometheus Utils contains utility functions for Prometheus Exporter | ||
*/ | ||
class PrometheusExporterUtils | ||
{ | ||
public: | ||
/** | ||
* Helper function to convert OpenTelemetry metrics data collection | ||
* to Prometheus metrics data collection | ||
* | ||
* @param records a collection of metrics in OpenTelemetry | ||
* @return a collection of translated metrics that is acceptable by Prometheus | ||
*/ | ||
static std::vector<::prometheus::MetricFamily> TranslateToPrometheus( | ||
const std::vector<metric_sdk::Record> &records); | ||
|
||
private: | ||
/** | ||
* Set value to metric family according to record | ||
*/ | ||
static void SetMetricFamily(metric_sdk::Record &record, | ||
::prometheus::MetricFamily *metric_family); | ||
|
||
/** | ||
* Sanitize the given metric name or label according to Prometheus rule. | ||
* | ||
* This function is needed because names in OpenTelemetry can contain | ||
* alphanumeric characters, '_', '.', and '-', whereas in Prometheus the | ||
* name should only contain alphanumeric characters and '_'. | ||
*/ | ||
static std::string SanitizeNames(std::string name); | ||
|
||
/** | ||
* Set value to metric family for different aggregator | ||
*/ | ||
template <typename T> | ||
static void SetMetricFamilyByAggregator(std::shared_ptr<metric_sdk::Aggregator<T>> aggregator, | ||
std::string labels_str, | ||
::prometheus::MetricFamily *metric_family); | ||
|
||
/** | ||
* Translate the OTel metric type to Prometheus metric type | ||
*/ | ||
static ::prometheus::MetricType TranslateType(metric_sdk::AggregatorKind kind); | ||
|
||
/** | ||
* Set metric data for: | ||
* Counter => Prometheus Counter | ||
* Gauge => Prometheus Gauge | ||
*/ | ||
template <typename T> | ||
static void SetData(std::vector<T> values, | ||
const std::string &labels, | ||
::prometheus::MetricType type, | ||
std::chrono::nanoseconds time, | ||
::prometheus::MetricFamily *metric_family); | ||
|
||
/** | ||
* Set metric data for: | ||
* Histogram => Prometheus Histogram | ||
*/ | ||
template <typename T> | ||
static void SetData(std::vector<T> values, | ||
const std::vector<double> &boundaries, | ||
const std::vector<int> &counts, | ||
const std::string &labels, | ||
std::chrono::nanoseconds time, | ||
::prometheus::MetricFamily *metric_family); | ||
|
||
/** | ||
* Set metric data for: | ||
* MinMaxSumCount => Prometheus Gauge | ||
* Use Average (sum / count) as the gauge metric | ||
*/ | ||
static void SetData(double value, | ||
const std::string &labels, | ||
std::chrono::nanoseconds time, | ||
::prometheus::MetricFamily *metric_family); | ||
|
||
/** | ||
* Set metric data for: | ||
* Exact => Prometheus Summary | ||
* Sketch => Prometheus Summary | ||
*/ | ||
template <typename T> | ||
static void SetData(std::vector<T> values, | ||
metric_sdk::AggregatorKind kind, | ||
const std::vector<T> &quantiles, | ||
const std::string &labels, | ||
std::chrono::nanoseconds time, | ||
::prometheus::MetricFamily *metric_family, | ||
bool do_quantile, | ||
std::vector<double> quantile_points); | ||
|
||
/** | ||
* Set time and labels to metric data | ||
*/ | ||
static void SetMetricBasic(::prometheus::ClientMetric &metric, | ||
std::chrono::nanoseconds time, | ||
const std::string &labels); | ||
|
||
/** | ||
* Parse a string of labels (key:value) into a vector of pairs | ||
* {,} | ||
* {l1:v1,l2:v2,...,} | ||
*/ | ||
static std::vector<std::pair<std::string, std::string>> ParseLabel(std::string labels); | ||
|
||
/** | ||
* Build a quantiles vector from aggregator | ||
*/ | ||
template <typename T> | ||
static std::vector<T> GetQuantilesVector(std::shared_ptr<metric_sdk::Aggregator<T>> aggregator, | ||
const std::vector<double> &quantile_points); | ||
|
||
/** | ||
* Handle Counter and Gauge. | ||
*/ | ||
template <typename T> | ||
static void SetValue(std::vector<T> values, | ||
::prometheus::MetricType type, | ||
::prometheus::ClientMetric *metric); | ||
|
||
/** | ||
* Handle Gauge from MinMaxSumCount | ||
*/ | ||
static void SetValue(double value, ::prometheus::ClientMetric *metric); | ||
|
||
/** | ||
* Handle Histogram | ||
*/ | ||
template <typename T> | ||
static void SetValue(std::vector<T> values, | ||
std::vector<double> boundaries, | ||
std::vector<int> counts, | ||
::prometheus::ClientMetric *metric); | ||
|
||
/** | ||
* Handle Exact and Sketch | ||
*/ | ||
template <typename T> | ||
static void SetValue(std::vector<T> values, | ||
metric_sdk::AggregatorKind kind, | ||
std::vector<T> quantiles, | ||
::prometheus::ClientMetric *metric, | ||
bool do_quantile, | ||
const std::vector<double> &quantile_points); | ||
}; | ||
} // namespace prometheus | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider extract this logic to a separate file under the Prometheus exporter folder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, correct me if I misunderstand something:
elif [[ "$1" == "cmake.exporter.prometheus.test" ]];
block herecd third_party
tosudo make install
) to a separate script inexporters/prometheus
. For example,exporters/prometheus/install_prometheus_client.sh
install_prometheus_client.sh
here