Skip to content

Commit

Permalink
Merge branch 'main' into fix_semconv_2459
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff authored Jan 8, 2024
2 parents 2f86e87 + c4f39f2 commit 90866c3
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 49 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: 0 additions & 7 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ if(OPENTELEMETRY_INSTALL)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
DIRECTORY include/opentelemetry
DESTINATION include
FILES_MATCHING
PATTERN "*.h"
PATTERN "metrics" EXCLUDE)

install(
DIRECTORY include/opentelemetry
DESTINATION include
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
2 changes: 1 addition & 1 deletion docs/cpp-ostream-exporter-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public:
return sdktrace::ExportResult::kSuccess;
}

bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept
bool Shutdown(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept
{
isShutdown = true;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ class ElasticsearchLogRecordExporter final : public opentelemetry::sdk::logs::Lo
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shutdown this exporter.
* @param timeout The maximum time to wait for the shutdown method to return
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// Stores if this exporter had its Shutdown() method called
Expand Down
2 changes: 1 addition & 1 deletion exporters/elasticsearch/src/es_log_record_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ bool ElasticsearchLogRecordExporter::ForceFlush(
std::chrono::duration_cast<std::chrono::steady_clock::duration>(timeout);
if (timeout_steady <= std::chrono::steady_clock::duration::zero())
{
timeout_steady = std::chrono::steady_clock::duration::max();
timeout_steady = (std::chrono::steady_clock::duration::max)();
}

std::unique_lock<std::mutex> lk_cv(synchronization_data_->force_flush_cv_m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shut down the exporter.
Expand All @@ -67,7 +67,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter
* @return return the status of this operation
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// The configuration options associated with this exporter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shutdown this exporter.
* @param timeout The maximum time to wait for the shutdown method to return.
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// Configuration options for the exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shut down the exporter.
Expand All @@ -68,7 +68,7 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t
* @return return the status of this operation
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// The configuration options associated with this exporter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shutdown this exporter.
* @param timeout The maximum time to wait for the shutdown method to return
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// Configuration options for the exporter
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept
std::chrono::duration_cast<std::chrono::steady_clock::duration>(timeout);
if (timeout_steady <= std::chrono::steady_clock::duration::zero())
{
timeout_steady = std::chrono::steady_clock::duration::max();
timeout_steady = (std::chrono::steady_clock::duration::max)();
}

while (timeout_steady > std::chrono::steady_clock::duration::zero())
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 PrometheusCollector : public prometheus_client::Collectable
*/
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 PrometheusCollector : public prometheus_client::Collectable
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 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 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
Loading

0 comments on commit 90866c3

Please sign in to comment.