From cccc5b4aeaaf871d8737be1dbd50fb95f46ac4e1 Mon Sep 17 00:00:00 2001 From: Bryce Arden Date: Wed, 27 Jul 2022 17:15:04 -0700 Subject: [PATCH] fix(metrics): ostream exporter should print out resource attributes --- .../exporters/ostream/metric_exporter.h | 6 ++++- exporters/ostream/src/metric_exporter.cc | 27 +++++++++++++++++-- 2 files changed, 30 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 4dc3e1e266..8f66e1c411 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h @@ -57,9 +57,13 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; void printInstrumentationInfoMetricData( - const sdk::metrics::InstrumentationInfoMetrics &info_metrics); + const sdk::metrics::InstrumentationInfoMetrics &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 2e90e0845b..4fdc46265e 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.instrumentation_info_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::InstrumentationInfoMetrics &info_metric) + const sdk::metrics::InstrumentationInfoMetrics &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"; }