From 094ebf55e7c0c986c681c7604a550ab1585e5861 Mon Sep 17 00:00:00 2001 From: Seufert Date: Thu, 3 Dec 2020 12:28:43 -0700 Subject: [PATCH 1/5] Added Log overloads, GetName() method, fixed LogRecord attributes and resources --- api/include/opentelemetry/logs/logger.h | 3 + api/test/logs/BUILD | 4 +- api/test/logs/CMakeLists.txt | 11 +-- api/test/logs/logger_test.cc | 84 +++++++++++++++---- ...gger_provider_test.cc => provider_test.cc} | 0 sdk/include/opentelemetry/sdk/logs/logger.h | 6 ++ sdk/src/logs/logger.cc | 5 ++ 7 files changed, 86 insertions(+), 27 deletions(-) rename api/test/logs/{logger_provider_test.cc => provider_test.cc} (100%) diff --git a/api/include/opentelemetry/logs/logger.h b/api/include/opentelemetry/logs/logger.h index fc486ec464..7e181d576b 100644 --- a/api/include/opentelemetry/logs/logger.h +++ b/api/include/opentelemetry/logs/logger.h @@ -44,6 +44,9 @@ class Logger public: virtual ~Logger() = default; + /* Returns the name of the logger */ + virtual const nostd::string_view GetName() noexcept = 0; + /** * Each of the following overloaded Log(...) methods * creates a log message with the specific parameters passed. diff --git a/api/test/logs/BUILD b/api/test/logs/BUILD index 899400b2e8..cba6f19220 100644 --- a/api/test/logs/BUILD +++ b/api/test/logs/BUILD @@ -1,9 +1,9 @@ load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") cc_test( - name = "logger_provider_test", + name = "provider_test", srcs = [ - "logger_provider_test.cc", + "provider_test.cc", ], deps = [ "//api", diff --git a/api/test/logs/CMakeLists.txt b/api/test/logs/CMakeLists.txt index 9b1b3b0e7d..8f797b97c1 100644 --- a/api/test/logs/CMakeLists.txt +++ b/api/test/logs/CMakeLists.txt @@ -1,9 +1,6 @@ -foreach(testname logger_provider_test logger_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} +foreach(testname provider_test logger_test) + add_executable(logs_api_${testname} "${testname}.cc") + target_link_libraries(logs_api_${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) - gtest_add_tests( - TARGET ${testname} - TEST_PREFIX logs. - TEST_LIST ${testname}) + gtest_add_tests(TARGET logs_api_${testname} TEST_PREFIX logs. TEST_LIST logs_api_${testname}) endforeach() diff --git a/api/test/logs/logger_test.cc b/api/test/logs/logger_test.cc index d875a54c35..ee37254b98 100644 --- a/api/test/logs/logger_test.cc +++ b/api/test/logs/logger_test.cc @@ -14,28 +14,28 @@ using opentelemetry::nostd::shared_ptr; using opentelemetry::nostd::span; using opentelemetry::nostd::string_view; -TEST(Logger, GetLoggerDefault) +TEST(LoggerTest, GetLoggerDefaultNoop) { auto lp = Provider::GetLoggerProvider(); auto logger = lp->GetLogger("TestLogger"); EXPECT_NE(nullptr, logger); + EXPECT_EQ(logger->GetName(), "noop logger"); } -TEST(Logger, GetNoopLoggerName) -{ - auto lp = Provider::GetLoggerProvider(); - auto logger = lp->GetLogger("TestLogger"); -} - -TEST(Logger, GetNoopLoggerNameWithArgs) +TEST(LoggerTest, GetLogger) { auto lp = Provider::GetLoggerProvider(); + // Get a logger with no arguments + auto logger1 = lp->GetLogger("TestLogger1"); + + // Get a logger with options passed + auto logger2 = lp->GetLogger("TestLogger2", "Options"); + + // Get a logger with arguments std::array sv{"string"}; span args{sv}; - auto logger = lp->GetLogger("NoopLoggerWithArgs", args); - // should probably also test that arguments were set properly too - // by adding a getArgs() method in NoopLogger + auto logger3 = lp->GetLogger("TestLogger3", args); } TEST(Logger, NoopLog) @@ -74,19 +74,67 @@ class TestProvider : public LoggerProvider } }; -TEST(Logger, PushLoggerImplementation) +TEST(LoggerTest, PushLoggerImplementation) { - // Push the new loggerprovider class into the global singleton + // Push the new loggerprovider class into the API auto test_provider = shared_ptr(new TestProvider()); Provider::SetLoggerProvider(test_provider); auto lp = Provider::GetLoggerProvider(); - // GetLogger(name, options) function + // Get a logger instance and check whether it's GetName() method returns + // "test logger" as defined in the custom implementation auto logger = lp->GetLogger("TestLogger"); + ASSERT_EQ("test logger", logger->GetName()); +} - // GetLogger(name, args) function - std::array sv{"string"}; - span args{sv}; - auto logger2 = lp->GetLogger("TestLogger2", args); +TEST(Logger, LogMethodOverloads) +{ + // Use the same TestProvider and TestLogger from the previous test + auto test_provider = shared_ptr(new TestProvider()); + Provider::SetLoggerProvider(test_provider); + + auto lp = Provider::GetLoggerProvider(); + auto logger = lp->GetLogger("TestLogger"); + + // Check that calling the Log() overloads correctly constructs a log record which is automatically put into the static logger_ for testing + + // Test Log(severity, name, message) method + logger->Log(Severity::kError, "Log Name", "This is the log message"); + ASSERT_EQ(record_->severity, Severity::kError); + ASSERT_EQ(record_->name, "Log Name"); + ASSERT_EQ(record_->body, "This is the log message"); + + // Test Trace(name, KVIterable) method + std::map m = {{"key1", "val1"}, {"key2", "val2"}}; + logger->Trace("Logging a map", m); + ASSERT_EQ(record_->severity, Severity::kTrace); + ASSERT_EQ(record_->name, "Logging a map"); + ASSERT_EQ(record_->attributes->size(), 2); +} + +TEST(LogRecord, SetDefault) +{ + LogRecord r; + + // Check that the timestamp is set to 0 by default + ASSERT_EQ(r.timestamp, opentelemetry::core::SystemTimestamp(std::chrono::seconds(0))); + + // Check that the severity is set to kDefault by default + ASSERT_EQ(r.severity, Severity::kDefault); + + // Check that trace_id is set to all zeros by default + char trace_buf[32]; + r.trace_id.ToLowerBase16(trace_buf); + ASSERT_EQ(std::string(trace_buf, sizeof(trace_buf)), "00000000000000000000000000000000"); + + // Check that span_id is set to all zeros by default + char span_buf[16]; + r.span_id.ToLowerBase16(span_buf); + ASSERT_EQ(std::string(span_buf, sizeof(span_buf)), "0000000000000000"); + + // Check that trace_flags is set to all zeros by default + char flags_buf[2]; + r.trace_flags.ToLowerBase16(flags_buf); + ASSERT_EQ(std::string(flags_buf, sizeof(flags_buf)), "00"); } diff --git a/api/test/logs/logger_provider_test.cc b/api/test/logs/provider_test.cc similarity index 100% rename from api/test/logs/logger_provider_test.cc rename to api/test/logs/provider_test.cc diff --git a/sdk/include/opentelemetry/sdk/logs/logger.h b/sdk/include/opentelemetry/sdk/logs/logger.h index 385ba3586b..8cf21a09f6 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger.h +++ b/sdk/include/opentelemetry/sdk/logs/logger.h @@ -34,11 +34,17 @@ class Logger final : public opentelemetry::logs::Logger public: /** * Initialize a new logger. + * @param name The name of this logger instance * @param logger_provider The logger provider that owns this logger. */ explicit Logger(opentelemetry::nostd::string_view name, std::shared_ptr logger_provider) noexcept; + /** + * Returns the name of this logger. + */ + const opentelemetry::nostd::string_view GetName() noexcept override; + /** * Writes a log record into the processor. * @param severity the severity level of the log event. diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index 6e201da04e..ad87ca505a 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -27,6 +27,11 @@ Logger::Logger(opentelemetry::nostd::string_view name, : logger_name_(std::string(name)), logger_provider_(logger_provider) {} +const opentelemetry::nostd::string_view Logger::GetName() noexcept +{ + return logger_name_; +} + /** * Create and populate recordable with the log event's fields passed in. * The timestamp, severity, traceid, spanid, and traceflags, are injected From 6b9b3126d27e1aded6050332d087fd8e46700455 Mon Sep 17 00:00:00 2001 From: Seufert Date: Tue, 15 Dec 2020 21:39:36 -0700 Subject: [PATCH 2/5] Rebased, overloads with initializer list --- api/include/opentelemetry/logs/logger.h | 454 +++++++++++++++++++++++- api/include/opentelemetry/logs/noop.h | 2 + api/test/logs/logger_test.cc | 107 ++---- 3 files changed, 483 insertions(+), 80 deletions(-) diff --git a/api/include/opentelemetry/logs/logger.h b/api/include/opentelemetry/logs/logger.h index 7e181d576b..c7e4896165 100644 --- a/api/include/opentelemetry/logs/logger.h +++ b/api/include/opentelemetry/logs/logger.h @@ -126,25 +126,457 @@ class Logger /** Wrapper methods that the user could call for convenience when logging **/ - // Set default values for unspecified fields, then call the base Log() method - void Log(Severity severity, nostd::string_view message, core::SystemTimestamp timestamp) noexcept + /** + * Writes a log. + * @param severity The severity of the log + * @param message The message to log + */ + void Log(Severity severity, nostd::string_view message) noexcept { - this->Log(severity, "", message, {}, {}, {}, {}, {}, timestamp); + this->Log(severity, "", message, {}, {}, {}, {}, {}, std::chrono::system_clock::now()); } - // Set default time, and call base Log(severity, message, time) method - void Log(Severity severity, nostd::string_view message) noexcept + /** + * Writes a log. + * @param severity The severity of the log + * @param name The name of the log + * @param message The message to log + */ + void Log(Severity severity, nostd::string_view name, nostd::string_view message) noexcept + { + this->Log(severity, name, message, {}, {}, {}, {}, {}, std::chrono::system_clock::now()); + } + + /** + * Writes a log. + * @param severity The severity of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + inline void Log(Severity severity, const T &attributes) noexcept + { + this->Log(severity, "", "", std::map{}, attributes, {}, {}, {}, + std::chrono::system_clock::now()); + } + + /** + * Writes a log. + * @param severity The severity of the log + * @param name The name of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + inline void Log(Severity severity, nostd::string_view name, const T &attributes) noexcept + { + this->Log(severity, name, "", std::map{}, attributes, {}, {}, {}, + std::chrono::system_clock::now()); + } + + /** + * Writes a log. + * @param severity The severity of the log + * @param attributes The attributes of the log as an initializer list + */ + void Log(Severity severity, + std::initializer_list> + attributes) noexcept + { + this->Log(severity, "", "", {}, attributes, {}, {}, {}, std::chrono::system_clock::now()); + } + + /** + * Writes a log. + * @param severity The severity of the log + * @param name The name of the log + * @param attributes The attributes of the log as an initializer list + */ + void Log(Severity severity, + nostd::string_view name, + std::initializer_list> + attributes) noexcept + { + this->Log(severity, name, "", {}, attributes, {}, {}, {}, std::chrono::system_clock::now()); + } + + /** Trace severity overloads **/ + + /** + * Writes a log with a severity of trace. + * @param message The message to log + */ + void Trace(nostd::string_view message) noexcept { this->Log(Severity::kTrace, message); } + + /** + * Writes a log with a severity of trace. + * @param name The name of the log + * @param message The message to log + */ + void Trace(nostd::string_view name, nostd::string_view message) noexcept + { + this->Log(Severity::kTrace, name, message); + } + + /** + * Writes a log with a severity of trace. + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Trace(const T &attributes) noexcept + { + this->Log(Severity::kTrace, attributes); + } + + /** + * Writes a log with a severity of trace. + * @param name The name of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Trace(nostd::string_view name, const T &attributes) noexcept + { + this->Log(Severity::kTrace, name, attributes); + } + + /** + * Writes a log with a severity of trace. + * @param attributes The attributes of the log as an initializer list + */ + void Trace(std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kTrace, attributes); + } + + /** + * Writes a log with a severity of trace. + * @param name The name of the log + * @param attributes The attributes of the log as an initializer list + */ + void Trace(nostd::string_view name, + std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kTrace, name, attributes); + } + + /** Debug severity overloads **/ + + /** + * Writes a log with a severity of debug. + * @param message The message to log + */ + void Debug(nostd::string_view message) noexcept { this->Log(Severity::kDebug, message); } + + /** + * Writes a log with a severity of debug. + * @param name The name of the log + * @param message The message to log + */ + void Debug(nostd::string_view name, nostd::string_view message) noexcept + { + this->Log(Severity::kDebug, name, message); + } + + /** + * Writes a log with a severity of debug. + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Debug(const T &attributes) noexcept + { + this->Log(Severity::kDebug, attributes); + } + + /** + * Writes a log with a severity of debug. + * @param name The name of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Debug(nostd::string_view name, const T &attributes) noexcept + { + this->Log(Severity::kDebug, name, attributes); + } + + /** + * Writes a log with a severity of debug. + * @param attributes The attributes of the log as an initializer list + */ + void Debug(std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kDebug, attributes); + } + + /** + * Writes a log with a severity of debug. + * @param name The name of the log + * @param attributes The attributes of the log as an initializer list + */ + void Debug(nostd::string_view name, + std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kDebug, name, attributes); + } + + /** Info severity overloads **/ + + /** + * Writes a log with a severity of info. + * @param message The message to log + */ + void Info(nostd::string_view message) noexcept { this->Log(Severity::kInfo, message); } + + /** + * Writes a log with a severity of info. + * @param name The name of the log + * @param message The message to log + */ + void Info(nostd::string_view name, nostd::string_view message) noexcept + { + this->Log(Severity::kInfo, name, message); + } + + /** + * Writes a log with a severity of info. + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Info(const T &attributes) noexcept + { + this->Log(Severity::kInfo, attributes); + } + + /** + * Writes a log with a severity of info. + * @param name The name of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Info(nostd::string_view name, const T &attributes) noexcept + { + this->Log(Severity::kInfo, name, attributes); + } + + /** + * Writes a log with a severity of info. + * @param attributes The attributes of the log as an initializer list + */ + void Info(std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kInfo, attributes); + } + + /** + * Writes a log with a severity of info. + * @param name The name of the log + * @param attributes The attributes of the log as an initializer list + */ + void Info(nostd::string_view name, + std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kInfo, name, attributes); + } + + /** Warn severity overloads **/ + + /** + * Writes a log with a severity of warn. + * @param message The message to log + */ + void Warn(nostd::string_view message) noexcept { this->Log(Severity::kWarn, message); } + + /** + * Writes a log with a severity of warn. + * @param name The name of the log + * @param message The message to log + */ + void Warn(nostd::string_view name, nostd::string_view message) noexcept + { + this->Log(Severity::kWarn, name, message); + } + + /** + * Writes a log with a severity of warn. + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Warn(const T &attributes) noexcept { - this->Log(severity, message, std::chrono::system_clock::now()); + this->Log(Severity::kWarn, attributes); } - // Set default severity then call Log(Severity, String message) method - void Log(nostd::string_view message) noexcept { this->Log(Severity::kInfo, message); } + /** + * Writes a log with a severity of warn. + * @param name The name of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Warn(nostd::string_view name, const T &attributes) noexcept + { + this->Log(Severity::kWarn, name, attributes); + } - // TODO: Add more overloaded Log(...) methods with different combiantions of parameters. + /** + * Writes a log with a severity of warn. + * @param attributes The attributes of the log as an initializer list + */ + void Warn(std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kWarn, attributes); + } - // TODO: Add function aliases such as void debug(), void trace(), void info(), etc. for each - // severity level + /** + * Writes a log with a severity of warn. + * @param name The name of the log + * @param attributes The attributes of the log as an initializer list + */ + void Warn(nostd::string_view name, + std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kWarn, name, attributes); + } + + /** Error severity overloads **/ + + /** + * Writes a log with a severity of error. + * @param message The message to log + */ + void Error(nostd::string_view message) noexcept { this->Log(Severity::kError, message); } + + /** + * Writes a log with a severity of error. + * @param name The name of the log + * @param message The message to log + */ + void Error(nostd::string_view name, nostd::string_view message) noexcept + { + this->Log(Severity::kError, name, message); + } + + /** + * Writes a log with a severity of error. + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Error(const T &attributes) noexcept + { + this->Log(Severity::kError, attributes); + } + + /** + * Writes a log with a severity of error. + * @param name The name of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Error(nostd::string_view name, const T &attributes) noexcept + { + this->Log(Severity::kError, name, attributes); + } + + /** + * Writes a log with a severity of error. + * @param attributes The attributes of the log as an initializer list + */ + void Error(std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kError, attributes); + } + + /** + * Writes a log with a severity of error. + * @param name The name of the log + * @param attributes The attributes of the log as an initializer list + */ + void Error(nostd::string_view name, + std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kError, name, attributes); + } + + /** Fatal severity overloads **/ + + /** + * Writes a log with a severity of fatal. + * @param message The message to log + */ + void Fatal(nostd::string_view message) noexcept { this->Log(Severity::kFatal, message); } + + /** + * Writes a log with a severity of fatal. + * @param name The name of the log + * @param message The message to log + */ + void Fatal(nostd::string_view name, nostd::string_view message) noexcept + { + this->Log(Severity::kFatal, name, message); + } + + /** + * Writes a log with a severity of fatal. + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Fatal(const T &attributes) noexcept + { + this->Log(Severity::kFatal, attributes); + } + + /** + * Writes a log with a severity of fatal. + * @param name The name of the log + * @param attributes The attributes of the log as a key/value object + */ + template ::value> * = nullptr> + void Fatal(nostd::string_view name, const T &attributes) noexcept + { + this->Log(Severity::kFatal, name, attributes); + } + + /** + * Writes a log with a severity of fatal. + * @param attributes The attributes of the log as an initializer list + */ + void Fatal(std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kFatal, attributes); + } + + /** + * Writes a log with a severity of fatal. + * @param name The name of the log + * @param attributes The attributes of the log as an initializer list + */ + void Fatal(nostd::string_view name, + std::initializer_list> + attributes) noexcept + { + this->Log(Severity::kFatal, name, attributes); + } }; } // namespace logs OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/logs/noop.h b/api/include/opentelemetry/logs/noop.h index 8a19e1a677..86738692c1 100644 --- a/api/include/opentelemetry/logs/noop.h +++ b/api/include/opentelemetry/logs/noop.h @@ -50,6 +50,8 @@ namespace logs class NoopLogger final : public Logger { public: + const nostd::string_view GetName() noexcept override { return "noop logger"; } + void Log(Severity severity, nostd::string_view name, nostd::string_view body, diff --git a/api/test/logs/logger_test.cc b/api/test/logs/logger_test.cc index ee37254b98..de03c8d403 100644 --- a/api/test/logs/logger_test.cc +++ b/api/test/logs/logger_test.cc @@ -14,40 +14,61 @@ using opentelemetry::nostd::shared_ptr; using opentelemetry::nostd::span; using opentelemetry::nostd::string_view; -TEST(LoggerTest, GetLoggerDefaultNoop) +// Check that the default logger is a noop logger instance +TEST(Logger, GetLoggerDefault) { auto lp = Provider::GetLoggerProvider(); auto logger = lp->GetLogger("TestLogger"); + auto name = logger->GetName(); EXPECT_NE(nullptr, logger); - EXPECT_EQ(logger->GetName(), "noop logger"); + EXPECT_EQ(name, "noop logger"); } -TEST(LoggerTest, GetLogger) +// Test the two additional overloads for GetLogger() +TEST(Logger, GetNoopLoggerNameWithArgs) { auto lp = Provider::GetLoggerProvider(); - // Get a logger with no arguments - auto logger1 = lp->GetLogger("TestLogger1"); - - // Get a logger with options passed - auto logger2 = lp->GetLogger("TestLogger2", "Options"); - - // Get a logger with arguments + // GetLogger(name, list(args)) std::array sv{"string"}; span args{sv}; - auto logger3 = lp->GetLogger("TestLogger3", args); + lp->GetLogger("NoopLoggerWithArgs", args); + + // GetLogger(name, string options) + lp->GetLogger("NoopLoggerWithOptions", "options"); } -TEST(Logger, NoopLog) +// Test the Log() overloads +TEST(Logger, LogMethodOverloads) { auto lp = Provider::GetLoggerProvider(); auto logger = lp->GetLogger("TestLogger"); - logger->Log("Noop log name"); + + // Create a map to test the logs with + std::map m = {{"key1", "value1"}}; + + // Log overloads + logger->Log(Severity::kTrace, "Test log message"); + logger->Log(Severity::kInfo, "Logging a message", "Test log message"); + logger->Log(Severity::kDebug, m); + logger->Log(Severity::kWarn, "Logging a map", m); + logger->Log(Severity::kError, {{"key1", "value 1"}, {"key2", 2}}); + logger->Log(Severity::kFatal, "Logging an initializer list", {{"key1", "value 1"}, {"key2", 2}}); + + // Severity methods + logger->Trace("Test log message"); + logger->Debug("Logging a message", "Test log message"); + logger->Info(m); + logger->Warn("Logging a map", m); + logger->Error({{"key1", "value 1"}, {"key2", 2}}); + logger->Fatal("Logging an initializer list", {{"key1", "value 1"}, {"key2", 2}}); } // Define a basic Logger class class TestLogger : public Logger { + const opentelemetry::nostd::string_view GetName() noexcept override { return "test logger"; } + void Log(Severity severity, string_view name, string_view body, @@ -74,67 +95,15 @@ class TestProvider : public LoggerProvider } }; -TEST(LoggerTest, PushLoggerImplementation) +TEST(Logger, PushLoggerImplementation) { - // Push the new loggerprovider class into the API + // Push the new loggerprovider class into the global singleton auto test_provider = shared_ptr(new TestProvider()); Provider::SetLoggerProvider(test_provider); auto lp = Provider::GetLoggerProvider(); - // Get a logger instance and check whether it's GetName() method returns - // "test logger" as defined in the custom implementation + // Check that the implementation was pushed by calling TestLogger's GetName() auto logger = lp->GetLogger("TestLogger"); ASSERT_EQ("test logger", logger->GetName()); -} - -TEST(Logger, LogMethodOverloads) -{ - // Use the same TestProvider and TestLogger from the previous test - auto test_provider = shared_ptr(new TestProvider()); - Provider::SetLoggerProvider(test_provider); - - auto lp = Provider::GetLoggerProvider(); - auto logger = lp->GetLogger("TestLogger"); - - // Check that calling the Log() overloads correctly constructs a log record which is automatically put into the static logger_ for testing - - // Test Log(severity, name, message) method - logger->Log(Severity::kError, "Log Name", "This is the log message"); - ASSERT_EQ(record_->severity, Severity::kError); - ASSERT_EQ(record_->name, "Log Name"); - ASSERT_EQ(record_->body, "This is the log message"); - - // Test Trace(name, KVIterable) method - std::map m = {{"key1", "val1"}, {"key2", "val2"}}; - logger->Trace("Logging a map", m); - ASSERT_EQ(record_->severity, Severity::kTrace); - ASSERT_EQ(record_->name, "Logging a map"); - ASSERT_EQ(record_->attributes->size(), 2); -} - -TEST(LogRecord, SetDefault) -{ - LogRecord r; - - // Check that the timestamp is set to 0 by default - ASSERT_EQ(r.timestamp, opentelemetry::core::SystemTimestamp(std::chrono::seconds(0))); - - // Check that the severity is set to kDefault by default - ASSERT_EQ(r.severity, Severity::kDefault); - - // Check that trace_id is set to all zeros by default - char trace_buf[32]; - r.trace_id.ToLowerBase16(trace_buf); - ASSERT_EQ(std::string(trace_buf, sizeof(trace_buf)), "00000000000000000000000000000000"); - - // Check that span_id is set to all zeros by default - char span_buf[16]; - r.span_id.ToLowerBase16(span_buf); - ASSERT_EQ(std::string(span_buf, sizeof(span_buf)), "0000000000000000"); - - // Check that trace_flags is set to all zeros by default - char flags_buf[2]; - r.trace_flags.ToLowerBase16(flags_buf); - ASSERT_EQ(std::string(flags_buf, sizeof(flags_buf)), "00"); -} +} \ No newline at end of file From 363e594a98df88af52a9f2b6262b9fe47e3fe4a7 Mon Sep 17 00:00:00 2001 From: Seufert Date: Tue, 15 Dec 2020 22:04:02 -0700 Subject: [PATCH 3/5] Fixed SDK tests to use new API overloads --- sdk/test/logs/logger_sdk_test.cc | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sdk/test/logs/logger_sdk_test.cc b/sdk/test/logs/logger_sdk_test.cc index e1993f15ec..669ae2fbc0 100644 --- a/sdk/test/logs/logger_sdk_test.cc +++ b/sdk/test/logs/logger_sdk_test.cc @@ -31,7 +31,7 @@ TEST(LoggerSDK, LogToNullProcessor) auto logger = lp->GetLogger("logger"); // Log a sample log record to a nullptr processor - logger->Log("Test log"); + logger->Debug("Test log"); } class MockProcessor final : public LogProcessor @@ -56,11 +56,11 @@ class MockProcessor final : public LogProcessor // Cast the recordable received into a concrete LogRecord type auto copy = std::shared_ptr(static_cast(record.release())); - // Copy over the received log record's name, body, timestamp fields over to the recordable + // Copy over the received log record's severity, name, and body fields over to the recordable // passed in the constructor record_received_->SetSeverity(copy->GetSeverity()); + record_received_->SetName(copy->GetName()); record_received_->SetBody(copy->GetBody()); - record_received_->SetTimestamp(copy->GetTimestamp()); } bool ForceFlush(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept { @@ -92,10 +92,9 @@ TEST(LoggerSDK, LogToAProcessor) ASSERT_EQ(processor, lp->GetProcessor()); // Check that the recordable created by the Log() statement is set properly - opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now()); - logger->Log(opentelemetry::logs::Severity::kWarn, "Message", now); + logger->Log(opentelemetry::logs::Severity::kWarn, "Log Name", "Log Message"); ASSERT_EQ(shared_recordable->GetSeverity(), opentelemetry::logs::Severity::kWarn); - ASSERT_EQ(shared_recordable->GetBody(), "Message"); - ASSERT_EQ(shared_recordable->GetTimestamp().time_since_epoch(), now.time_since_epoch()); + ASSERT_EQ(shared_recordable->GetName(), "Log Name"); + ASSERT_EQ(shared_recordable->GetBody(), "Log Message"); } From 070a66b5d268f11871e9981c0365131dfc92e675 Mon Sep 17 00:00:00 2001 From: Seufert Date: Tue, 15 Dec 2020 22:49:46 -0700 Subject: [PATCH 4/5] formatting --- api/test/logs/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/test/logs/CMakeLists.txt b/api/test/logs/CMakeLists.txt index 8f797b97c1..a5f9c04084 100644 --- a/api/test/logs/CMakeLists.txt +++ b/api/test/logs/CMakeLists.txt @@ -2,5 +2,8 @@ foreach(testname provider_test logger_test) add_executable(logs_api_${testname} "${testname}.cc") target_link_libraries(logs_api_${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) - gtest_add_tests(TARGET logs_api_${testname} TEST_PREFIX logs. TEST_LIST logs_api_${testname}) + gtest_add_tests( + TARGET logs_api_${testname} + TEST_PREFIX logs. + TEST_LIST logs_api_${testname}) endforeach() From 4310cfcd366df9d8901a6d133be4fa93381d2eff Mon Sep 17 00:00:00 2001 From: Seufert Date: Fri, 18 Dec 2020 13:25:39 -0700 Subject: [PATCH 5/5] Rebased with ostream, changed test to pass CI --- exporters/ostream/test/ostream_log_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/ostream/test/ostream_log_test.cc b/exporters/ostream/test/ostream_log_test.cc index e91528b20a..b091a71b3d 100644 --- a/exporters/ostream/test/ostream_log_test.cc +++ b/exporters/ostream/test/ostream_log_test.cc @@ -253,7 +253,7 @@ TEST(OStreamLogExporter, IntegrationTest) // Write a log to ostream exporter opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now()); - logger->Log(opentelemetry::logs::Severity::kDebug, "Hello", now); + logger->Log(opentelemetry::logs::Severity::kDebug, "", "Hello", {}, {}, {}, {}, {}, now); // Restore cout's original streambuf std::cout.rdbuf(original);