Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/main' into emits_resource_for_pr…
Browse files Browse the repository at this point in the history
…ometheus
  • Loading branch information
owent committed Sep 23, 2023
2 parents a0b322b + 41a7875 commit 33d3a47
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 144 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Increment the:
[#2291](https://github.com/open-telemetry/opentelemetry-cpp/pull/2291)
* [EXPORTER] Prometheus exporter emit resource attributes
[#2301](https://github.com/open-telemetry/opentelemetry-cpp/pull/2301)
* [EXPORTER] Remove explicit timestamps from metric points exported by Prometheus
[#2324](https://github.com/open-telemetry/opentelemetry-cpp/pull/2324)

## [1.11.0] 2023-08-21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class PrometheusExporterUtils
static void SetData(std::vector<T> values,
const opentelemetry::sdk::metrics::PointAttributes &labels,
::prometheus::MetricType type,
std::chrono::nanoseconds time,
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);

Expand All @@ -99,15 +98,13 @@ class PrometheusExporterUtils
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
const opentelemetry::sdk::metrics::PointAttributes &labels,
std::chrono::nanoseconds time,
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);

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

Expand Down
25 changes: 8 additions & 17 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
prometheus_client::MetricFamily metric_family;
metric_family.name = sanitized + "_" + unit;
metric_family.help = metric_data.instrument_descriptor.description_;
auto time = metric_data.end_ts.time_since_epoch();
for (const auto &point_data_attr : metric_data.point_data_attr_)
{
auto kind = getAggregationType(point_data_attr.point_data);
Expand Down Expand Up @@ -98,7 +97,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
sum = static_cast<double>(nostd::get<int64_t>(histogram_point_data.sum_));
}
SetData(std::vector<double>{sum, (double)histogram_point_data.count_}, boundaries, counts,
point_data_attr.attributes, time, &metric_family, data.resource_);
point_data_attr.attributes, &metric_family, data.resource_);
}
else if (type == prometheus_client::MetricType::Gauge)
{
Expand All @@ -108,14 +107,14 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto last_value_point_data =
nostd::get<sdk::metrics::LastValuePointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{last_value_point_data.value_};
SetData(values, point_data_attr.attributes, type, time, &metric_family, data.resource_);
SetData(values, point_data_attr.attributes, type, &metric_family, data.resource_);
}
else if (nostd::holds_alternative<sdk::metrics::SumPointData>(point_data_attr.point_data))
{
auto sum_point_data =
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{sum_point_data.value_};
SetData(values, point_data_attr.attributes, type, time, &metric_family, data.resource_);
SetData(values, point_data_attr.attributes, type, &metric_family, data.resource_);
}
else
{
Expand All @@ -131,7 +130,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto sum_point_data =
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{sum_point_data.value_};
SetData(values, point_data_attr.attributes, type, time, &metric_family, data.resource_);
SetData(values, point_data_attr.attributes, type, &metric_family, data.resource_);
}
else
{
Expand Down Expand Up @@ -292,11 +291,8 @@ void PrometheusExporterUtils::SetTarget(const sdk::metrics::ResourceMetrics &dat
prometheus_client::ClientMetric &metric = metric_family.metric.back();
metric.info.value = 1.0;

std::chrono::nanoseconds time =
(*data.scope_metric_data_.begin()).metric_data_.begin()->end_ts.time_since_epoch();

metric_sdk::PointAttributes empty_attributes;
SetMetricBasic(metric, time, empty_attributes, data.resource_);
SetMetricBasic(metric, empty_attributes, data.resource_);

for (auto &label : data.resource_->GetAttributes())
{
Expand All @@ -320,13 +316,12 @@ template <typename T>
void PrometheusExporterUtils::SetData(std::vector<T> values,
const metric_sdk::PointAttributes &labels,
prometheus_client::MetricType type,
std::chrono::nanoseconds time,
prometheus_client::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource)
{
metric_family->metric.emplace_back();
prometheus_client::ClientMetric &metric = metric_family->metric.back();
SetMetricBasic(metric, time, labels, resource);
SetMetricBasic(metric, labels, resource);
SetValue(values, type, &metric);
}

Expand All @@ -339,26 +334,22 @@ void PrometheusExporterUtils::SetData(std::vector<T> values,
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
const metric_sdk::PointAttributes &labels,
std::chrono::nanoseconds time,
prometheus_client::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource)
{
metric_family->metric.emplace_back();
prometheus_client::ClientMetric &metric = metric_family->metric.back();
SetMetricBasic(metric, time, labels, resource);
SetMetricBasic(metric, labels, resource);
SetValue(values, boundaries, counts, &metric);
}

/**
* Set time and labels to metric data
* Set labels to metric data
*/
void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &metric,
std::chrono::nanoseconds time,
const metric_sdk::PointAttributes &labels,
const opentelemetry::sdk::resource::Resource *resource)
{
metric.timestamp_ms = time.count() / 1000000;

std::size_t label_size = 0;
if (nullptr != resource)
{
Expand Down
4 changes: 3 additions & 1 deletion exporters/prometheus/test/collector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace metric_exporter = opentelemetry::exporter::metrics;

class MockMetricProducer : public opentelemetry::sdk::metrics::MetricProducer
{
TestDataPoints test_data_points_;

public:
MockMetricProducer(std::chrono::microseconds sleep_ms = std::chrono::microseconds::zero())
: sleep_ms_{sleep_ms}, data_sent_size_(0)
Expand All @@ -28,7 +30,7 @@ class MockMetricProducer : public opentelemetry::sdk::metrics::MetricProducer
{
std::this_thread::sleep_for(sleep_ms_);
data_sent_size_++;
ResourceMetrics data = CreateSumPointData();
ResourceMetrics data = test_data_points_.CreateSumPointData();
callback(data);
return true;
}
Expand Down
16 changes: 12 additions & 4 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

using opentelemetry::exporter::metrics::PrometheusExporterUtils;
namespace metric_sdk = opentelemetry::sdk::metrics;
namespace metric_api = opentelemetry::metrics;
namespace prometheus_client = ::prometheus;

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -43,6 +42,12 @@ void assert_basic(prometheus_client::MetricFamily &metric,
ASSERT_EQ(metric.help, description); // description not changed
ASSERT_EQ(metric.type, type); // type translated

// Prometheus metric data points should not have explicit timestamps
for (const prometheus::ClientMetric &cm : metric.metric)
{
ASSERT_EQ(cm.timestamp_ms, 0);
}

auto metric_data = metric.metric[0];
ASSERT_EQ(metric_data.label.size(), label_num);

Expand Down Expand Up @@ -113,7 +118,8 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter)
{"service.namespace", "test_namespace"},
{"service.instance.id", "localhost:8000"},
{"custom_resource_attr", "custom_resource_value"}});
metric_sdk::ResourceMetrics metrics_data = CreateSumPointData();
TestDataPoints dp;
metric_sdk::ResourceMetrics metrics_data = dp.CreateSumPointData();
metrics_data.resource_ = &resource;

auto translated = PrometheusExporterUtils::TranslateToPrometheus(metrics_data);
Expand Down Expand Up @@ -169,7 +175,8 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)
{{"service.name", "test_service"},
{"service.instance.id", "localhost:8000"},
{"custom_resource_attr", "custom_resource_value"}});
metric_sdk::ResourceMetrics metrics_data = CreateLastValuePointData();
TestDataPoints dp;
metric_sdk::ResourceMetrics metrics_data = dp.CreateLastValuePointData();
metrics_data.resource_ = &resource;

auto translated = PrometheusExporterUtils::TranslateToPrometheus(metrics_data);
Expand Down Expand Up @@ -224,7 +231,8 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal)
{{"job", "test_service2"},
{"instance", "localhost:8001"},
{"custom_resource_attr", "custom_resource_value"}});
metric_sdk::ResourceMetrics metrics_data = CreateHistogramPointData();
TestDataPoints dp;
metric_sdk::ResourceMetrics metrics_data = dp.CreateHistogramPointData();
metrics_data.resource_ = &resource;

auto translated = PrometheusExporterUtils::TranslateToPrometheus(metrics_data);
Expand Down
Loading

0 comments on commit 33d3a47

Please sign in to comment.