Skip to content

Commit

Permalink
Add stream interface
Browse files Browse the repository at this point in the history
  • Loading branch information
benibus committed Mar 12, 2024
1 parent 1f38ca4 commit ec6dcbd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
34 changes: 26 additions & 8 deletions cpp/src/arrow/telemetry/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ class OtelLogger : public Logger {
return;
}

const auto timestamp =
otel::common::SystemTimestamp(std::chrono::system_clock::now());

auto log = logger_->CreateLogRecord();
if (log == nullptr) {
return;
Expand All @@ -262,18 +259,15 @@ class OtelLogger : public Logger {

// We set the remaining attributes AFTER the custom attributes in AttributeHolder
// because, in the event of key collisions, these should take precedence.
log->SetTimestamp(timestamp);
log->SetTimestamp(otel::common::SystemTimestamp(desc.timestamp));
log->SetSeverity(ToOtelSeverity(desc.severity));

auto span_ctx = otel::trace::Tracer::GetCurrentSpan()->GetContext();
log->SetSpanId(span_ctx.span_id());
log->SetTraceId(span_ctx.trace_id());
log->SetTraceFlags(span_ctx.trace_flags());

if (desc.body) {
auto body = *desc.body;
log->SetBody(ToOtel(body));
}
log->SetBody(ToOtel(desc.body));

if (const auto& event = desc.event_id; event.is_valid()) {
log->SetEventId(event.id, ToOtel(event.name));
Expand Down Expand Up @@ -343,5 +337,29 @@ std::unique_ptr<Logger> GlobalLogger::logger_ = nullptr;

std::unique_ptr<Logger> MakeNoopLogger() { return std::make_unique<NoopLogger>(); }

class LogMessage::Impl {
public:
Impl(LogLevel severity, Logger* logger) : logger(logger), severity(severity) {}

~Impl() {
if (logger) {
auto body = stream.str();
LogDescriptor desc;
desc.body = body;
desc.severity = severity;
logger->Log(desc);
}
}

Logger* logger;
LogLevel severity;
std::stringstream stream;
};

LogMessage::LogMessage(LogLevel severity, Logger* logger)
: impl_(std::make_shared<Impl>(severity, logger)) {}

std::ostream& LogMessage::Stream() { return impl_->stream; }

} // namespace telemetry
} // namespace arrow
21 changes: 20 additions & 1 deletion cpp/src/arrow/telemetry/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ struct EventId {
struct LogDescriptor {
LogLevel severity = kDefaultSeverity;

std::optional<std::string_view> body = std::nullopt;
std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();

std::string_view body = "";

EventId event_id = EventId::Invalid();

Expand Down Expand Up @@ -190,5 +192,22 @@ class ARROW_EXPORT GlobalLogger {
static std::unique_ptr<Logger> logger_;
};

class ARROW_EXPORT LogMessage {
public:
explicit LogMessage(LogLevel, Logger*);

std::ostream& Stream();

template <typename T>
LogMessage& operator<<(const T& t) {
Stream() << t;
return *this;
}

private:
class Impl;
std::shared_ptr<Impl> impl_;
};

} // namespace telemetry
} // namespace arrow
3 changes: 3 additions & 0 deletions cpp/src/arrow/telemetry/telemetry_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class OtelEnvironment : public ::testing::Environment {
auto provider_options = LoggerProviderOptions::Defaults();
ASSERT_OK(GlobalLoggerProvider::Initialize(provider_options));
auto logging_options = LoggingOptions::Defaults();
logging_options.severity_threshold = LogLevel::ARROW_TRACE;
logging_options.flush_severity = LogLevel::ARROW_TRACE;
ASSERT_OK_AND_ASSIGN(
auto logger,
Expand Down Expand Up @@ -90,6 +91,8 @@ TEST_F(TestLogging, Basics) {
Log(LogLevel::ARROW_WARNING, "baz bal",
AttributeList{Attribute{"intAttr", 24}, Attribute{"boolAttr", true},
Attribute{"strAttr", std::string("ab") + "c"}});
LogMessage(LogLevel::ARROW_INFO, GlobalLogger::Get()) << "This is a "
<< "log message";
}

} // namespace telemetry
Expand Down

0 comments on commit ec6dcbd

Please sign in to comment.