Skip to content
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Increment the:

## [Unreleased]

* [EXPORTER] Remove explicit timestamps from metric points exported by Prometheus
[#3895](https://github.com/open-telemetry/opentelemetry-cpp/pull/3895)

* [BUILD] Fix benchmark genrule capturing stderr into JSON output
[#3925](https://github.com/open-telemetry/opentelemetry-cpp/pull/3925)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ class PrometheusExporterUtils
* Add a target_info metric to collect resource attributes
*/
static void SetTarget(const sdk::metrics::ResourceMetrics &data,
std::chrono::nanoseconds time,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
std::vector<::prometheus::MetricFamily> *output);

Expand All @@ -166,7 +165,6 @@ class PrometheusExporterUtils
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
::prometheus::MetricType type,
std::chrono::nanoseconds time,
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);

Expand All @@ -180,7 +178,6 @@ class PrometheusExporterUtils
const std::vector<uint64_t> &counts,
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
std::chrono::nanoseconds time,
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);

Expand All @@ -190,7 +187,6 @@ class PrometheusExporterUtils
static void SetMetricBasic(
::prometheus::ClientMetric &metric,
const opentelemetry::sdk::metrics::PointAttributes &labels,
std::chrono::nanoseconds time,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
const opentelemetry::sdk::resource::Resource *resource);

Expand Down
27 changes: 9 additions & 18 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <prometheus/metric_family.h>
#include <prometheus/metric_type.h>
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
Expand All @@ -19,7 +18,6 @@
#include <utility>
#include <vector>

#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/exporters/prometheus/exporter_utils.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/common/attribute_utils.h"
Expand Down Expand Up @@ -138,9 +136,8 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
// Append target_info as the first metric
if (populate_target_info && !data.scope_metric_data_.empty())
{
SetTarget(data,
data.scope_metric_data_.begin()->metric_data_.begin()->end_ts.time_since_epoch(),
without_otel_scope ? nullptr : (*data.scope_metric_data_.begin()).scope_, &output);
SetTarget(data, without_otel_scope ? nullptr : (*data.scope_metric_data_.begin()).scope_,
&output);
}

for (const auto &instrumentation_info : data.scope_metric_data_)
Expand All @@ -151,7 +148,6 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto unit = metric_data.instrument_descriptor.unit_;
prometheus_client::MetricFamily metric_family;
metric_family.help = metric_data.instrument_descriptor.description_;
auto time = metric_data.end_ts.time_since_epoch();
auto front = metric_data.point_data_attr_.front();
auto kind = getAggregationType(front.point_data);
bool is_monotonic = true;
Expand Down Expand Up @@ -185,7 +181,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, static_cast<double>(histogram_point_data.count_)},
boundaries, counts, point_data_attr.attributes, scope, time, &metric_family,
boundaries, counts, point_data_attr.attributes, scope, &metric_family,
data.resource_);
}
else if (type == prometheus_client::MetricType::Gauge)
Expand All @@ -196,15 +192,15 @@ 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, scope, type, time, &metric_family,
SetData(values, point_data_attr.attributes, scope, 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, scope, type, time, &metric_family,
SetData(values, point_data_attr.attributes, scope, type, &metric_family,
data.resource_);
}
else
Expand All @@ -221,7 +217,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, scope, type, time, &metric_family,
SetData(values, point_data_attr.attributes, scope, type, &metric_family,
data.resource_);
}
else
Expand Down Expand Up @@ -605,7 +601,6 @@ prometheus_client::MetricType PrometheusExporterUtils::TranslateType(

void PrometheusExporterUtils::SetTarget(
const sdk::metrics::ResourceMetrics &data,
std::chrono::nanoseconds time,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
std::vector<::prometheus::MetricFamily> *output)
{
Expand All @@ -624,7 +619,7 @@ void PrometheusExporterUtils::SetTarget(
metric.info.value = 1.0;

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

for (auto &label : data.resource_->GetAttributes())
{
Expand All @@ -645,13 +640,12 @@ void PrometheusExporterUtils::SetData(
const metric_sdk::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
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, labels, time, scope, resource);
SetMetricBasic(metric, labels, scope, resource);
SetValue(values, type, &metric);
}

Expand All @@ -666,13 +660,12 @@ void PrometheusExporterUtils::SetData(
const std::vector<uint64_t> &counts,
const metric_sdk::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
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, labels, time, scope, resource);
SetMetricBasic(metric, labels, scope, resource);
SetValue(values, boundaries, counts, &metric);
}

Expand All @@ -682,11 +675,9 @@ void PrometheusExporterUtils::SetData(
void PrometheusExporterUtils::SetMetricBasic(
prometheus_client::ClientMetric &metric,
const metric_sdk::PointAttributes &labels,
std::chrono::nanoseconds time,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
const opentelemetry::sdk::resource::Resource *resource)
{
metric.timestamp_ms = time.count() / 1000000;
if (labels.empty() && nullptr == resource)
{
return;
Expand Down
17 changes: 9 additions & 8 deletions exporters/prometheus/test/prometheus_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ struct TestDataPoints
Resource resource = Resource::Create(ResourceAttributes{});
nostd::unique_ptr<InstrumentationScope> instrumentation_scope =
InstrumentationScope::Create("library_name", "1.2.0");
opentelemetry::common::SystemTimestamp start_ts =
opentelemetry::common::SystemTimestamp{std::chrono::microseconds{1766662500000}};
opentelemetry::common::SystemTimestamp end_ts =
opentelemetry::common::SystemTimestamp{std::chrono::microseconds{1766662560000}};

/**
* Helper function to create ResourceMetrics
Expand All @@ -39,8 +43,7 @@ struct TestDataPoints
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, start_ts, end_ts,
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, sum_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, sum_point_data2}}};
Expand All @@ -67,13 +70,13 @@ struct TestDataPoints
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kHistogram,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, start_ts, end_ts,
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, histogram_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, histogram_point_data2}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};

return data;
}

Expand All @@ -93,8 +96,7 @@ struct TestDataPoints
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, start_ts, end_ts,
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, last_value_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, last_value_point_data2}}};
Expand All @@ -113,8 +115,7 @@ struct TestDataPoints
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
metric_sdk::AggregationTemporality::kDelta, start_ts, end_ts,
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, drop_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, drop_point_data2}}};
Expand Down
Loading