diff --git a/sdk/src/logs/logger_provider.cc b/sdk/src/logs/logger_provider.cc index d64863ed73..46eaa01054 100644 --- a/sdk/src/logs/logger_provider.cc +++ b/sdk/src/logs/logger_provider.cc @@ -57,6 +57,12 @@ nostd::shared_ptr LoggerProvider::GetLogger( nostd::string_view schema_url, const opentelemetry::common::KeyValueIterable &attributes) noexcept { + // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-instrumentationscope + if (library_name.empty()) + { + library_name = logger_name; + } + // Ensure only one thread can read/write from the map of loggers std::lock_guard lock_guard{lock_}; @@ -84,18 +90,9 @@ nostd::shared_ptr LoggerProvider::GetLogger( } */ - // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-instrumentationscope - std::unique_ptr lib; - if (library_name.empty()) - { - lib = instrumentationscope::InstrumentationScope::Create(logger_name, library_version, - schema_url, attributes); - } - else - { - lib = instrumentationscope::InstrumentationScope::Create(library_name, library_version, - schema_url, attributes); - } + std::unique_ptr lib = + instrumentationscope::InstrumentationScope::Create(library_name, library_version, schema_url, + attributes); loggers_.push_back(std::shared_ptr( new Logger(logger_name, context_, std::move(lib)))); diff --git a/sdk/test/logs/logger_provider_sdk_test.cc b/sdk/test/logs/logger_provider_sdk_test.cc index 51f40178ba..ecb9ea6f9c 100644 --- a/sdk/test/logs/logger_provider_sdk_test.cc +++ b/sdk/test/logs/logger_provider_sdk_test.cc @@ -111,6 +111,20 @@ TEST(LoggerProviderSDK, EventLoggerProviderFactory) auto event_logger = elp->CreateEventLogger(logger1, "otel-cpp.test"); } +TEST(LoggerPviderSDK, LoggerEquityCheck) +{ + auto lp = std::shared_ptr(new LoggerProvider()); + nostd::string_view schema_url{"https://opentelemetry.io/schemas/1.11.0"}; + + auto logger1 = lp->GetLogger("logger1", "opentelelemtry_library", "", schema_url); + auto logger2 = lp->GetLogger("logger1", "opentelelemtry_library", "", schema_url); + EXPECT_EQ(logger1, logger2); + + auto logger3 = lp->GetLogger("logger3"); + auto another_logger3 = lp->GetLogger("logger3"); + EXPECT_EQ(logger3, another_logger3); +} + class DummyLogRecordable final : public opentelemetry::sdk::logs::Recordable { public: