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 22 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
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
13 changes: 13 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,16 @@ 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",
strip_prefix = "prometheus-cpp-master",
Copy link
Contributor

Choose a reason for hiding this comment

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

Please pin to a version (and add sha256), for consistency with other deps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pin to v0.9.0, and sha256 added

urls = ["https://github.com/jupp0r/prometheus-cpp/archive/master.zip"],
)

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

prometheus_cpp_repositories()
4 changes: 3 additions & 1 deletion ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ ADD install_gcc48.sh /setup-ci
ADD install_bazelisk.sh /setup-ci
ADD install_protobuf.sh /setup-ci
ADD install_format_tools.sh /setup-ci
ADD install_prometheus_client.sh /setup-ci

RUN /setup-ci/setup_ci_environment.sh \
&& /setup-ci/setup_cmake.sh \
&& /setup-ci/install_gcc48.sh \
&& /setup-ci/install_bazelisk.sh \
&& /setup-ci/install_protobuf.sh \
&& /setup-ci/install_format_tools.sh
&& /setup-ci/install_format_tools.sh \
&& /setup-ci/install_prometheus_client.sh
10 changes: 10 additions & 0 deletions ci/install_prometheus_client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

git clone https://github.com/jupp0r/prometheus-cpp.git
cd prometheus-cpp
git checkout v0.9.0
git submodule init
git submodule update
mkdir _build && cd _build
cmake .. -DBUILD_SHARED_LIBS=ON
make && make install
20 changes: 20 additions & 0 deletions ci/ports/prometheus-cpp/CONTROL
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Source: prometheus-cpp
Version: 0.9.0
Description: Prometheus Client Library for Modern C++
Default-Features: compression, pull

Feature: compression
Build-Depends: zlib
Description: Enable zlib compression

Feature: tests
Build-Depends: gtest
Description: Additional testing support

Feature: pull
Build-Depends: civetweb
Description: Support for regular pull mode

Feature: push
Build-Depends: curl
Description: Support push gateway
43 changes: 43 additions & 0 deletions ci/ports/prometheus-cpp/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
include(vcpkg_common_functions)

vcpkg_check_linkage(ONLY_STATIC_LIBRARY)

vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO jupp0r/prometheus-cpp
REF v0.9.0
SHA512 d9d5fbbd8c8aad5dd6a5e872275324d689a0c57199e4158d74e13ea62b286fa71dee01bb4197b906b79792bf1ca4e67a46b5c04621d7070241ac32876f6de891
HEAD_REF master
)

macro(feature FEATURENAME OPTIONNAME)
if ("${FEATURENAME}" IN_LIST FEATURES)
list(APPEND FEATURE_OPTIONS -D${OPTIONNAME}=TRUE)
else ()
list(APPEND FEATURE_OPTIONS -D${OPTIONNAME}=FALSE)
endif ()
endmacro()

feature(compression ENABLE_COMPRESSION)
feature(pull ENABLE_PULL)
feature(push ENABLE_PUSH)
feature(tests ENABLE_TESTING)

vcpkg_configure_cmake(
SOURCE_PATH ${SOURCE_PATH}
PREFER_NINJA
OPTIONS
-DUSE_THIRDPARTY_LIBRARIES=OFF # use vcpkg packages
${FEATURE_OPTIONS}
)

vcpkg_install_cmake()

vcpkg_copy_pdbs()

vcpkg_fixup_cmake_targets(CONFIG_PATH lib/cmake/prometheus-cpp)

file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/debug/include)

# Handle copyright
configure_file(${SOURCE_PATH}/LICENSE ${CURRENT_PACKAGES_DIR}/share/prometheus-cpp/copyright COPYONLY)
3 changes: 2 additions & 1 deletion ci/setup_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ set -e
apt-get install --no-install-recommends --no-install-suggests -y \
cmake \
libbenchmark-dev \
libgtest-dev
libgtest-dev \
libcurl4-openssl-dev

# Follows these instructions for setting up gtest
# https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
Expand Down
2 changes: 2 additions & 0 deletions ci/setup_windows_ci_environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ $VCPKG_DIR=(Get-Item -Path ".\").FullName
./vcpkg install --overlay-ports="$PSScriptRoot\ports" benchmark:x64-windows

./vcpkg install gtest:x64-windows

./vcpkg install --overlay-ports="$PSScriptRoot\ports" prometheus-cpp:x64-windows
Pop-Location
1 change: 1 addition & 0 deletions exporters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ if(WITH_OTPROTOCOL)
add_subdirectory(otlp)
endif()
add_subdirectory(ostream)
add_subdirectory(prometheus)
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,183 @@
/*
* 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 prometheus_client = ::prometheus;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to limit the scope of these? I'm worried about having them in a header file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed it from header, and put it in implementation files

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_client::MetricFamily> TranslateToPrometheus(
std::vector<metric_sdk::Record> &records);

private:
/**
* Set value to metric family according to record
*/
static void SetMetricFamily(metric_sdk::Record &record,
prometheus_client::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_client::MetricFamily *metric_family);

/**
* Translate the OTel metric type to Prometheus metric type
*/
static prometheus_client::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_client::MetricType type,
std::chrono::nanoseconds time,
prometheus_client::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_client::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_client::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_client::MetricFamily *metric_family,
bool do_quantile,
std::vector<double> quantile_points);

/**
* Set time and labels to metric data
*/
static void SetMetricBasic(prometheus_client::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_client::MetricType type,
prometheus_client::ClientMetric *metric);

/**
* Handle Gauge from MinMaxSumCount
*/
static void SetValue(double value, prometheus_client::ClientMetric *metric);

/**
* Handle Histogram
*/
template <typename T>
static void SetValue(std::vector<T> values,
std::vector<double> boundaries,
std::vector<int> counts,
prometheus_client::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_client::ClientMetric *metric,
bool do_quantile,
const std::vector<double> &quantile_points);
};
} // namespace prometheus
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Loading