Skip to content

Commit

Permalink
feat(logcollector): format event.created as ISO8601 timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
vikman90 committed Dec 17, 2024
1 parent 66bae5d commit 9ff0be5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/common/pal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if(NOT SOURCE_FILES)
endif()

add_library(pal STATIC ${SOURCE_FILES})
target_include_directories(pal PUBLIC include)
target_include_directories(pal PUBLIC include/${OS_NAME})

if(BUILD_TESTS)
Expand Down
5 changes: 4 additions & 1 deletion src/modules/logcollector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ add_library(Logcollector ${LOGCOLLECTOR_SOURCES})
target_include_directories(Logcollector PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../include
${COMMON_FOLDER})
${COMMON_FOLDER}
${COMMON_FOLDER}/stringHelper/include
${COMMON_FOLDER}/timeHelper/include)

target_link_libraries(Logcollector
PUBLIC
Expand All @@ -45,6 +47,7 @@ target_link_libraries(Logcollector
nlohmann_json::nlohmann_json
PRIVATE
Logger
pal
)

include(../../cmake/ConfigureTarget.cmake)
Expand Down
25 changes: 2 additions & 23 deletions src/modules/logcollector/src/logcollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <config.h>
#include <timeHelper.h>

#include <chrono>
#include <iomanip>
Expand Down Expand Up @@ -78,35 +79,13 @@ void Logcollector::SendMessage(const std::string& location, const std::string& l
auto metadata = nlohmann::json::object();
auto data = nlohmann::json::object();


auto getCurrentTimestamp = []() {
constexpr int MILLISECS_IN_A_SEC = 1000;
auto now = std::chrono::system_clock::now();
auto time_t_now = std::chrono::system_clock::to_time_t(now);
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() % MILLISECS_IN_A_SEC;

std::tm tm_now{};
#ifdef _WIN32
gmtime_s(&tm_now, &time_t_now); // MSVC (Windows)
#else
gmtime_r(&time_t_now, &tm_now); // Linux/macOS (POSIX)
#endif

// Formatear el timestamp
std::ostringstream oss;
oss << std::put_time(&tm_now, "%Y-%m-%dT%H:%M:%S") << '.'
<< std::setw(3) << std::setfill('0') << milliseconds << "Z"; // Usar 'milliseconds' como entero

return oss.str();
};

metadata["module"] = m_moduleName;
metadata["type"] = collectorType;

data["log"]["file"]["path"] = location;
data["tags"] = nlohmann::json::array({"mvp"});
data["event"]["original"] = log;
data["event"]["created"] = getCurrentTimestamp();
data["event"]["created"] = Utils::getCurrentISO8601();
data["event"]["module"] = m_moduleName;
data["event"]["provider"] = "syslog";

Expand Down

0 comments on commit 9ff0be5

Please sign in to comment.