2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_OTLPPAYLOADLOGGER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_OTLPPAYLOADLOGGER_HPP_INCLUDED
9# error "OtlpPayloadLogger requires LOGIT_WITH_OTLP=1. Enable LOGIT_WITH_OTLP in CMake."
19#include <condition_variable>
58 m_worker = std::thread(&OtlpPayloadLogger::worker_loop, this);
73 void log(
const LogRecord& record,
const std::string& message)
override {
86 std::lock_guard<std::mutex> lock(
m_mutex);
92 std::vector<OtlpLogItem> batch;
93 batch.push_back(item);
97 for (
auto& chunk : chunks) {
108 std::unique_lock<std::mutex> lock(
m_mutex);
141 std::unique_lock<std::mutex> lock(
m_mutex);
142 m_cv.wait(lock, [
this]() {
164 return std::string();
245 std::vector<OtlpLogItem> batch;
249 std::unique_lock<std::mutex> lock(
m_mutex);
254 return m_stopping || !m_queue.empty();
270 batch.push_back(
m_queue.front());
277 if (!batch.empty()) {
281 for (
auto& chunk : chunks) {
292 std::lock_guard<std::mutex> lock(
m_mutex);
302 std::lock_guard<std::mutex> lock(
m_mutex);
331 return now > last ? now - last : 0;
338 const uint64_t max_value =
static_cast<uint64_t
>((std::numeric_limits<int64_t>::max)());
339 return value > max_value ? (std::numeric_limits<int64_t>::max)() :
static_cast<int64_t
>(value);
Defines the interface for loggers used in the logging system.
Defines OTLP/HTTP JSON serialization helpers for logs.
Splits oversized OTLP log batches into multiple JSON payload chunks.
Optional buffered snapshot access returns empty results by default.
Exports logs as OTLP JSON payloads via a user-provided callback.
std::atomic< int > m_log_level
int64_t get_last_log_ts() const
Returns last log timestamp.
void set_log_level(LogLevel level) override
Sets minimal log level for this logger.
void shutdown() override
Stops the OTLP worker after draining queued items.
OtlpPayloadLogger & operator=(const OtlpPayloadLogger &)=delete
std::atomic< int64_t > m_last_log_ts
std::atomic< uint64_t > m_dropped
void log(const LogRecord &record, const std::string &message) override
Queues or exports a log message.
~OtlpPayloadLogger() override
Stops worker and drains queue.
int64_t get_time_since_last_log() const
Returns elapsed time since last log.
OtlpPayloadLogger(const Config &config)
Constructs OTLP payload logger with custom configuration.
static int64_t counter_to_int64(uint64_t value)
Converts unsigned counter value to int64_t with saturation.
void stop()
Stops worker and drains remaining queue.
std::condition_variable m_cv
double get_float_param(const LoggerParam ¶m) const override
Retrieves a floating-point parameter from the logger.
int64_t get_int_param(const LoggerParam ¶m) const override
Retrieves an integer parameter from the logger.
std::deque< OtlpLogItem > m_queue
OtlpPayloadLogger()
Constructs OTLP payload logger with default configuration.
void worker_loop()
Worker thread loop.
void wait() override
Waits until queue is empty and worker is idle.
LogLevel get_log_level() const override
Gets minimal log level for this logger.
std::atomic< uint64_t > m_failed_exports
OtlpPayloadLogger(const OtlpPayloadLogger &)=delete
uint64_t failed_export_count() const
Returns number of failed export attempts.
uint64_t dropped_count() const
Returns number of dropped records.
std::string get_string_param(const LoggerParam ¶m) const override
Retrieves a string parameter from the logger.
std::condition_variable m_space_cv
#define LOGIT_CURRENT_TIMESTAMP_MS()
Macro to get the current timestamp in milliseconds.
The primary namespace for the LogIt++ library.
@ LOG_LVL_TRACE
Trace level logging.
std::vector< std::string > build_otlp_logs_json_payload_chunks(const std::vector< OtlpLogItem > &batch, const OtlpJsonFormatConfig &format, std::size_t max_payload_bytes)
Builds OTLP JSON payload chunks from a batch, splitting when a chunk would exceed max_payload_bytes.
OtlpRecordSnapshot make_otlp_record_snapshot(const LogRecord &record)
Creates an owned snapshot from a LogRecord.
LoggerParam
Enumeration for different logger parameters that can be retrieved.
@ TimeSinceLastLog
The time elapsed since the last log in seconds.
@ LastLogTimestamp
The timestamp of the last log.
@ DroppedLogCount
Number of log records dropped by the backend.
@ FailedExportCount
Number of failed export attempts reported by the backend.
Stores log metadata and content.
const int64_t timestamp_ms
Timestamp in milliseconds.
Owns one formatted log message and its structured metadata.
OtlpRecordSnapshot record
Structured log metadata snapshot.
std::string message
Formatted message body.
std::function< void(std::string)> on_payload
unsigned export_interval_ms
std::size_t max_batch_size
OtlpJsonFormatConfig format
std::size_t max_payload_bytes
std::size_t max_queue_size