Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prometheus Exporter: Step 1 #280

Merged
merged 40 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f171647
add Prometheus Exporter Utils build files, headers, implementations, …
CunjunWang Aug 17, 2020
75df0a8
remove helper of validating name because it's guaranteed in sdk
CunjunWang Aug 18, 2020
9c74f41
format code
CunjunWang Aug 18, 2020
91f382a
delete useless try catch
CunjunWang Aug 18, 2020
b826383
delete useless try catch
CunjunWang Aug 18, 2020
d98baf5
try to add promethues client to ci
CunjunWang Aug 18, 2020
deb2d4b
try to add promethues client to ci
CunjunWang Aug 18, 2020
c099a76
try to add promethues client to ci
CunjunWang Aug 18, 2020
215a938
try to add promethues client to ci
CunjunWang Aug 18, 2020
eee7fac
modify prometheus client build
CunjunWang Aug 18, 2020
eb4957e
extract prometheus client instalation to a separate script
CunjunWang Aug 18, 2020
c1e1da3
change script to use prometheus client 0.9.0 release
CunjunWang Aug 18, 2020
942714e
try to add prom-client git submodule; try to add script to install on…
CunjunWang Aug 19, 2020
3a70d63
Merge branch 'master' into prometheus-exporter-utils
reyang Aug 19, 2020
a13bf69
add prometheus-client as subdirectory in main cmake file
CunjunWang Aug 19, 2020
5a21c58
merge with remote
CunjunWang Aug 19, 2020
7f8f2f5
Removed submodule
CunjunWang Aug 19, 2020
26c735a
try to install prometheus client with vcpkg
CunjunWang Aug 20, 2020
019e3f5
Merge branch 'master' into prometheus-exporter-utils
reyang Aug 21, 2020
c083771
add windows prometheus client installation
CunjunWang Aug 21, 2020
31d5992
Merge branch 'prometheus-exporter-utils' of https://github.com/Cunjun…
CunjunWang Aug 21, 2020
140c73d
modify prometheus client command
CunjunWang Aug 24, 2020
5e7453f
stick prometheus client to v0.9.0 and added sha256
CunjunWang Aug 24, 2020
0383028
fix problems from review comments
CunjunWang Aug 24, 2020
645da39
change CMakefile to conditional build prometheus
CunjunWang Aug 24, 2020
22237b4
modify CI bazel config so that prometheus is excluded for noexcept test
CunjunWang Aug 24, 2020
1d2930e
try to fix 404 not found problem in setup cmake
CunjunWang Aug 24, 2020
09eb665
format code
CunjunWang Aug 24, 2020
6e525d9
restore setup cmake file
CunjunWang Aug 24, 2020
e5f63c9
add prometheus exporter tests to ci workflow
CunjunWang Aug 24, 2020
17639bc
install dependencies in setup_cmake
CunjunWang Aug 24, 2020
7c6b308
try to figure out install permission problem
CunjunWang Aug 24, 2020
eaceeb5
Merge branch 'master' into prometheus-exporter-utils
reyang Aug 24, 2020
0d7b02c
Merge branch 'master' into prometheus-exporter-utils
reyang Aug 25, 2020
fda7664
remove printing for sanitize names
CunjunWang Aug 25, 2020
6569d3e
add comment for ignoring Prometheus Exporter in noexcept test
CunjunWang Aug 25, 2020
781277d
format code
CunjunWang Aug 25, 2020
c370218
format code
CunjunWang Aug 25, 2020
dc4c1fe
Merge branch 'master' into prometheus-exporter-utils
reyang Aug 26, 2020
3f81dee
Merge branch 'master' into prometheus-exporter-utils
reyang Aug 26, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
sudo ./ci/setup_ci_environment.sh
- name: run tests
run: ./ci/do_ci.sh cmake.test
- name: run prometheus exporter tests
run: ./ci/do_ci.sh cmake.exporter.prometheus.test

cmake_test_cxx20:
name: CMake C++20 test
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ endif()
option(WITH_OTPROTOCOL
"Whether to include the OpenTelemetry Protocol in the SDK" OFF)

option(WITH_PROMETHEUS "Whether to include the Prometheus Client in the SDK"
OFF)

set(WITH_PROTOBUF OFF)
if(WITH_OTPROTOCOL)
set(WITH_PROTOBUF ON)
Expand Down
14 changes: 14 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,17 @@ http_archive(
"https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip",
],
)

# C++ Prometheus Client library.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

http_archive(
name = "com_github_jupp0r_prometheus_cpp",
sha256 = "85ad6fea0f0dcb413104366b7d6109acdb015aff8767945511c5cad8202a28a6",
strip_prefix = "prometheus-cpp-0.9.0",
urls = ["https://github.com/jupp0r/prometheus-cpp/archive/v0.9.0.tar.gz"],
)

load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories")

prometheus_cpp_repositories()
33 changes: 31 additions & 2 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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.

Copy link
Contributor Author

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:

  1. keep the elif [[ "$1" == "cmake.exporter.prometheus.test" ]]; block here
  2. move the scripts of installing the Prometheus client (from cd third_party to sudo make install) to a separate script in exporters/prometheus. For example, exporters/prometheus/install_prometheus_client.sh
  3. run install_prometheus_client.sh here

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}"
Expand Down Expand Up @@ -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/...
Copy link
Member

Choose a reason for hiding this comment

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

Why would we want to take exception here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 //...
Expand Down
7 changes: 6 additions & 1 deletion ci/setup_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

set -e

export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install --no-install-recommends --no-install-suggests -y \
cmake \
libbenchmark-dev \
libgtest-dev
libgtest-dev \
zlib1g-dev \
sudo \
libcurl4-openssl-dev

# Follows these instructions for setting up gtest
# https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
Expand Down
5 changes: 5 additions & 0 deletions exporters/CMakeLists.txt
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()
42 changes: 42 additions & 0 deletions exporters/prometheus/BUILD
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",
],
)
22 changes: 22 additions & 0 deletions exporters/prometheus/CMakeLists.txt
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
Loading