From 314a8fd12c997f59e2f3a7036b6ea7dcd2344f50 Mon Sep 17 00:00:00 2001 From: Babul Kumar Date: Fri, 17 Jul 2026 19:52:54 +0530 Subject: [PATCH 1/3] fix(ext): enable TCP keepalive for curl HTTP client connections --- ext/src/http/client/curl/http_operation_curl.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ext/src/http/client/curl/http_operation_curl.cc b/ext/src/http/client/curl/http_operation_curl.cc index 0f1bda4035..6a1eb0b292 100644 --- a/ext/src/http/client/curl/http_operation_curl.cc +++ b/ext/src/http/client/curl/http_operation_curl.cc @@ -1219,6 +1219,12 @@ CURLcode HttpOperation::Setup() return rc; } + rc = SetCurlLongOption(CURLOPT_TCP_KEEPALIVE, 1L); + if (rc != CURLE_OK) + { + return rc; + } + if (reuse_connection_) { rc = SetCurlLongOption(CURLOPT_FRESH_CONNECT, 0L); From 765872f9b1e273cc62dd8f0f20b56fc9097293c4 Mon Sep 17 00:00:00 2001 From: Babul Kumar Date: Wed, 19 Aug 2026 10:09:03 +0530 Subject: [PATCH 2/3] fix(ext): make TCP keepalive configurable for HTTP client and exporters (#4266) - Make TCP keepalive opt-in ( default ) - Add and options - Propagate options through Request, OtlpHttpClient, and all OTLP HTTP exporters - Add unit tests for keepalive option propagation and curl request settings --- CHANGELOG.md | 15 ++++ .../exporters/otlp/otlp_http_client.h | 51 ++++++++++++- .../otlp/otlp_http_exporter_options.h | 41 +++++++++++ .../otlp_http_log_record_exporter_options.h | 41 +++++++++++ .../otlp/otlp_http_metric_exporter_options.h | 41 +++++++++++ exporters/otlp/src/otlp_http_client.cc | 2 + exporters/otlp/src/otlp_http_exporter.cc | 69 +++++++++++++++--- .../otlp/src/otlp_http_exporter_options.cc | 5 +- .../otlp/src/otlp_http_log_record_exporter.cc | 69 +++++++++++++++--- .../otlp_http_log_record_exporter_options.cc | 5 +- .../otlp/src/otlp_http_metric_exporter.cc | 69 +++++++++++++++--- .../src/otlp_http_metric_exporter_options.cc | 5 +- .../otlp/test/otlp_http_exporter_test.cc | 73 +++++++++++++++++++ .../otlp_http_log_record_exporter_test.cc | 73 +++++++++++++++++++ .../test/otlp_http_metric_exporter_test.cc | 72 ++++++++++++++++++ .../ext/http/client/curl/http_client_curl.h | 12 +++ .../http/client/curl/http_operation_curl.h | 9 ++- .../ext/http/client/http_client.h | 28 +++++++ ext/src/http/client/curl/http_client_curl.cc | 4 +- .../http/client/curl/http_operation_curl.cc | 35 ++++++++- ext/test/http/curl_http_test.cc | 44 +++++++++++ 21 files changed, 717 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d16d636e1..28f41403c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,14 @@ Increment the: ## [Unreleased] +* [EXT] TCP keepalive is now configurable via `Request::SetKeepAlive()` and per-exporter + options `http_keepalive`, `http_keepalive_idle`, and `http_keepalive_intvl` on + `OtlpHttpExporterOptions`, `OtlpHttpLogRecordExporterOptions`, + `OtlpHttpMetricExporterOptions`, and `OtlpHttpClientOptions`. Keepalive is disabled + by default (opt-in). `CURLOPT_TCP_KEEPIDLE` and `CURLOPT_TCP_KEEPINTVL` are + configurable; zero values fall back to the libcurl/OS default. + [#4266](https://github.com/open-telemetry/opentelemetry-cpp/pull/4266) + * [CONFIGURATION] Build the configured resource detectors in SdkBuilder, apply the `detection.attributes` include/exclude filter to the detected attributes, and merge the resource per the resource SDK specification. @@ -162,6 +170,13 @@ Increment the: * [CODE HEALTH] Fix more clang tidy warnings (member initialization) [#4270](https://github.com/open-telemetry/opentelemetry-cpp/pull/4270) +* docs: update supported development platforms + [#4260](https://github.com/open-telemetry/opentelemetry-cpp/pull/4260) + +* [RELEASE] Bump main branch to 1.29.0-dev + [#4259](https://github.com/open-telemetry/opentelemetry-cpp/pull/4259) + + * docs: update supported development platforms [#4260](https://github.com/open-telemetry/opentelemetry-cpp/pull/4260) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index da28fbfdec..0b01a142ae 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -92,6 +92,47 @@ struct OtlpHttpClientOptions std::shared_ptr thread_instrumentation = std::shared_ptr(nullptr); + /** + * Enable TCP keepalive probes on the underlying curl connection. + * + * When true, the OS will send keepalive probes after the connection + * has been idle for @p http_keepalive_idle seconds, then repeat every + * @p http_keepalive_intvl seconds until the peer acknowledges or the + * connection is declared dead. This detects half-open TCP connections + * caused by silent peer disappearance. + * + * Default: false (keepalive disabled). + */ + bool http_keepalive = false; + + /** + * Idle time in seconds before TCP keepalive probes begin. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 60-75 s on + * Linux, 7200 s on macOS). Set to a smaller value (e.g., 30) for + * faster detection of half-open connections. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_idle = std::chrono::seconds::zero(); + + /** + * Interval in seconds between successive TCP keepalive probes. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 75 s on + * Linux). Set to a smaller value (e.g., 5) to detect failures more + * quickly once probing has started. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_intvl = std::chrono::seconds::zero(); + inline OtlpHttpClientOptions( nostd::string_view input_url, bool input_ssl_insecure_skip_verify, @@ -119,7 +160,10 @@ struct OtlpHttpClientOptions const std::shared_ptr &input_thread_instrumentation, std::size_t input_concurrent_sessions = 64, std::size_t input_max_requests_per_connection = 8, - nostd::string_view input_user_agent = GetOtlpDefaultUserAgent()) + nostd::string_view input_user_agent = GetOtlpDefaultUserAgent(), + bool input_http_keepalive = false, + std::chrono::seconds input_http_keepalive_idle = std::chrono::seconds::zero(), + std::chrono::seconds input_http_keepalive_intvl = std::chrono::seconds::zero()) : url(input_url), ssl_options(input_url, input_ssl_insecure_skip_verify, @@ -145,7 +189,10 @@ struct OtlpHttpClientOptions max_concurrent_requests(input_concurrent_sessions), max_requests_per_connection(input_max_requests_per_connection), user_agent(input_user_agent), - thread_instrumentation(input_thread_instrumentation) + thread_instrumentation(input_thread_instrumentation), + http_keepalive(input_http_keepalive), + http_keepalive_idle(input_http_keepalive_idle), + http_keepalive_intvl(input_http_keepalive_intvl) {} }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_options.h index 56750015a4..4030c34d82 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_options.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_options.h @@ -137,6 +137,47 @@ struct OPENTELEMETRY_EXPORT OtlpHttpExporterOptions std::uint32_t max_links = (std::numeric_limits::max)(); std::uint32_t max_attributes_per_event = (std::numeric_limits::max)(); std::uint32_t max_attributes_per_link = (std::numeric_limits::max)(); + + /** + * Enable TCP keepalive probes on the underlying curl connection. + * + * When true, the OS will send keepalive probes after the connection + * has been idle for @p http_keepalive_idle seconds, then repeat every + * @p http_keepalive_intvl seconds until the peer acknowledges or the + * connection is declared dead. This detects half-open TCP connections + * caused by silent peer disappearance. + * + * Default: false (keepalive disabled, preserves pre-existing behavior). + */ + bool http_keepalive{false}; + + /** + * Idle time in seconds before TCP keepalive probes begin. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 60-75 s on + * Linux, 7200 s on macOS). Set to a smaller value (e.g., 30) for + * faster detection of half-open connections. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_idle{std::chrono::seconds::zero()}; + + /** + * Interval in seconds between successive TCP keepalive probes. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 75 s on + * Linux). Set to a smaller value (e.g., 5) to detect failures more + * quickly once probing has started. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_intvl{std::chrono::seconds::zero()}; }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h index 6f176a4fd1..0287953d48 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h @@ -125,6 +125,47 @@ struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterOptions /** The backoff will be multiplied by this value after each retry attempt. */ float retry_policy_backoff_multiplier{}; + + /** + * Enable TCP keepalive probes on the underlying curl connection. + * + * When true, the OS will send keepalive probes after the connection + * has been idle for @p http_keepalive_idle seconds, then repeat every + * @p http_keepalive_intvl seconds until the peer acknowledges or the + * connection is declared dead. This detects half-open TCP connections + * caused by silent peer disappearance. + * + * Default: false (keepalive disabled, preserves pre-existing behavior). + */ + bool http_keepalive{false}; + + /** + * Idle time in seconds before TCP keepalive probes begin. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 60-75 s on + * Linux, 7200 s on macOS). Set to a smaller value (e.g., 30) for + * faster detection of half-open connections. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_idle{std::chrono::seconds::zero()}; + + /** + * Interval in seconds between successive TCP keepalive probes. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 75 s on + * Linux). Set to a smaller value (e.g., 5) to detect failures more + * quickly once probing has started. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_intvl{std::chrono::seconds::zero()}; }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h index 3d872d638e..04b667c866 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h @@ -129,6 +129,47 @@ struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterOptions /** The backoff will be multiplied by this value after each retry attempt. */ float retry_policy_backoff_multiplier{}; + + /** + * Enable TCP keepalive probes on the underlying curl connection. + * + * When true, the OS will send keepalive probes after the connection + * has been idle for @p http_keepalive_idle seconds, then repeat every + * @p http_keepalive_intvl seconds until the peer acknowledges or the + * connection is declared dead. This detects half-open TCP connections + * caused by silent peer disappearance. + * + * Default: false (keepalive disabled, preserves pre-existing behavior). + */ + bool http_keepalive{false}; + + /** + * Idle time in seconds before TCP keepalive probes begin. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 60-75 s on + * Linux, 7200 s on macOS). Set to a smaller value (e.g., 30) for + * faster detection of half-open connections. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_idle{std::chrono::seconds::zero()}; + + /** + * Interval in seconds between successive TCP keepalive probes. + * + * Only effective when @p http_keepalive is true. + * A value of zero uses the libcurl / OS default (typically 75 s on + * Linux). Set to a smaller value (e.g., 5) to detect failures more + * quickly once probing has started. + * + * Requires libcurl >= 7.25.0; silently ignored on older versions. + * + * Default: 0 (use OS default). + */ + std::chrono::seconds http_keepalive_intvl{std::chrono::seconds::zero()}; }; } // namespace otlp diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 1f2bf98071..e40b3c16b4 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -1043,6 +1043,8 @@ OtlpHttpClient::createSession( request->ReplaceHeader("User-Agent", options_.user_agent); request->EnableLogging(options_.console_debug); request->SetRetryPolicy(options_.retry_policy); + request->SetKeepAlive(options_.http_keepalive, options_.http_keepalive_idle, + options_.http_keepalive_intvl); if (options_.compression == "gzip") { diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 1155b683bd..f8aa233066 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -78,9 +78,16 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ))) + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl))) {} OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, @@ -115,9 +122,16 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ))) + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl))) {} OtlpHttpExporter::OtlpHttpExporter( @@ -153,9 +167,16 @@ OtlpHttpExporter::OtlpHttpExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), factory)) {} @@ -193,9 +214,16 @@ OtlpHttpExporter::OtlpHttpExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), factory)) {} @@ -231,9 +259,16 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), std::move(http_client))) {} @@ -270,9 +305,16 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), std::move(http_client))) {} @@ -296,6 +338,9 @@ OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr http_client) options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; + options_.http_keepalive = http_client_->GetOptions().http_keepalive; + options_.http_keepalive_idle = http_client_->GetOptions().http_keepalive_idle; + options_.http_keepalive_intvl = http_client_->GetOptions().http_keepalive_intvl; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_exporter_options.cc b/exporters/otlp/src/otlp_http_exporter_options.cc index 05be6a0140..c934d3d70d 100644 --- a/exporters/otlp/src/otlp_http_exporter_options.cc +++ b/exporters/otlp/src/otlp_http_exporter_options.cc @@ -31,7 +31,10 @@ OtlpHttpExporterOptions::OtlpHttpExporterOptions() retry_policy_max_attempts(GetOtlpDefaultTracesRetryMaxAttempts()), retry_policy_initial_backoff(GetOtlpDefaultTracesRetryInitialBackoff()), retry_policy_max_backoff(GetOtlpDefaultTracesRetryMaxBackoff()), - retry_policy_backoff_multiplier(GetOtlpDefaultTracesRetryBackoffMultiplier()) + retry_policy_backoff_multiplier(GetOtlpDefaultTracesRetryBackoffMultiplier()), + http_keepalive(false), + http_keepalive_idle(std::chrono::seconds::zero()), + http_keepalive_intvl(std::chrono::seconds::zero()) {} OtlpHttpExporterOptions::OtlpHttpExporterOptions(void *) {} diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index 10d6af9653..52cf642b20 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -81,9 +81,16 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ))) + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl))) {} OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( @@ -119,9 +126,16 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ))) + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl))) {} OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( @@ -157,9 +171,16 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), factory)) {} @@ -197,9 +218,16 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), factory)) {} @@ -236,9 +264,16 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), std::move(http_client))) {} @@ -276,9 +311,16 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), std::move(http_client))) {} @@ -302,6 +344,9 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(std::unique_ptrGetOptions().max_requests_per_connection; #endif runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; + options_.http_keepalive = http_client_->GetOptions().http_keepalive; + options_.http_keepalive_idle = http_client_->GetOptions().http_keepalive_idle; + options_.http_keepalive_intvl = http_client_->GetOptions().http_keepalive_intvl; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_log_record_exporter_options.cc b/exporters/otlp/src/otlp_http_log_record_exporter_options.cc index 9698836976..0d95abedc8 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter_options.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter_options.cc @@ -31,7 +31,10 @@ OtlpHttpLogRecordExporterOptions::OtlpHttpLogRecordExporterOptions() retry_policy_max_attempts(GetOtlpDefaultLogsRetryMaxAttempts()), retry_policy_initial_backoff(GetOtlpDefaultLogsRetryInitialBackoff()), retry_policy_max_backoff(GetOtlpDefaultLogsRetryMaxBackoff()), - retry_policy_backoff_multiplier(GetOtlpDefaultLogsRetryBackoffMultiplier()) + retry_policy_backoff_multiplier(GetOtlpDefaultLogsRetryBackoffMultiplier()), + http_keepalive(false), + http_keepalive_idle(std::chrono::seconds::zero()), + http_keepalive_intvl(std::chrono::seconds::zero()) {} OtlpHttpLogRecordExporterOptions::OtlpHttpLogRecordExporterOptions(void *) {} diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index 782fc20616..136ba1a3eb 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter.cc @@ -82,9 +82,16 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptio #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ))) + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl))) {} OtlpHttpMetricExporter::OtlpHttpMetricExporter( @@ -122,9 +129,16 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ))) + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl))) {} OtlpHttpMetricExporter::OtlpHttpMetricExporter( @@ -162,9 +176,16 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), factory)) {} @@ -204,9 +225,16 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), factory)) {} @@ -245,9 +273,16 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), std::move(http_client))) {} @@ -287,9 +322,16 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter( #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, - options.max_requests_per_connection + options.max_requests_per_connection, +#else + , + 64, + 8, #endif - ), + GetOtlpDefaultUserAgent(), + options.http_keepalive, + options.http_keepalive_idle, + options.http_keepalive_intvl), std::move(http_client))) {} @@ -316,6 +358,9 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(std::unique_ptr h options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; + options_.http_keepalive = http_client_->GetOptions().http_keepalive; + options_.http_keepalive_idle = http_client_->GetOptions().http_keepalive_idle; + options_.http_keepalive_intvl = http_client_->GetOptions().http_keepalive_intvl; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_metric_exporter_options.cc b/exporters/otlp/src/otlp_http_metric_exporter_options.cc index 9cbac492e3..9265ce918b 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter_options.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter_options.cc @@ -31,7 +31,10 @@ OtlpHttpMetricExporterOptions::OtlpHttpMetricExporterOptions() retry_policy_max_attempts(GetOtlpDefaultMetricsRetryMaxAttempts()), retry_policy_initial_backoff(GetOtlpDefaultMetricsRetryInitialBackoff()), retry_policy_max_backoff(GetOtlpDefaultMetricsRetryMaxBackoff()), - retry_policy_backoff_multiplier(GetOtlpDefaultMetricsRetryBackoffMultiplier()) + retry_policy_backoff_multiplier(GetOtlpDefaultMetricsRetryBackoffMultiplier()), + http_keepalive(false), + http_keepalive_idle(std::chrono::seconds::zero()), + http_keepalive_intvl(std::chrono::seconds::zero()) {} OtlpHttpMetricExporterOptions::OtlpHttpMetricExporterOptions(void *) {} diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 72adc689ed..7e4c97d7e2 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -1008,4 +1008,77 @@ TEST_F(OtlpHttpExporterTestPeer, ExportParseFailureReturnsFailure) } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE + +// --------------------------------------------------------------------------- +// TCP keepalive configuration tests +// --------------------------------------------------------------------------- + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +TEST(OtlpHttpExporterOptionsTest, KeepAliveDefaultsDisabled) +{ + OtlpHttpExporterOptions opts{}; + EXPECT_FALSE(opts.http_keepalive); + EXPECT_EQ(opts.http_keepalive_idle, std::chrono::seconds::zero()); + EXPECT_EQ(opts.http_keepalive_intvl, std::chrono::seconds::zero()); +} + +TEST(OtlpHttpExporterOptionsTest, KeepAliveEnabledPropagatesIntoClientOptions) +{ + OtlpHttpExporterOptions opts{}; + opts.http_keepalive = true; + opts.http_keepalive_idle = std::chrono::seconds{30}; + opts.http_keepalive_intvl = std::chrono::seconds{5}; + + std::shared_ptr not_instrumented; + OtlpHttpClientOptions client_opts( + opts.url, opts.ssl_insecure_skip_verify, opts.ssl_ca_cert_path, opts.ssl_ca_cert_string, + opts.ssl_client_key_path, opts.ssl_client_key_string, opts.ssl_client_cert_path, + opts.ssl_client_cert_string, opts.ssl_min_tls, opts.ssl_max_tls, opts.ssl_cipher, + opts.ssl_cipher_suite, opts.content_type, opts.json_bytes_mapping, opts.compression, + opts.use_json_name, opts.console_debug, opts.timeout, opts.http_headers, + opts.retry_policy_max_attempts, opts.retry_policy_initial_backoff, + opts.retry_policy_max_backoff, opts.retry_policy_backoff_multiplier, not_instrumented, + /*concurrent_sessions=*/64, /*max_requests_per_connection=*/8, + /*user_agent=*/GetOtlpDefaultUserAgent(), + /*keepalive=*/opts.http_keepalive, + /*keepalive_idle=*/opts.http_keepalive_idle, + /*keepalive_intvl=*/opts.http_keepalive_intvl); + + EXPECT_TRUE(client_opts.http_keepalive); + EXPECT_EQ(client_opts.http_keepalive_idle, std::chrono::seconds{30}); + EXPECT_EQ(client_opts.http_keepalive_intvl, std::chrono::seconds{5}); +} + +TEST(OtlpHttpExporterOptionsTest, KeepAliveDisabledPropagatesIntoClientOptions) +{ + OtlpHttpExporterOptions opts{}; + std::shared_ptr not_instrumented; + OtlpHttpClientOptions client_opts( + opts.url, opts.ssl_insecure_skip_verify, opts.ssl_ca_cert_path, opts.ssl_ca_cert_string, + opts.ssl_client_key_path, opts.ssl_client_key_string, opts.ssl_client_cert_path, + opts.ssl_client_cert_string, opts.ssl_min_tls, opts.ssl_max_tls, opts.ssl_cipher, + opts.ssl_cipher_suite, opts.content_type, opts.json_bytes_mapping, opts.compression, + opts.use_json_name, opts.console_debug, opts.timeout, opts.http_headers, + opts.retry_policy_max_attempts, opts.retry_policy_initial_backoff, + opts.retry_policy_max_backoff, opts.retry_policy_backoff_multiplier, not_instrumented, + /*concurrent_sessions=*/64, /*max_requests_per_connection=*/8, + /*user_agent=*/GetOtlpDefaultUserAgent(), + /*keepalive=*/opts.http_keepalive, + /*keepalive_idle=*/opts.http_keepalive_idle, + /*keepalive_intvl=*/opts.http_keepalive_intvl); + + EXPECT_FALSE(client_opts.http_keepalive); + EXPECT_EQ(client_opts.http_keepalive_idle, std::chrono::seconds::zero()); + EXPECT_EQ(client_opts.http_keepalive_intvl, std::chrono::seconds::zero()); +} + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE + #endif /* OPENTELEMETRY_STL_VERSION */ diff --git a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc index 3dbf115b58..d78e0ae27c 100644 --- a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc @@ -1019,4 +1019,77 @@ TEST_F(OtlpHttpLogRecordExporterTestPeer, ExportParseFailureReturnsFailure) } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE + +// --------------------------------------------------------------------------- +// TCP keepalive configuration tests +// --------------------------------------------------------------------------- + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +TEST(OtlpHttpLogRecordExporterOptionsTest, KeepAliveDefaultsDisabled) +{ + OtlpHttpLogRecordExporterOptions opts{}; + EXPECT_FALSE(opts.http_keepalive); + EXPECT_EQ(opts.http_keepalive_idle, std::chrono::seconds::zero()); + EXPECT_EQ(opts.http_keepalive_intvl, std::chrono::seconds::zero()); +} + +TEST(OtlpHttpLogRecordExporterOptionsTest, KeepAliveEnabledPropagatesIntoClientOptions) +{ + OtlpHttpLogRecordExporterOptions opts{}; + opts.http_keepalive = true; + opts.http_keepalive_idle = std::chrono::seconds{30}; + opts.http_keepalive_intvl = std::chrono::seconds{5}; + + std::shared_ptr not_instrumented; + OtlpHttpClientOptions client_opts( + opts.url, opts.ssl_insecure_skip_verify, opts.ssl_ca_cert_path, opts.ssl_ca_cert_string, + opts.ssl_client_key_path, opts.ssl_client_key_string, opts.ssl_client_cert_path, + opts.ssl_client_cert_string, opts.ssl_min_tls, opts.ssl_max_tls, opts.ssl_cipher, + opts.ssl_cipher_suite, opts.content_type, opts.json_bytes_mapping, opts.compression, + opts.use_json_name, opts.console_debug, opts.timeout, opts.http_headers, + opts.retry_policy_max_attempts, opts.retry_policy_initial_backoff, + opts.retry_policy_max_backoff, opts.retry_policy_backoff_multiplier, not_instrumented, + /*concurrent_sessions=*/64, /*max_requests_per_connection=*/8, + /*user_agent=*/GetOtlpDefaultUserAgent(), + /*keepalive=*/opts.http_keepalive, + /*keepalive_idle=*/opts.http_keepalive_idle, + /*keepalive_intvl=*/opts.http_keepalive_intvl); + + EXPECT_TRUE(client_opts.http_keepalive); + EXPECT_EQ(client_opts.http_keepalive_idle, std::chrono::seconds{30}); + EXPECT_EQ(client_opts.http_keepalive_intvl, std::chrono::seconds{5}); +} + +TEST(OtlpHttpLogRecordExporterOptionsTest, KeepAliveDisabledPropagatesIntoClientOptions) +{ + OtlpHttpLogRecordExporterOptions opts{}; + std::shared_ptr not_instrumented; + OtlpHttpClientOptions client_opts( + opts.url, opts.ssl_insecure_skip_verify, opts.ssl_ca_cert_path, opts.ssl_ca_cert_string, + opts.ssl_client_key_path, opts.ssl_client_key_string, opts.ssl_client_cert_path, + opts.ssl_client_cert_string, opts.ssl_min_tls, opts.ssl_max_tls, opts.ssl_cipher, + opts.ssl_cipher_suite, opts.content_type, opts.json_bytes_mapping, opts.compression, + opts.use_json_name, opts.console_debug, opts.timeout, opts.http_headers, + opts.retry_policy_max_attempts, opts.retry_policy_initial_backoff, + opts.retry_policy_max_backoff, opts.retry_policy_backoff_multiplier, not_instrumented, + /*concurrent_sessions=*/64, /*max_requests_per_connection=*/8, + /*user_agent=*/GetOtlpDefaultUserAgent(), + /*keepalive=*/opts.http_keepalive, + /*keepalive_idle=*/opts.http_keepalive_idle, + /*keepalive_intvl=*/opts.http_keepalive_intvl); + + EXPECT_FALSE(client_opts.http_keepalive); + EXPECT_EQ(client_opts.http_keepalive_idle, std::chrono::seconds::zero()); + EXPECT_EQ(client_opts.http_keepalive_intvl, std::chrono::seconds::zero()); +} + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE + #endif /* OPENTELEMETRY_STL_VERSION */ diff --git a/exporters/otlp/test/otlp_http_metric_exporter_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_test.cc index 787c982bf0..38b78216c3 100644 --- a/exporters/otlp/test/otlp_http_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_metric_exporter_test.cc @@ -1428,3 +1428,75 @@ TEST_F(OtlpHttpMetricExporterTestPeer, ExportParseFailureReturnsFailure) } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE + +// --------------------------------------------------------------------------- +// TCP keepalive configuration tests +// --------------------------------------------------------------------------- + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +TEST(OtlpHttpMetricExporterOptionsTest, KeepAliveDefaultsDisabled) +{ + OtlpHttpMetricExporterOptions opts{}; + EXPECT_FALSE(opts.http_keepalive); + EXPECT_EQ(opts.http_keepalive_idle, std::chrono::seconds::zero()); + EXPECT_EQ(opts.http_keepalive_intvl, std::chrono::seconds::zero()); +} + +TEST(OtlpHttpMetricExporterOptionsTest, KeepAliveEnabledPropagatesIntoClientOptions) +{ + OtlpHttpMetricExporterOptions opts{}; + opts.http_keepalive = true; + opts.http_keepalive_idle = std::chrono::seconds{30}; + opts.http_keepalive_intvl = std::chrono::seconds{5}; + + std::shared_ptr not_instrumented; + OtlpHttpClientOptions client_opts( + opts.url, opts.ssl_insecure_skip_verify, opts.ssl_ca_cert_path, opts.ssl_ca_cert_string, + opts.ssl_client_key_path, opts.ssl_client_key_string, opts.ssl_client_cert_path, + opts.ssl_client_cert_string, opts.ssl_min_tls, opts.ssl_max_tls, opts.ssl_cipher, + opts.ssl_cipher_suite, opts.content_type, opts.json_bytes_mapping, opts.compression, + opts.use_json_name, opts.console_debug, opts.timeout, opts.http_headers, + opts.retry_policy_max_attempts, opts.retry_policy_initial_backoff, + opts.retry_policy_max_backoff, opts.retry_policy_backoff_multiplier, not_instrumented, + /*concurrent_sessions=*/64, /*max_requests_per_connection=*/8, + /*user_agent=*/GetOtlpDefaultUserAgent(), + /*keepalive=*/opts.http_keepalive, + /*keepalive_idle=*/opts.http_keepalive_idle, + /*keepalive_intvl=*/opts.http_keepalive_intvl); + + EXPECT_TRUE(client_opts.http_keepalive); + EXPECT_EQ(client_opts.http_keepalive_idle, std::chrono::seconds{30}); + EXPECT_EQ(client_opts.http_keepalive_intvl, std::chrono::seconds{5}); +} + +TEST(OtlpHttpMetricExporterOptionsTest, KeepAliveDisabledPropagatesIntoClientOptions) +{ + OtlpHttpMetricExporterOptions opts{}; + std::shared_ptr not_instrumented; + OtlpHttpClientOptions client_opts( + opts.url, opts.ssl_insecure_skip_verify, opts.ssl_ca_cert_path, opts.ssl_ca_cert_string, + opts.ssl_client_key_path, opts.ssl_client_key_string, opts.ssl_client_cert_path, + opts.ssl_client_cert_string, opts.ssl_min_tls, opts.ssl_max_tls, opts.ssl_cipher, + opts.ssl_cipher_suite, opts.content_type, opts.json_bytes_mapping, opts.compression, + opts.use_json_name, opts.console_debug, opts.timeout, opts.http_headers, + opts.retry_policy_max_attempts, opts.retry_policy_initial_backoff, + opts.retry_policy_max_backoff, opts.retry_policy_backoff_multiplier, not_instrumented, + /*concurrent_sessions=*/64, /*max_requests_per_connection=*/8, + /*user_agent=*/GetOtlpDefaultUserAgent(), + /*keepalive=*/opts.http_keepalive, + /*keepalive_idle=*/opts.http_keepalive_idle, + /*keepalive_intvl=*/opts.http_keepalive_intvl); + + EXPECT_FALSE(client_opts.http_keepalive); + EXPECT_EQ(client_opts.http_keepalive_idle, std::chrono::seconds::zero()); + EXPECT_EQ(client_opts.http_keepalive_intvl, std::chrono::seconds::zero()); +} + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index 9a09fac9aa..6d3fcea42b 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -112,6 +112,15 @@ class Request : public opentelemetry::ext::http::client::Request retry_policy_ = retry_policy; } + void SetKeepAlive(bool allow, + std::chrono::seconds idle = std::chrono::seconds::zero(), + std::chrono::seconds intvl = std::chrono::seconds::zero()) noexcept override + { + keepalive_allow_ = allow; + keepalive_idle_ = idle; + keepalive_intvl_ = intvl; + } + public: opentelemetry::ext::http::client::Method method_{opentelemetry::ext::http::client::Method::Get}; opentelemetry::ext::http::client::HttpSslOptions ssl_options_; @@ -123,6 +132,9 @@ class Request : public opentelemetry::ext::http::client::Request opentelemetry::ext::http::client::Compression::kNone}; bool is_log_enabled_{false}; opentelemetry::ext::http::client::RetryPolicy retry_policy_; + bool keepalive_allow_{false}; + std::chrono::seconds keepalive_idle_{std::chrono::seconds::zero()}; + std::chrono::seconds keepalive_intvl_{std::chrono::seconds::zero()}; }; class Response : public opentelemetry::ext::http::client::Response diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h index c57309ccd6..10962bfd40 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h @@ -174,7 +174,10 @@ class HttpOperation std::chrono::milliseconds http_conn_timeout = kDefaultHttpConnTimeout, bool reuse_connection = false, bool is_log_enabled = false, - const opentelemetry::ext::http::client::RetryPolicy &retry_policy = {}); + const opentelemetry::ext::http::client::RetryPolicy &retry_policy = {}, + bool keepalive_allow = false, + std::chrono::seconds keepalive_idle = std::chrono::seconds::zero(), + std::chrono::seconds keepalive_intvl = std::chrono::seconds::zero()); HttpOperation(const HttpOperation &) = delete; HttpOperation(HttpOperation &&) = delete; @@ -350,6 +353,10 @@ class HttpOperation std::chrono::system_clock::time_point last_attempt_time_; std::chrono::system_clock::time_point retry_after_time_point_{}; + const bool keepalive_allow_{false}; + const std::chrono::seconds keepalive_idle_{std::chrono::seconds::zero()}; + const std::chrono::seconds keepalive_intvl_{std::chrono::seconds::zero()}; + // Processed response headers and body // See CURLINFO_RESPONSE_CODE, type is long // NOLINTNEXTLINE(google-runtime-int) diff --git a/ext/include/opentelemetry/ext/http/client/http_client.h b/ext/include/opentelemetry/ext/http/client/http_client.h index b477292fed..68903fe1e7 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client.h +++ b/ext/include/opentelemetry/ext/http/client/http_client.h @@ -278,6 +278,34 @@ class Request virtual void SetRetryPolicy(const RetryPolicy &retry_policy) noexcept = 0; + /** + * Configure TCP keepalive for this HTTP request. + * + * When enabled, the underlying transport (e.g., libcurl) will send + * keepalive probes after the connection has been idle for @p idle + * seconds, repeating every @p intvl seconds until the peer responds + * or the connection is declared dead. This allows detection of + * half-open TCP connections caused by silent peer disappearance. + * + * @param allow Enable TCP keepalive probes. When false, keepalive + * is disabled and @p idle / @p intvl are ignored. + * @param idle Seconds of idle time before the first probe is sent. + * Zero means use the libcurl / OS default (typically + * 60-75 s on Linux, 7200 s on macOS). Only effective + * when @p allow is true and libcurl >= 7.25.0. + * @param intvl Seconds between successive keepalive probes. + * Zero means use the libcurl / OS default (typically + * 75 s on Linux). Only effective when @p allow is true + * and libcurl >= 7.25.0. + * + * The default implementation is a no-op; concrete implementations + * (e.g., the libcurl backend) override this to apply the settings. + */ + virtual void SetKeepAlive(bool allow, + std::chrono::seconds idle = std::chrono::seconds::zero(), + std::chrono::seconds intvl = std::chrono::seconds::zero()) noexcept + {} + virtual ~Request() = default; }; diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 76f67fcd90..0a28bbbd44 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -204,7 +204,9 @@ void Session::SendRequest( new HttpOperation(http_request_->method_, url, http_request_->ssl_options_, callback_ptr, http_request_->headers_, http_request_->body_, http_request_->compression_, false, http_request_->timeout_ms_, reuse_connection, - http_request_->is_log_enabled_, http_request_->retry_policy_)); + http_request_->is_log_enabled_, http_request_->retry_policy_, + http_request_->keepalive_allow_, http_request_->keepalive_idle_, + http_request_->keepalive_intvl_)); bool success = CURLE_OK == curl_operation_->SendAsync(this, [this, callback](HttpOperation &operation) { // Both can hold at once: cancelling only raises a flag, and the transfer may already diff --git a/ext/src/http/client/curl/http_operation_curl.cc b/ext/src/http/client/curl/http_operation_curl.cc index 6a1eb0b292..81fbb5cf24 100644 --- a/ext/src/http/client/curl/http_operation_curl.cc +++ b/ext/src/http/client/curl/http_operation_curl.cc @@ -425,7 +425,10 @@ HttpOperation::HttpOperation(opentelemetry::ext::http::client::Method method, std::chrono::milliseconds http_conn_timeout, bool reuse_connection, bool is_log_enabled, - const RetryPolicy &retry_policy) + const RetryPolicy &retry_policy, + bool keepalive_allow, + std::chrono::seconds keepalive_idle, + std::chrono::seconds keepalive_intvl) : // Optional connection params is_raw_response_(is_raw_response), reuse_connection_(reuse_connection), @@ -446,7 +449,10 @@ HttpOperation::HttpOperation(opentelemetry::ext::http::client::Method method, retry_policy.max_backoff > SecondsDecimal::zero() && retry_policy.backoff_multiplier > 0.0f) ? 0 - : retry_policy.max_attempts) + : retry_policy.max_attempts), + keepalive_allow_(keepalive_allow), + keepalive_idle_(keepalive_idle), + keepalive_intvl_(keepalive_intvl) { /* get a curl handle */ curl_resource_.easy_handle = curl_easy_init(); @@ -1219,12 +1225,35 @@ CURLcode HttpOperation::Setup() return rc; } - rc = SetCurlLongOption(CURLOPT_TCP_KEEPALIVE, 1L); + rc = SetCurlLongOption(CURLOPT_TCP_KEEPALIVE, keepalive_allow_ ? 1L : 0L); if (rc != CURLE_OK) { return rc; } + if (keepalive_allow_) + { +#if LIBCURL_VERSION_NUM >= 0x071900 /* libcurl 7.25.0 */ + if (keepalive_idle_.count() > 0) + { + rc = SetCurlLongOption(CURLOPT_TCP_KEEPIDLE, static_cast(keepalive_idle_.count())); + if (rc != CURLE_OK) + { + return rc; + } + } + + if (keepalive_intvl_.count() > 0) + { + rc = SetCurlLongOption(CURLOPT_TCP_KEEPINTVL, static_cast(keepalive_intvl_.count())); + if (rc != CURLE_OK) + { + return rc; + } + } +#endif + } + if (reuse_connection_) { rc = SetCurlLongOption(CURLOPT_FRESH_CONNECT, 0L); diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 90142962d6..2f89df4840 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -1090,4 +1090,48 @@ TEST_F(BasicCurlHttpTests, GzipIncompressibleData) } #endif // ENABLE_OTLP_COMPRESSION_PREVIEW +// --------------------------------------------------------------------------- +// TCP keepalive configuration tests +// --------------------------------------------------------------------------- + +TEST(CurlHttpRequestTest, KeepAliveDefaultsDisabled) +{ + curl::Request req; + EXPECT_FALSE(req.keepalive_allow_); + EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds::zero()); + EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds::zero()); +} + +TEST(CurlHttpRequestTest, KeepAliveEnabledWithExplicitTimings) +{ + curl::Request req; + req.SetKeepAlive(true, std::chrono::seconds{30}, std::chrono::seconds{5}); + EXPECT_TRUE(req.keepalive_allow_); + EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds{30}); + EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds{5}); +} + +TEST(CurlHttpRequestTest, KeepAliveEnabledWithOsDefaultTimings) +{ + // Zero idle/intvl means "use OS default" — both fields should store zero. + curl::Request req; + req.SetKeepAlive(true); // idle and intvl default to zero() + EXPECT_TRUE(req.keepalive_allow_); + EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds::zero()); + EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds::zero()); +} + +TEST(CurlHttpRequestTest, KeepAliveDisabledExplicitly) +{ + curl::Request req; + // First enable, then disable — ensure values revert. + req.SetKeepAlive(true, std::chrono::seconds{60}, std::chrono::seconds{10}); + req.SetKeepAlive(false); + EXPECT_FALSE(req.keepalive_allow_); + // idle/intvl are set to their parameter values; when allow=false and defaults + // are used, they become zero() from the default argument. + EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds::zero()); + EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds::zero()); +} + } // namespace From e2ad5ab27d2f5773863babaac26f70cbd7cb61cd Mon Sep 17 00:00:00 2001 From: Babul Kumar Date: Wed, 19 Aug 2026 13:33:49 +0530 Subject: [PATCH 3/3] fix(ext): bound HTTP connection establishment timeout --- exporters/otlp/src/otlp_http_client.cc | 2 - .../ext/http/client/curl/http_client_curl.h | 12 ---- .../http/client/curl/http_operation_curl.h | 9 +-- .../ext/http/client/http_client.h | 28 --------- ext/src/http/client/curl/http_client_curl.cc | 4 +- .../http/client/curl/http_operation_curl.cc | 42 +++----------- ext/test/http/curl_http_test.cc | 57 ++++++++----------- 7 files changed, 32 insertions(+), 122 deletions(-) diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index e40b3c16b4..1f2bf98071 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -1043,8 +1043,6 @@ OtlpHttpClient::createSession( request->ReplaceHeader("User-Agent", options_.user_agent); request->EnableLogging(options_.console_debug); request->SetRetryPolicy(options_.retry_policy); - request->SetKeepAlive(options_.http_keepalive, options_.http_keepalive_idle, - options_.http_keepalive_intvl); if (options_.compression == "gzip") { diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index 6d3fcea42b..9a09fac9aa 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -112,15 +112,6 @@ class Request : public opentelemetry::ext::http::client::Request retry_policy_ = retry_policy; } - void SetKeepAlive(bool allow, - std::chrono::seconds idle = std::chrono::seconds::zero(), - std::chrono::seconds intvl = std::chrono::seconds::zero()) noexcept override - { - keepalive_allow_ = allow; - keepalive_idle_ = idle; - keepalive_intvl_ = intvl; - } - public: opentelemetry::ext::http::client::Method method_{opentelemetry::ext::http::client::Method::Get}; opentelemetry::ext::http::client::HttpSslOptions ssl_options_; @@ -132,9 +123,6 @@ class Request : public opentelemetry::ext::http::client::Request opentelemetry::ext::http::client::Compression::kNone}; bool is_log_enabled_{false}; opentelemetry::ext::http::client::RetryPolicy retry_policy_; - bool keepalive_allow_{false}; - std::chrono::seconds keepalive_idle_{std::chrono::seconds::zero()}; - std::chrono::seconds keepalive_intvl_{std::chrono::seconds::zero()}; }; class Response : public opentelemetry::ext::http::client::Response diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h index 10962bfd40..c57309ccd6 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_operation_curl.h @@ -174,10 +174,7 @@ class HttpOperation std::chrono::milliseconds http_conn_timeout = kDefaultHttpConnTimeout, bool reuse_connection = false, bool is_log_enabled = false, - const opentelemetry::ext::http::client::RetryPolicy &retry_policy = {}, - bool keepalive_allow = false, - std::chrono::seconds keepalive_idle = std::chrono::seconds::zero(), - std::chrono::seconds keepalive_intvl = std::chrono::seconds::zero()); + const opentelemetry::ext::http::client::RetryPolicy &retry_policy = {}); HttpOperation(const HttpOperation &) = delete; HttpOperation(HttpOperation &&) = delete; @@ -353,10 +350,6 @@ class HttpOperation std::chrono::system_clock::time_point last_attempt_time_; std::chrono::system_clock::time_point retry_after_time_point_{}; - const bool keepalive_allow_{false}; - const std::chrono::seconds keepalive_idle_{std::chrono::seconds::zero()}; - const std::chrono::seconds keepalive_intvl_{std::chrono::seconds::zero()}; - // Processed response headers and body // See CURLINFO_RESPONSE_CODE, type is long // NOLINTNEXTLINE(google-runtime-int) diff --git a/ext/include/opentelemetry/ext/http/client/http_client.h b/ext/include/opentelemetry/ext/http/client/http_client.h index 68903fe1e7..b477292fed 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client.h +++ b/ext/include/opentelemetry/ext/http/client/http_client.h @@ -278,34 +278,6 @@ class Request virtual void SetRetryPolicy(const RetryPolicy &retry_policy) noexcept = 0; - /** - * Configure TCP keepalive for this HTTP request. - * - * When enabled, the underlying transport (e.g., libcurl) will send - * keepalive probes after the connection has been idle for @p idle - * seconds, repeating every @p intvl seconds until the peer responds - * or the connection is declared dead. This allows detection of - * half-open TCP connections caused by silent peer disappearance. - * - * @param allow Enable TCP keepalive probes. When false, keepalive - * is disabled and @p idle / @p intvl are ignored. - * @param idle Seconds of idle time before the first probe is sent. - * Zero means use the libcurl / OS default (typically - * 60-75 s on Linux, 7200 s on macOS). Only effective - * when @p allow is true and libcurl >= 7.25.0. - * @param intvl Seconds between successive keepalive probes. - * Zero means use the libcurl / OS default (typically - * 75 s on Linux). Only effective when @p allow is true - * and libcurl >= 7.25.0. - * - * The default implementation is a no-op; concrete implementations - * (e.g., the libcurl backend) override this to apply the settings. - */ - virtual void SetKeepAlive(bool allow, - std::chrono::seconds idle = std::chrono::seconds::zero(), - std::chrono::seconds intvl = std::chrono::seconds::zero()) noexcept - {} - virtual ~Request() = default; }; diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 0a28bbbd44..76f67fcd90 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -204,9 +204,7 @@ void Session::SendRequest( new HttpOperation(http_request_->method_, url, http_request_->ssl_options_, callback_ptr, http_request_->headers_, http_request_->body_, http_request_->compression_, false, http_request_->timeout_ms_, reuse_connection, - http_request_->is_log_enabled_, http_request_->retry_policy_, - http_request_->keepalive_allow_, http_request_->keepalive_idle_, - http_request_->keepalive_intvl_)); + http_request_->is_log_enabled_, http_request_->retry_policy_)); bool success = CURLE_OK == curl_operation_->SendAsync(this, [this, callback](HttpOperation &operation) { // Both can hold at once: cancelling only raises a flag, and the transfer may already diff --git a/ext/src/http/client/curl/http_operation_curl.cc b/ext/src/http/client/curl/http_operation_curl.cc index 81fbb5cf24..2152b23348 100644 --- a/ext/src/http/client/curl/http_operation_curl.cc +++ b/ext/src/http/client/curl/http_operation_curl.cc @@ -425,10 +425,7 @@ HttpOperation::HttpOperation(opentelemetry::ext::http::client::Method method, std::chrono::milliseconds http_conn_timeout, bool reuse_connection, bool is_log_enabled, - const RetryPolicy &retry_policy, - bool keepalive_allow, - std::chrono::seconds keepalive_idle, - std::chrono::seconds keepalive_intvl) + const RetryPolicy &retry_policy) : // Optional connection params is_raw_response_(is_raw_response), reuse_connection_(reuse_connection), @@ -449,10 +446,7 @@ HttpOperation::HttpOperation(opentelemetry::ext::http::client::Method method, retry_policy.max_backoff > SecondsDecimal::zero() && retry_policy.backoff_multiplier > 0.0f) ? 0 - : retry_policy.max_attempts), - keepalive_allow_(keepalive_allow), - keepalive_idle_(keepalive_idle), - keepalive_intvl_(keepalive_intvl) + : retry_policy.max_attempts) { /* get a curl handle */ curl_resource_.easy_handle = curl_easy_init(); @@ -1212,48 +1206,26 @@ CURLcode HttpOperation::Setup() return rc; } - // abort if slower than 4kb/sec during 30 seconds - rc = SetCurlLongOption(CURLOPT_LOW_SPEED_TIME, 30L); + // NOLINTNEXTLINE(google-runtime-int) + rc = SetCurlLongOption(CURLOPT_CONNECTTIMEOUT_MS, static_cast(http_conn_timeout_.count())); if (rc != CURLE_OK) { return rc; } - rc = SetCurlLongOption(CURLOPT_LOW_SPEED_LIMIT, 4096L); + // abort if slower than 4kb/sec during 30 seconds + rc = SetCurlLongOption(CURLOPT_LOW_SPEED_TIME, 30L); if (rc != CURLE_OK) { return rc; } - rc = SetCurlLongOption(CURLOPT_TCP_KEEPALIVE, keepalive_allow_ ? 1L : 0L); + rc = SetCurlLongOption(CURLOPT_LOW_SPEED_LIMIT, 4096L); if (rc != CURLE_OK) { return rc; } - if (keepalive_allow_) - { -#if LIBCURL_VERSION_NUM >= 0x071900 /* libcurl 7.25.0 */ - if (keepalive_idle_.count() > 0) - { - rc = SetCurlLongOption(CURLOPT_TCP_KEEPIDLE, static_cast(keepalive_idle_.count())); - if (rc != CURLE_OK) - { - return rc; - } - } - - if (keepalive_intvl_.count() > 0) - { - rc = SetCurlLongOption(CURLOPT_TCP_KEEPINTVL, static_cast(keepalive_intvl_.count())); - if (rc != CURLE_OK) - { - return rc; - } - } -#endif - } - if (reuse_connection_) { rc = SetCurlLongOption(CURLOPT_FRESH_CONNECT, 0L); diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 2f89df4840..247600e658 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -33,6 +33,7 @@ #include "opentelemetry/ext/http/server/http_server.h" #include "opentelemetry/nostd/function_ref.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/env_variables.h" #include "opentelemetry/version.h" constexpr int HTTP_PORT{19000}; @@ -1094,44 +1095,32 @@ TEST_F(BasicCurlHttpTests, GzipIncompressibleData) // TCP keepalive configuration tests // --------------------------------------------------------------------------- -TEST(CurlHttpRequestTest, KeepAliveDefaultsDisabled) +TEST_F(BasicCurlHttpTests, ConnectTimeoutBoundsConnectionPhase) { - curl::Request req; - EXPECT_FALSE(req.keepalive_allow_); - EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds::zero()); - EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds::zero()); -} + // Explicitly disable environment proxies for this direct connection test + setenv("no_proxy", "*", 1); + setenv("NO_PROXY", "*", 1); -TEST(CurlHttpRequestTest, KeepAliveEnabledWithExplicitTimings) -{ - curl::Request req; - req.SetKeepAlive(true, std::chrono::seconds{30}, std::chrono::seconds{5}); - EXPECT_TRUE(req.keepalive_allow_); - EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds{30}); - EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds{5}); -} + received_requests_.clear(); + curl::HttpClient http_client; -TEST(CurlHttpRequestTest, KeepAliveEnabledWithOsDefaultTimings) -{ - // Zero idle/intvl means "use OS default" — both fields should store zero. - curl::Request req; - req.SetKeepAlive(true); // idle and intvl default to zero() - EXPECT_TRUE(req.keepalive_allow_); - EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds::zero()); - EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds::zero()); -} + auto session = http_client.CreateSession("http://192.0.2.0:19000/get/"); + auto request = session->CreateRequest(); + request->SetMethod(http_client::Method::Get); + request->SetUri("get/"); + request->SetTimeoutMs(std::chrono::milliseconds(500)); -TEST(CurlHttpRequestTest, KeepAliveDisabledExplicitly) -{ - curl::Request req; - // First enable, then disable — ensure values revert. - req.SetKeepAlive(true, std::chrono::seconds{60}, std::chrono::seconds{10}); - req.SetKeepAlive(false); - EXPECT_FALSE(req.keepalive_allow_); - // idle/intvl are set to their parameter values; when allow=false and defaults - // are used, they become zero() from the default argument. - EXPECT_EQ(req.keepalive_idle_, std::chrono::seconds::zero()); - EXPECT_EQ(req.keepalive_intvl_, std::chrono::seconds::zero()); + auto handler = std::make_shared(); + auto start_time = std::chrono::steady_clock::now(); + session->SendRequest(handler); + session->FinishSession(); + auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time) + .count(); + + ASSERT_TRUE(handler->is_called_.load(std::memory_order_acquire)); + ASSERT_FALSE(handler->got_response_.load(std::memory_order_acquire)); + EXPECT_LT(elapsed, 2000); } } // namespace