LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
OtlpPayloadSplitter.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_OTLP_OTLPPAYLOADSPLITTER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_OTLP_OTLPPAYLOADSPLITTER_HPP_INCLUDED
4
7
9#include <string>
10#include <vector>
11
12namespace logit {
13
20 inline std::vector<std::string> build_otlp_logs_json_payload_chunks(
21 const std::vector<OtlpLogItem>& batch,
23 std::size_t max_payload_bytes) {
24 if (max_payload_bytes == 0) {
25 std::vector<std::string> chunks;
26 chunks.push_back(build_otlp_logs_json_payload(batch, format));
27 return chunks;
28 }
29
30 if (batch.empty()) {
31 return {};
32 }
33
34 std::vector<std::string> chunks;
35 std::vector<OtlpLogItem> current_payload;
36
37 for (std::size_t i = 0; i < batch.size(); ++i) {
38 std::vector<OtlpLogItem> candidate_payload = current_payload;
39 candidate_payload.push_back(batch[i]);
40
41 std::string candidate_json = build_otlp_logs_json_payload(candidate_payload, format);
42
43 if (candidate_json.size() <= max_payload_bytes) {
44 current_payload = std::move(candidate_payload);
45 } else if (current_payload.empty()) {
46 // Single oversized record: emit it as its own chunk anyway.
47 chunks.push_back(std::move(candidate_json));
48 } else {
49 // Close current chunk and start a new one with this record.
50 chunks.push_back(build_otlp_logs_json_payload(current_payload, format));
51 current_payload.clear();
52 current_payload.push_back(batch[i]);
53 }
54 }
55
56 if (!current_payload.empty()) {
57 chunks.push_back(build_otlp_logs_json_payload(current_payload, format));
58 }
59
60 return chunks;
61 }
62
63} // namespace logit
64
65#endif // LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_OTLP_OTLPPAYLOADSPLITTER_HPP_INCLUDED
Defines OTLP/HTTP JSON serialization helpers for logs.
The primary namespace for the LogIt++ library.
std::string format(const char *fmt,...)
Formats a string according to the specified format.
Definition format.hpp:22
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.
std::string build_otlp_logs_json_payload(const std::vector< OtlpLogItem > &batch, const OtlpJsonFormatConfig &config)
Builds an OTLP ExportLogsServiceRequest JSON payload.
Serialization settings for OTLP JSON payload construction.