-
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
Merged
reyang
merged 40 commits into
open-telemetry:master
from
CunjunWang:prometheus-exporter-utils
Aug 26, 2020
Merged
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 75df0a8
remove helper of validating name because it's guaranteed in sdk
CunjunWang 9c74f41
format code
CunjunWang 91f382a
delete useless try catch
CunjunWang b826383
delete useless try catch
CunjunWang d98baf5
try to add promethues client to ci
CunjunWang deb2d4b
try to add promethues client to ci
CunjunWang c099a76
try to add promethues client to ci
CunjunWang 215a938
try to add promethues client to ci
CunjunWang eee7fac
modify prometheus client build
CunjunWang eb4957e
extract prometheus client instalation to a separate script
CunjunWang c1e1da3
change script to use prometheus client 0.9.0 release
CunjunWang 942714e
try to add prom-client git submodule; try to add script to install on…
CunjunWang 3a70d63
Merge branch 'master' into prometheus-exporter-utils
reyang a13bf69
add prometheus-client as subdirectory in main cmake file
CunjunWang 5a21c58
merge with remote
CunjunWang 7f8f2f5
Removed submodule
CunjunWang 26c735a
try to install prometheus client with vcpkg
CunjunWang 019e3f5
Merge branch 'master' into prometheus-exporter-utils
reyang c083771
add windows prometheus client installation
CunjunWang 31d5992
Merge branch 'prometheus-exporter-utils' of https://github.com/Cunjun…
CunjunWang 140c73d
modify prometheus client command
CunjunWang 5e7453f
stick prometheus client to v0.9.0 and added sha256
CunjunWang 0383028
fix problems from review comments
CunjunWang 645da39
change CMakefile to conditional build prometheus
CunjunWang 22237b4
modify CI bazel config so that prometheus is excluded for noexcept test
CunjunWang 1d2930e
try to fix 404 not found problem in setup cmake
CunjunWang 09eb665
format code
CunjunWang 6e525d9
restore setup cmake file
CunjunWang e5f63c9
add prometheus exporter tests to ci workflow
CunjunWang 17639bc
install dependencies in setup_cmake
CunjunWang 7c6b308
try to figure out install permission problem
CunjunWang eaceeb5
Merge branch 'master' into prometheus-exporter-utils
reyang 0d7b02c
Merge branch 'master' into prometheus-exporter-utils
reyang fda7664
remove printing for sanitize names
CunjunWang 6569d3e
add comment for ignoring Prometheus Exporter in noexcept test
CunjunWang 781277d
format code
CunjunWang c370218
format code
CunjunWang dc4c1fe
Merge branch 'master' into prometheus-exporter-utils
reyang 3f81dee
Merge branch 'master' into prometheus-exporter-utils
reyang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ if(WITH_OTPROTOCOL) | |
add_subdirectory(otlp) | ||
endif() | ||
add_subdirectory(ostream) | ||
add_subdirectory(prometheus) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
183 changes: 183 additions & 0 deletions
183
exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter_utils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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. Is there a way to limit the scope of these? I'm worried about having them in a header file. 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. 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please pin to a version (and add sha256), for consistency with other deps.
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.
pin to v0.9.0, and sha256 added