From c7566453e5b92577f7823f8de659056249876333 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Thu, 21 Apr 2022 17:33:44 -0700 Subject: [PATCH 1/3] Fix output time in metrics OStream exporter --- exporters/ostream/src/metric_exporter.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index 5cdd3fe78d..6ab6c25fc3 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -14,7 +14,7 @@ namespace std::string timeToString(opentelemetry::common::SystemTimestamp time_stamp) { std::time_t epoch_time = std::chrono::system_clock::to_time_t(time_stamp); - return std::ctime(&epoch_time); + return std::string{std::asctime(std::gmtime(&epoch_time))}; } } // namespace From ca5ebe04a8a910c9e323ccc402075ee39b439c6d Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Sat, 23 Apr 2022 00:02:22 -0700 Subject: [PATCH 2/3] Switch to strftime --- exporters/ostream/src/metric_exporter.cc | 35 ++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index 6ab6c25fc3..418dd78942 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -14,7 +14,36 @@ namespace std::string timeToString(opentelemetry::common::SystemTimestamp time_stamp) { std::time_t epoch_time = std::chrono::system_clock::to_time_t(time_stamp); - return std::string{std::asctime(std::gmtime(&epoch_time))}; + + struct tm * tm_ptr = nullptr; +#if defined(_MSC_VER) + struct tm buf_tm; + if (!gmtime_s(&buf_tm, &epoch_time)) + { + tm_ptr = &buf_tm; + } +#else + tm_ptr = std::gmtime(&epoch_time); +#endif + + char buf[100]; + char * date_str = nullptr; + if (tm_ptr == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("[OStream Metric] gmtime failed for " + << epoch_time); + } + else if(std::strftime(buf, sizeof(buf), "%c", tm_ptr) > 0) + { + date_str = buf; + } + else + { + OTEL_INTERNAL_LOG_ERROR("[OStream Metric] strftime failed for " + << epoch_time); + } + + return std::string{date_str}; } } // namespace @@ -68,8 +97,8 @@ void OStreamMetricExporter::printInstrumentationInfoMetricData( for (const auto &record : info_metric.metric_data_) { sout_ << "\n start time\t: " << timeToString(record.start_ts) - << " end time\t: " << timeToString(record.end_ts) - << " description\t: " << record.instrument_descriptor.description_ + << "\n end time\t: " << timeToString(record.end_ts) + << "\n description\t: " << record.instrument_descriptor.description_ << "\n unit\t\t: " << record.instrument_descriptor.unit_; for (const auto &pd : record.point_data_attr_) From c1c1916d9608fba3b1dceb457bcb7bc2430c208d Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Sat, 23 Apr 2022 12:55:34 -0700 Subject: [PATCH 3/3] Fix format --- exporters/ostream/src/metric_exporter.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index 418dd78942..bf97db04c2 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -15,32 +15,30 @@ std::string timeToString(opentelemetry::common::SystemTimestamp time_stamp) { std::time_t epoch_time = std::chrono::system_clock::to_time_t(time_stamp); - struct tm * tm_ptr = nullptr; -#if defined(_MSC_VER) + struct tm *tm_ptr = nullptr; +# if defined(_MSC_VER) struct tm buf_tm; if (!gmtime_s(&buf_tm, &epoch_time)) { tm_ptr = &buf_tm; } -#else +# else tm_ptr = std::gmtime(&epoch_time); -#endif +# endif char buf[100]; - char * date_str = nullptr; + char *date_str = nullptr; if (tm_ptr == nullptr) { - OTEL_INTERNAL_LOG_ERROR("[OStream Metric] gmtime failed for " - << epoch_time); + OTEL_INTERNAL_LOG_ERROR("[OStream Metric] gmtime failed for " << epoch_time); } - else if(std::strftime(buf, sizeof(buf), "%c", tm_ptr) > 0) + else if (std::strftime(buf, sizeof(buf), "%c", tm_ptr) > 0) { date_str = buf; } else { - OTEL_INTERNAL_LOG_ERROR("[OStream Metric] strftime failed for " - << epoch_time); + OTEL_INTERNAL_LOG_ERROR("[OStream Metric] strftime failed for " << epoch_time); } return std::string{date_str};