Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
otlp_writer.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
33#include "base_writer.h"
36
38
39#include <atomic>
40#include <chrono>
41#include <condition_variable>
42#include <memory>
43#include <mutex>
44#include <queue>
45#include <string>
46#include <thread>
47#include <unordered_map>
48#include <vector>
49
50namespace kcenon::logger {
51
75public:
80 enum class protocol_type {
81 http,
82 grpc
83 };
84
89 struct config {
96 std::string endpoint = "http://localhost:4318/v1/logs";
97
101 protocol_type protocol = protocol_type::http;
102
106 std::chrono::milliseconds timeout{5000};
107
111 bool use_tls = false;
112
116 std::string service_name;
117
121 std::string service_version;
122
126 std::string service_namespace;
127
132
136 std::unordered_map<std::string, std::string> resource_attributes;
137
141 std::size_t max_batch_size = 512;
142
146 std::chrono::milliseconds flush_interval{5000};
147
151 std::size_t max_queue_size = 10000;
152
156 std::size_t max_retries = 3;
157
161 std::chrono::milliseconds retry_delay{100};
162
166 std::unordered_map<std::string, std::string> headers;
167 };
168
174 uint64_t logs_exported{0};
175 uint64_t logs_dropped{0};
176 uint64_t export_success{0};
177 uint64_t export_failures{0};
178 uint64_t retries{0};
179 std::chrono::system_clock::time_point last_export;
180 std::chrono::system_clock::time_point last_error;
181 };
182
188 std::atomic<uint64_t> logs_exported{0};
189 std::atomic<uint64_t> logs_dropped{0};
190 std::atomic<uint64_t> export_success{0};
191 std::atomic<uint64_t> export_failures{0};
192 std::atomic<uint64_t> retries{0};
193 std::chrono::system_clock::time_point last_export;
194 std::chrono::system_clock::time_point last_error;
195 };
196
201 explicit otlp_writer(const config& cfg);
202
206 ~otlp_writer() override;
207
208 // Non-copyable, non-movable
209 otlp_writer(const otlp_writer&) = delete;
213
220 common::VoidResult write(const log_entry& entry) override;
221
225 common::VoidResult flush() override;
226
230 std::string get_name() const override { return "otlp"; }
231
235 bool is_healthy() const override;
236
240 export_stats get_stats() const;
241
245 void force_export();
246
247private:
248 // Background export thread
249 void export_thread_func();
250
251 // Export batch to collector
252 bool export_batch(const std::vector<log_entry>& batch);
253
254 // Convert log level to OTLP severity
255 static int to_otlp_severity(common::interfaces::log_level level);
256
257#ifdef LOGGER_HAS_OTLP
258 // Export using OpenTelemetry SDK
259 bool export_with_otel_sdk(const std::vector<log_entry>& batch);
260#else
261 // Fallback: Export using HTTP directly
262 bool export_with_http(const std::vector<log_entry>& batch);
263
264 // JSON string escaping helper
265 static std::string escape_json(const std::string& str);
266#endif
267
268private:
271
272 // Queue management
273 std::queue<log_entry> queue_;
274 mutable std::mutex queue_mutex_;
275 std::condition_variable queue_cv_;
276
277 // Background thread
278 std::unique_ptr<std::thread> export_thread_;
279 std::atomic<bool> running_{false};
280 std::atomic<bool> healthy_{true};
281
282#ifdef LOGGER_HAS_OTLP
283 // OpenTelemetry SDK components (forward declared, implemented in cpp)
284 class otel_impl;
285 std::unique_ptr<otel_impl> otel_impl_;
286#endif
287};
288
289} // namespace kcenon::logger
Abstract base class for all log output writers kcenon.
Abstract base class for all log output writers.
Definition base_writer.h:95
OTLP log exporter for OpenTelemetry integration.
Definition otlp_writer.h:74
otlp_writer(const otlp_writer &)=delete
std::unique_ptr< std::thread > export_thread_
otlp_writer(otlp_writer &&)=delete
std::string get_name() const override
Get writer name.
protocol_type
Transport protocol for OTLP export.
Definition otlp_writer.h:80
std::condition_variable queue_cv_
otlp_writer & operator=(const otlp_writer &)=delete
std::queue< log_entry > queue_
otlp_writer & operator=(otlp_writer &&)=delete
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
OpenTelemetry context structure for trace correlation kcenon.
Tag interface for asynchronous writers.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
Configuration for OTLP writer.
Definition otlp_writer.h:89
std::unordered_map< std::string, std::string > resource_attributes
Custom resource attributes.
std::unordered_map< std::string, std::string > headers
HTTP headers for authentication.
std::string service_namespace
Service namespace.
std::string service_name
Service name (required for resource attributes)
std::string service_instance_id
Service instance ID.
std::string service_version
Service version.
Statistics snapshot for OTLP export (copyable)
std::chrono::system_clock::time_point last_error
std::chrono::system_clock::time_point last_export
Internal atomic statistics (non-copyable)
std::chrono::system_clock::time_point last_error
std::chrono::system_clock::time_point last_export
Writer category interfaces and type traits kcenon.