Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Fix GetLogger with empty library name #2398

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Increment the:
[#2385](https://github.com/open-telemetry/opentelemetry-cpp/pull/2385)
* [API] Add a new AddLink() operation to Span
[#2380](https://github.com/open-telemetry/opentelemetry-cpp/pull/2380)
* [SDK] Fix GetLogger with empty library
name[#2398](https://github.com/open-telemetry/opentelemetry-cpp/pull/2398)

Important changes:

Expand Down
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