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_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/src/http/client/curl/http_operation_curl.cc b/ext/src/http/client/curl/http_operation_curl.cc index 0f1bda4035..2152b23348 100644 --- a/ext/src/http/client/curl/http_operation_curl.cc +++ b/ext/src/http/client/curl/http_operation_curl.cc @@ -1206,6 +1206,13 @@ CURLcode HttpOperation::Setup() return rc; } + // NOLINTNEXTLINE(google-runtime-int) + rc = SetCurlLongOption(CURLOPT_CONNECTTIMEOUT_MS, static_cast(http_conn_timeout_.count())); + if (rc != CURLE_OK) + { + return rc; + } + // abort if slower than 4kb/sec during 30 seconds rc = SetCurlLongOption(CURLOPT_LOW_SPEED_TIME, 30L); if (rc != CURLE_OK) diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 90142962d6..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}; @@ -1090,4 +1091,36 @@ TEST_F(BasicCurlHttpTests, GzipIncompressibleData) } #endif // ENABLE_OTLP_COMPRESSION_PREVIEW +// --------------------------------------------------------------------------- +// TCP keepalive configuration tests +// --------------------------------------------------------------------------- + +TEST_F(BasicCurlHttpTests, ConnectTimeoutBoundsConnectionPhase) +{ + // Explicitly disable environment proxies for this direct connection test + setenv("no_proxy", "*", 1); + setenv("NO_PROXY", "*", 1); + + received_requests_.clear(); + curl::HttpClient http_client; + + 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)); + + 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