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

[exporters/prometheus] Several refactors #2328

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "opentelemetry/exporters/prometheus/exporter_utils.h"
#include "opentelemetry/sdk/metrics/metric_reader.h"

namespace prometheus_client = ::prometheus;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
Expand All @@ -22,7 +20,7 @@ namespace metrics
/**
* The Prometheus Collector maintains the intermediate collection in Prometheus Exporter
*/
class PrometheusCollector : public prometheus_client::Collectable
class PrometheusCollector : public ::prometheus::Collectable
{
public:
/**
Expand All @@ -38,7 +36,7 @@ class PrometheusCollector : public prometheus_client::Collectable
*
* @return all metrics in the metricsToCollect snapshot
*/
std::vector<prometheus_client::MetricFamily> Collect() const override;
std::vector<::prometheus::MetricFamily> Collect() const override;

private:
sdk::metrics::MetricReader *reader_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,98 +16,13 @@ namespace exporter
namespace metrics
{
/**
* The Prometheus Utils contains utility functions for Prometheus Exporter
* Convert OpenTelemetry metrics data collection to Prometheus metrics data collection
*
* @param data a collection of metrics in OpenTelemetry
* @return a collection of translated metrics that is acceptable by Prometheus
*/
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 sdk::metrics::ResourceMetrics &data);

private:
/**
* 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);

static opentelemetry::sdk::metrics::AggregationType getAggregationType(
const opentelemetry::sdk::metrics::PointType &point_type);

/**
* Translate the OTel metric type to Prometheus metric type
*/
static ::prometheus::MetricType TranslateType(opentelemetry::sdk::metrics::AggregationType kind,
bool is_monotonic = true);

/**
* Set metric data for:
* Counter => Prometheus Counter
*/
template <typename T>
static void SetData(std::vector<T> values,
const opentelemetry::sdk::metrics::PointAttributes &labels,
::prometheus::MetricType type,
::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<uint64_t> &counts,
const opentelemetry::sdk::metrics::PointAttributes &labels,
::prometheus::MetricFamily *metric_family);

/**
* Set time and labels to metric data
*/
static void SetMetricBasic(::prometheus::ClientMetric &metric,
const opentelemetry::sdk::metrics::PointAttributes &labels);

/**
* Convert attribute value to string
*/
static std::string AttributeValueToString(
const opentelemetry::sdk::common::OwnedAttributeValue &value);

/**
* 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,
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
::prometheus::ClientMetric *metric);

// For testing
friend class SanitizeNameTester;
};
std::vector<::prometheus::MetricFamily> TranslateToPrometheus(
const sdk::metrics::ResourceMetrics &data);
} // namespace metrics
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
11 changes: 6 additions & 5 deletions exporters/prometheus/src/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include "opentelemetry/exporters/prometheus/collector.h"
#include "opentelemetry/sdk/common/global_log_handler.h"

namespace metric_sdk = opentelemetry::sdk::metrics;

OPENTELEMETRY_BEGIN_NAMESPACE

namespace metric_sdk = sdk::metrics;

namespace exporter
{
namespace metrics
Expand All @@ -24,7 +25,7 @@ PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader) : r
*
* @return all metrics in the metricsToCollect snapshot
*/
std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() const
std::vector<::prometheus::MetricFamily> PrometheusCollector::Collect() const
{
if (reader_->IsShutdown())
{
Expand All @@ -35,9 +36,9 @@ std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() cons
}
collection_lock_.lock();

std::vector<prometheus_client::MetricFamily> result;
std::vector<::prometheus::MetricFamily> result;
reader_->Collect([&result](sdk::metrics::ResourceMetrics &metric_data) {
auto prometheus_metric_data = PrometheusExporterUtils::TranslateToPrometheus(metric_data);
auto prometheus_metric_data = TranslateToPrometheus(metric_data);
for (auto &data : prometheus_metric_data)
result.emplace_back(data);
return true;
Expand Down
Loading
Loading