Skip to content

Commit

Permalink
loggers vector
Browse files Browse the repository at this point in the history
  • Loading branch information
E-Saei committed Dec 18, 2021
1 parent 7007659 commit 0e4df67
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 1 addition & 3 deletions sdk/include/opentelemetry/sdk/logs/logger_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# include <memory>
# include <mutex>
# include <string>
# include <unordered_map>
# include <vector>

# include "opentelemetry/logs/logger_provider.h"
Expand Down Expand Up @@ -84,8 +83,7 @@ class LoggerProvider final : public opentelemetry::logs::LoggerProvider,
opentelemetry::sdk::common::AtomicSharedPtr<LogProcessor> processor_;

// A vector of pointers to all the loggers that have been created
std::unordered_map<std::string, opentelemetry::nostd::shared_ptr<opentelemetry::logs::Logger>>
loggers_;
std::vector<std::shared_ptr<opentelemetry::sdk::logs::Logger>> loggers_;

// A mutex that ensures only one thread is using the map of loggers
std::mutex mu_;
Expand Down
16 changes: 9 additions & 7 deletions sdk/src/logs/logger_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ nostd::shared_ptr<opentelemetry::logs::Logger> LoggerProvider::GetLogger(
std::lock_guard<std::mutex> lock_guard{mu_};

// If a logger with a name "logger_name" already exists, return it
auto loggerkv = loggers_.find(logger_name.data());
if (loggerkv != loggers_.end())
for (auto &logger : loggers_)
{
return nostd::shared_ptr<logs_api::Logger>(loggerkv->second);
auto& logger_lib = logger->GetInstrumentationLibrary();
if (logger->GetName() == logger_name && logger_lib.equal(library_name, library_version, schema_url))
{
return nostd::shared_ptr<opentelemetry::logs::Logger>{logger};
}
}

// Check if creating a new logger would exceed the max number of loggers
Expand All @@ -50,10 +53,9 @@ nostd::shared_ptr<opentelemetry::logs::Logger> LoggerProvider::GetLogger(

auto lib = instrumentationlibrary::InstrumentationLibrary::Create(library_name, library_version,
schema_url);
nostd::shared_ptr<logs_api::Logger> logger(
new Logger(logger_name, this->shared_from_this(), std::move(lib)));
loggers_[logger_name.data()] = logger;
return logger;
loggers_.push_back(std::shared_ptr<opentelemetry::sdk::logs::Logger>(
new Logger(logger_name, this->shared_from_this(), std::move(lib))));
return nostd::shared_ptr<opentelemetry::logs::Logger>{loggers_.back()};
}

nostd::shared_ptr<opentelemetry::logs::Logger> LoggerProvider::GetLogger(
Expand Down
2 changes: 1 addition & 1 deletion sdk/test/logs/logger_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TEST(LoggerSDK, LogToAProcessor)
// 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", "", schema_url);
auto logger2 = lp->GetLogger("logger", "", "lib_name", "", schema_url);
ASSERT_EQ(logger, logger2);

auto sdk_logger = static_cast<opentelemetry::sdk::logs::Logger *>(logger.get());
Expand Down

0 comments on commit 0e4df67

Please sign in to comment.