From 63683c1596484161c085ff642cb0e34b7bdf2bfb Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Thu, 17 Oct 2024 15:57:10 +0800 Subject: [PATCH 1/3] [BUILD] Fix compiling problems for gcc 4.8 (#3100) * Fix compiling problems for gcc 4.8 * Apply suggestions from code review --------- Co-authored-by: Marc Alff --- exporters/memory/src/in_memory_metric_data.cc | 7 +++++-- exporters/memory/src/in_memory_metric_exporter_factory.cc | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/exporters/memory/src/in_memory_metric_data.cc b/exporters/memory/src/in_memory_metric_data.cc index 2a77e0b5a3..90f1c1450f 100644 --- a/exporters/memory/src/in_memory_metric_data.cc +++ b/exporters/memory/src/in_memory_metric_data.cc @@ -31,7 +31,9 @@ void SimpleAggregateInMemoryMetricData::Add(std::unique_ptr res const auto &metric = m.instrument_descriptor.name_; for (const auto &pda : m.point_data_attr_) { - data_[{scope, metric}].insert({pda.attributes, pda.point_data}); + // NOTE: Explicit type conversion added for C++11 (gcc 4.8) + data_[std::tuple{scope, metric}].insert( + {pda.attributes, pda.point_data}); } } } @@ -41,7 +43,8 @@ const SimpleAggregateInMemoryMetricData::AttributeToPoint &SimpleAggregateInMemo const std::string &scope, const std::string &metric) { - return data_[{scope, metric}]; + // NOTE: Explicit type conversion added for C++11 (gcc 4.8) + return data_[std::tuple{scope, metric}]; } void SimpleAggregateInMemoryMetricData::Clear() diff --git a/exporters/memory/src/in_memory_metric_exporter_factory.cc b/exporters/memory/src/in_memory_metric_exporter_factory.cc index 7f6ab83346..0deff73dcd 100644 --- a/exporters/memory/src/in_memory_metric_exporter_factory.cc +++ b/exporters/memory/src/in_memory_metric_exporter_factory.cc @@ -78,7 +78,8 @@ class InMemoryMetricExporter final : public sdk::metrics::PushMetricExporter std::unique_ptr InMemoryMetricExporterFactory::Create( const std::shared_ptr &data) { - return Create(data, [](auto) { return AggregationTemporality::kCumulative; }); + return Create(data, + [](sdk::metrics::InstrumentType) { return AggregationTemporality::kCumulative; }); } std::unique_ptr InMemoryMetricExporterFactory::Create( From 1185405c46290175127f279193a71e2d4f68f7ce Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Fri, 18 Oct 2024 18:20:42 +0800 Subject: [PATCH 2/3] [TEST] Fix linking order and gmock linking (#3106) * Fix link order * Fix gmock linking --- ext/test/http/CMakeLists.txt | 6 +++--- test_common/src/http/client/nosend/CMakeLists.txt | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/test/http/CMakeLists.txt b/ext/test/http/CMakeLists.txt index 328f78638b..9f5514d07b 100644 --- a/ext/test/http/CMakeLists.txt +++ b/ext/test/http/CMakeLists.txt @@ -5,7 +5,7 @@ if(WITH_HTTP_CLIENT_CURL) set(FILENAME curl_http_test) add_compile_definitions(WITH_CURL) add_executable(${FILENAME} ${FILENAME}.cc) - target_link_libraries(${FILENAME} ${GTEST_BOTH_LIBRARIES} + target_link_libraries(${FILENAME} ${GMOCK_LIB} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) if(TARGET CURL::libcurl) @@ -24,8 +24,8 @@ endif() set(URL_PARSER_FILENAME url_parser_test) add_executable(${URL_PARSER_FILENAME} ${URL_PARSER_FILENAME}.cc) -target_link_libraries(${URL_PARSER_FILENAME} ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) +target_link_libraries(${URL_PARSER_FILENAME} opentelemetry_api ${GMOCK_LIB} + ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) gtest_add_tests( TARGET ${URL_PARSER_FILENAME} TEST_PREFIX ext.http.urlparser. diff --git a/test_common/src/http/client/nosend/CMakeLists.txt b/test_common/src/http/client/nosend/CMakeLists.txt index 92a2c1f98c..7394053a22 100644 --- a/test_common/src/http/client/nosend/CMakeLists.txt +++ b/test_common/src/http/client/nosend/CMakeLists.txt @@ -28,7 +28,7 @@ if(${BUILD_TESTING}) endif() target_link_libraries( - opentelemetry_http_client_nosend ${GTEST_BOTH_LIBRARIES} opentelemetry_ext - opentelemetry_test_common) + opentelemetry_http_client_nosend opentelemetry_ext + opentelemetry_test_common ${GMOCK_LIB} ${GTEST_BOTH_LIBRARIES}) endif() From b1488cd37c3077d67021c3a63b3dabe1d7147a10 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Mon, 21 Oct 2024 16:43:30 -0400 Subject: [PATCH 3/3] [EXPORTER] Add config options to prometheus exporter (#3104) --- .../exporters/prometheus/exporter_options.h | 6 ++ .../exporters/prometheus/exporter_utils.h | 12 ++- exporters/prometheus/src/exporter_options.cc | 25 ++++- exporters/prometheus/src/exporter_utils.cc | 62 +++++++----- .../prometheus/test/exporter_utils_test.cc | 94 ++++++++++++++++++- 5 files changed, 169 insertions(+), 30 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h index 5d8b4932fc..da9d124029 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_options.h @@ -28,6 +28,12 @@ struct PrometheusExporterOptions // Populating otel_scope_name/otel_scope_labels attributes bool without_otel_scope = false; + + // Option to export metrics without the unit suffix + bool without_units = false; + + // Option to export metrics without the type suffix + bool without_type_suffix = false; }; } // namespace metrics diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h index ccf3d03ff3..29c0f66daf 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h @@ -30,12 +30,18 @@ class PrometheusExporterUtils * @param populate_target_info whether to populate target_info * @param without_otel_scope whether to populate otel_scope_name and otel_scope_version * attributes + * @param without_units exporter configuration controlling whether to append unit suffix in + * the exported metrics. + * @param without_type_suffix exporter configuration controlling whether to append type suffix in + * the exported metrics. * @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 without_otel_scope = false); + bool without_otel_scope = false, + bool without_units = false, + bool without_type_suffix = false); private: /** @@ -61,7 +67,9 @@ class PrometheusExporterUtils static std::string MapToPrometheusName(const std::string &name, const std::string &unit, - ::prometheus::MetricType prometheus_type); + ::prometheus::MetricType prometheus_type, + bool without_units, + bool without_type_suffix); /** * A utility function that returns the equivalent Prometheus name for the provided OTLP metric diff --git a/exporters/prometheus/src/exporter_options.cc b/exporters/prometheus/src/exporter_options.cc index f2c49f7a57..a9de8f4eb4 100644 --- a/exporters/prometheus/src/exporter_options.cc +++ b/exporters/prometheus/src/exporter_options.cc @@ -48,10 +48,33 @@ inline bool GetPrometheusPopulateTargetInfo() return exists ? setting : true; } +inline bool GetPrometheusWithoutUnits() +{ + constexpr char kPrometheusWithoutUnits[] = "OTEL_CPP_PROMETHEUS_EXPORTER_WITHOUT_UNITS"; + bool setting; + const auto exists = + opentelemetry::sdk::common::GetBoolEnvironmentVariable(kPrometheusWithoutUnits, setting); + + return exists ? setting : false; +} + +inline bool GetPrometheusWithoutTypeSuffix() +{ + constexpr char kPrometheusWithoutTypeSuffix[] = + "OTEL_CPP_PROMETHEUS_EXPORTER_WITHOUT_TYPE_SUFFIX"; + bool setting; + const auto exists = + opentelemetry::sdk::common::GetBoolEnvironmentVariable(kPrometheusWithoutTypeSuffix, setting); + + return exists ? setting : false; +} + PrometheusExporterOptions::PrometheusExporterOptions() : url(GetPrometheusDefaultHttpEndpoint()), populate_target_info(GetPrometheusPopulateTargetInfo()), - without_otel_scope(GetPrometheusWithoutOtelScope()) + without_otel_scope(GetPrometheusWithoutOtelScope()), + without_units(GetPrometheusWithoutUnits()), + without_type_suffix(GetPrometheusWithoutTypeSuffix()) {} } // namespace metrics diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 77fcd56ad2..27385f3f6f 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -102,13 +102,15 @@ std::string SanitizeLabel(std::string label_key) * Helper function to convert OpenTelemetry metrics data collection * to Prometheus metrics data collection * - * @param records a collection of metrics in OpenTelemetry + * @param data a collection of metrics in OpenTelemetry * @return a collection of translated metrics that is acceptable by Prometheus */ std::vector PrometheusExporterUtils::TranslateToPrometheus( const sdk::metrics::ResourceMetrics &data, bool populate_target_info, - bool without_otel_scope) + bool without_otel_scope, + bool without_units, + bool without_type_suffix) { // initialize output vector @@ -150,7 +152,8 @@ std::vector PrometheusExporterUtils::TranslateT } const prometheus_client::MetricType type = TranslateType(kind, is_monotonic); metric_family.name = MapToPrometheusName(metric_data.instrument_descriptor.name_, - metric_data.instrument_descriptor.unit_, type); + metric_data.instrument_descriptor.unit_, type, + without_units, without_type_suffix); metric_family.type = type; const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope = without_otel_scope ? nullptr : instrumentation_info.scope_; @@ -492,34 +495,43 @@ std::string PrometheusExporterUtils::CleanUpString(const std::string &str) std::string PrometheusExporterUtils::MapToPrometheusName( const std::string &name, const std::string &unit, - prometheus_client::MetricType prometheus_type) + prometheus_client::MetricType prometheus_type, + bool without_units, + bool without_type_suffix) { - auto sanitized_name = SanitizeNames(name); - std::string prometheus_equivalent_unit = GetEquivalentPrometheusUnit(unit); - - // Append prometheus unit if not null or empty. - if (!prometheus_equivalent_unit.empty() && - sanitized_name.find(prometheus_equivalent_unit) == std::string::npos) - { - sanitized_name += "_" + prometheus_equivalent_unit; - } - - // Special case - counter - if (prometheus_type == prometheus_client::MetricType::Counter) - { - auto t_pos = sanitized_name.rfind("_total"); - bool ends_with_total = t_pos == sanitized_name.size() - 6; - if (!ends_with_total) + auto sanitized_name = SanitizeNames(name); + // append unit suffixes + if (!without_units) + { + std::string prometheus_equivalent_unit = GetEquivalentPrometheusUnit(unit); + // Append prometheus unit if not null or empty. + if (!prometheus_equivalent_unit.empty() && + sanitized_name.find(prometheus_equivalent_unit) == std::string::npos) { - sanitized_name += "_total"; + sanitized_name += "_" + prometheus_equivalent_unit; + } + // Special case - gauge + if (unit == "1" && prometheus_type == prometheus_client::MetricType::Gauge && + sanitized_name.find("ratio") == std::string::npos) + { + // this is replacing the unit name + sanitized_name += "_ratio"; } } - // Special case - gauge - if (unit == "1" && prometheus_type == prometheus_client::MetricType::Gauge && - sanitized_name.find("ratio") == std::string::npos) + // append type suffixes + if (!without_type_suffix) { - sanitized_name += "_ratio"; + // Special case - counter + if (prometheus_type == prometheus_client::MetricType::Counter) + { + auto t_pos = sanitized_name.rfind("_total"); + bool ends_with_total = t_pos == sanitized_name.size() - 6; + if (!ends_with_total) + { + sanitized_name += "_total"; + } + } } return CleanUpString(SanitizeNames(sanitized_name)); diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index 7ad5eee1fd..76ec869a6f 100644 --- a/exporters/prometheus/test/exporter_utils_test.cc +++ b/exporters/prometheus/test/exporter_utils_test.cc @@ -54,9 +54,12 @@ class SanitizeNameTester } static std::string mapToPrometheusName(const std::string &name, const std::string &unit, - prometheus_client::MetricType prometheus_type) + prometheus_client::MetricType prometheus_type, + bool without_units = false, + bool without_type_suffix = false) { - return PrometheusExporterUtils::MapToPrometheusName(name, unit, prometheus_type); + return PrometheusExporterUtils::MapToPrometheusName(name, unit, prometheus_type, without_units, + without_type_suffix); } }; } // namespace metrics @@ -419,6 +422,93 @@ TEST(PrometheusExporterUtils, ConvertRateExpressedToPrometheusUnit) "_per_minute"); } +TEST(PromentheusExporterUtils, PrometheusNameMapping) +{ + // General test cases on unit expansions and name sanitization + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric___name", "g", prometheus::MetricType::Counter), + "sample_metric_name_grams_total"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "s", prometheus::MetricType::Counter), + "sample_metric_name_seconds_total"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "s", prometheus::MetricType::Gauge), + "sample_metric_name_seconds"); + // Test without_units & without_type_suffix with Counters and unit = 1 + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Counter), + "sample_metric_name_total"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Counter, true, false), + "sample_metric_name_total"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Counter, false, true), + "sample_metric_name"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Counter, true, true), + "sample_metric_name"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Counter, true, true), + "sample_metric_name"); + // Test without_units & without_type_suffix with Counters and non-special units + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "%", prometheus::MetricType::Counter), + "sample_metric_name_percent_total"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "m", prometheus::MetricType::Counter, true, false), + "sample_metric_name_total"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "By", prometheus::MetricType::Counter, false, true), + "sample_metric_name_bytes"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "s", prometheus::MetricType::Counter, true, true), + "sample_metric_name"); + // Special case Gauges & ratio + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Gauge), + "sample_metric_name_ratio"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Gauge, false, true), + "sample_metric_name_ratio"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Gauge, true, false), + "sample_metric_name"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "1", prometheus::MetricType::Gauge, true, true), + "sample_metric_name"); + // Test without_type_suffix affects only counters + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Counter), + "sample_metric_name_hertz_total"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Counter, false, true), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Gauge), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Gauge, false, true), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Histogram), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Histogram, false, true), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Summary), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Summary, false, true), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Info), + "sample_metric_name_hertz"); + ASSERT_EQ(exporter::metrics::SanitizeNameTester::mapToPrometheusName( + "sample_metric_name", "Hz", prometheus::MetricType::Info, false, true), + "sample_metric_name_hertz"); +} + TEST_F(AttributeCollisionTest, JoinsCollidingKeys) { CheckTranslation({{"foo.a", "value1"}, {"foo_a", "value2"}}, {{"foo_a", "value1;value2"},