Skip to content

Commit

Permalink
[API] Return NoopLogRecord from NoopLogger (#2668)
Browse files Browse the repository at this point in the history
Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
  • Loading branch information
marcalff and yurishkuro authored May 16, 2024
1 parent 2db27dd commit 9c767df
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
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());
}

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();
EXPECT_EQ(name, "noop logger");
auto record = logger->CreateLogRecord();
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_)
{
return nullptr;
}

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

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

1 comment on commit 9c767df

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 9c767df Previous: 2db27dd Ratio
BM_SpinLockThrashing/1/process_time/real_time 0.7434369002100766 ms/iter 0.10600875327409104 ms/iter 7.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.