From 22f07a0c3a85cd0ade303b1ae959b01e8865644e Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Fri, 5 Aug 2022 18:43:14 -0500 Subject: [PATCH] fix(metrics): ostream exporter should print out resource attributes (#1523) --- .../exporters/ostream/metric_exporter.h | 6 +++- exporters/ostream/src/metric_exporter.cc | 34 +++++++++++++++++-- exporters/ostream/test/ostream_metric_test.cc | 33 +++++++++++++++--- 3 files changed, 66 insertions(+), 7 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..584b07db5d 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h @@ -56,9 +56,13 @@ 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::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..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" @@ -79,13 +80,39 @@ 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::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()) + { + // 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"); + } +} + 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 +136,9 @@ void OStreamMetricExporter::printInstrumentationInfoMetricData( printPointAttributes(pd.attributes); } } + + sout_ << "\n resources\t:"; + printResources(*data.resource_); } sout_ << "\n}\n"; } diff --git a/exporters/ostream/test/ostream_metric_test.cc b/exporters/ostream/test/ostream_metric_test.cc index ee89d0652b..c6363143fe 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" @@ -81,7 +82,13 @@ TEST(OStreamMetricsExporter, ExportSumPointData) "\n value\t\t: 20" "\n attributes\t\t: " "\n\ta1: b1" - "\n}\n"; + "\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}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -151,7 +158,13 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData) "\n counts : [200, 300, 400, 500, ]" "\n attributes\t\t: " "\n\ta1: b1" - "\n}\n"; + "\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}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -214,7 +227,13 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData) "\n valid : true" "\n value : 20" "\n attributes\t\t: " - "\n}\n"; + "\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}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); } @@ -261,7 +280,13 @@ TEST(OStreamMetricsExporter, ExportDropPointData) "\n name\t\t: library_name" "\n description\t: description" "\n unit\t\t: unit" - "\n}\n"; + "\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}\n"; ASSERT_EQ(stdoutOutput.str(), expected_output); }