Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dstanev-atvi committed Jan 10, 2024
2 parents 201f2b9 + c4f39f2 commit 35bb335
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Increment the:
[#2449](https://github.com/open-telemetry/opentelemetry-cpp/pull/2449)
* [BUILD] Introduce CXX 20 CI pipeline for MSVC/Windows
[#2450](https://github.com/open-telemetry/opentelemetry-cpp/pull/2450)
* [EXPORTER] Set `is_monotonic` flag for Observable Counters
[#2478](https://github.com/open-telemetry/opentelemetry-cpp/pull/2478)

Important changes:

Expand Down
7 changes: 5 additions & 2 deletions cmake/patch-imported-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ if(TARGET c-ares::cares)
endif()

# curl targets
if(TARGET CURL::libcurl)
project_build_tools_patch_default_imported_config(CURL::libcurl)
if(TARGET CURL::libcurl
OR TARGET CURL::libcurl_static
OR TARGET CURL::libcurl_shared)
project_build_tools_patch_default_imported_config(
CURL::libcurl CURL::libcurl_static CURL::libcurl_shared)
endif()

# abseil targets
Expand Down
16 changes: 15 additions & 1 deletion cmake/tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ endmacro()
if(NOT PATCH_PROTOBUF_SOURCES_OPTIONS_SET)
if(MSVC)
unset(PATCH_PROTOBUF_SOURCES_OPTIONS CACHE)
set(PATCH_PROTOBUF_SOURCES_OPTIONS /wd4244 /wd4251 /wd4267 /wd4309 /wd4668 /wd4946 /wd6001 /wd6244 /wd6246)
set(PATCH_PROTOBUF_SOURCES_OPTIONS
/wd4244
/wd4251
/wd4267
/wd4309
/wd4668
/wd4946
/wd6001
/wd6244
/wd6246)

if(MSVC_VERSION GREATER_EQUAL 1922)
# see
Expand Down Expand Up @@ -147,6 +156,11 @@ function(project_build_tools_patch_default_imported_config)
continue()
endif()

get_target_property(IS_ALIAS_TARGET ${TARGET_NAME} ALIASED_TARGET)
if(IS_ALIAS_TARGET)
continue()
endif()

if(CMAKE_VERSION VERSION_LESS "3.19.0")
get_target_property(TARGET_TYPE_NAME ${TARGET_NAME} TYPE)
if(TARGET_TYPE_NAME STREQUAL "INTERFACE_LIBRARY")
Expand Down
5 changes: 3 additions & 2 deletions exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ void OtlpMetricUtils::ConvertSumMetric(const metric_sdk::MetricData &metric_data
{
sum->set_aggregation_temporality(
GetProtoAggregationTemporality(metric_data.aggregation_temporality));
sum->set_is_monotonic(metric_data.instrument_descriptor.type_ ==
metric_sdk::InstrumentType::kCounter);
sum->set_is_monotonic(
(metric_data.instrument_descriptor.type_ == metric_sdk::InstrumentType::kCounter) ||
(metric_data.instrument_descriptor.type_ == metric_sdk::InstrumentType::kObservableCounter));
auto start_ts = metric_data.start_ts.time_since_epoch().count();
auto ts = metric_data.end_ts.time_since_epoch().count();
for (auto &point_data_with_attributes : metric_data.point_data_attr_)
Expand Down
91 changes: 91 additions & 0 deletions exporters/otlp/test/otlp_metrics_serialization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,66 @@ static metrics_sdk::MetricData CreateObservableGaugeAggregationData()
return data;
}

static metrics_sdk::MetricData CreateObservableCounterAggregationData()
{
metrics_sdk::MetricData data;
data.start_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now());
metrics_sdk::InstrumentDescriptor inst_desc = {
"ObservableCounter", "test description", "test unit",
metrics_sdk::InstrumentType::kObservableCounter, metrics_sdk::InstrumentValueType::kDouble};
metrics_sdk::SumPointData s_data_1, s_data_2;
s_data_1.value_ = 1.23;
s_data_2.value_ = 4.56;

data.aggregation_temporality = metrics_sdk::AggregationTemporality::kCumulative;
data.end_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now());
data.instrument_descriptor = inst_desc;
metrics_sdk::PointDataAttributes point_data_attr_1, point_data_attr_2;
point_data_attr_1.attributes = {{"key1", "value1"}};
point_data_attr_1.point_data = s_data_1;

point_data_attr_2.attributes = {{"key2", "value2"}};
point_data_attr_2.point_data = s_data_2;
std::vector<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
data.point_data_attr_ = std::move(point_data_attr);
return data;
}

static metrics_sdk::MetricData CreateObservableUpDownCounterAggregationData()
{
metrics_sdk::MetricData data;
data.start_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now());
metrics_sdk::InstrumentDescriptor inst_desc = {
"ObservableUpDownCounter", "test description", "test unit",
metrics_sdk::InstrumentType::kObservableUpDownCounter,
metrics_sdk::InstrumentValueType::kDouble};
metrics_sdk::SumPointData s_data_1, s_data_2, s_data_3;
s_data_1.value_ = 1.23;
s_data_2.value_ = 4.56;
s_data_3.value_ = 2.34;

data.aggregation_temporality = metrics_sdk::AggregationTemporality::kCumulative;
data.end_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now());
data.instrument_descriptor = inst_desc;
metrics_sdk::PointDataAttributes point_data_attr_1, point_data_attr_2, point_data_attr_3;
point_data_attr_1.attributes = {{"key1", "value1"}};
point_data_attr_1.point_data = s_data_1;

point_data_attr_2.attributes = {{"key2", "value2"}};
point_data_attr_2.point_data = s_data_2;

point_data_attr_3.attributes = {{"key3", "value3"}};
point_data_attr_3.point_data = s_data_3;
std::vector<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
point_data_attr.push_back(point_data_attr_3);
data.point_data_attr_ = std::move(point_data_attr);
return data;
}

TEST(OtlpMetricSerializationTest, Counter)
{
metrics_sdk::MetricData data = CreateSumAggregationData();
Expand Down Expand Up @@ -191,6 +251,37 @@ TEST(OtlpMetricSerializationTest, ObservableGauge)
EXPECT_EQ(1, 1);
}

TEST(OtlpMetricSerializationTest, ObservableCounter)
{
metrics_sdk::MetricData data = CreateObservableCounterAggregationData();
opentelemetry::proto::metrics::v1::Sum sum;
otlp_exporter::OtlpMetricUtils::ConvertSumMetric(data, &sum);
EXPECT_EQ(sum.aggregation_temporality(),
proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE);
EXPECT_EQ(sum.is_monotonic(), true);
EXPECT_EQ(sum.data_points_size(), 2);
EXPECT_EQ(sum.data_points(0).as_double(), 1.23);
EXPECT_EQ(sum.data_points(1).as_double(), 4.56);

EXPECT_EQ(1, 1);
}

TEST(OtlpMetricSerializationTest, ObservableUpDownCounter)
{
metrics_sdk::MetricData data = CreateObservableUpDownCounterAggregationData();
opentelemetry::proto::metrics::v1::Sum sum;
otlp_exporter::OtlpMetricUtils::ConvertSumMetric(data, &sum);
EXPECT_EQ(sum.aggregation_temporality(),
proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE);
EXPECT_EQ(sum.is_monotonic(), false);
EXPECT_EQ(sum.data_points_size(), 3);
EXPECT_EQ(sum.data_points(0).as_double(), 1.23);
EXPECT_EQ(sum.data_points(1).as_double(), 4.56);
EXPECT_EQ(sum.data_points(2).as_double(), 2.34);

EXPECT_EQ(1, 1);
}

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OPENTELEMETRY_EXPORT PrometheusCollector : public prometheus_client::Colle
*/
explicit PrometheusCollector(sdk::metrics::MetricReader *reader,
bool populate_target_info,
bool populate_otel_scope);
bool without_otel_scope);

/**
* Collects all metrics data from metricsToCollect collection.
Expand All @@ -45,7 +45,7 @@ class OPENTELEMETRY_EXPORT PrometheusCollector : public prometheus_client::Colle
private:
sdk::metrics::MetricReader *reader_;
bool populate_target_info_;
bool populate_otel_scope_;
bool without_otel_scope_;

/*
* Lock when operating the metricsToCollect collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct OPENTELEMETRY_EXPORT PrometheusExporterOptions
bool populate_target_info = true;

// Populating otel_scope_name/otel_scope_labels attributes
bool populate_otel_scope = true;
bool without_otel_scope = false;
};

} // namespace metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class OPENTELEMETRY_EXPORT PrometheusExporterUtils
*
* @param records a collection of metrics in OpenTelemetry
* @param populate_target_info whether to populate target_info
* @param populate_otel_scope whether to populate otel_scope_name and otel_scope_version
* @param without_otel_scope whether to populate otel_scope_name and otel_scope_version
* attributes
* @return a collection of translated metrics that is acceptable by Prometheus
*/
static std::vector<::prometheus::MetricFamily> TranslateToPrometheus(
const sdk::metrics::ResourceMetrics &data,
bool populate_target_info = true,
bool populate_otel_scope = true);
bool without_otel_scope = false);

private:
/**
Expand Down
6 changes: 3 additions & 3 deletions exporters/prometheus/src/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace metrics
*/
PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader,
bool populate_target_info,
bool populate_otel_scope)
bool without_otel_scope)
: reader_(reader),
populate_target_info_(populate_target_info),
populate_otel_scope_(populate_otel_scope)
without_otel_scope_(without_otel_scope)
{}

/**
Expand All @@ -45,7 +45,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() cons

reader_->Collect([&result, this](sdk::metrics::ResourceMetrics &metric_data) {
auto prometheus_metric_data = PrometheusExporterUtils::TranslateToPrometheus(
metric_data, this->populate_target_info_, this->populate_otel_scope_);
metric_data, this->populate_target_info_, this->without_otel_scope_);
for (auto &data : prometheus_metric_data)
result.emplace_back(data);
return true;
Expand Down
2 changes: 1 addition & 1 deletion exporters/prometheus/src/exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PrometheusExporter::PrometheusExporter(const PrometheusExporterOptions &options)
return;
}
collector_ = std::shared_ptr<PrometheusCollector>(
new PrometheusCollector(this, options_.populate_target_info, options_.populate_otel_scope));
new PrometheusCollector(this, options_.populate_target_info, options_.without_otel_scope));

exposer_->RegisterCollectable(collector_);
}
Expand Down
9 changes: 4 additions & 5 deletions exporters/prometheus/src/exporter_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ inline const std::string GetPrometheusDefaultHttpEndpoint()
return exists ? endpoint : kPrometheusEndpointDefault;
}

inline bool GetPrometheusPopulateOtelScope()
inline bool GetPrometheusWithoutOtelScope()
{
constexpr char kPrometheusPopulateOtelScope[] =
"OTEL_CPP_PROMETHEUS_EXPORTER_POPULATE_OTEL_SCOPE";
constexpr char kPrometheusWithoutOtelScope[] = "OTEL_CPP_PROMETHEUS_EXPORTER_WITHOUT_OTEL_SCOPE";

bool setting;
auto exists =
opentelemetry::sdk::common::GetBoolEnvironmentVariable(kPrometheusPopulateOtelScope, setting);
opentelemetry::sdk::common::GetBoolEnvironmentVariable(kPrometheusWithoutOtelScope, setting);

return exists ? setting : true;
}
Expand All @@ -52,7 +51,7 @@ inline bool GetPrometheusPopulateTargetInfo()
PrometheusExporterOptions::PrometheusExporterOptions()
: url(GetPrometheusDefaultHttpEndpoint()),
populate_target_info(GetPrometheusPopulateTargetInfo()),
populate_otel_scope(GetPrometheusPopulateOtelScope())
without_otel_scope(GetPrometheusWithoutOtelScope())
{}

} // namespace metrics
Expand Down
6 changes: 3 additions & 3 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ std::string SanitizeLabel(std::string label_key)
std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateToPrometheus(
const sdk::metrics::ResourceMetrics &data,
bool populate_target_info,
bool populate_otel_scope)
bool without_otel_scope)
{

// initialize output vector
Expand All @@ -129,7 +129,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
{
SetTarget(data,
data.scope_metric_data_.begin()->metric_data_.begin()->end_ts.time_since_epoch(),
populate_otel_scope ? (*data.scope_metric_data_.begin()).scope_ : nullptr, &output);
without_otel_scope ? nullptr : (*data.scope_metric_data_.begin()).scope_, &output);
}

for (const auto &instrumentation_info : data.scope_metric_data_)
Expand All @@ -153,7 +153,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
metric_data.instrument_descriptor.unit_, type);
metric_family.type = type;
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope =
populate_otel_scope ? instrumentation_info.scope_ : nullptr;
without_otel_scope ? nullptr : instrumentation_info.scope_;

for (const auto &point_data_attr : metric_data.point_data_attr_)
{
Expand Down
2 changes: 1 addition & 1 deletion exporters/prometheus/test/collector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ TEST(PrometheusCollector, BasicTests)
MockMetricReader *reader = new MockMetricReader();
MockMetricProducer *producer = new MockMetricProducer();
reader->SetMetricProducer(producer);
PrometheusCollector collector(reader, true, true);
PrometheusCollector collector(reader, true, false);
auto data = collector.Collect();

// Collection size should be the same as the size
Expand Down

0 comments on commit 35bb335

Please sign in to comment.