From 6a518dc2f169959069f444e11c4f3bef4f182d2b Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Wed, 27 Jul 2022 17:15:04 -0700 Subject: [PATCH 1/6] fix(metrics): ostream exporter should print out resource attributes --- .../exporters/ostream/metric_exporter.h | 7 ++++- exporters/ostream/src/metric_exporter.cc | 27 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h index 5db433fe9a..043ddc847c 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h @@ -56,9 +56,14 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx bool is_shutdown_ = false; mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; - void printInstrumentationInfoMetricData(const sdk::metrics::ScopeMetrics &info_metrics); + void printInstrumentationInfoMetricData( + const sdk::metrics::ScopeMetrics &info_metrics, const sdk::metrics::ResourceMetrics &data); void printPointData(const opentelemetry::sdk::metrics::PointType &point_data); void printPointAttributes(const opentelemetry::sdk::metrics::PointAttributes &point_attributes); + void printAttributes( + const std::unordered_map &map, + const std::string prefix); + void printResources(const opentelemetry::sdk::resource::Resource &resources); }; } // namespace metrics } // namespace exporter diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index 17f54bbb09..a485b395f4 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -79,13 +79,33 @@ sdk::common::ExportResult OStreamMetricExporter::Export( for (auto &record : data.scope_metric_data_) { - printInstrumentationInfoMetricData(record); + printInstrumentationInfoMetricData(record, data); } return sdk::common::ExportResult::kSuccess; } +void OStreamMetricExporter::printAttributes( + const std::unordered_map &map, + const std::string prefix) +{ + for (const auto &kv : map) + { + sout_ << prefix << kv.first << ": "; + opentelemetry::exporter::ostream_common::print_value(kv.second, sout_); + } +} + +void OStreamMetricExporter::printResources(const opentelemetry::sdk::resource::Resource &resources) +{ + auto attributes = resources.GetAttributes(); + if (attributes.size()) + { + printAttributes(attributes, "\n\t"); + } +} + void OStreamMetricExporter::printInstrumentationInfoMetricData( - const sdk::metrics::ScopeMetrics &info_metric) + const sdk::metrics::ScopeMetrics &info_metric, const sdk::metrics::ResourceMetrics &data) { // sout_ is shared const std::lock_guard locked(lock_); @@ -109,6 +129,9 @@ void OStreamMetricExporter::printInstrumentationInfoMetricData( printPointAttributes(pd.attributes); } } + + sout_ << "\n resources\t:"; + printResources(*data.resource_); } sout_ << "\n}\n"; } From 1eec14e1a45dcdfdeee1b4af47e17e917ce2f097 Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Thu, 28 Jul 2022 10:13:55 -0700 Subject: [PATCH 2/6] test(exporters): update ostream_metric_test to check for resource attrs --- exporters/ostream/test/ostream_metric_test.cc | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/exporters/ostream/test/ostream_metric_test.cc b/exporters/ostream/test/ostream_metric_test.cc index ee89d0652b..f5f8466815 100644 --- a/exporters/ostream/test/ostream_metric_test.cc +++ b/exporters/ostream/test/ostream_metric_test.cc @@ -81,6 +81,11 @@ TEST(OStreamMetricsExporter, ExportSumPointData) "\n value\t\t: 20" "\n attributes\t\t: " "\n\ta1: b1" + "\n resources\t:" + "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.name: opentelemetry" + "\n\ttelemetry.sdk.language: cpp" "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -151,6 +156,11 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData) "\n counts : [200, 300, 400, 500, ]" "\n attributes\t\t: " "\n\ta1: b1" + "\n resources\t:" + "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.name: opentelemetry" + "\n\ttelemetry.sdk.language: cpp" "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -214,6 +224,11 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData) "\n valid : true" "\n value : 20" "\n attributes\t\t: " + "\n resources\t:" + "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.name: opentelemetry" + "\n\ttelemetry.sdk.language: cpp" "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -261,6 +276,11 @@ TEST(OStreamMetricsExporter, ExportDropPointData) "\n name\t\t: library_name" "\n description\t: description" "\n unit\t\t: unit" + "\n resources\t:" + "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.name: opentelemetry" + "\n\ttelemetry.sdk.language: cpp" "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); From 79124bba7b43dec6bee64e6fb9dc127ea511d765 Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Fri, 29 Jul 2022 05:37:32 -0700 Subject: [PATCH 3/6] chore: fix lint --- .../opentelemetry/exporters/ostream/metric_exporter.h | 9 ++++----- exporters/ostream/src/metric_exporter.cc | 3 ++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h index 043ddc847c..5769db25a1 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h @@ -56,13 +56,12 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx bool is_shutdown_ = false; mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; - void printInstrumentationInfoMetricData( - const sdk::metrics::ScopeMetrics &info_metrics, const sdk::metrics::ResourceMetrics &data); + void printInstrumentationInfoMetricData(const sdk::metrics::ScopeMetrics &info_metrics, + const sdk::metrics::ResourceMetrics &data); void printPointData(const opentelemetry::sdk::metrics::PointType &point_data); void printPointAttributes(const opentelemetry::sdk::metrics::PointAttributes &point_attributes); - void printAttributes( - const std::unordered_map &map, - const std::string prefix); + void printAttributes(const std::unordered_map &map, + const std::string prefix); void printResources(const opentelemetry::sdk::resource::Resource &resources); }; } // namespace metrics diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index a485b395f4..b187b98ee3 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -105,7 +105,8 @@ void OStreamMetricExporter::printResources(const opentelemetry::sdk::resource::R } void OStreamMetricExporter::printInstrumentationInfoMetricData( - const sdk::metrics::ScopeMetrics &info_metric, const sdk::metrics::ResourceMetrics &data) + const sdk::metrics::ScopeMetrics &info_metric, + const sdk::metrics::ResourceMetrics &data) { // sout_ is shared const std::lock_guard locked(lock_); From 361c13f934e08029450f97fc7c65a66c6172357b Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Sun, 31 Jul 2022 10:13:53 -0700 Subject: [PATCH 4/6] fix(test): update ostream_metrics_test to new opentelemetry sdk version --- exporters/ostream/test/ostream_metric_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporters/ostream/test/ostream_metric_test.cc b/exporters/ostream/test/ostream_metric_test.cc index f5f8466815..c4e36bbd16 100644 --- a/exporters/ostream/test/ostream_metric_test.cc +++ b/exporters/ostream/test/ostream_metric_test.cc @@ -83,7 +83,7 @@ TEST(OStreamMetricsExporter, ExportSumPointData) "\n\ta1: b1" "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.version: 1.5.0" "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; @@ -158,7 +158,7 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData) "\n\ta1: b1" "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.version: 1.5.0" "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; @@ -226,7 +226,7 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData) "\n attributes\t\t: " "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.version: 1.5.0" "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; @@ -278,7 +278,7 @@ TEST(OStreamMetricsExporter, ExportDropPointData) "\n unit\t\t: unit" "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.4.1" + "\n\ttelemetry.sdk.version: 1.5.0" "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; From ade63779a67ca30091f182b13bbeaca6cdc78ec3 Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Sun, 31 Jul 2022 12:55:56 -0700 Subject: [PATCH 5/6] test(ostream_metrics): use version.h instead of hardcoding sdk version --- exporters/ostream/test/ostream_metric_test.cc | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/exporters/ostream/test/ostream_metric_test.cc b/exporters/ostream/test/ostream_metric_test.cc index c4e36bbd16..83ee8d5819 100644 --- a/exporters/ostream/test/ostream_metric_test.cc +++ b/exporters/ostream/test/ostream_metric_test.cc @@ -7,6 +7,7 @@ # include # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/resource/resource_detector.h" +# include "opentelemetry/sdk/version/version.h" # include # include "opentelemetry/exporters/ostream/metric_exporter.h" @@ -83,7 +84,9 @@ TEST(OStreamMetricsExporter, ExportSumPointData) "\n\ta1: b1" "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.5.0" + "\n\ttelemetry.sdk.version: "; + expected_output += OPENTELEMETRY_SDK_VERSION; + expected_output += "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; @@ -158,7 +161,9 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData) "\n\ta1: b1" "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.5.0" + "\n\ttelemetry.sdk.version: "; + expected_output += OPENTELEMETRY_SDK_VERSION; + expected_output += "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; @@ -226,7 +231,9 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData) "\n attributes\t\t: " "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.5.0" + "\n\ttelemetry.sdk.version: "; + expected_output += OPENTELEMETRY_SDK_VERSION; + expected_output += "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; @@ -278,7 +285,9 @@ TEST(OStreamMetricsExporter, ExportDropPointData) "\n unit\t\t: unit" "\n resources\t:" "\n\tservice.name: unknown_service" - "\n\ttelemetry.sdk.version: 1.5.0" + "\n\ttelemetry.sdk.version: "; + expected_output += OPENTELEMETRY_SDK_VERSION; + expected_output += "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.language: cpp" "\n}\n"; From 05489f8e81250b26b0cd2ea4ec7dc1037c556398 Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Fri, 5 Aug 2022 13:47:05 -0700 Subject: [PATCH 6/6] feat(metrics): enforce iteration order when printing resource attrs --- .../exporters/ostream/metric_exporter.h | 2 +- exporters/ostream/src/metric_exporter.cc | 10 +++++-- exporters/ostream/test/ostream_metric_test.cc | 28 ++++++++----------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h index 5769db25a1..584b07db5d 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h @@ -60,7 +60,7 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx const sdk::metrics::ResourceMetrics &data); void printPointData(const opentelemetry::sdk::metrics::PointType &point_data); void printPointAttributes(const opentelemetry::sdk::metrics::PointAttributes &point_attributes); - void printAttributes(const std::unordered_map &map, + void printAttributes(const std::map &map, const std::string prefix); void printResources(const opentelemetry::sdk::resource::Resource &resources); }; diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index b187b98ee3..e16fb76de3 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -4,6 +4,7 @@ #include #ifndef ENABLE_METRICS_PREVIEW # include +# include # include "opentelemetry/exporters/ostream/common_utils.h" # include "opentelemetry/exporters/ostream/metric_exporter.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" @@ -85,7 +86,7 @@ sdk::common::ExportResult OStreamMetricExporter::Export( } void OStreamMetricExporter::printAttributes( - const std::unordered_map &map, + const std::map &map, const std::string prefix) { for (const auto &kv : map) @@ -100,7 +101,12 @@ void OStreamMetricExporter::printResources(const opentelemetry::sdk::resource::R auto attributes = resources.GetAttributes(); if (attributes.size()) { - printAttributes(attributes, "\n\t"); + // Convert unordered_map to map for printing so that iteration + // order is guaranteed. + std::map attr_map; + for (auto &kv : attributes) + attr_map[kv.first] = std::move(kv.second); + printAttributes(attr_map, "\n\t"); } } diff --git a/exporters/ostream/test/ostream_metric_test.cc b/exporters/ostream/test/ostream_metric_test.cc index 83ee8d5819..c6363143fe 100644 --- a/exporters/ostream/test/ostream_metric_test.cc +++ b/exporters/ostream/test/ostream_metric_test.cc @@ -84,12 +84,11 @@ TEST(OStreamMetricsExporter, ExportSumPointData) "\n\ta1: b1" "\n resources\t:" "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.language: cpp" + "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.version: "; expected_output += OPENTELEMETRY_SDK_VERSION; - expected_output += - "\n\ttelemetry.sdk.name: opentelemetry" - "\n\ttelemetry.sdk.language: cpp" - "\n}\n"; + expected_output += "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -161,12 +160,11 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData) "\n\ta1: b1" "\n resources\t:" "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.language: cpp" + "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.version: "; expected_output += OPENTELEMETRY_SDK_VERSION; - expected_output += - "\n\ttelemetry.sdk.name: opentelemetry" - "\n\ttelemetry.sdk.language: cpp" - "\n}\n"; + expected_output += "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -231,12 +229,11 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData) "\n attributes\t\t: " "\n resources\t:" "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.language: cpp" + "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.version: "; expected_output += OPENTELEMETRY_SDK_VERSION; - expected_output += - "\n\ttelemetry.sdk.name: opentelemetry" - "\n\ttelemetry.sdk.language: cpp" - "\n}\n"; + expected_output += "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -285,12 +282,11 @@ TEST(OStreamMetricsExporter, ExportDropPointData) "\n unit\t\t: unit" "\n resources\t:" "\n\tservice.name: unknown_service" + "\n\ttelemetry.sdk.language: cpp" + "\n\ttelemetry.sdk.name: opentelemetry" "\n\ttelemetry.sdk.version: "; expected_output += OPENTELEMETRY_SDK_VERSION; - expected_output += - "\n\ttelemetry.sdk.name: opentelemetry" - "\n\ttelemetry.sdk.language: cpp" - "\n}\n"; + expected_output += "\n}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); }