From 75d76a1cbb949e88bd2a8801b31120a95af082df Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Thu, 11 May 2023 18:08:31 +0800 Subject: [PATCH] Add unit test for log body implicit conversions. (#2136) --- sdk/test/logs/log_record_test.cc | 255 +++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) diff --git a/sdk/test/logs/log_record_test.cc b/sdk/test/logs/log_record_test.cc index 3ad3acaca5..204827bfb0 100644 --- a/sdk/test/logs/log_record_test.cc +++ b/sdk/test/logs/log_record_test.cc @@ -3,6 +3,13 @@ #ifdef ENABLE_LOGS_PREVIEW +# include +# include +# include +# include + +# include "opentelemetry/logs/logger.h" +# include "opentelemetry/logs/provider.h" # include "opentelemetry/nostd/variant.h" # include "opentelemetry/sdk/logs/read_write_log_record.h" # include "opentelemetry/trace/span_id.h" @@ -69,4 +76,252 @@ TEST(ReadWriteLogRecord, SetAndGet) ASSERT_EQ(record.GetTraceFlags(), trace_flags); ASSERT_EQ(record.GetTimestamp().time_since_epoch(), now.time_since_epoch()); } + +// Define a basic Logger class +class TestBodyLogger : public opentelemetry::logs::Logger +{ +public: + const nostd::string_view GetName() noexcept override { return "test body logger"; } + + nostd::unique_ptr CreateLogRecord() noexcept override + { + return nostd::unique_ptr(new ReadWriteLogRecord()); + } + + using opentelemetry::logs::Logger::EmitLogRecord; + + void EmitLogRecord(nostd::unique_ptr &&record) noexcept override + { + if (record) + { + last_body_ = static_cast(record.get())->GetBody(); + } + } + + const opentelemetry::v1::common::AttributeValue &GetLastLogRecord() const noexcept + { + return last_body_; + } + +private: + opentelemetry::v1::common::AttributeValue last_body_; +}; + +// Define a basic LoggerProvider class that returns an instance of the logger class defined above +class TestBodyProvider : public opentelemetry::logs::LoggerProvider +{ +public: + using opentelemetry::logs::LoggerProvider::GetLogger; + + nostd::shared_ptr GetLogger( + nostd::string_view /* logger_name */, + nostd::string_view /* library_name */, + nostd::string_view /* library_version */, + nostd::string_view /* schema_url */, + bool /* include_trace_context */, + const opentelemetry::common::KeyValueIterable & /* attributes */) override + { + return nostd::shared_ptr(new TestBodyLogger()); + } +}; + +TEST(LogBody, BodyConversation) +{ + // Push the new loggerprovider class into the global singleton + TestBodyProvider lp; + + // Check that the implementation was pushed by calling TestLogger's GetName() + nostd::string_view schema_url{"https://opentelemetry.io/schemas/1.11.0"}; + auto logger = + lp.GetLogger("TestBodyProvider", "opentelelemtry_library", "", schema_url, false, {}); + + auto real_logger = static_cast(logger.get()); + + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, true); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())); + ASSERT_TRUE(opentelemetry::nostd::get(real_logger->GetLastLogRecord())); + + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, static_cast(123)); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())); + ASSERT_EQ(123, opentelemetry::nostd::get(real_logger->GetLastLogRecord())); + + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, static_cast(124)); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())); + ASSERT_EQ(124, opentelemetry::nostd::get(real_logger->GetLastLogRecord())); + + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, static_cast(125)); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())); + ASSERT_EQ(125, opentelemetry::nostd::get(real_logger->GetLastLogRecord())); + + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, static_cast(126)); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())); + ASSERT_EQ(126, opentelemetry::nostd::get(real_logger->GetLastLogRecord())); + + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, static_cast(127.0)); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())); + ASSERT_TRUE(std::abs(127.0 - opentelemetry::nostd::get(real_logger->GetLastLogRecord())) < + 0.0001); + + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, "128"); + ASSERT_TRUE( + opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord()) || + opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())); + if (opentelemetry::nostd::holds_alternative(real_logger->GetLastLogRecord())) + { + ASSERT_EQ(nostd::string_view{"128"}, + opentelemetry::nostd::get(real_logger->GetLastLogRecord())); + } + else + { + ASSERT_EQ(nostd::string_view{"128"}, + opentelemetry::nostd::get(real_logger->GetLastLogRecord())); + } + + { + bool data[] = {true, false, true}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>(real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_EQ(data_span[i], output[i]); + } + } + + { + int32_t data[] = {221, 222, 223}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>(real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_EQ(data_span[i], output[i]); + } + } + + { + uint32_t data[] = {231, 232, 233}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>(real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_EQ(data_span[i], output[i]); + } + } + + { + int64_t data[] = {241, 242, 243}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>(real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_EQ(data_span[i], output[i]); + } + } + + { + uint64_t data[] = {251, 252, 253}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>(real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_EQ(data_span[i], output[i]); + } + } + + { + uint8_t data[] = {161, 162, 163}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>(real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_EQ(data_span[i], output[i]); + } + } + + { + double data[] = {271.0, 272.0, 273.0}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>(real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_TRUE(std::abs(data_span[i] - output[i]) < 0.0001); + } + } + + { + std::string data_origin[] = {"281", "282", "283"}; + nostd::string_view data[] = {data_origin[0], data_origin[1], data_origin[2]}; + nostd::span data_span = data; + real_logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, data_span); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( + real_logger->GetLastLogRecord())); + + nostd::span output = + opentelemetry::nostd::get>( + real_logger->GetLastLogRecord()); + + ASSERT_EQ(data_span.size(), output.size()); + + for (size_t i = 0; i < data_span.size(); ++i) + { + ASSERT_EQ(data_span[i], output[i]); + } + } +} + #endif