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] support for the new OTel log API #2123

Merged
merged 20 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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: 3 additions & 1 deletion api/include/opentelemetry/logs/event_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ namespace logs
class EventId
{
public:
EventId(int64_t id, nostd::string_view name) noexcept
EventId(int64_t id, nostd::string_view name) noexcept : id_{id}
ThomsonTan marked this conversation as resolved.
Show resolved Hide resolved
{
id_ = id;
name_ = nostd::unique_ptr<char[]>{new char[name.length() + 1]};
std::copy(name.begin(), name.end(), name_.get());
name_.get()[name.length()] = 0;
}

EventId(int64_t id) noexcept : id_{id}, name_{nullptr} {}

public:
int64_t id_;
nostd::unique_ptr<char[]> name_;
Expand Down
8 changes: 8 additions & 0 deletions api/include/opentelemetry/logs/log_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class LogRecord
virtual void SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept = 0;

/**
* Set the Event Id.
* @param id The event id to set
* @param name Optional event name to set
*/
// TODO: mark this as pure virtual once all exporters have been updated
virtual void SetEventId(int64_t id, nostd::string_view name = {}) noexcept = 0;

/**
* Set the trace id for this log.
* @param trace_id the trace id to set
Expand Down
14 changes: 7 additions & 7 deletions api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class Logger
nostd::string_view format,
const common::KeyValueIterable &attributes) noexcept
{
this->EmitLogRecord(severity, event_id, format, attributes);
this->EmitLogRecord(severity, EventId{event_id}, format, attributes);
}

virtual void Log(Severity severity,
Expand Down Expand Up @@ -318,7 +318,7 @@ class Logger
nostd::string_view format,
const common::KeyValueIterable &attributes) noexcept
{
this->Log(Severity::kTrace, event_id, format, attributes);
this->Log(Severity::kTrace, EventId{event_id}, format, attributes);
}

inline void Trace(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
Expand All @@ -339,7 +339,7 @@ class Logger
nostd::string_view format,
const common::KeyValueIterable &attributes) noexcept
{
this->Log(Severity::kDebug, event_id, format, attributes);
this->Log(Severity::kDebug, EventId{event_id}, format, attributes);
}

inline void Debug(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
Expand All @@ -360,7 +360,7 @@ class Logger
nostd::string_view format,
const common::KeyValueIterable &attributes) noexcept
{
this->Log(Severity::kInfo, event_id, format, attributes);
this->Log(Severity::kInfo, EventId{event_id}, format, attributes);
}

inline void Info(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
Expand All @@ -381,7 +381,7 @@ class Logger
nostd::string_view format,
const common::KeyValueIterable &attributes) noexcept
{
this->Log(Severity::kWarn, event_id, format, attributes);
this->Log(Severity::kWarn, EventId{event_id}, format, attributes);
}

inline void Warn(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
Expand All @@ -402,7 +402,7 @@ class Logger
nostd::string_view format,
const common::KeyValueIterable &attributes) noexcept
{
this->Log(Severity::kError, event_id, format, attributes);
this->Log(Severity::kError, EventId{event_id}, format, attributes);
}

inline void Error(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
Expand All @@ -423,7 +423,7 @@ class Logger
nostd::string_view format,
const common::KeyValueIterable &attributes) noexcept
{
this->Log(Severity::kFatal, event_id, format, attributes);
this->Log(Severity::kFatal, EventId{event_id}, format, attributes);
}

inline void Fatal(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
Expand Down
5 changes: 3 additions & 2 deletions api/include/opentelemetry/logs/logger_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ template <>
struct LogRecordSetterTrait<EventId>
{
template <class ArgumentType>
inline static LogRecord *Set(LogRecord *log_record, ArgumentType && /*arg*/) noexcept
inline static LogRecord *Set(LogRecord *log_record, ArgumentType &&arg) noexcept
{
// TODO: set log_record
log_record->SetEventId(arg.id_, nostd::string_view{arg.name_.get()});

return log_record;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ class ElasticSearchRecordable final : public sdk::logs::Recordable
*/
void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override;

/**
* Set the Event Id
* @param id the event id to set
* @param name the event name to set
*/
void SetEventId(int64_t /* id */, nostd::string_view /* name */) noexcept override
{
// TODO: implement event id
}

/**
* Set the trace id for this log.
* @param trace_id the trace id to set
Expand Down
4 changes: 4 additions & 0 deletions exporters/ostream/src/log_record_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ sdk::common::ExportResult OStreamLogRecordExporter::Export(
continue;
}

int64_t event_id = log_record->GetEventId();

// Convert trace, spanid, traceflags into exportable representation
constexpr int trace_id_len = 32;
constexpr int span_id__len = 16;
Expand Down Expand Up @@ -96,6 +98,8 @@ sdk::common::ExportResult OStreamLogRecordExporter::Export(
printAttributes(log_record->GetAttributes(), "\n ");

sout_ << "\n"
<< " event_id : " << event_id << "\n"
<< " event_name : " << log_record->GetEventName() << "\n"
<< " trace_id : " << std::string(trace_id, trace_id_len) << "\n"
<< " span_id : " << std::string(span_id, span_id__len) << "\n"
<< " trace_flags : " << std::string(trace_flags, trace_flags_len) << "\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class OtlpLogRecordable final : public opentelemetry::sdk::logs::Recordable
*/
void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override;

/**
* @brief Set the Event Id for this log.
* @param id the event Id to set
* @param name the event name to set
*/
void SetEventId(int64_t /* id */, nostd::string_view /* name */) noexcept override
Copy link
Member

@owent owent May 3, 2023

Choose a reason for hiding this comment

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

Sorry but did I miss anything? I can find any specification about event id in OTLP, should we add it just like semantic convention for event attributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the idea is adding it as attribute to OTLP recordable, mark it as TODO for now.

{
// TODO: export Event Id to OTLP
}

/**
* Set the trace id for this log.
* @param trace_id the trace id to set
Expand Down
8 changes: 8 additions & 0 deletions sdk/include/opentelemetry/sdk/logs/multi_recordable.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class MultiRecordable final : public Recordable
*/
void SetBody(const opentelemetry::common::AttributeValue &message) noexcept override;

/**
* Set the Event Id
* @param id the event id to set
* @param name the event name to set
*/
// TODO: implement set event id
void SetEventId(int64_t /* id */, nostd::string_view /* name */) noexcept override {}
owent marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set the trace id for this log.
* @param trace_id the trace id to set
Expand Down
22 changes: 22 additions & 0 deletions sdk/include/opentelemetry/sdk/logs/read_write_log_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ class ReadWriteLogRecord final : public ReadableLogRecord
*/
const opentelemetry::common::AttributeValue &GetBody() const noexcept override;

/**
* Set the Event Id object
* @param id the event Id to set
* @param name the event name to set
*/
void SetEventId(int64_t id, nostd::string_view name) noexcept override;

/**
* Get event Id of this log.
* @return the event Id of this log.
*/
int64_t GetEventId() const noexcept override;

/**
* Get event name of this log.
* @return the event name of this log.
*/
nostd::string_view GetEventName() const noexcept override;

/**
* Set the trace id for this log.
* @param trace_id the trace id to set
Expand Down Expand Up @@ -176,6 +195,9 @@ class ReadWriteLogRecord final : public ReadableLogRecord
opentelemetry::common::SystemTimestamp timestamp_;
opentelemetry::common::SystemTimestamp observed_timestamp_;

int64_t event_id_;
std::string event_name_;

// We do not pay for trace state when not necessary
struct TraceState
{
Expand Down
12 changes: 12 additions & 0 deletions sdk/include/opentelemetry/sdk/logs/readable_log_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ class ReadableLogRecord : public Recordable
*/
virtual const opentelemetry::common::AttributeValue &GetBody() const noexcept = 0;

/**
* Get the Event id.
* @return the event id
*/
virtual int64_t GetEventId() const noexcept = 0;

/**
* Get the Event Name.
* @return the event name
*/
virtual nostd::string_view GetEventName() const noexcept = 0;

/**
* Get the trace id of this log.
* @return the trace id of this log
Expand Down
20 changes: 19 additions & 1 deletion sdk/src/logs/read_write_log_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ ReadWriteLogRecord::ReadWriteLogRecord()
resource_(nullptr),
instrumentation_scope_(nullptr),
body_(nostd::string_view()),
observed_timestamp_(std::chrono::system_clock::now())
observed_timestamp_(std::chrono::system_clock::now()),
event_id_(0),
event_name_("")
{}

ReadWriteLogRecord::~ReadWriteLogRecord() {}
Expand Down Expand Up @@ -65,6 +67,22 @@ const opentelemetry::common::AttributeValue &ReadWriteLogRecord::GetBody() const
return body_;
}

void ReadWriteLogRecord::SetEventId(int64_t id, nostd::string_view name) noexcept
{
event_id_ = id;
event_name_ = std::string{name};
}

int64_t ReadWriteLogRecord::GetEventId() const noexcept
{
return event_id_;
}

nostd::string_view ReadWriteLogRecord::GetEventName() const noexcept
{
return nostd::string_view{event_name_};
}

void ReadWriteLogRecord::SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept
{
if (!trace_state_)
Expand Down
2 changes: 2 additions & 0 deletions sdk/test/logs/batch_log_record_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable

void SetBody(const std::string &message) noexcept { body_ = message; }

void SetEventId(int64_t, nostd::string_view) noexcept override {}

void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {}

void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {}
Expand Down
2 changes: 2 additions & 0 deletions sdk/test/logs/logger_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class DummyLogRecordable final : public opentelemetry::sdk::logs::Recordable

void SetBody(const opentelemetry::common::AttributeValue &) noexcept override {}

void SetEventId(int64_t, nostd::string_view) noexcept override {}

void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {}

void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {}
Expand Down
8 changes: 8 additions & 0 deletions sdk/test/logs/logger_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable

void SetBody(const std::string &message) noexcept { body_ = message; }

void SetEventId(int64_t id, nostd::string_view name) noexcept override
{
event_id_ = id;
log_record_event_name_ = static_cast<std::string>(name);
}

void SetTraceId(const opentelemetry::trace::TraceId &trace_id) noexcept override
{
trace_id_ = trace_id;
Expand Down Expand Up @@ -144,6 +150,8 @@ class MockLogRecordable final : public opentelemetry::sdk::logs::Recordable
private:
opentelemetry::logs::Severity severity_ = opentelemetry::logs::Severity::kInvalid;
std::string body_;
int64_t event_id_;
std::string log_record_event_name_;
opentelemetry::trace::TraceId trace_id_;
opentelemetry::trace::SpanId span_id_;
opentelemetry::trace::TraceFlags trace_flags_;
Expand Down
2 changes: 2 additions & 0 deletions sdk/test/logs/simple_log_record_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class TestLogRecordable final : public opentelemetry::sdk::logs::Recordable

void SetBody(const char *message) noexcept { body_ = message; }

void SetEventId(int64_t, nostd::string_view) noexcept override {}

void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {}

void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {}
Expand Down