Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo committed Dec 17, 2021
1 parent 8cb1a58 commit 74bee16
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
8 changes: 5 additions & 3 deletions api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ namespace trace = opentelemetry::trace;
// Check that the default logger is a noop logger instance
TEST(Logger, GetLoggerDefault)
{
auto lp = Provider::GetLoggerProvider();
auto logger = lp->GetLogger("TestLogger");
auto lp = Provider::GetLoggerProvider();
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp->GetLogger("TestLogger", "", schema_url);
auto name = logger->GetName();
EXPECT_NE(nullptr, logger);
EXPECT_EQ(name, "noop logger");
Expand Down Expand Up @@ -95,7 +96,8 @@ TEST(Logger, PushLoggerImplementation)
auto lp = Provider::GetLoggerProvider();

// Check that the implementation was pushed by calling TestLogger's GetName()
auto logger = lp->GetLogger("TestLogger");
nostd::string_view schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp->GetLogger("TestLogger", "", schema_url);
ASSERT_EQ("test logger", logger->GetName());
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class LoggerProvider : public opentelemetry::logs::LoggerProvider
}

/**
* @brief Obtain ETW Logger.
* @brief Obtain ETW Tracer.
* @param name ProviderId (instrumentation name) - Name or GUID
*
* @param version Library version
Expand All @@ -223,7 +223,8 @@ class LoggerProvider : public opentelemetry::logs::LoggerProvider
nostd::string_view version = "",
nostd::string_view schema_url = "") override
{
UNREFERENCED_PARAMETER(args);
UNREFERENCED_PARAMETER(version);
UNREFERENCED_PARAMETER(schema_url);
ETWProvider::EventFormat evtFmt = config_.encoding;
return nostd::shared_ptr<opentelemetry::logs::Logger>{new (std::nothrow)
etw::Logger(*this, name, evtFmt)};
Expand Down
6 changes: 4 additions & 2 deletions exporters/etw/test/etw_logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ TEST(ETWLogger, LoggerCheckWithBody)
std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
exporter::etw::LoggerProvider lp;

auto logger = lp.GetLogger(providerName);
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp.GetLogger(providerName, "", schema_url);
Properties attribs = {{"attrib1", 1}, {"attrib2", 2}};
EXPECT_NO_THROW(
logger->Log(opentelemetry::logs::Severity::kDebug, "My Log", "This is test log body"));
Expand Down Expand Up @@ -90,7 +91,8 @@ TEST(ETWLogger, LoggerCheckWithAttributes)
std::string providerName = kGlobalProviderName; // supply unique instrumentation name here
exporter::etw::LoggerProvider lp;

auto logger = lp.GetLogger(providerName);
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp.GetLogger(providerName, "", schema_url);
// Log attributes
Properties attribs = {{"attrib1", 1}, {"attrib2", 2}};
EXPECT_NO_THROW(logger->Log(opentelemetry::logs::Severity::kDebug, "My Log", attribs));
Expand Down
5 changes: 0 additions & 5 deletions sdk/include/opentelemetry/sdk/logs/recordable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# include "opentelemetry/version.h"

# include <map>
# include <memory>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
Expand Down Expand Up @@ -101,10 +100,6 @@ class Recordable
virtual void SetInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library) noexcept = 0;

private:
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
*instrumentation_library_ = nullptr;
};
} // namespace logs
} // namespace sdk
Expand Down
12 changes: 12 additions & 0 deletions sdk/test/logs/logger_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ TEST(LoggerProviderSDK, LoggerProviderGetLoggerSimple)
ASSERT_NE(logger1, nullptr);
ASSERT_NE(logger2, nullptr);

auto sdk_logger1 = static_cast<opentelemetry::sdk::logs::Logger *>(logger1.get());
auto sdk_logger2 = static_cast<opentelemetry::sdk::logs::Logger *>(logger2.get());
ASSERT_EQ(sdk_logger1->GetInstrumentationLibrary().GetName(), "logger1");
ASSERT_EQ(sdk_logger1->GetInstrumentationLibrary().GetVersion(), "");
ASSERT_EQ(sdk_logger1->GetInstrumentationLibrary().GetSchemaURL(), schema_url);

ASSERT_EQ(sdk_logger2->GetInstrumentationLibrary().GetName(), "logger2");
ASSERT_EQ(sdk_logger2->GetInstrumentationLibrary().GetVersion(), "");
ASSERT_EQ(sdk_logger2->GetInstrumentationLibrary().GetSchemaURL(), schema_url);

// Check that two loggers with different names aren't the same instance
ASSERT_NE(logger1, logger2);

// Check that two loggers with the same name are the same instance
auto logger3 = lp->GetLogger("logger1");
ASSERT_EQ(logger1, logger3);
auto sdk_logger3 = static_cast<opentelemetry::sdk::logs::Logger *>(logger3.get());
ASSERT_EQ(sdk_logger3->GetInstrumentationLibrary(), sdk_logger1->GetInstrumentationLibrary());
}

class DummyProcessor : public LogProcessor
Expand Down
20 changes: 15 additions & 5 deletions sdk/test/logs/logger_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ TEST(LoggerSDK, LogToNullProcessor)
// even when there is no processor set
// since it calls Processor::OnReceive()

auto lp = std::shared_ptr<logs_api::LoggerProvider>(new LoggerProvider());
auto logger = lp->GetLogger("logger");

auto lp = std::shared_ptr<logs_api::LoggerProvider>(new LoggerProvider());
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = lp->GetLogger("logger", "", schema_url);

auto sdk_logger = static_cast<opentelemetry::sdk::logs::Logger *>(logger.get());
ASSERT_EQ(sdk_logger->GetInstrumentationLibrary().GetName(), "logger");
ASSERT_EQ(sdk_logger->GetInstrumentationLibrary().GetVersion(), "");
ASSERT_EQ(sdk_logger->GetInstrumentationLibrary().GetSchemaURL(), schema_url);
// Log a sample log record to a nullptr processor
logger->Debug("Test log");
}
Expand Down Expand Up @@ -66,15 +71,20 @@ TEST(LoggerSDK, LogToAProcessor)
{
// Create an API LoggerProvider and logger
auto api_lp = std::shared_ptr<logs_api::LoggerProvider>(new LoggerProvider());
auto logger = api_lp->GetLogger("logger");
const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"};
auto logger = api_lp->GetLogger("logger", "", schema_url);

// Cast the API LoggerProvider to an SDK Logger Provider and assert that it is still the same
// LoggerProvider by checking that getting a logger with the same name as the previously defined
// logger is the same instance
auto lp = static_cast<LoggerProvider *>(api_lp.get());
auto logger2 = lp->GetLogger("logger");
auto logger2 = lp->GetLogger("logger", "", schema_url);
ASSERT_EQ(logger, logger2);

auto sdk_logger = static_cast<opentelemetry::sdk::logs::Logger *>(logger.get());
ASSERT_EQ(sdk_logger->GetInstrumentationLibrary().GetName(), "logger");
ASSERT_EQ(sdk_logger->GetInstrumentationLibrary().GetVersion(), "");
ASSERT_EQ(sdk_logger->GetInstrumentationLibrary().GetSchemaURL(), schema_url);
// Set a processor for the LoggerProvider
auto shared_recordable = std::shared_ptr<LogRecord>(new LogRecord());
auto processor = std::shared_ptr<LogProcessor>(new MockProcessor(shared_recordable));
Expand Down

0 comments on commit 74bee16

Please sign in to comment.