From f39ffb3448e22a39d4401f459b93408bcd535e65 Mon Sep 17 00:00:00 2001 From: owent Date: Wed, 15 Feb 2023 10:51:48 +0800 Subject: [PATCH 1/2] Fix missing ObservedTimestamp. Signed-off-by: owent --- sdk/src/logs/logger.cc | 3 +++ sdk/test/logs/logger_sdk_test.cc | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index 5fc58cf0ad..190bfe6f5a 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -40,6 +40,9 @@ nostd::unique_ptr Logger::CreateLogRecord() noex } auto recordable = context_->GetProcessor().MakeRecordable(); + + recordable->SetObservedTimestamp(std::chrono::system_clock::now()); + if (include_trace_context_ && opentelemetry::context::RuntimeContext::GetCurrent().HasKey(opentelemetry::trace::kSpanKey)) { diff --git a/sdk/test/logs/logger_sdk_test.cc b/sdk/test/logs/logger_sdk_test.cc index 848d9f19df..2127ccf459 100644 --- a/sdk/test/logs/logger_sdk_test.cc +++ b/sdk/test/logs/logger_sdk_test.cc @@ -3,6 +3,7 @@ #ifdef ENABLE_LOGS_PREVIEW +# include # include # include "opentelemetry/nostd/string_view.h" @@ -43,7 +44,16 @@ class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable public: void SetTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} - void SetObservedTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {} + void SetObservedTimestamp( + opentelemetry::common::SystemTimestamp observed_timestamp) noexcept override + { + observed_timestamp_ = observed_timestamp; + } + + const opentelemetry::common::SystemTimestamp &GetObservedTimestamp() const noexcept + { + return observed_timestamp_; + } opentelemetry::logs::Severity GetSeverity() const noexcept { return severity_; } @@ -138,6 +148,8 @@ class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable opentelemetry::trace::TraceFlags trace_flags_; std::string event_name_; std::string event_domain_; + opentelemetry::common::SystemTimestamp observed_timestamp_ = + std::chrono::system_clock::from_time_t(0); }; class MockProcessor final : public LogRecordProcessor @@ -196,6 +208,8 @@ TEST(LoggerSDK, LogToAProcessor) } opentelemetry::trace::Scope trace_scope{include_span}; + auto now = std::chrono::system_clock::now(); + auto sdk_logger = static_cast(logger.get()); ASSERT_EQ(sdk_logger->GetInstrumentationScope().GetName(), "opentelelemtry_library"); ASSERT_EQ(sdk_logger->GetInstrumentationScope().GetVersion(), ""); @@ -226,6 +240,10 @@ TEST(LoggerSDK, LogToAProcessor) std::string span_id_text_in_span{span_id_in_span, sizeof(span_id_in_span)}; ASSERT_EQ(trace_id_text_in_logger, trace_id_text_in_span); ASSERT_EQ(span_id_text_in_logger, span_id_text_in_span); + + ASSERT_GE( + static_cast(shared_recordable->GetObservedTimestamp()), + now); } TEST(LoggerSDK, EventLog) From 469eb1c57e58655391955bdd3be7ad1b8128e546 Mon Sep 17 00:00:00 2001 From: owent Date: Wed, 15 Feb 2023 11:52:56 +0800 Subject: [PATCH 2/2] Fix `MockLogRecordable` Signed-off-by: owent --- sdk/test/logs/logger_sdk_test.cc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sdk/test/logs/logger_sdk_test.cc b/sdk/test/logs/logger_sdk_test.cc index 2127ccf459..5f6467cd91 100644 --- a/sdk/test/logs/logger_sdk_test.cc +++ b/sdk/test/logs/logger_sdk_test.cc @@ -131,13 +131,14 @@ class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable void CopyFrom(const MockLogRecordable &other) { - severity_ = other.severity_; - body_ = other.body_; - trace_id_ = other.trace_id_; - span_id_ = other.span_id_; - trace_flags_ = other.trace_flags_; - event_name_ = other.event_name_; - event_domain_ = other.event_domain_; + severity_ = other.severity_; + body_ = other.body_; + trace_id_ = other.trace_id_; + span_id_ = other.span_id_; + trace_flags_ = other.trace_flags_; + event_name_ = other.event_name_; + event_domain_ = other.event_domain_; + observed_timestamp_ = other.observed_timestamp_; } private: