Skip to content

Commit

Permalink
Fix GetLogger with empty library name
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan committed Nov 10, 2023
1 parent 35a9362 commit 72473db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
21 changes: 9 additions & 12 deletions sdk/src/logs/logger_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ nostd::shared_ptr<opentelemetry::logs::Logger> 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<std::mutex> lock_guard{lock_};

Expand Down Expand Up @@ -84,18 +90,9 @@ nostd::shared_ptr<opentelemetry::logs::Logger> LoggerProvider::GetLogger(
}
*/

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-instrumentationscope
std::unique_ptr<instrumentationscope::InstrumentationScope> 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<instrumentationscope::InstrumentationScope> lib =
instrumentationscope::InstrumentationScope::Create(library_name, library_version, schema_url,
attributes);

loggers_.push_back(std::shared_ptr<opentelemetry::sdk::logs::Logger>(
new Logger(logger_name, context_, std::move(lib))));
Expand Down
14 changes: 14 additions & 0 deletions sdk/test/logs/logger_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ TEST(LoggerProviderSDK, EventLoggerProviderFactory)
auto event_logger = elp->CreateEventLogger(logger1, "otel-cpp.test");
}

TEST(LoggerPviderSDK, LoggerEquityCheck)
{
auto lp = std::shared_ptr<logs_api::LoggerProvider>(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:
Expand Down

0 comments on commit 72473db

Please sign in to comment.