Skip to content

Commit

Permalink
fixup! [EXPORTERS]: elastic search set timestamp within @timestamp
Browse files Browse the repository at this point in the history
…instead of `timestamp`
  • Loading branch information
ShadowMaxLeb committed Oct 18, 2024
1 parent 67d7e6b commit 3e4a517
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions exporters/elasticsearch/src/es_log_recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,25 @@ nlohmann::json ElasticSearchRecordable::GetJSON() noexcept
void ElasticSearchRecordable::SetTimestamp(
opentelemetry::common::SystemTimestamp timestamp) noexcept
{
const std::chrono::system_clock::time_point timePoint{timestamp};

// If built with with at least cpp 20 then use std::format
// Otherwise use the old style to format the timestamp in UTC
#if __cplusplus >= 202002L
const std::chrono::system_clock::time_point timePoint{timestamp};
const std::string dateStr = std::format("{:%FT%T%Ez}", timePoint);
#else
const static int dateToSecondsSize = 19;
const static int millisecondsSize = 8;
const static int timeZoneSize = 1;
const static int dateSize = dateToSecondsSize + millisecondsSize + timeZoneSize;

std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(timestamp);
std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
std::tm tm = *std::gmtime(&time);

char bufferDate[dateSize]; // example: 2024-10-18T07:26:00.123456Z
std::strftime(bufferDate, sizeof(bufferDate), "%Y-%m-%dT%H:%M:%S", &tm);
auto microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(timestamp.time_since_epoch()) %
std::chrono::duration_cast<std::chrono::microseconds>(timePoint.time_since_epoch()) %
std::chrono::seconds(1);

char bufferMilliseconds[millisecondsSize];
Expand Down

0 comments on commit 3e4a517

Please sign in to comment.