From 9ff0be5650ddd0a9aba0951b0a28befe3927354e Mon Sep 17 00:00:00 2001 From: Vikman Fernandez-Castro Date: Tue, 17 Dec 2024 08:51:32 +0100 Subject: [PATCH] feat(logcollector): format event.created as ISO8601 timestamp --- src/common/pal/CMakeLists.txt | 1 + src/modules/logcollector/CMakeLists.txt | 5 +++- src/modules/logcollector/src/logcollector.cpp | 25 ++----------------- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/common/pal/CMakeLists.txt b/src/common/pal/CMakeLists.txt index 98fffe954f..57ad14693b 100644 --- a/src/common/pal/CMakeLists.txt +++ b/src/common/pal/CMakeLists.txt @@ -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) diff --git a/src/modules/logcollector/CMakeLists.txt b/src/modules/logcollector/CMakeLists.txt index e776954929..3620324796 100644 --- a/src/modules/logcollector/CMakeLists.txt +++ b/src/modules/logcollector/CMakeLists.txt @@ -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 @@ -45,6 +47,7 @@ target_link_libraries(Logcollector nlohmann_json::nlohmann_json PRIVATE Logger + pal ) include(../../cmake/ConfigureTarget.cmake) diff --git a/src/modules/logcollector/src/logcollector.cpp b/src/modules/logcollector/src/logcollector.cpp index 876d70511c..f774c80bfb 100644 --- a/src/modules/logcollector/src/logcollector.cpp +++ b/src/modules/logcollector/src/logcollector.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -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(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";