diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index dbea61ec4a..724fc5006f 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -361,6 +361,18 @@ static void ConvertGenericMessageToJson(nlohmann::json &value, } } +static bool SerializeToHttpBody(http_client::Body &output, const google::protobuf::Message &message) +{ + auto body_size = message.ByteSizeLong(); + if (body_size > 0) + { + output.resize(body_size); + return message.SerializeWithCachedSizesToArray( + reinterpret_cast(&output[0])); + } + return true; +} + void ConvertGenericFieldToJson(nlohmann::json &value, const google::protobuf::Message &message, const google::protobuf::FieldDescriptor *field_descriptor, @@ -600,9 +612,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpClient::Export( std::string content_type; if (options_.content_type == HttpRequestContentType::kBinary) { - body_vec.resize(message.ByteSizeLong()); - if (message.SerializeWithCachedSizesToArray( - reinterpret_cast(&body_vec[0]))) + if (SerializeToHttpBody(body_vec, message)) { if (options_.console_debug) { diff --git a/sdk/src/logs/multi_log_processor.cc b/sdk/src/logs/multi_log_processor.cc index f9767b5e82..b73cc9e7a6 100644 --- a/sdk/src/logs/multi_log_processor.cc +++ b/sdk/src/logs/multi_log_processor.cc @@ -68,23 +68,39 @@ void MultiLogProcessor::OnReceive(std::unique_ptr &&record) noexcept bool MultiLogProcessor::ForceFlush(std::chrono::microseconds timeout) noexcept { - bool result = true; - auto start_time = std::chrono::system_clock::now(); - auto expire_time = start_time + timeout; + // Converto nanos to prevent overflow + std::chrono::nanoseconds timeout_ns = std::chrono::nanoseconds::max(); + if (std::chrono::duration_cast(timeout_ns) > timeout) + { + timeout_ns = std::chrono::duration_cast(timeout); + } + bool result = true; + auto start_time = std::chrono::system_clock::now(); + auto overflow_checker = std::chrono::system_clock::time_point::max(); + std::chrono::system_clock::time_point expire_time; + if (overflow_checker - start_time <= timeout_ns) + { + expire_time = overflow_checker; + } + else + { + expire_time = + start_time + std::chrono::duration_cast(timeout_ns); + } for (auto &processor : processors_) { - if (!processor->ForceFlush(timeout)) + if (!processor->ForceFlush(std::chrono::duration_cast(timeout_ns))) { result = false; } start_time = std::chrono::system_clock::now(); if (expire_time > start_time) { - timeout = std::chrono::duration_cast(expire_time - start_time); + timeout_ns = std::chrono::duration_cast(expire_time - start_time); } else { - timeout = std::chrono::microseconds::zero(); + timeout_ns = std::chrono::nanoseconds::zero(); } } return result; @@ -92,23 +108,39 @@ bool MultiLogProcessor::ForceFlush(std::chrono::microseconds timeout) noexcept bool MultiLogProcessor::Shutdown(std::chrono::microseconds timeout) noexcept { - bool result = true; - auto start_time = std::chrono::system_clock::now(); - auto expire_time = start_time + timeout; + // Converto nanos to prevent overflow + std::chrono::nanoseconds timeout_ns = std::chrono::nanoseconds::max(); + if (std::chrono::duration_cast(timeout_ns) > timeout) + { + timeout_ns = std::chrono::duration_cast(timeout); + } + bool result = true; + auto start_time = std::chrono::system_clock::now(); + auto overflow_checker = std::chrono::system_clock::time_point::max(); + std::chrono::system_clock::time_point expire_time; + if (overflow_checker - start_time <= timeout_ns) + { + expire_time = overflow_checker; + } + else + { + expire_time = + start_time + std::chrono::duration_cast(timeout_ns); + } for (auto &processor : processors_) { - if (!processor->Shutdown(timeout)) + if (!processor->Shutdown(std::chrono::duration_cast(timeout_ns))) { result = false; } start_time = std::chrono::system_clock::now(); if (expire_time > start_time) { - timeout = std::chrono::duration_cast(expire_time - start_time); + timeout_ns = std::chrono::duration_cast(expire_time - start_time); } else { - timeout = std::chrono::microseconds::zero(); + timeout_ns = std::chrono::nanoseconds::zero(); } } return result;