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

[API] Return NoopLogRecord from NoopLogger #2668

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 0 additions & 4 deletions api/include/opentelemetry/logs/event_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ class EventLogger
return;
}
nostd::unique_ptr<LogRecord> log_record = delegate_logger->CreateLogRecord();
if (!log_record)
{
return;
}

IgnoreTraitResult(
detail::LogRecordSetterTrait<typename std::decay<ArgumentType>::type>::template Set(
Expand Down
4 changes: 0 additions & 4 deletions api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ class Logger
void EmitLogRecord(ArgumentType &&... args)
{
nostd::unique_ptr<LogRecord> log_record = CreateLogRecord();
if (!log_record)
{
return;
}

EmitLogRecord(std::move(log_record), std::forward<ArgumentType>(args)...);
}
Expand Down
30 changes: 29 additions & 1 deletion api/include/opentelemetry/logs/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,39 @@ class NoopLogger final : public Logger
public:
const nostd::string_view GetName() noexcept override { return "noop logger"; }

nostd::unique_ptr<LogRecord> CreateLogRecord() noexcept override { return nullptr; }
nostd::unique_ptr<LogRecord> CreateLogRecord() noexcept override
{
/*
* Do not return memory shared between threads,
* a `new` + `delete` for each noop record can not be avoided,
* due to the semantic of unique_ptr.
*/
return nostd::unique_ptr<LogRecord>(new NoopLogRecord());
lalitb marked this conversation as resolved.
Show resolved Hide resolved
}

using Logger::EmitLogRecord;

void EmitLogRecord(nostd::unique_ptr<LogRecord> &&) noexcept override {}

private:
class NoopLogRecord : public LogRecord
{
public:
NoopLogRecord() = default;
~NoopLogRecord() override = default;

void SetTimestamp(common::SystemTimestamp /* timestamp */) noexcept override {}
void SetObservedTimestamp(common::SystemTimestamp /* timestamp */) noexcept override {}
void SetSeverity(logs::Severity /* severity */) noexcept override {}
void SetBody(const common::AttributeValue & /* message */) noexcept override {}
void SetAttribute(nostd::string_view /* key */,
const common::AttributeValue & /* value */) noexcept override
{}
void SetEventId(int64_t /* id */, nostd::string_view /* name */) noexcept override {}
void SetTraceId(const trace::TraceId & /* trace_id */) noexcept override {}
void SetSpanId(const trace::SpanId & /* span_id */) noexcept override {}
void SetTraceFlags(const trace::TraceFlags & /* trace_flags */) noexcept override {}
};
};

/**
Expand Down
4 changes: 3 additions & 1 deletion api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ TEST(Logger, GetLoggerDefault)
auto lp = Provider::GetLoggerProvider();
const std::string schema_url{"https://opentelemetry.io/schemas/1.11.0"};
auto logger = lp->GetLogger("TestLogger", "opentelelemtry_library", "", schema_url);
auto name = logger->GetName();
EXPECT_NE(nullptr, logger);
auto name = logger->GetName();
marcalff marked this conversation as resolved.
Show resolved Hide resolved
auto record = logger->CreateLogRecord();
EXPECT_EQ(name, "noop logger");
EXPECT_NE(nullptr, record);
}

// Test the two additional overloads for GetLogger()
Expand Down
6 changes: 0 additions & 6 deletions sdk/src/logs/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ const nostd::string_view Logger::GetName() noexcept

nostd::unique_ptr<opentelemetry::logs::LogRecord> Logger::CreateLogRecord() noexcept
{
// If this logger does not have a processor, no need to create a log recordable
if (!context_)
marcalff marked this conversation as resolved.
Show resolved Hide resolved
{
return nullptr;
}

auto recordable = context_->GetProcessor().MakeRecordable();

recordable->SetObservedTimestamp(std::chrono::system_clock::now());
Expand Down