Skip to content

Commit

Permalink
Fix output time in metrics OStream exporter (#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Apr 28, 2022
1 parent a4bd3b0 commit 3a4a3a3
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions exporters/ostream/src/metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@ 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);

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

Expand Down Expand Up @@ -68,8 +95,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_)
Expand Down

1 comment on commit 3a4a3a3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3a4a3a3 Previous: a4bd3b0 Ratio
BM_BaselineBuffer/1 7783557.653427124 ns/iter 460155.0384714099 ns/iter 16.92
BM_BaselineBuffer/2 7643220.4246521 ns/iter 3378431.3201904297 ns/iter 2.26
BM_LockFreeBuffer/1 3665022.134780884 ns/iter 353697.5940660439 ns/iter 10.36
BM_LockFreeBuffer/2 3697491.16897583 ns/iter 1178330.1830291748 ns/iter 3.14

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.