Skip to content

Commit

Permalink
bug logging: fix OTLP logger forwarding
Browse files Browse the repository at this point in the history
commit_hash:a2a1547e3b2c9a714cb3368d7e3cfdd7dfce96cc
  • Loading branch information
segoon committed Jan 24, 2025
1 parent b54347f commit 6f713f4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
11 changes: 8 additions & 3 deletions otlp/src/otlp/logs/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "logger.hpp"

#include <chrono>
#include <iostream>

#include <userver/engine/async.hpp>
#include <userver/formats/parse/common_containers.hpp>
Expand Down Expand Up @@ -172,7 +171,7 @@ void Logger::PrependCommonTags(logging::impl::TagWriter writer) const {

bool Logger::DoShouldLog(logging::Level level) const noexcept { return logging::impl::default_::DoShouldLog(level); }

void Logger::Log(logging::Level, logging::impl::formatters::LoggerItemRef item) {
void Logger::Log(logging::Level level, logging::impl::formatters::LoggerItemRef item) {
UASSERT(dynamic_cast<Item*>(&item));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
auto& log = static_cast<Item&>(item);
Expand All @@ -184,10 +183,16 @@ void Logger::Log(logging::Level, logging::impl::formatters::LoggerItemRef item)
++stats_.dropped;
}
}

if (default_logger_ && log.forwarded_formatter) {
auto& fwd_item = log.forwarded_formatter->ExtractLoggerItem();
default_logger_->Log(level, fwd_item);
}
}

logging::impl::formatters::BasePtr Logger::MakeFormatter(logging::Level level, logging::LogClass log_class) {
return std::make_unique<Formatter>(level, log_class, config_.logs_sink, default_logger_, *this);
auto sink = log_class == logging::LogClass::kLog ? config_.logs_sink : config_.tracing_sink;
return std::make_unique<Formatter>(level, log_class, sink, default_logger_, *this);
}

void Logger::SetAttributeValue(
Expand Down
16 changes: 15 additions & 1 deletion otlp/tests/service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <otlp/logs/logger.hpp>
#include <userver/engine/sleep.hpp>

#include <userver/logging/impl/mem_logger.hpp>
#include <userver/ugrpc/tests/service_fixtures.hpp>
#include <userver/utest/default_logger_fixture.hpp>

Expand Down Expand Up @@ -89,16 +90,20 @@ class TraceService final : public opentelemetry::proto::collector::trace::v1::Tr
class LogServiceTest : public Service<LogService, TraceService>, public utest::DefaultLoggerFixture<::testing::Test> {
public:
LogServiceTest() : Service({}) {
otlp::LoggerConfig config;
config.logs_sink = otlp::SinkType::kBoth;
logger_ = std::make_shared<otlp::Logger>(
MakeClient<opentelemetry::proto::collector::logs::v1::LogsServiceClient>(),
MakeClient<opentelemetry::proto::collector::trace::v1::TraceServiceClient>(),
otlp::LoggerConfig{}
otlp::LoggerConfig{config}
);
SetDefaultLogger(logger_);
}

~LogServiceTest() override { logger_->Stop(); }

otlp::Logger& GetLogger() { return *logger_; }

private:
std::shared_ptr<otlp::Logger> logger_;
};
Expand All @@ -112,6 +117,15 @@ UTEST_F(LogServiceTest, NoInfiniteLogsInTrace) {
EXPECT_EQ(GetService1().logs.size(), 1);
}

UTEST_F(LogServiceTest, ForwardLogs) {
auto mem_logger = std::make_shared<logging::impl::MemLogger>();

GetLogger().SetDefaultLogger(mem_logger);
LOG_INFO() << "dummy log";

EXPECT_EQ(mem_logger->GetPendingLogsCount(), 1);
}

UTEST_F(LogServiceTest, SmokeLogs) {
auto timestamp =
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch());
Expand Down
2 changes: 2 additions & 0 deletions universal/include/userver/logging/impl/mem_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class MemLogger final : public LoggerBase {

void DropLogs();

size_t GetPendingLogsCount();

protected:
bool DoShouldLog(Level) const noexcept override;

Expand Down
5 changes: 5 additions & 0 deletions universal/src/logging/impl/mem_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ void MemLogger::DropLogs() {
pimpl_->data_.clear();
}

size_t MemLogger::GetPendingLogsCount() {
std::unique_lock lock(pimpl_->mutex_);
return pimpl_->data_.size();
}

void MemLogger::Log(Level level, formatters::LoggerItemRef msg) {
UASSERT(dynamic_cast<formatters::LogItem*>(&msg));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
Expand Down

0 comments on commit 6f713f4

Please sign in to comment.