From 397585541149971deffbac91a14d1cb657c30511 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 26 Nov 2024 23:04:32 +0100 Subject: [PATCH 01/21] [POC] Better control of threads executed by opentelemetry-cpp Fixes #3174 --- .../exporters/otlp/otlp_http_client.h | 52 ++++++++++-------- .../otlp/otlp_http_exporter_options.h | 4 ++ .../otlp_http_log_record_exporter_options.h | 4 ++ .../otlp/otlp_http_metric_exporter_options.h | 4 ++ exporters/otlp/src/otlp_http_client.cc | 2 +- exporters/otlp/src/otlp_http_exporter.cc | 3 +- .../otlp/src/otlp_http_log_record_exporter.cc | 3 +- .../otlp/src/otlp_http_metric_exporter.cc | 3 +- .../otlp/test/otlp_http_exporter_test.cc | 3 +- .../otlp_http_log_record_exporter_test.cc | 3 +- .../test/otlp_http_metric_exporter_test.cc | 3 +- .../ext/http/client/curl/http_client_curl.h | 3 + .../ext/http/client/http_client_factory.h | 5 ++ ext/src/http/client/curl/http_client_curl.cc | 23 +++++++- .../client/curl/http_client_factory_curl.cc | 6 ++ .../sdk/common/thread_instrumentation.h | 30 ++++++++++ .../sdk/logs/batch_log_record_processor.h | 1 + .../logs/batch_log_record_processor_options.h | 3 + .../export/periodic_exporting_metric_reader.h | 3 + ...periodic_exporting_metric_reader_options.h | 4 ++ .../sdk/trace/batch_span_processor.h | 2 + .../sdk/trace/batch_span_processor_options.h | 3 + sdk/src/logs/batch_log_record_processor.cc | 36 +++++++++++- .../periodic_exporting_metric_reader.cc | 55 ++++++++++++++++++- sdk/src/trace/batch_span_processor.cc | 33 ++++++++++- 25 files changed, 254 insertions(+), 37 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/common/thread_instrumentation.h 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 3753704bb3..27a0d5fdbb 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -20,6 +20,7 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/exporter_utils.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" // forward declare google::protobuf::Message @@ -83,28 +84,32 @@ struct OtlpHttpClientOptions // User agent std::string user_agent; - inline OtlpHttpClientOptions(nostd::string_view input_url, - bool input_ssl_insecure_skip_verify, - nostd::string_view input_ssl_ca_cert_path, - nostd::string_view input_ssl_ca_cert_string, - nostd::string_view input_ssl_client_key_path, - nostd::string_view input_ssl_client_key_string, - nostd::string_view input_ssl_client_cert_path, - nostd::string_view input_ssl_client_cert_string, - nostd::string_view input_ssl_min_tls, - nostd::string_view input_ssl_max_tls, - nostd::string_view input_ssl_cipher, - nostd::string_view input_ssl_cipher_suite, - HttpRequestContentType input_content_type, - JsonBytesMappingKind input_json_bytes_mapping, - nostd::string_view input_compression, - bool input_use_json_name, - bool input_console_debug, - std::chrono::system_clock::duration input_timeout, - const OtlpHeaders &input_http_headers, - std::size_t input_concurrent_sessions = 64, - std::size_t input_max_requests_per_connection = 8, - nostd::string_view input_user_agent = GetOtlpDefaultUserAgent()) + std::shared_ptr thread_instrumentation; + + inline OtlpHttpClientOptions( + nostd::string_view input_url, + bool input_ssl_insecure_skip_verify, + nostd::string_view input_ssl_ca_cert_path, + nostd::string_view input_ssl_ca_cert_string, + nostd::string_view input_ssl_client_key_path, + nostd::string_view input_ssl_client_key_string, + nostd::string_view input_ssl_client_cert_path, + nostd::string_view input_ssl_client_cert_string, + nostd::string_view input_ssl_min_tls, + nostd::string_view input_ssl_max_tls, + nostd::string_view input_ssl_cipher, + nostd::string_view input_ssl_cipher_suite, + HttpRequestContentType input_content_type, + JsonBytesMappingKind input_json_bytes_mapping, + nostd::string_view input_compression, + bool input_use_json_name, + bool input_console_debug, + std::chrono::system_clock::duration input_timeout, + const OtlpHeaders &input_http_headers, + 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()) : url(input_url), ssl_options(input_url, input_ssl_insecure_skip_verify, @@ -127,7 +132,8 @@ struct OtlpHttpClientOptions http_headers(input_http_headers), max_concurrent_requests(input_concurrent_sessions), max_requests_per_connection(input_max_requests_per_connection), - user_agent(input_user_agent) + user_agent(input_user_agent), + thread_instrumentation(input_thread_instrumentation) {} }; 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 7f6d5a1b35..e54702059d 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 @@ -4,10 +4,12 @@ #pragma once #include +#include #include #include "opentelemetry/exporters/otlp/otlp_environment.h" #include "opentelemetry/exporters/otlp/otlp_http.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -101,6 +103,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpExporterOptions /** Compression type. */ std::string compression; + + std::shared_ptr thread_instrumentation; }; } // 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 60f674a3a7..77453be894 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 @@ -4,10 +4,12 @@ #pragma once #include +#include #include #include "opentelemetry/exporters/otlp/otlp_environment.h" #include "opentelemetry/exporters/otlp/otlp_http.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -101,6 +103,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterOptions /** Compression type. */ std::string compression; + + std::shared_ptr thread_instrumentation; }; } // 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 82c91aa51f..0f16c54db5 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 @@ -4,11 +4,13 @@ #pragma once #include +#include #include #include "opentelemetry/exporters/otlp/otlp_environment.h" #include "opentelemetry/exporters/otlp/otlp_http.h" #include "opentelemetry/exporters/otlp/otlp_preferred_temporality.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -104,6 +106,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterOptions /** Compression type. */ std::string compression; + + std::shared_ptr thread_instrumentation; }; } // namespace otlp diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index c330ffae2f..a20f88bf21 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -665,7 +665,7 @@ void ConvertListFieldToJson(nlohmann::json &value, OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options) : is_shutdown_(false), options_(options), - http_client_(http_client::HttpClientFactory::Create()), + http_client_(http_client::HttpClientFactory::Create(options.thread_instrumentation)), start_session_counter_(0), finished_session_counter_(0) { diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index f740a0babf..5719903562 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -57,7 +57,8 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index bd76bd10bd..cff14392a9 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -60,7 +60,8 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index 18b4ed0712..ae3ecf5aee 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter.cc @@ -61,7 +61,8 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptio options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 4c4a6dbeb2..0824a59a06 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -54,6 +54,7 @@ static nostd::span MakeSpan(T (&array)[N]) OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type, bool async_mode) { + std::shared_ptr not_instrumented; OtlpHttpExporterOptions options; options.content_type = content_type; options.console_debug = true; @@ -70,7 +71,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t "", /* ssl_cipher */ "", /* ssl_cipher_suite */ options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + options.console_debug, options.timeout, options.http_headers, not_instrumented); if (!async_mode) { otlp_http_client_options.max_concurrent_requests = 0; 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 35a0bb62e2..606f162024 100644 --- a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc @@ -54,6 +54,7 @@ static nostd::span MakeSpan(T (&array)[N]) OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type, bool async_mode) { + std::shared_ptr not_instrumented; OtlpHttpLogRecordExporterOptions options; options.content_type = content_type; options.console_debug = true; @@ -69,7 +70,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t "", /* ssl_cipher */ "", /* ssl_cipher_suite */ options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + options.console_debug, options.timeout, options.http_headers, not_instrumented); if (!async_mode) { otlp_http_client_options.max_concurrent_requests = 0; diff --git a/exporters/otlp/test/otlp_http_metric_exporter_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_test.cc index 174597248b..3b603d0e31 100644 --- a/exporters/otlp/test/otlp_http_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_metric_exporter_test.cc @@ -61,6 +61,7 @@ static IntegerType JsonToInteger(nlohmann::json value) OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type, bool async_mode) { + std::shared_ptr not_instrumented; OtlpHttpMetricExporterOptions options; options.content_type = content_type; options.console_debug = true; @@ -76,7 +77,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t "", /* ssl_cipher */ "", /* ssl_cipher_suite */ options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + options.console_debug, options.timeout, options.http_headers, not_instrumented); if (!async_mode) { otlp_http_client_options.max_concurrent_requests = 0; 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 ef65388fe1..92ad8435a8 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 @@ -23,6 +23,7 @@ #include "opentelemetry/nostd/function_ref.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -304,6 +305,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient public: // The call (curl_global_init) is not thread safe. Ensure this is called only once. HttpClient(); + HttpClient(const std::shared_ptr &thread_instrumentation); ~HttpClient() override; std::shared_ptr CreateSession( @@ -366,6 +368,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient std::mutex background_thread_m_; std::unique_ptr background_thread_; + std::shared_ptr background_thread_instrumentation_; std::chrono::milliseconds scheduled_delay_milliseconds_; nostd::shared_ptr curl_global_initializer_; diff --git a/ext/include/opentelemetry/ext/http/client/http_client_factory.h b/ext/include/opentelemetry/ext/http/client/http_client_factory.h index 43e15cf255..c1a326b7e6 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client_factory.h +++ b/ext/include/opentelemetry/ext/http/client/http_client_factory.h @@ -2,7 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once + #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace ext @@ -17,6 +20,8 @@ class HttpClientFactory static std::shared_ptr CreateSync(); static std::shared_ptr Create(); + static std::shared_ptr Create( + const std::shared_ptr &thread_instrumentation); }; } // namespace client } // namespace http diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 6827b9f9c7..fe70b670a5 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -189,7 +189,18 @@ HttpClient::HttpClient() next_session_id_{0}, max_sessions_per_connection_{8}, scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, - curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) + curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()), + background_thread_instrumentation_(nullptr) +{} + +HttpClient::HttpClient( + const std::shared_ptr &thread_instrumentation) + : multi_handle_(curl_multi_init()), + next_session_id_{0}, + max_sessions_per_connection_{8}, + scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, + curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()), + background_thread_instrumentation_(thread_instrumentation) {} HttpClient::~HttpClient() @@ -345,6 +356,11 @@ void HttpClient::MaybeSpawnBackgroundThread() background_thread_.reset(new std::thread( [](HttpClient *self) { + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->OnStart(); + } + int still_running = 1; while (true) { @@ -452,6 +468,11 @@ void HttpClient::MaybeSpawnBackgroundThread() } } } + + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->OnEnd(); + } }, this)); } diff --git a/ext/src/http/client/curl/http_client_factory_curl.cc b/ext/src/http/client/curl/http_client_factory_curl.cc index fd0a7d6a81..05cb111f6d 100644 --- a/ext/src/http/client/curl/http_client_factory_curl.cc +++ b/ext/src/http/client/curl/http_client_factory_curl.cc @@ -14,6 +14,12 @@ std::shared_ptr http_client::HttpClientFactory::Create( return std::make_shared(); } +std::shared_ptr http_client::HttpClientFactory::Create( + const std::shared_ptr &thread_instrumentation) +{ + return std::make_shared(thread_instrumentation); +} + std::shared_ptr http_client::HttpClientFactory::CreateSync() { return std::make_shared(); diff --git a/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h new file mode 100644 index 0000000000..e04ac60903 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace common +{ + +class ThreadInstrumentation +{ +public: + ThreadInstrumentation() = default; + virtual ~ThreadInstrumentation() = default; + + virtual void OnStart() = 0; + virtual void OnEnd() = 0; + virtual void BeforeWait() = 0; + virtual void AfterWait() = 0; + virtual void BeforeLoad() = 0; + virtual void AfterLoad() = 0; +}; + +} // namespace common +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h index b52476071a..d66e8610f3 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h @@ -158,6 +158,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor /* The background worker thread */ std::thread worker_thread_; + std::shared_ptr worker_thread_instrumentation_; }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h index b130f947af..b21e972228 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h @@ -6,6 +6,7 @@ #include #include +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -34,6 +35,8 @@ struct BatchLogRecordProcessorOptions * equal to max_queue_size. */ size_t max_export_batch_size = 512; + + std::shared_ptr thread_instrumentation; }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h index d72ec08839..6d26f55e1c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h @@ -11,6 +11,7 @@ #include #include +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/metric_reader.h" @@ -49,6 +50,8 @@ class PeriodicExportingMetricReader : public MetricReader /* The background worker thread */ std::thread worker_thread_; + std::shared_ptr worker_thread_instrumentation_; + std::shared_ptr collect_thread_instrumentation_; /* Synchronization primitives */ std::atomic is_force_wakeup_background_worker_{false}; diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h index 80514f96d8..07594bedea 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h @@ -3,6 +3,7 @@ #pragma once +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" #include @@ -28,6 +29,9 @@ struct PeriodicExportingMetricReaderOptions /* how long the export can run before it is cancelled. */ std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); + + std::shared_ptr periodic_thread_instrumentation; + std::shared_ptr collect_thread_instrumentation; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h index e069a461e6..16e1d48ee6 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h @@ -159,6 +159,8 @@ class BatchSpanProcessor : public SpanProcessor /* The background worker thread */ std::thread worker_thread_; + + std::shared_ptr worker_thread_instrumentation_; }; } // namespace trace diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h index ac1052383b..e4b13b8ce0 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h @@ -5,6 +5,7 @@ #include +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -33,6 +34,8 @@ struct BatchSpanProcessorOptions * equal to max_queue_size. */ size_t max_export_batch_size = 512; + + std::shared_ptr thread_instrumentation; }; } // namespace trace diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index f5c35ad339..3e8cbf9155 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -44,7 +44,8 @@ BatchLogRecordProcessor::BatchLogRecordProcessor( max_export_batch_size_(max_export_batch_size), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), - worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this) + worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this), + worker_thread_instrumentation_(nullptr) {} BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr &&exporter, @@ -55,7 +56,8 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr()), - worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this) + worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this), + worker_thread_instrumentation_(options.thread_instrumentation) {} std::unique_ptr BatchLogRecordProcessor::MakeRecordable() noexcept @@ -151,10 +153,20 @@ bool BatchLogRecordProcessor::ForceFlush(std::chrono::microseconds timeout) noex void BatchLogRecordProcessor::DoBackgroundWork() { + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnStart(); + } + auto timeout = scheduled_delay_millis_; while (true) { + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeWait(); + } + // Wait for `timeout` milliseconds std::unique_lock lk(synchronization_data_->cv_m); synchronization_data_->cv.wait_for(lk, timeout, [this] { @@ -168,6 +180,11 @@ void BatchLogRecordProcessor::DoBackgroundWork() synchronization_data_->is_force_wakeup_background_worker.store(false, std::memory_order_release); + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterWait(); + } + if (synchronization_data_->is_shutdown.load() == true) { DrainQueue(); @@ -182,10 +199,20 @@ void BatchLogRecordProcessor::DoBackgroundWork() // Subtract the duration of this export call from the next `timeout`. timeout = scheduled_delay_millis_ - duration; } + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnEnd(); + } } void BatchLogRecordProcessor::Export() { + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeLoad(); + } + do { std::vector> records_arr; @@ -224,6 +251,11 @@ void BatchLogRecordProcessor::Export() nostd::span>(records_arr.data(), records_arr.size())); NotifyCompletion(notify_force_flush, exporter_, synchronization_data_); } while (true); + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterLoad(); + } } void BatchLogRecordProcessor::NotifyCompletion( diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc index 682badf3a4..13b701216c 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -43,7 +43,9 @@ PeriodicExportingMetricReader::PeriodicExportingMetricReader( const PeriodicExportingMetricReaderOptions &option) : exporter_{std::move(exporter)}, export_interval_millis_{option.export_interval_millis}, - export_timeout_millis_{option.export_timeout_millis} + export_timeout_millis_{option.export_timeout_millis}, + worker_thread_instrumentation_(option.periodic_thread_instrumentation), + collect_thread_instrumentation_(option.collect_thread_instrumentation) { if (export_interval_millis_ <= export_timeout_millis_) { @@ -67,18 +69,42 @@ void PeriodicExportingMetricReader::OnInitialized() noexcept void PeriodicExportingMetricReader::DoBackgroundWork() { - std::unique_lock lk(cv_m_); + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnStart(); + } + do { - auto start = std::chrono::steady_clock::now(); + auto start = std::chrono::steady_clock::now(); + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeLoad(); + } + auto status = CollectAndExportOnce(); + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterLoad(); + } + if (!status) { OTEL_INTERNAL_LOG_ERROR("[Periodic Exporting Metric Reader] Collect-Export Cycle Failure.") } + auto end = std::chrono::steady_clock::now(); auto export_time_ms = std::chrono::duration_cast(end - start); auto remaining_wait_interval_ms = export_interval_millis_ - export_time_ms; + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeWait(); + } + + std::unique_lock lk(cv_m_); cv_.wait_for(lk, remaining_wait_interval_ms, [this]() { if (is_force_wakeup_background_worker_.load(std::memory_order_acquire)) { @@ -87,7 +113,18 @@ void PeriodicExportingMetricReader::DoBackgroundWork() } return IsShutdown(); }); + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterWait(); + } + } while (IsShutdown() != true); + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnEnd(); + } } bool PeriodicExportingMetricReader::CollectAndExportOnce() @@ -106,6 +143,12 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce() task_thread.reset( new std::thread([this, &cancel_export_for_timeout, sender = std::move(sender)] { + if (collect_thread_instrumentation_ != nullptr) + { + collect_thread_instrumentation_->OnStart(); + collect_thread_instrumentation_->BeforeLoad(); + } + this->Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) { if (cancel_export_for_timeout.load(std::memory_order_acquire)) { @@ -119,6 +162,12 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce() }); const_cast &>(sender).set_value(); + + if (collect_thread_instrumentation_ != nullptr) + { + collect_thread_instrumentation_->AfterLoad(); + collect_thread_instrumentation_->OnEnd(); + } })); std::future_status status; diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index f0f55d20e8..62d309c5da 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -46,7 +46,8 @@ BatchSpanProcessor::BatchSpanProcessor(std::unique_ptr &&exporter, max_export_batch_size_(options.max_export_batch_size), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), - worker_thread_(&BatchSpanProcessor::DoBackgroundWork, this) + worker_thread_(&BatchSpanProcessor::DoBackgroundWork, this), + worker_thread_instrumentation_(options.thread_instrumentation) {} std::unique_ptr BatchSpanProcessor::MakeRecordable() noexcept @@ -149,10 +150,20 @@ bool BatchSpanProcessor::ForceFlush(std::chrono::microseconds timeout) noexcept void BatchSpanProcessor::DoBackgroundWork() { + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnStart(); + } + auto timeout = schedule_delay_millis_; while (true) { + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeWait(); + } + // Wait for `timeout` milliseconds std::unique_lock lk(synchronization_data_->cv_m); synchronization_data_->cv.wait_for(lk, timeout, [this] { @@ -166,6 +177,11 @@ void BatchSpanProcessor::DoBackgroundWork() synchronization_data_->is_force_wakeup_background_worker.store(false, std::memory_order_release); + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterWait(); + } + if (synchronization_data_->is_shutdown.load() == true) { DrainQueue(); @@ -180,10 +196,20 @@ void BatchSpanProcessor::DoBackgroundWork() // Subtract the duration of this export call from the next `timeout`. timeout = schedule_delay_millis_ - duration; } + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnEnd(); + } } void BatchSpanProcessor::Export() { + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeLoad(); + } + do { std::vector> spans_arr; @@ -222,6 +248,11 @@ void BatchSpanProcessor::Export() exporter_->Export(nostd::span>(spans_arr.data(), spans_arr.size())); NotifyCompletion(notify_force_flush, exporter_, synchronization_data_); } while (true); + + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterLoad(); + } } void BatchSpanProcessor::NotifyCompletion( From b594ee041d85e0787b9a989b12fc6b15c0b80755 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 27 Nov 2024 09:58:37 +0100 Subject: [PATCH 02/21] warning cleanup --- ext/src/http/client/curl/http_client_curl.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index fe70b670a5..17ba379050 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -188,9 +188,9 @@ HttpClient::HttpClient() : multi_handle_(curl_multi_init()), next_session_id_{0}, max_sessions_per_connection_{8}, + background_thread_instrumentation_(nullptr), scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, - curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()), - background_thread_instrumentation_(nullptr) + curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) {} HttpClient::HttpClient( @@ -198,9 +198,9 @@ HttpClient::HttpClient( : multi_handle_(curl_multi_init()), next_session_id_{0}, max_sessions_per_connection_{8}, + background_thread_instrumentation_(thread_instrumentation), scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, - curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()), - background_thread_instrumentation_(thread_instrumentation) + curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) {} HttpClient::~HttpClient() From 88bd0bf47f8c445984483a7cdd6a7ff990f6b03e Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 27 Nov 2024 12:54:05 +0100 Subject: [PATCH 03/21] cleanup initializers in structs --- .../include/opentelemetry/exporters/otlp/otlp_http_client.h | 3 ++- .../exporters/otlp/otlp_http_exporter_options.h | 3 ++- .../exporters/otlp/otlp_http_log_record_exporter_options.h | 3 ++- .../exporters/otlp/otlp_http_metric_exporter_options.h | 3 ++- .../sdk/logs/batch_log_record_processor_options.h | 4 +++- .../export/periodic_exporting_metric_reader_options.h | 6 ++++-- .../opentelemetry/sdk/trace/batch_span_processor_options.h | 3 ++- 7 files changed, 17 insertions(+), 8 deletions(-) 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 27a0d5fdbb..1f3be445f5 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -84,7 +84,8 @@ struct OtlpHttpClientOptions // User agent std::string user_agent; - std::shared_ptr thread_instrumentation; + std::shared_ptr thread_instrumentation = + std::shared_ptr{}; inline OtlpHttpClientOptions( nostd::string_view input_url, 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 e54702059d..9123e122bc 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 @@ -104,7 +104,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpExporterOptions /** Compression type. */ std::string compression; - std::shared_ptr thread_instrumentation; + std::shared_ptr thread_instrumentation = + std::shared_ptr{}; }; } // 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 77453be894..805f4dca86 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 @@ -104,7 +104,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterOptions /** Compression type. */ std::string compression; - std::shared_ptr thread_instrumentation; + std::shared_ptr thread_instrumentation = + std::shared_ptr{}; }; } // 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 0f16c54db5..07a7d75219 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 @@ -107,7 +107,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterOptions /** Compression type. */ std::string compression; - std::shared_ptr thread_instrumentation; + std::shared_ptr thread_instrumentation = + std::shared_ptr{}; }; } // namespace otlp diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h index b21e972228..cea1260929 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h @@ -36,7 +36,9 @@ struct BatchLogRecordProcessorOptions */ size_t max_export_batch_size = 512; - std::shared_ptr thread_instrumentation; + std::shared_ptr thread_instrumentation = + std::shared_ptr + {} }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h index 07594bedea..79896eac86 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h @@ -30,8 +30,10 @@ struct PeriodicExportingMetricReaderOptions /* how long the export can run before it is cancelled. */ std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); - std::shared_ptr periodic_thread_instrumentation; - std::shared_ptr collect_thread_instrumentation; + std::shared_ptr periodic_thread_instrumentation = + std::shared_ptr{}; + std::shared_ptr collect_thread_instrumentation = + std::shared_ptr{}; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h index e4b13b8ce0..fb280ed89d 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h @@ -35,7 +35,8 @@ struct BatchSpanProcessorOptions */ size_t max_export_batch_size = 512; - std::shared_ptr thread_instrumentation; + std::shared_ptr thread_instrumentation = + std::shared_ptr{}; }; } // namespace trace From abb967448d458bf8ba7078abd60a30d6d5de5e14 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 27 Nov 2024 12:59:21 +0100 Subject: [PATCH 04/21] typo --- .../sdk/logs/batch_log_record_processor_options.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h index cea1260929..b62f99099f 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h @@ -37,8 +37,7 @@ struct BatchLogRecordProcessorOptions size_t max_export_batch_size = 512; std::shared_ptr thread_instrumentation = - std::shared_ptr - {} + std::shared_ptr{}; }; } // namespace logs From 24816a8a078654ce56b389d8f22b39176b54c13f Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 27 Nov 2024 17:09:27 +0100 Subject: [PATCH 05/21] cleanup --- .../exporters/otlp/otlp_http_client.h | 2 +- .../otlp/otlp_http_exporter_options.h | 2 +- .../otlp_http_log_record_exporter_options.h | 2 +- .../otlp/otlp_http_metric_exporter_options.h | 2 +- exporters/otlp/src/otlp_http_exporter.cc | 1 + .../otlp/src/otlp_http_log_record_exporter.cc | 15 ++++++++------- .../otlp/src/otlp_http_metric_exporter.cc | 1 + .../test/otlp_http_metric_exporter_test.cc | 1 + ext/src/http/client/curl/http_client_curl.cc | 1 + .../client/curl/http_client_factory_curl.cc | 1 + .../sdk/logs/batch_log_record_processor.h | 2 +- .../logs/batch_log_record_processor_options.h | 2 +- .../export/periodic_exporting_metric_reader.h | 10 +++++----- ...periodic_exporting_metric_reader_options.h | 4 ++-- .../sdk/trace/batch_span_processor.h | 3 +-- .../sdk/trace/batch_span_processor_options.h | 2 +- sdk/src/logs/batch_log_record_processor.cc | 19 +++++++++++++------ .../periodic_exporting_metric_reader.cc | 1 + sdk/src/trace/batch_span_processor.cc | 10 +++++++--- 19 files changed, 49 insertions(+), 32 deletions(-) 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 1f3be445f5..1668f0381d 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -85,7 +85,7 @@ struct OtlpHttpClientOptions std::string user_agent; std::shared_ptr thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); inline OtlpHttpClientOptions( nostd::string_view input_url, 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 9123e122bc..8818ff7174 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 @@ -105,7 +105,7 @@ struct OPENTELEMETRY_EXPORT OtlpHttpExporterOptions std::string compression; std::shared_ptr thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); }; } // 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 805f4dca86..4cc3d6266c 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 @@ -105,7 +105,7 @@ struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterOptions std::string compression; std::shared_ptr thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); }; } // 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 07a7d75219..78b81838dc 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 @@ -108,7 +108,7 @@ struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterOptions std::string compression; std::shared_ptr thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); }; } // namespace otlp diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 5719903562..2b417e8189 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -78,6 +78,7 @@ OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr http_client) options.console_debug = http_client_->GetOptions().console_debug; options.timeout = http_client_->GetOptions().timeout; options.http_headers = http_client_->GetOptions().http_headers; + options.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; #ifdef ENABLE_ASYNC_EXPORT options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index cff14392a9..4dc71fe2e3 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -75,13 +75,14 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(std::unique_ptr(options_); - options.url = http_client_->GetOptions().url; - options.content_type = http_client_->GetOptions().content_type; - options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; - options.use_json_name = http_client_->GetOptions().use_json_name; - options.console_debug = http_client_->GetOptions().console_debug; - options.timeout = http_client_->GetOptions().timeout; - options.http_headers = http_client_->GetOptions().http_headers; + options.url = http_client_->GetOptions().url; + options.content_type = http_client_->GetOptions().content_type; + options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; + options.use_json_name = http_client_->GetOptions().use_json_name; + options.console_debug = http_client_->GetOptions().console_debug; + options.timeout = http_client_->GetOptions().timeout; + options.http_headers = http_client_->GetOptions().http_headers; + options.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; #ifdef ENABLE_ASYNC_EXPORT options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index ae3ecf5aee..4748d22b47 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter.cc @@ -85,6 +85,7 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(std::unique_ptr h options.console_debug = http_client_->GetOptions().console_debug; options.timeout = http_client_->GetOptions().timeout; options.http_headers = http_client_->GetOptions().http_headers; + options.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; #ifdef ENABLE_ASYNC_EXPORT options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; diff --git a/exporters/otlp/test/otlp_http_metric_exporter_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_test.cc index e2b2ab5602..3c438093db 100644 --- a/exporters/otlp/test/otlp_http_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_metric_exporter_test.cc @@ -28,6 +28,7 @@ #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" #include "opentelemetry/sdk/metrics/data/metric_data.h" #include "opentelemetry/sdk/metrics/data/point_data.h" diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 17ba379050..c4f26a0dea 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -21,6 +21,7 @@ #include "opentelemetry/ext/http/common/url_parser.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" #ifdef ENABLE_OTLP_COMPRESSION_PREVIEW diff --git a/ext/src/http/client/curl/http_client_factory_curl.cc b/ext/src/http/client/curl/http_client_factory_curl.cc index 05cb111f6d..b339c2d93c 100644 --- a/ext/src/http/client/curl/http_client_factory_curl.cc +++ b/ext/src/http/client/curl/http_client_factory_curl.cc @@ -6,6 +6,7 @@ #include "opentelemetry/ext/http/client/curl/http_client_curl.h" #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/ext/http/client/http_client_factory.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" namespace http_client = opentelemetry::ext::http::client; diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h index d66e8610f3..da2bd25e9a 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h @@ -157,8 +157,8 @@ class BatchLogRecordProcessor : public LogRecordProcessor std::shared_ptr synchronization_data_; /* The background worker thread */ - std::thread worker_thread_; std::shared_ptr worker_thread_instrumentation_; + std::thread worker_thread_; }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h index b62f99099f..1f27d0f9b8 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h @@ -37,7 +37,7 @@ struct BatchLogRecordProcessorOptions size_t max_export_batch_size = 512; std::shared_ptr thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h index 6d26f55e1c..a8e51c2a25 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h @@ -48,17 +48,17 @@ class PeriodicExportingMetricReader : public MetricReader void DoBackgroundWork(); bool CollectAndExportOnce(); - /* The background worker thread */ - std::thread worker_thread_; - std::shared_ptr worker_thread_instrumentation_; - std::shared_ptr collect_thread_instrumentation_; - /* Synchronization primitives */ std::atomic is_force_wakeup_background_worker_{false}; std::atomic force_flush_pending_sequence_{0}; std::atomic force_flush_notified_sequence_{0}; std::condition_variable cv_, force_flush_cv_; std::mutex cv_m_, force_flush_m_; + + /* The background worker thread */ + std::shared_ptr worker_thread_instrumentation_; + std::shared_ptr collect_thread_instrumentation_; + std::thread worker_thread_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h index 79896eac86..6c2f4b889c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h @@ -31,9 +31,9 @@ struct PeriodicExportingMetricReaderOptions std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); std::shared_ptr periodic_thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); std::shared_ptr collect_thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h index 16e1d48ee6..a6ba35f389 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h @@ -158,9 +158,8 @@ class BatchSpanProcessor : public SpanProcessor std::shared_ptr synchronization_data_; /* The background worker thread */ - std::thread worker_thread_; - std::shared_ptr worker_thread_instrumentation_; + std::thread worker_thread_; }; } // namespace trace diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h index fb280ed89d..eda4533f43 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h @@ -36,7 +36,7 @@ struct BatchSpanProcessorOptions size_t max_export_batch_size = 512; std::shared_ptr thread_instrumentation = - std::shared_ptr{}; + std::shared_ptr(nullptr); }; } // namespace trace diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index 3e8cbf9155..8ed5c18269 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -19,6 +19,7 @@ #include "opentelemetry/sdk/common/atomic_unique_ptr.h" #include "opentelemetry/sdk/common/circular_buffer.h" #include "opentelemetry/sdk/common/circular_buffer_range.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/logs/batch_log_record_processor.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" #include "opentelemetry/sdk/logs/exporter.h" @@ -44,9 +45,12 @@ BatchLogRecordProcessor::BatchLogRecordProcessor( max_export_batch_size_(max_export_batch_size), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), - worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this), - worker_thread_instrumentation_(nullptr) -{} + worker_thread_instrumentation_(nullptr), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchLogRecordProcessor::DoBackgroundWork, this); +} BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options) @@ -56,9 +60,12 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr()), - worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this), - worker_thread_instrumentation_(options.thread_instrumentation) -{} + worker_thread_instrumentation_(options.thread_instrumentation), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchLogRecordProcessor::DoBackgroundWork, this); +} std::unique_ptr BatchLogRecordProcessor::MakeRecordable() noexcept { diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc index 13b701216c..8eeb56a53e 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -14,6 +14,7 @@ #include "opentelemetry/common/timestamp.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index 62d309c5da..578b0a4fa0 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -20,6 +20,7 @@ #include "opentelemetry/sdk/common/circular_buffer.h" #include "opentelemetry/sdk/common/circular_buffer_range.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/trace/batch_span_processor.h" #include "opentelemetry/sdk/trace/batch_span_processor_options.h" #include "opentelemetry/sdk/trace/exporter.h" @@ -46,9 +47,12 @@ BatchSpanProcessor::BatchSpanProcessor(std::unique_ptr &&exporter, max_export_batch_size_(options.max_export_batch_size), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), - worker_thread_(&BatchSpanProcessor::DoBackgroundWork, this), - worker_thread_instrumentation_(options.thread_instrumentation) -{} + worker_thread_instrumentation_(options.thread_instrumentation), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchSpanProcessor::DoBackgroundWork, this); +} std::unique_ptr BatchSpanProcessor::MakeRecordable() noexcept { From d5f107586fed33ff05713591d02249ccafdf9d9c Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 27 Nov 2024 17:57:36 +0100 Subject: [PATCH 06/21] Fix curl thread detach --- ext/src/http/client/curl/http_client_curl.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index c4f26a0dea..a3445ea5bb 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -462,6 +462,11 @@ void HttpClient::MaybeSpawnBackgroundThread() { if (self->background_thread_) { + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->OnEnd(); + self->background_thread_instrumentation_.reset(); + } self->background_thread_->detach(); self->background_thread_.reset(); } @@ -469,11 +474,6 @@ void HttpClient::MaybeSpawnBackgroundThread() } } } - - if (self->background_thread_instrumentation_ != nullptr) - { - self->background_thread_instrumentation_->OnEnd(); - } }, this)); } From 2d223d83decd64f950c3bcb5544a8847a15f1c88 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 17 Dec 2024 18:59:46 +0100 Subject: [PATCH 07/21] Moved runtime options to a dedicated structure. --- .../exporters/otlp/otlp_http_exporter.h | 11 +++- .../otlp/otlp_http_exporter_factory.h | 8 +++ .../otlp/otlp_http_exporter_options.h | 5 -- .../otlp/otlp_http_exporter_runtime_options.h | 31 ++++++++++ .../otlp/otlp_http_log_record_exporter.h | 13 +++- .../otlp_http_log_record_exporter_factory.h | 8 +++ .../otlp_http_log_record_exporter_options.h | 5 -- ...http_log_record_exporter_runtime_options.h | 31 ++++++++++ .../otlp/otlp_http_metric_exporter.h | 13 +++- .../otlp/otlp_http_metric_exporter_factory.h | 8 +++ .../otlp/otlp_http_metric_exporter_options.h | 5 -- ...tlp_http_metric_exporter_runtime_options.h | 31 ++++++++++ .../otlp_file_log_record_exporter_factory.cc | 12 +++- exporters/otlp/src/otlp_http_exporter.cc | 57 ++++++++++++++---- .../otlp/src/otlp_http_exporter_factory.cc | 12 +++- .../otlp/src/otlp_http_log_record_exporter.cc | 59 ++++++++++++++---- .../otlp/src/otlp_http_metric_exporter.cc | 60 +++++++++++++++---- .../src/otlp_http_metric_exporter_factory.cc | 11 +++- .../sdk/logs/batch_log_record_processor.h | 17 +++++- .../logs/batch_log_record_processor_factory.h | 9 +++ .../logs/batch_log_record_processor_options.h | 4 -- ...tch_log_record_processor_runtime_options.h | 30 ++++++++++ .../export/periodic_exporting_metric_reader.h | 7 ++- ...periodic_exporting_metric_reader_factory.h | 8 ++- ...periodic_exporting_metric_reader_options.h | 6 -- ..._exporting_metric_reader_runtime_options.h | 30 ++++++++++ .../sdk/trace/batch_span_processor.h | 17 +++++- .../sdk/trace/batch_span_processor_factory.h | 9 +++ .../sdk/trace/batch_span_processor_options.h | 4 -- .../batch_span_processor_runtime_options.h | 29 +++++++++ sdk/src/logs/batch_log_record_processor.cc | 20 ++++++- .../batch_log_record_processor_factory.cc | 11 +++- .../periodic_exporting_metric_reader.cc | 31 ++++++++-- ...eriodic_exporting_metric_reader_factory.cc | 14 ++++- sdk/src/trace/batch_span_processor.cc | 18 +++++- sdk/src/trace/batch_span_processor_factory.cc | 13 +++- 36 files changed, 568 insertions(+), 89 deletions(-) create mode 100644 exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h create mode 100644 exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h create mode 100644 exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h create mode 100644 sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h create mode 100644 sdk/include/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h index 38f1d9e306..454959dd45 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/trace/exporter.h" @@ -36,6 +37,12 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t */ explicit OtlpHttpExporter(const OtlpHttpExporterOptions &options); + /** + * Create an OtlpHttpExporter using the given options. + */ + OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options); + /** * Create a span recordable. * @return a newly initialized Recordable object @@ -69,7 +76,9 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t private: // The configuration options associated with this exporter. - const OtlpHttpExporterOptions options_; + OtlpHttpExporterOptions options_; + // The runtime options associated with this exporter. + OtlpHttpExporterRuntimeOptions runtime_options_; // Object that stores the HTTP sessions that have been created std::unique_ptr http_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h index 9e072a0534..bf42d26354 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporterFactory */ static std::unique_ptr Create( const OtlpHttpExporterOptions &options); + + /** + * Create an OtlpHttpExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options); }; } // namespace otlp 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 8818ff7174..7f6d5a1b35 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 @@ -4,12 +4,10 @@ #pragma once #include -#include #include #include "opentelemetry/exporters/otlp/otlp_environment.h" #include "opentelemetry/exporters/otlp/otlp_http.h" -#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -103,9 +101,6 @@ struct OPENTELEMETRY_EXPORT OtlpHttpExporterOptions /** Compression type. */ std::string compression; - - std::shared_ptr thread_instrumentation = - std::shared_ptr(nullptr); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h new file mode 100644 index 0000000000..02ec76acf9 --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP HTTP traces exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpHttpExporterRuntimeOptions +{ + OtlpHttpExporterRuntimeOptions() = default; + ~OtlpHttpExporterRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h index 9439e5725b..aba430522e 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/logs/exporter.h" @@ -37,6 +38,14 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco */ OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options); + /** + * Create an OtlpHttpLogRecordExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options); + /** * Creates a recordable that stores the data in a JSON object */ @@ -68,7 +77,9 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco private: // Configuration options for the exporter - const OtlpHttpLogRecordExporterOptions options_; + OtlpHttpLogRecordExporterOptions options_; + // Runtime options for the exporter + OtlpHttpLogRecordExporterRuntimeOptions runtime_options_; // Object that stores the HTTP sessions that have been created std::unique_ptr http_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h index c1e9de9ae1..d01eb4259a 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterFactory */ static std::unique_ptr Create( const OtlpHttpLogRecordExporterOptions &options); + + /** + * Create a OtlpHttpLogRecordExporter. + */ + static std::unique_ptr Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options); }; } // 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 4cc3d6266c..60f674a3a7 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 @@ -4,12 +4,10 @@ #pragma once #include -#include #include #include "opentelemetry/exporters/otlp/otlp_environment.h" #include "opentelemetry/exporters/otlp/otlp_http.h" -#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -103,9 +101,6 @@ struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterOptions /** Compression type. */ std::string compression; - - std::shared_ptr thread_instrumentation = - std::shared_ptr(nullptr); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h new file mode 100644 index 0000000000..b5ef2ed967 --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP HTTP log record exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterRuntimeOptions +{ + OtlpHttpLogRecordExporterRuntimeOptions() = default; + ~OtlpHttpLogRecordExporterRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h index d74177e5c9..8733a740a9 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" @@ -36,6 +37,14 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::PushMet */ OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options); + /** + * Create an OtlpHttpMetricExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options); + /** * Get the AggregationTemporality for exporter * @@ -58,7 +67,9 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::PushMet private: // Configuration options for the exporter - const OtlpHttpMetricExporterOptions options_; + OtlpHttpMetricExporterOptions options_; + // Runtime options for the exporter + OtlpHttpMetricExporterRuntimeOptions runtime_options_; // Aggregation Temporality Selector const sdk::metrics::AggregationTemporalitySelector aggregation_temporality_selector_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h index a290d91fd0..cb026cbe1e 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpHttpMetricExporterFactory */ static std::unique_ptr Create( const OtlpHttpMetricExporterOptions &options); + + /** + * Create a OtlpHttpMetricExporter. + */ + static std::unique_ptr Create( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options); }; } // 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 78b81838dc..82c91aa51f 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 @@ -4,13 +4,11 @@ #pragma once #include -#include #include #include "opentelemetry/exporters/otlp/otlp_environment.h" #include "opentelemetry/exporters/otlp/otlp_http.h" #include "opentelemetry/exporters/otlp/otlp_preferred_temporality.h" -#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -106,9 +104,6 @@ struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterOptions /** Compression type. */ std::string compression; - - std::shared_ptr thread_instrumentation = - std::shared_ptr(nullptr); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h new file mode 100644 index 0000000000..c0e91a342c --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP HTTP metrics exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterRuntimeOptions +{ + OtlpHttpMetricExporterRuntimeOptions() = default; + ~OtlpHttpMetricExporterRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc index 145e52262a..3bd1bbb237 100644 --- a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc @@ -4,6 +4,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +21,18 @@ OtlpFileLogRecordExporterFactory::Create() std::unique_ptr OtlpFileLogRecordExporterFactory::Create(const OtlpFileLogRecordExporterOptions &options) +{ + OtlpFileLogRecordExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpFileLogRecordExporterFactory::Create( + const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpFileLogRecordExporter(options)); + new OtlpFileLogRecordExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 2b417e8189..c5afee8f0b 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -39,6 +39,40 @@ OtlpHttpExporter::OtlpHttpExporter() : OtlpHttpExporter(OtlpHttpExporterOptions( OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions( + options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ))) +{} + +OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, options.ssl_insecure_skip_verify, options.ssl_ca_cert_path, @@ -58,7 +92,7 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) options.console_debug, options.timeout, options.http_headers, - options.thread_instrumentation + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -70,19 +104,18 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr http_client) : options_(OtlpHttpExporterOptions()), http_client_(std::move(http_client)) { - OtlpHttpExporterOptions &options = const_cast(options_); - options.url = http_client_->GetOptions().url; - options.content_type = http_client_->GetOptions().content_type; - options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; - options.use_json_name = http_client_->GetOptions().use_json_name; - options.console_debug = http_client_->GetOptions().console_debug; - options.timeout = http_client_->GetOptions().timeout; - options.http_headers = http_client_->GetOptions().http_headers; - options.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; + options_.url = http_client_->GetOptions().url; + options_.content_type = http_client_->GetOptions().content_type; + options_.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; + options_.use_json_name = http_client_->GetOptions().use_json_name; + options_.console_debug = http_client_->GetOptions().console_debug; + options_.timeout = http_client_->GetOptions().timeout; + options_.http_headers = http_client_->GetOptions().http_headers; #ifdef ENABLE_ASYNC_EXPORT - options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; - options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; + options_.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; + options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif + runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_exporter_factory.cc b/exporters/otlp/src/otlp_http_exporter_factory.cc index 7d892bc337..eca3552146 100644 --- a/exporters/otlp/src/otlp_http_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_exporter_factory.cc @@ -4,6 +4,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,7 +21,16 @@ std::unique_ptr OtlpHttpExporterFactory std::unique_ptr OtlpHttpExporterFactory::Create( const OtlpHttpExporterOptions &options) { - std::unique_ptr exporter(new OtlpHttpExporter(options)); + OtlpHttpExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr OtlpHttpExporterFactory::Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options) +{ + std::unique_ptr exporter( + new OtlpHttpExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index 4dc71fe2e3..3423ad8708 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -42,6 +42,41 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter() OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( const OtlpHttpLogRecordExporterOptions &options) : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions( + options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ))) +{} + +OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, options.ssl_insecure_skip_verify, options.ssl_ca_cert_path, @@ -61,7 +96,7 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( options.console_debug, options.timeout, options.http_headers, - options.thread_instrumentation + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -73,20 +108,18 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(std::unique_ptr http_client) : options_(OtlpHttpLogRecordExporterOptions()), http_client_(std::move(http_client)) { - OtlpHttpLogRecordExporterOptions &options = - const_cast(options_); - options.url = http_client_->GetOptions().url; - options.content_type = http_client_->GetOptions().content_type; - options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; - options.use_json_name = http_client_->GetOptions().use_json_name; - options.console_debug = http_client_->GetOptions().console_debug; - options.timeout = http_client_->GetOptions().timeout; - options.http_headers = http_client_->GetOptions().http_headers; - options.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; + options_.url = http_client_->GetOptions().url; + options_.content_type = http_client_->GetOptions().content_type; + options_.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; + options_.use_json_name = http_client_->GetOptions().use_json_name; + options_.console_debug = http_client_->GetOptions().console_debug; + options_.timeout = http_client_->GetOptions().timeout; + options_.http_headers = http_client_->GetOptions().http_headers; #ifdef ENABLE_ASYNC_EXPORT - options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; - options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; + options_.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; + options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif + runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index 4748d22b47..18a6908187 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter.cc @@ -41,6 +41,43 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter() OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options) : options_(options), + runtime_options_(), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, + http_client_(new OtlpHttpClient(OtlpHttpClientOptions( + options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ))) +{} + +OtlpHttpMetricExporter::OtlpHttpMetricExporter( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), aggregation_temporality_selector_{ OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, @@ -62,7 +99,7 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptio options.console_debug, options.timeout, options.http_headers, - options.thread_instrumentation + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -77,19 +114,18 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(std::unique_ptr h OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, http_client_(std::move(http_client)) { - OtlpHttpMetricExporterOptions &options = const_cast(options_); - options.url = http_client_->GetOptions().url; - options.content_type = http_client_->GetOptions().content_type; - options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; - options.use_json_name = http_client_->GetOptions().use_json_name; - options.console_debug = http_client_->GetOptions().console_debug; - options.timeout = http_client_->GetOptions().timeout; - options.http_headers = http_client_->GetOptions().http_headers; - options.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; + options_.url = http_client_->GetOptions().url; + options_.content_type = http_client_->GetOptions().content_type; + options_.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; + options_.use_json_name = http_client_->GetOptions().use_json_name; + options_.console_debug = http_client_->GetOptions().console_debug; + options_.timeout = http_client_->GetOptions().timeout; + options_.http_headers = http_client_->GetOptions().http_headers; #ifdef ENABLE_ASYNC_EXPORT - options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; - options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; + options_.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; + options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif + runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_metric_exporter_factory.cc b/exporters/otlp/src/otlp_http_metric_exporter_factory.cc index 883ce2c503..1bd6d8a644 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter_factory.cc @@ -4,6 +4,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +21,17 @@ OtlpHttpMetricExporterFactory::Create() std::unique_ptr OtlpHttpMetricExporterFactory::Create(const OtlpHttpMetricExporterOptions &options) +{ + OtlpHttpMetricExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpHttpMetricExporterFactory::Create(const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpHttpMetricExporter(options)); + new OtlpHttpMetricExporter(options, runtime_options)); return exporter; } diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h index da2bd25e9a..9f36b18fca 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h @@ -14,6 +14,7 @@ #include "opentelemetry/sdk/common/circular_buffer.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/sdk/logs/recordable.h" @@ -54,12 +55,24 @@ class BatchLogRecordProcessor : public LogRecordProcessor * Creates a batch log processor by configuring the specified exporter and other parameters * as per the official, language-agnostic opentelemetry specs. * - * @param exporter - The backend exporter to pass the logs to - * @param options - The batch SpanProcessor options. + * @param exporter The backend exporter to pass the logs to + * @param options The batch SpanProcessor configuration options. */ explicit BatchLogRecordProcessor(std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options); + /** + * Creates a batch log processor by configuring the specified exporter and other parameters + * as per the official, language-agnostic opentelemetry specs. + * + * @param exporter The backend exporter to pass the logs to + * @param options The batch SpanProcessor configuration options. + * @param runtime_options The batch SpanProcessor runtime options. + */ + explicit BatchLogRecordProcessor(std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options); + /** Makes a new recordable **/ std::unique_ptr MakeRecordable() noexcept override; diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h index 983f2e7508..14be61450a 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/version.h" @@ -28,6 +29,14 @@ class OPENTELEMETRY_EXPORT BatchLogRecordProcessorFactory */ static std::unique_ptr Create(std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options); + + /** + * Create a BatchLogRecordProcessor. + */ + static std::unique_ptr Create( + std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options); }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h index 1f27d0f9b8..b130f947af 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h @@ -6,7 +6,6 @@ #include #include -#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -35,9 +34,6 @@ struct BatchLogRecordProcessorOptions * equal to max_queue_size. */ size_t max_export_batch_size = 512; - - std::shared_ptr thread_instrumentation = - std::shared_ptr(nullptr); }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h new file mode 100644 index 0000000000..9201ea1298 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ + +namespace logs +{ + +/** + * Struct to hold batch SpanProcessor runtime options. + */ +struct BatchLogRecordProcessorRuntimeOptions +{ + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace logs +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h index a8e51c2a25..bb8db1ac09 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h @@ -13,6 +13,7 @@ #include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" @@ -29,7 +30,11 @@ class PeriodicExportingMetricReader : public MetricReader public: PeriodicExportingMetricReader(std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option); + const PeriodicExportingMetricReaderOptions &options); + + PeriodicExportingMetricReader(std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options); AggregationTemporality GetAggregationTemporality( InstrumentType instrument_type) const noexcept override; diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h index 8cb439599e..4325edfc34 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -20,7 +21,12 @@ class OPENTELEMETRY_EXPORT PeriodicExportingMetricReaderFactory { public: static std::unique_ptr Create(std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option); + const PeriodicExportingMetricReaderOptions &options); + + static std::unique_ptr Create( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options); }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h index 6c2f4b889c..80514f96d8 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h @@ -3,7 +3,6 @@ #pragma once -#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" #include @@ -29,11 +28,6 @@ struct PeriodicExportingMetricReaderOptions /* how long the export can run before it is cancelled. */ std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis); - - std::shared_ptr periodic_thread_instrumentation = - std::shared_ptr(nullptr); - std::shared_ptr collect_thread_instrumentation = - std::shared_ptr(nullptr); }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h new file mode 100644 index 0000000000..f816fc05a0 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +/** + * Struct to hold PeriodicExortingMetricReader runtime options. + */ +struct PeriodicExportingMetricReaderRuntimeOptions +{ + std::shared_ptr periodic_thread_instrumentation = + std::shared_ptr(nullptr); + std::shared_ptr collect_thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h index a6ba35f389..3a78d6f33c 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h @@ -14,6 +14,7 @@ #include "opentelemetry/sdk/common/circular_buffer.h" #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/recordable.h" @@ -37,12 +38,24 @@ class BatchSpanProcessor : public SpanProcessor * Creates a batch span processor by configuring the specified exporter and other parameters * as per the official, language-agnostic opentelemetry specs. * - * @param exporter - The backend exporter to pass the ended spans to. - * @param options - The batch SpanProcessor options. + * @param exporter The backend exporter to pass the ended spans to. + * @param options The batch SpanProcessor configuration options. */ BatchSpanProcessor(std::unique_ptr &&exporter, const BatchSpanProcessorOptions &options); + /** + * Creates a batch span processor by configuring the specified exporter and other parameters + * as per the official, language-agnostic opentelemetry specs. + * + * @param exporter The backend exporter to pass the ended spans to. + * @param options The batch SpanProcessor configuration options. + * @param runtime_options The batch SpanProcessor runtime options. + */ + BatchSpanProcessor(std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options); + /** * Requests a Recordable(Span) from the configured exporter. * diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h index bfec277b1d..4ceddc6ae3 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/version.h" @@ -27,6 +28,14 @@ class OPENTELEMETRY_EXPORT BatchSpanProcessorFactory */ static std::unique_ptr Create(std::unique_ptr &&exporter, const BatchSpanProcessorOptions &options); + + /** + * Create a BatchSpanProcessor. + */ + static std::unique_ptr Create( + std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options); }; } // namespace trace diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h index eda4533f43..ac1052383b 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h @@ -5,7 +5,6 @@ #include -#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -34,9 +33,6 @@ struct BatchSpanProcessorOptions * equal to max_queue_size. */ size_t max_export_batch_size = 512; - - std::shared_ptr thread_instrumentation = - std::shared_ptr(nullptr); }; } // namespace trace diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h new file mode 100644 index 0000000000..25674649e7 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h @@ -0,0 +1,29 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ + +namespace trace +{ + +/** + * Struct to hold batch SpanProcessor runtime options. + */ +struct BatchSpanProcessorRuntimeOptions +{ + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace trace +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index 8ed5c18269..e5cc700317 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -22,6 +22,7 @@ #include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/logs/batch_log_record_processor.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/recordable.h" #include "opentelemetry/version.h" @@ -60,7 +61,24 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr()), - worker_thread_instrumentation_(options.thread_instrumentation), + worker_thread_instrumentation_(nullptr), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchLogRecordProcessor::DoBackgroundWork, this); +} + +BatchLogRecordProcessor::BatchLogRecordProcessor( + std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options) + : exporter_(std::move(exporter)), + max_queue_size_(options.max_queue_size), + scheduled_delay_millis_(options.schedule_delay_millis), + max_export_batch_size_(options.max_export_batch_size), + buffer_(options.max_queue_size), + synchronization_data_(std::make_shared()), + worker_thread_instrumentation_(runtime_options.thread_instrumentation), worker_thread_() { // Make sure the constructor is complete before giving 'this' to a thread. diff --git a/sdk/src/logs/batch_log_record_processor_factory.cc b/sdk/src/logs/batch_log_record_processor_factory.cc index a6e29e4967..df981dd91d 100644 --- a/sdk/src/logs/batch_log_record_processor_factory.cc +++ b/sdk/src/logs/batch_log_record_processor_factory.cc @@ -20,9 +20,18 @@ namespace logs std::unique_ptr BatchLogRecordProcessorFactory::Create( std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options) +{ + BatchLogRecordProcessorRuntimeOptions runtime_options; + return Create(std::move(exporter), options, runtime_options); +} + +std::unique_ptr BatchLogRecordProcessorFactory::Create( + std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options) { std::unique_ptr processor( - new BatchLogRecordProcessor(std::move(exporter), options)); + new BatchLogRecordProcessor(std::move(exporter), options, runtime_options)); return processor; } diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc index 8eeb56a53e..21ef276c8c 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -18,6 +18,7 @@ #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -41,12 +42,32 @@ namespace metrics PeriodicExportingMetricReader::PeriodicExportingMetricReader( std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option) + const PeriodicExportingMetricReaderOptions &options) : exporter_{std::move(exporter)}, - export_interval_millis_{option.export_interval_millis}, - export_timeout_millis_{option.export_timeout_millis}, - worker_thread_instrumentation_(option.periodic_thread_instrumentation), - collect_thread_instrumentation_(option.collect_thread_instrumentation) + export_interval_millis_{options.export_interval_millis}, + export_timeout_millis_{options.export_timeout_millis}, + worker_thread_instrumentation_(nullptr), + collect_thread_instrumentation_(nullptr) +{ + if (export_interval_millis_ <= export_timeout_millis_) + { + OTEL_INTERNAL_LOG_WARN( + "[Periodic Exporting Metric Reader] Invalid configuration: " + "export_timeout_millis_ should be less than export_interval_millis_, using default values"); + export_interval_millis_ = kExportIntervalMillis; + export_timeout_millis_ = kExportTimeOutMillis; + } +} + +PeriodicExportingMetricReader::PeriodicExportingMetricReader( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options) + : exporter_{std::move(exporter)}, + export_interval_millis_{options.export_interval_millis}, + export_timeout_millis_{options.export_timeout_millis}, + worker_thread_instrumentation_(runtime_options.periodic_thread_instrumentation), + collect_thread_instrumentation_(runtime_options.collect_thread_instrumentation) { if (export_interval_millis_ <= export_timeout_millis_) { diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc index 6c85caaa2a..b1542d4209 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc @@ -7,6 +7,7 @@ #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -19,10 +20,19 @@ namespace metrics std::unique_ptr PeriodicExportingMetricReaderFactory::Create( std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option) + const PeriodicExportingMetricReaderOptions &options) +{ + PeriodicExportingMetricReaderRuntimeOptions runtime_options; + return Create(std::move(exporter), options, runtime_options); +} + +std::unique_ptr PeriodicExportingMetricReaderFactory::Create( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options) { std::unique_ptr reader( - new PeriodicExportingMetricReader(std::move(exporter), option)); + new PeriodicExportingMetricReader(std::move(exporter), options, runtime_options)); return reader; } diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index 578b0a4fa0..386a51e3da 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -47,7 +47,23 @@ BatchSpanProcessor::BatchSpanProcessor(std::unique_ptr &&exporter, max_export_batch_size_(options.max_export_batch_size), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), - worker_thread_instrumentation_(options.thread_instrumentation), + worker_thread_instrumentation_(nullptr), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchSpanProcessor::DoBackgroundWork, this); +} + +BatchSpanProcessor::BatchSpanProcessor(std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options) + : exporter_(std::move(exporter)), + max_queue_size_(options.max_queue_size), + schedule_delay_millis_(options.schedule_delay_millis), + max_export_batch_size_(options.max_export_batch_size), + buffer_(max_queue_size_), + synchronization_data_(std::make_shared()), + worker_thread_instrumentation_(runtime_options.thread_instrumentation), worker_thread_() { // Make sure the constructor is complete before giving 'this' to a thread. diff --git a/sdk/src/trace/batch_span_processor_factory.cc b/sdk/src/trace/batch_span_processor_factory.cc index a21b056cee..5c99c3f94c 100644 --- a/sdk/src/trace/batch_span_processor_factory.cc +++ b/sdk/src/trace/batch_span_processor_factory.cc @@ -16,11 +16,22 @@ namespace sdk { namespace trace { + std::unique_ptr BatchSpanProcessorFactory::Create( std::unique_ptr &&exporter, const BatchSpanProcessorOptions &options) { - std::unique_ptr processor(new BatchSpanProcessor(std::move(exporter), options)); + BatchSpanProcessorRuntimeOptions runtime_options; + return Create(std::move(exporter), options, runtime_options); +} + +std::unique_ptr BatchSpanProcessorFactory::Create( + std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options) +{ + std::unique_ptr processor( + new BatchSpanProcessor(std::move(exporter), options, runtime_options)); return processor; } From 738f95f3dc40aac424e760ec4ae0c28383d49733 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 17 Dec 2024 20:53:14 +0100 Subject: [PATCH 08/21] cleanup --- .../src/otlp_file_log_record_exporter_factory.cc | 12 +----------- .../src/otlp_http_log_record_exporter_factory.cc | 12 +++++++++++- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc index 3bd1bbb237..145e52262a 100644 --- a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc @@ -4,7 +4,6 @@ #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" -#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -21,18 +20,9 @@ OtlpFileLogRecordExporterFactory::Create() std::unique_ptr OtlpFileLogRecordExporterFactory::Create(const OtlpFileLogRecordExporterOptions &options) -{ - OtlpFileLogRecordExporterRuntimeOptions runtime_options; - return Create(options, runtime_options); -} - -std::unique_ptr -OtlpFileLogRecordExporterFactory::Create( - const OtlpFileLogRecordExporterOptions &options, - const OtlpFileLogRecordExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpFileLogRecordExporter(options, runtime_options)); + new OtlpFileLogRecordExporter(options)); return exporter; } diff --git a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc index c224acc053..54e78bf4e9 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc @@ -4,6 +4,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +21,18 @@ OtlpHttpLogRecordExporterFactory::Create() std::unique_ptr OtlpHttpLogRecordExporterFactory::Create(const OtlpHttpLogRecordExporterOptions &options) +{ + OtlpHttpLogRecordExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpHttpLogRecordExporterFactory::Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpHttpLogRecordExporter(options)); + new OtlpHttpLogRecordExporter(options, runtime_options)); return exporter; } From 86504780f86c80ea1f2b0fbe0bc929786fd3f0e3 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 17 Dec 2024 23:30:45 +0100 Subject: [PATCH 09/21] bazel build --- exporters/otlp/BUILD | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index eb3d23d579..4671a60830 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -155,6 +155,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_http_exporter.h", "include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_http_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -270,6 +271,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h", "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -326,6 +328,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h", "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], From bae36b87e4909053e98fe19095fe90e0abace42e Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 18 Dec 2024 17:37:58 +0100 Subject: [PATCH 10/21] Added example --- examples/otlp/BUILD | 23 ++ examples/otlp/CMakeLists.txt | 23 ++ examples/otlp/http_instrumented_main.cc | 327 ++++++++++++++++++ ext/src/http/client/curl/http_client_curl.cc | 10 +- .../sdk/common/thread_instrumentation.h | 69 +++- sdk/src/trace/batch_span_processor.cc | 2 +- sdk/src/trace/batch_span_processor_factory.cc | 1 + 7 files changed, 443 insertions(+), 12 deletions(-) create mode 100644 examples/otlp/http_instrumented_main.cc diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index fd4e0dc171..943e8babf0 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -168,3 +168,26 @@ cc_binary( "//sdk/src/metrics", ], ) + +cc_binary( + name = "example_otlp_instrumented_http", + srcs = [ + "http_instrumented_main.cc", + ], + tags = [ + "examples", + "otlp", + "otlp_http", + ], + deps = [ + "//api", + "//examples/common/logs_foo_library:common_logs_foo_library", + "//examples/common/metrics_foo_library:common_metrics_foo_library", + "//exporters/otlp:otlp_http_exporter", + "//exporters/otlp:otlp_http_log_record_exporter", + "//exporters/otlp:otlp_http_metric_exporter", + "//sdk/src/log", + "//sdk/src/metrics", + "//sdk/src/trace", + ], +) diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt index 145fafca87..170017c965 100644 --- a/examples/otlp/CMakeLists.txt +++ b/examples/otlp/CMakeLists.txt @@ -99,6 +99,29 @@ if(WITH_OTLP_HTTP) opentelemetry_exporter_otlp_http_log) endif() + # ALL, instrumented + + add_executable(example_otlp_instrumented_http http_instrumented_main.cc) + + # Note: common_logs_foo_library provide traces and logs + target_link_libraries( + example_otlp_instrumented_http ${CMAKE_THREAD_LIBS_INIT} + common_metrics_foo_library common_logs_foo_library) + + if(DEFINED OPENTELEMETRY_BUILD_DLL) + target_link_libraries(example_otlp_instrumented_http opentelemetry_cpp + opentelemetry_common) + else() + target_link_libraries( + example_otlp_instrumented_http + opentelemetry_trace + opentelemetry_metrics + opentelemetry_logs + opentelemetry_exporter_otlp_http + opentelemetry_exporter_otlp_http_metric + opentelemetry_exporter_otlp_http_log) + endif() + endif() if(WITH_OTLP_FILE) diff --git a/examples/otlp/http_instrumented_main.cc b/examples/otlp/http_instrumented_main.cc new file mode 100644 index 0000000000..710cde4c65 --- /dev/null +++ b/examples/otlp/http_instrumented_main.cc @@ -0,0 +1,327 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#include +#include +#include + +#include "opentelemetry/exporters/otlp/otlp_environment.h" +#include "opentelemetry/exporters/otlp/otlp_http.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" +#include "opentelemetry/logs/log_record.h" +#include "opentelemetry/logs/logger_provider.h" +#include "opentelemetry/logs/provider.h" +#include "opentelemetry/metrics/meter_provider.h" +#include "opentelemetry/metrics/provider.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_factory.h" +#include "opentelemetry/sdk/logs/logger_provider.h" +#include "opentelemetry/sdk/logs/logger_provider_factory.h" +#include "opentelemetry/sdk/logs/recordable.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" +#include "opentelemetry/sdk/metrics/meter_context.h" +#include "opentelemetry/sdk/metrics/meter_context_factory.h" +#include "opentelemetry/sdk/metrics/meter_provider_factory.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/sdk/metrics/push_metric_exporter.h" +#include "opentelemetry/sdk/trace/batch_span_processor_factory.h" +#include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" +#include "opentelemetry/sdk/trace/processor.h" +#include "opentelemetry/sdk/trace/recordable.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/sdk/trace/tracer_provider_factory.h" +#include "opentelemetry/trace/provider.h" +#include "opentelemetry/trace/span_id.h" +#include "opentelemetry/trace/tracer_provider.h" + +#ifdef BAZEL_BUILD +# include "examples/common/foo_library/foo_library.h" +# include "examples/common/metrics_foo_library/foo_library.h" +#else +# include "foo_library/foo_library.h" +# include "metrics_foo_library/foo_library.h" +#endif + +namespace +{ + +std::mutex serialize; + +class MyThreadInstrumentation : public opentelemetry::sdk::common::ThreadInstrumentation +{ +public: + MyThreadInstrumentation(const std::string &thread_name, + const std::string &network_name, + const std::string &priority) + : thread_name_(thread_name), network_name_(network_name), priority_(priority) + {} + ~MyThreadInstrumentation() override = default; + + void OnStart() override + { + std::lock_guard lock_guard(serialize); + std::cout << "OnStart() thread " << thread_name_ << ", id " << std::this_thread::get_id(); + if (!network_name_.empty()) + { + std::cout << ", network_name " << network_name_; + } + if (!priority_.empty()) + { + std::cout << ", priority " << priority_; + } + std::cout << std::endl << std::flush; + } + + void OnEnd() override + { + std::lock_guard lock_guard(serialize); + std::cout << "OnEnd() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << std::endl + << std::flush; + } + + void BeforeWait() override + { + std::lock_guard lock_guard(serialize); + std::cout << "BeforeWait() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", waiting" << std::endl + << std::flush; + } + + void AfterWait() override + { + std::lock_guard lock_guard(serialize); + std::cout << "AfterWait() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", done waiting" << std::endl + << std::flush; + } + + void BeforeLoad() override + { + std::lock_guard lock_guard(serialize); + std::cout << "BeforeLoad() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", about to work" << std::endl + << std::flush; + } + + void AfterLoad() override + { + std::lock_guard lock_guard(serialize); + std::cout << "AfterLoad() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", done working" << std::endl + << std::flush; + } + +private: + std::string thread_name_; + std::string network_name_; + std::string priority_; +}; + +opentelemetry::exporter::otlp::OtlpHttpExporterOptions tracer_opts; +opentelemetry::exporter::otlp::OtlpHttpMetricExporterOptions meter_opts; +opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterOptions logger_opts; + +std::shared_ptr tracer_provider; +std::shared_ptr meter_provider; +std::shared_ptr logger_provider; + +void InitTracer() +{ + // Create OTLP exporter instance + opentelemetry::exporter::otlp::OtlpHttpExporterRuntimeOptions exp_rt_opts; + auto exp_instr = std::shared_ptr( + new MyThreadInstrumentation("OtlpHttpExporter", "trace-net", "high")); + exp_rt_opts.thread_instrumentation = exp_instr; + auto exporter = + opentelemetry::exporter::otlp::OtlpHttpExporterFactory::Create(tracer_opts, exp_rt_opts); + + // Create Processor instance + opentelemetry::sdk::trace::BatchSpanProcessorOptions pro_opts; + opentelemetry::sdk::trace::BatchSpanProcessorRuntimeOptions pro_rt_opts; + auto pro_instr = std::shared_ptr( + new MyThreadInstrumentation("BatchSpanProcessor", "", "high")); + pro_rt_opts.thread_instrumentation = pro_instr; + auto processor = opentelemetry::sdk::trace::BatchSpanProcessorFactory::Create( + std::move(exporter), pro_opts, pro_rt_opts); + + // Create Provider instance + tracer_provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor)); + + // Set the global trace provider + std::shared_ptr api_provider = tracer_provider; + opentelemetry::trace::Provider::SetTracerProvider(api_provider); +} + +void CleanupTracer() +{ + // We call ForceFlush to prevent to cancel running exportings, It's optional. + if (tracer_provider) + { + tracer_provider->ForceFlush(); + tracer_provider->Shutdown(); + } + + tracer_provider.reset(); + std::shared_ptr none; + opentelemetry::trace::Provider::SetTracerProvider(none); +} + +void InitMetrics() +{ + // Create OTLP exporter instance + opentelemetry::exporter::otlp::OtlpHttpMetricExporterRuntimeOptions exp_rt_opts; + auto exp_instr = std::shared_ptr( + new MyThreadInstrumentation("OtlpHttpMetricExporter", "metric-net", "medium")); + exp_rt_opts.thread_instrumentation = exp_instr; + auto exporter = + opentelemetry::exporter::otlp::OtlpHttpMetricExporterFactory::Create(meter_opts, exp_rt_opts); + + std::string version{"1.2.0"}; + std::string schema{"https://opentelemetry.io/schemas/1.2.0"}; + + // Initialize and set the global MeterProvider + opentelemetry::sdk::metrics::PeriodicExportingMetricReaderOptions reader_options; + reader_options.export_interval_millis = std::chrono::milliseconds(1000); + reader_options.export_timeout_millis = std::chrono::milliseconds(500); + + opentelemetry::sdk::metrics::PeriodicExportingMetricReaderRuntimeOptions reader_rt_opts; + auto reader_periodic_instr = std::shared_ptr( + new MyThreadInstrumentation("PeriodicExportingMetricReader(periodic)", "", "medium")); + auto reader_collect_instr = std::shared_ptr( + new MyThreadInstrumentation("PeriodicExportingMetricReader(collect)", "", "medium")); + reader_rt_opts.periodic_thread_instrumentation = reader_periodic_instr; + reader_rt_opts.collect_thread_instrumentation = reader_collect_instr; + auto reader = opentelemetry::sdk::metrics::PeriodicExportingMetricReaderFactory::Create( + std::move(exporter), reader_options, reader_rt_opts); + + auto context = opentelemetry::sdk::metrics::MeterContextFactory::Create(); + context->AddMetricReader(std::move(reader)); + + meter_provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create(std::move(context)); + std::shared_ptr api_provider = meter_provider; + + opentelemetry::metrics::Provider::SetMeterProvider(api_provider); +} + +void CleanupMetrics() +{ + // We call ForceFlush to prevent to cancel running exportings, It's optional. + if (meter_provider) + { + meter_provider->ForceFlush(); + meter_provider->Shutdown(); + } + + meter_provider.reset(); + std::shared_ptr none; + opentelemetry::metrics::Provider::SetMeterProvider(none); +} + +void InitLogger() +{ + // Create OTLP exporter instance + opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterRuntimeOptions exp_rt_opts; + auto exp_instr = std::shared_ptr( + new MyThreadInstrumentation("OtlpHttpLogRecordExporter", "log-net", "low")); + exp_rt_opts.thread_instrumentation = exp_instr; + auto exporter = opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterFactory::Create( + logger_opts, exp_rt_opts); + + // Create Processor instance + opentelemetry::sdk::logs::BatchLogRecordProcessorOptions pro_opts; + opentelemetry::sdk::logs::BatchLogRecordProcessorRuntimeOptions pro_rt_opts; + auto pro_instr = std::shared_ptr( + new MyThreadInstrumentation("BatchLogRecordProcessor", "", "low")); + pro_rt_opts.thread_instrumentation = pro_instr; + auto processor = opentelemetry::sdk::logs::BatchLogRecordProcessorFactory::Create( + std::move(exporter), pro_opts, pro_rt_opts); + + logger_provider = opentelemetry::sdk::logs::LoggerProviderFactory::Create(std::move(processor)); + + std::shared_ptr api_provider = logger_provider; + opentelemetry::logs::Provider::SetLoggerProvider(api_provider); +} + +void CleanupLogger() +{ + // We call ForceFlush to prevent to cancel running exportings, It's optional. + if (logger_provider) + { + logger_provider->ForceFlush(); + logger_provider->Shutdown(); + } + + logger_provider.reset(); + std::shared_ptr none; + opentelemetry::logs::Provider::SetLoggerProvider(none); +} + +} // namespace + +/* + Usage: + - example_otlp_http + - example_otlp_http +*/ +int main(int argc, char *argv[]) +{ + if (argc > 1) + { + tracer_opts.url = argv[1]; + } + else + { + tracer_opts.url = "http://localhost:4318/v1/traces"; + } + + if (argc > 2) + { + meter_opts.url = argv[2]; + } + else + { + meter_opts.url = "http://localhost:4318/v1/metrics"; + } + + if (argc > 3) + { + logger_opts.url = argv[3]; + } + else + { + logger_opts.url = "http://localhost:4318/v1/logs"; + } + + std::cout << "Initializing opentelemetry-cpp" << std::endl << std::flush; + + InitTracer(); + InitMetrics(); + InitLogger(); + + std::cout << "Application payload" << std::endl << std::flush; + + foo_library(); + + std::string name{"otlp_http_metric_example"}; + foo_library::observable_counter_example(name); + + std::cout << "Shutting down opentelemetry-cpp" << std::endl << std::flush; + + CleanupLogger(); + CleanupMetrics(); + CleanupTracer(); + + std::cout << "Done" << std::endl << std::flush; +} diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 6f9b2df240..8878bd6786 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -493,13 +493,13 @@ bool HttpClient::MaybeSpawnBackgroundThread() // If there is no pending jobs, we can stop the background thread. if (still_running == 0) { + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->OnEnd(); + } + if (self->background_thread_) { - if (self->background_thread_instrumentation_ != nullptr) - { - self->background_thread_instrumentation_->OnEnd(); - self->background_thread_instrumentation_.reset(); - } self->background_thread_->detach(); self->background_thread_.reset(); } diff --git a/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h index e04ac60903..1db45a478f 100644 --- a/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h +++ b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h @@ -11,18 +11,75 @@ namespace sdk namespace common { +/** + * Thread instrumentation interface. + * When the opentelemetry-cpp library executes internal threads, + * the application linking with the opentelemetry-cpp library + * may want to have some control on how the thread executes. + * + * There are many different use cases for this, + * listing a few to illustrate. + * + * (1) The application may need to initialize thread local storage, + * in an application specific way, because application code that + * relies on thread local storage will be executed in the thread code path. + * For example, custom samplers for traces, or custom observable instruments + * for metrics, may expect specific thread local storage keys to exist. + * + * (2) The application may want opentelemetry-cpp threads to be observable + * in a given way when exposed to the operating system. + * For example, a linux application may want to give a name to + * opentelemetry-cpp threads, + * using [pthread_setname_np(3)](https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html), + * to help troubleshooting. + * + * (3) The application may want specific opentelemetry-cpp threads to use + * application defined specific named network. + * For example, a linux application may want to use + * [setns(2)](https://man7.org/linux/man-pages/man2/setns.2.html) + * on a per exporter basis, so that different exporters uses different networks. + * + * (4) The application may want to bind specific opentelemetry-cpp threads + * to specific CPUs, for performance reasons. + * For example, a linux application may want to use + * [sched_setaffinity(2)](https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html) + * on a per thread basis. + * + * Providing dedicated opentelemetry-cpp interfaces in the SDK or exporters, + * to support these use cases, is not practical, because the code involved + * is highly platform dependent and use case dependent. + * + * Instead, the opentelemetry-cpp library provide hooks for applications + * to implement their own, arbitrary, platform specific, logic. + * This is done by implementing the ThreadInstrumentation interface + * in the application, and providing a given ThreadInstrumentation object + * when initializing the SDK or exporters. + * + * The opentelemetry-cpp library calls the following extension points, + * when a ThreadInstrumentation is provided. + * + * Upon thread creation and termination, the methods OnStart() and OnEnd() + * are invoked, respectively. + * + * When a thread is to block and wait, for example on a timer, + * the methods BeforeWait() and AfterWait() are invoked. + * + * When a thread is to perform a chunk of work, + * for example to process all the available data in an exporter, + * the methods BeforeLoad() and AfterLoad() are invoked. + */ class ThreadInstrumentation { public: ThreadInstrumentation() = default; virtual ~ThreadInstrumentation() = default; - virtual void OnStart() = 0; - virtual void OnEnd() = 0; - virtual void BeforeWait() = 0; - virtual void AfterWait() = 0; - virtual void BeforeLoad() = 0; - virtual void AfterLoad() = 0; + virtual void OnStart() {} + virtual void OnEnd() {} + virtual void BeforeWait() {} + virtual void AfterWait() {} + virtual void BeforeLoad() {} + virtual void AfterLoad() {} }; } // namespace common diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index 386a51e3da..cc9061b2d6 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -205,7 +205,7 @@ void BatchSpanProcessor::DoBackgroundWork() if (synchronization_data_->is_shutdown.load() == true) { DrainQueue(); - return; + break; } auto start = std::chrono::steady_clock::now(); diff --git a/sdk/src/trace/batch_span_processor_factory.cc b/sdk/src/trace/batch_span_processor_factory.cc index 5c99c3f94c..caaafbacb7 100644 --- a/sdk/src/trace/batch_span_processor_factory.cc +++ b/sdk/src/trace/batch_span_processor_factory.cc @@ -7,6 +7,7 @@ #include "opentelemetry/sdk/trace/batch_span_processor.h" #include "opentelemetry/sdk/trace/batch_span_processor_factory.h" #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/version.h" From 941dd20edfbf3da3304206e3f586f666590d5f57 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 18 Dec 2024 17:51:13 +0100 Subject: [PATCH 11/21] fix bazel --- examples/otlp/BUILD | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index 943e8babf0..b27cbef32d 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -186,7 +186,6 @@ cc_binary( "//exporters/otlp:otlp_http_exporter", "//exporters/otlp:otlp_http_log_record_exporter", "//exporters/otlp:otlp_http_metric_exporter", - "//sdk/src/log", "//sdk/src/metrics", "//sdk/src/trace", ], From 85be1cac5f0b9fc56e5225059cffc6827a088de2 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 18 Dec 2024 18:12:38 +0100 Subject: [PATCH 12/21] cleanup --- examples/otlp/http_instrumented_main.cc | 4 ++-- ext/test/http/curl_http_test.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/otlp/http_instrumented_main.cc b/examples/otlp/http_instrumented_main.cc index 710cde4c65..8b6dce8193 100644 --- a/examples/otlp/http_instrumented_main.cc +++ b/examples/otlp/http_instrumented_main.cc @@ -46,10 +46,10 @@ #include "opentelemetry/trace/tracer_provider.h" #ifdef BAZEL_BUILD -# include "examples/common/foo_library/foo_library.h" +# include "examples/common/logs_foo_library/foo_library.h" # include "examples/common/metrics_foo_library/foo_library.h" #else -# include "foo_library/foo_library.h" +# include "logs_foo_library/foo_library.h" # include "metrics_foo_library/foo_library.h" #endif diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 497c5bb529..246f7fc750 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -564,7 +564,7 @@ TEST_F(BasicCurlHttpTests, BackgroundThreadWaitMore) { curl::HttpClient http_client; http_client.MaybeSpawnBackgroundThread(); - std::this_thread::sleep_for(std::chrono::milliseconds{10}); + std::this_thread::sleep_for(std::chrono::milliseconds{50}); #if LIBCURL_VERSION_NUM >= 0x074200 ASSERT_FALSE(http_client.MaybeSpawnBackgroundThread()); #else From a2fcfc9a56cb3165041db7d54f4fd9352a8c2fae Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 18 Dec 2024 18:35:40 +0100 Subject: [PATCH 13/21] Fixed bad merge --- ext/src/http/client/curl/http_client_curl.cc | 1 + ext/test/http/curl_http_test.cc | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 8878bd6786..835824e049 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -191,6 +191,7 @@ HttpClient::HttpClient() max_sessions_per_connection_{8}, background_thread_instrumentation_(nullptr), scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, + background_thread_wait_for_{std::chrono::minutes{1}}, curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) {} diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 246f7fc750..497c5bb529 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -564,7 +564,7 @@ TEST_F(BasicCurlHttpTests, BackgroundThreadWaitMore) { curl::HttpClient http_client; http_client.MaybeSpawnBackgroundThread(); - std::this_thread::sleep_for(std::chrono::milliseconds{50}); + std::this_thread::sleep_for(std::chrono::milliseconds{10}); #if LIBCURL_VERSION_NUM >= 0x074200 ASSERT_FALSE(http_client.MaybeSpawnBackgroundThread()); #else From fb3d60bada590b4d8a60993a3737e7822bbce799 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 18 Dec 2024 21:56:06 +0100 Subject: [PATCH 14/21] Add changelog. --- CHANGELOG.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac5e7012d..2b2a0e0110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,43 @@ Increment the: * [SDK] Fix instrumentation scope attributes evaluated in equal method [#3214](https://github.com/open-telemetry/opentelemetry-cpp/pull/3214) +New features: + +* [SDK] Better control of threads executed by opentelemetry-cpp + [#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3175) + + * This feature provides a way for applications, when configuring the SDK and exporters, + to participate in the execution path of internal opentelemetry-cpp threads. + + * The opentelemetry-cpp library provides the following: + + * a new ThreadInstrumentation interface, + * new runtime options structures, to optionally configure the SDK: + * BatchSpanProcessorRuntimeOptions + * PeriodicExportingMetricReaderRuntimeOptions + * BatchLogRecordProcessorRuntimeOptions + * new runtime options structures, to optionally configure the OTLP HTTP exporters: + * OtlpHttpExporterRuntimeOptions + * OtlpHttpMetricExporterRuntimeOptions + * OtlpHttpLogRecordExporterRuntimeOptions + * new ThreadInstrumentation parameters, to optionally configure the CURL HttpClient + + * Using the optional runtime options structures, + an application can subclass the ThreadInstrumentation interface, + and be notified of specific events of interest during the execution of an internal opentelemetry-cpp thread. + + * This allows an application to call, for example: + + * pthread_setaffinity_np(), for better performances, + * setns(), to control the network namespace used by HTTP CURL connections + * pthread_setname_np(), for better observability from the operating system + * many more specific apis, as needed + + * See the documentation for ThreadInstrumentation for details. + + * A new example program, example_otlp_instrumented_http, shows how to use the feature, + and add application logic in the thread execution code path. + ## [1.18 2024-11-25] * [EXPORTER] Fix crash in ElasticsearchLogRecordExporter From 715b76d76ad4d8bab537f71ad047a97c3da601e8 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 18 Dec 2024 22:11:15 +0100 Subject: [PATCH 15/21] cleanup --- CHANGELOG.md | 18 ++++++++++++------ examples/otlp/http_instrumented_main.cc | 18 ++++++++++++++++-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b2a0e0110..c7d1c5d99f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,8 +26,10 @@ New features: * [SDK] Better control of threads executed by opentelemetry-cpp [#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3175) - * This feature provides a way for applications, when configuring the SDK and exporters, - to participate in the execution path of internal opentelemetry-cpp threads. + * This feature provides a way for applications, + when configuring the SDK and exporters, + to participate in the execution path + of internal opentelemetry-cpp threads. * The opentelemetry-cpp library provides the following: @@ -36,15 +38,18 @@ New features: * BatchSpanProcessorRuntimeOptions * PeriodicExportingMetricReaderRuntimeOptions * BatchLogRecordProcessorRuntimeOptions - * new runtime options structures, to optionally configure the OTLP HTTP exporters: + * new runtime options structures, + to optionally configure the OTLP HTTP exporters: * OtlpHttpExporterRuntimeOptions * OtlpHttpMetricExporterRuntimeOptions * OtlpHttpLogRecordExporterRuntimeOptions - * new ThreadInstrumentation parameters, to optionally configure the CURL HttpClient + * new ThreadInstrumentation parameters, + to optionally configure the CURL HttpClient * Using the optional runtime options structures, an application can subclass the ThreadInstrumentation interface, - and be notified of specific events of interest during the execution of an internal opentelemetry-cpp thread. + and be notified of specific events of interest during the execution + of an internal opentelemetry-cpp thread. * This allows an application to call, for example: @@ -55,7 +60,8 @@ New features: * See the documentation for ThreadInstrumentation for details. - * A new example program, example_otlp_instrumented_http, shows how to use the feature, + * A new example program, example_otlp_instrumented_http, + shows how to use the feature, and add application logic in the thread execution code path. ## [1.18 2024-11-25] diff --git a/examples/otlp/http_instrumented_main.cc b/examples/otlp/http_instrumented_main.cc index 8b6dce8193..38f8850360 100644 --- a/examples/otlp/http_instrumented_main.cc +++ b/examples/otlp/http_instrumented_main.cc @@ -58,6 +58,20 @@ namespace std::mutex serialize; +/** + The purpose of MyThreadInstrumentation is to demonstrate + how notifications are delivered to the application. + + Printing to std::cout is useful for debugging, + to understand the overall thread execution in the library. + + In production, a real application would instead: + - set thread priorities / CPU affinity + - set thread local storage keys + - set a thread name to the operating system + - set network namespaces + in the OnXXX() code here. +*/ class MyThreadInstrumentation : public opentelemetry::sdk::common::ThreadInstrumentation { public: @@ -272,8 +286,8 @@ void CleanupLogger() /* Usage: - - example_otlp_http - - example_otlp_http + - example_otlp_instrumented_http + - example_otlp_instrumented_http */ int main(int argc, char *argv[]) { From e9b09a02298b19ba7ed07dfc53145bed68064e54 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 19 Dec 2024 12:10:01 +0100 Subject: [PATCH 16/21] Instrumented the OTLP FILE exporter. --- .../exporters/otlp/otlp_file_client.h | 6 ++- .../otlp/otlp_file_client_runtime_options.h | 31 ++++++++++++++ .../exporters/otlp/otlp_file_exporter.h | 9 ++++ .../otlp/otlp_file_exporter_factory.h | 8 ++++ .../otlp/otlp_file_exporter_runtime_options.h | 27 ++++++++++++ .../otlp/otlp_file_log_record_exporter.h | 11 +++++ .../otlp_file_log_record_exporter_factory.h | 8 ++++ ...file_log_record_exporter_runtime_options.h | 27 ++++++++++++ .../otlp/otlp_file_metric_exporter.h | 11 +++++ .../otlp/otlp_file_metric_exporter_factory.h | 8 ++++ ...tlp_file_metric_exporter_runtime_options.h | 27 ++++++++++++ exporters/otlp/src/otlp_file_client.cc | 41 ++++++++++++++++--- exporters/otlp/src/otlp_file_exporter.cc | 10 ++++- .../otlp/src/otlp_file_exporter_factory.cc | 14 ++++++- .../otlp/src/otlp_file_log_record_exporter.cc | 11 ++++- .../otlp_file_log_record_exporter_factory.cc | 13 +++++- .../otlp/src/otlp_file_metric_exporter.cc | 10 ++++- .../src/otlp_file_metric_exporter_factory.cc | 12 +++++- exporters/otlp/test/otlp_file_client_test.cc | 18 +++++--- ext/src/http/client/curl/http_client_curl.cc | 10 +++++ 20 files changed, 292 insertions(+), 20 deletions(-) create mode 100644 exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h create mode 100644 exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h create mode 100644 exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h create mode 100644 exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h index e377b140b6..987606ecdb 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h @@ -7,6 +7,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/version.h" @@ -35,7 +36,8 @@ class OtlpFileClient /** * Create an OtlpFileClient using the given options. */ - explicit OtlpFileClient(OtlpFileClientOptions &&options); + explicit OtlpFileClient(OtlpFileClientOptions &&options, + OtlpFileClientRuntimeOptions &&runtime_options); ~OtlpFileClient(); @@ -80,6 +82,8 @@ class OtlpFileClient // The configuration options associated with this file client. const OtlpFileClientOptions options_; + // The runtime options associated with this file client. + const OtlpFileClientRuntimeOptions runtime_options_; opentelemetry::nostd::shared_ptr backend_; }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h new file mode 100644 index 0000000000..2fc4c2e31c --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP FILE client runtime options. + */ +struct OtlpFileClientRuntimeOptions +{ + OtlpFileClientRuntimeOptions() = default; + ~OtlpFileClientRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h index b49dcef77f..e2b3b4bd6c 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/trace/exporter.h" @@ -36,6 +37,12 @@ class OPENTELEMETRY_EXPORT OtlpFileExporter final : public opentelemetry::sdk::t */ explicit OtlpFileExporter(const OtlpFileExporterOptions &options); + /** + * Create an OtlpFileExporter using the given options. + */ + explicit OtlpFileExporter(const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options); + /** * Create a span recordable. * @return a newly initialized Recordable object @@ -70,6 +77,8 @@ class OPENTELEMETRY_EXPORT OtlpFileExporter final : public opentelemetry::sdk::t private: // The configuration options associated with this exporter. const OtlpFileExporterOptions options_; + // The runtime options associated with this exporter. + const OtlpFileExporterRuntimeOptions runtime_options_; // Object that stores the file context. std::unique_ptr file_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h index bf4ba3288f..326fba4f26 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpFileExporterFactory */ static std::unique_ptr Create( const OtlpFileExporterOptions &options); + + /** + * Create an OtlpFileExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h new file mode 100644 index 0000000000..dcd30239a7 --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/version.h" + +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP File traces exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpFileExporterRuntimeOptions : public OtlpFileClientRuntimeOptions +{ + OtlpFileExporterRuntimeOptions() = default; + ~OtlpFileExporterRuntimeOptions() = default; +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h index 6fb9f6d844..23af07c027 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/logs/exporter.h" @@ -37,6 +38,14 @@ class OtlpFileLogRecordExporter final : public opentelemetry::sdk::logs::LogReco */ OtlpFileLogRecordExporter(const OtlpFileLogRecordExporterOptions &options); + /** + * Create an OtlpFileLogRecordExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpFileLogRecordExporter(const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options); + /** * Creates a recordable that stores the data in a JSON object */ @@ -69,6 +78,8 @@ class OtlpFileLogRecordExporter final : public opentelemetry::sdk::logs::LogReco private: // Configuration options for the exporter const OtlpFileLogRecordExporterOptions options_; + // Runtime options for the exporter + const OtlpFileLogRecordExporterRuntimeOptions runtime_options_; // Object that stores the file context. std::unique_ptr file_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h index 663eeb189a..5a72043de6 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpFileLogRecordExporterFactory */ static std::unique_ptr Create( const OtlpFileLogRecordExporterOptions &options); + + /** + * Create an OtlpFileExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h new file mode 100644 index 0000000000..4c10ab298f --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP File log record exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpFileLogRecordExporterRuntimeOptions + : public OtlpFileClientRuntimeOptions +{ + OtlpFileLogRecordExporterRuntimeOptions() = default; + ~OtlpFileLogRecordExporterRuntimeOptions() = default; +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h index e03e544de9..7439c40323 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" @@ -36,6 +37,14 @@ class OtlpFileMetricExporter final : public opentelemetry::sdk::metrics::PushMet */ OtlpFileMetricExporter(const OtlpFileMetricExporterOptions &options); + /** + * Create an OtlpFileMetricExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpFileMetricExporter(const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options); + /** * Get the AggregationTemporality for exporter * @@ -61,6 +70,8 @@ class OtlpFileMetricExporter final : public opentelemetry::sdk::metrics::PushMet // Configuration options for the exporter const OtlpFileMetricExporterOptions options_; + // Runtime options for the exporter + const OtlpFileMetricExporterRuntimeOptions runtime_options_; // Aggregation Temporality Selector const sdk::metrics::AggregationTemporalitySelector aggregation_temporality_selector_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h index a34c057fad..355c4693bc 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpFileMetricExporterFactory */ static std::unique_ptr Create( const OtlpFileMetricExporterOptions &options); + + /** + * Create an OtlpFileExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h new file mode 100644 index 0000000000..be8f3e2caa --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP File metrics exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpFileMetricExporterRuntimeOptions + : public OtlpFileClientRuntimeOptions +{ + OtlpFileMetricExporterRuntimeOptions() = default; + ~OtlpFileMetricExporterRuntimeOptions() = default; +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/src/otlp_file_client.cc b/exporters/otlp/src/otlp_file_client.cc index 7817dde85e..d299ad9af0 100644 --- a/exporters/otlp/src/otlp_file_client.cc +++ b/exporters/otlp/src/otlp_file_client.cc @@ -104,6 +104,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" @@ -970,8 +971,9 @@ void ConvertListFieldToJson(nlohmann::json &value, class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender { public: - explicit OtlpFileSystemBackend(const OtlpFileClientFileSystemOptions &options) - : options_(options), is_initialized_{false} + explicit OtlpFileSystemBackend(const OtlpFileClientFileSystemOptions &options, + const OtlpFileClientRuntimeOptions &runtime_options) + : options_(options), runtime_options_(runtime_options), is_initialized_{false} { file_->is_shutdown.store(false); file_->rotate_index = 0; @@ -1444,11 +1446,18 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender std::shared_ptr concurrency_file = file_; std::chrono::microseconds flush_interval = options_.flush_interval; - file_->background_flush_thread.reset(new std::thread([concurrency_file, flush_interval]() { + auto thread_instrumentation = runtime_options_.thread_instrumentation; + file_->background_flush_thread.reset(new std::thread([concurrency_file, flush_interval, + thread_instrumentation]() { std::chrono::system_clock::time_point last_free_job_timepoint = std::chrono::system_clock::now(); std::size_t last_record_count = 0; + if (thread_instrumentation != nullptr) + { + thread_instrumentation->OnStart(); + } + while (true) { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); @@ -1463,11 +1472,21 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender break; } + if (thread_instrumentation != nullptr) + { + thread_instrumentation->BeforeWait(); + } + { std::unique_lock lk(concurrency_file->background_thread_waker_lock); concurrency_file->background_thread_waker_cv.wait_for(lk, flush_interval); } + if (thread_instrumentation != nullptr) + { + thread_instrumentation->AfterWait(); + } + { std::size_t current_record_count = concurrency_file->record_count.load(std::memory_order_acquire); @@ -1496,6 +1515,12 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender std::lock_guard lock_guard_inner{concurrency_file->background_thread_lock}; background_flush_thread.swap(concurrency_file->background_flush_thread); } + + if (thread_instrumentation != nullptr) + { + thread_instrumentation->OnEnd(); + } + if (background_flush_thread && background_flush_thread->joinable()) { background_flush_thread->detach(); @@ -1519,6 +1544,7 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender private: OtlpFileClientFileSystemOptions options_; + OtlpFileClientRuntimeOptions runtime_options_; struct FileStats { @@ -1571,13 +1597,16 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileOstreamBackend : public OtlpFileAppende std::reference_wrapper os_; }; -OtlpFileClient::OtlpFileClient(OtlpFileClientOptions &&options) - : is_shutdown_(false), options_(std::move(options)) +OtlpFileClient::OtlpFileClient(OtlpFileClientOptions &&options, + OtlpFileClientRuntimeOptions &&runtime_options) + : is_shutdown_(false), + options_(std::move(options)), + runtime_options_(std::move(runtime_options)) { if (nostd::holds_alternative(options_.backend_options)) { backend_ = opentelemetry::nostd::shared_ptr(new OtlpFileSystemBackend( - nostd::get(options_.backend_options))); + nostd::get(options_.backend_options), runtime_options_)); } else if (nostd::holds_alternative>(options_.backend_options)) { diff --git a/exporters/otlp/src/otlp_file_exporter.cc b/exporters/otlp/src/otlp_file_exporter.cc index b98a6b2007..ccf0906271 100644 --- a/exporters/otlp/src/otlp_file_exporter.cc +++ b/exporters/otlp/src/otlp_file_exporter.cc @@ -35,7 +35,15 @@ namespace otlp OtlpFileExporter::OtlpFileExporter() : OtlpFileExporter(OtlpFileExporterOptions()) {} OtlpFileExporter::OtlpFileExporter(const OtlpFileExporterOptions &options) - : options_(options), file_client_(new OtlpFileClient(OtlpFileClientOptions(options))) + : OtlpFileExporter(options, OtlpFileExporterRuntimeOptions()) +{} + +OtlpFileExporter::OtlpFileExporter(const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), + file_client_(new OtlpFileClient(OtlpFileClientOptions(options), + OtlpFileExporterRuntimeOptions(runtime_options))) {} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_file_exporter_factory.cc b/exporters/otlp/src/otlp_file_exporter_factory.cc index 245b3a9152..57c327a0c7 100644 --- a/exporters/otlp/src/otlp_file_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_exporter_factory.cc @@ -2,9 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/exporters/otlp/otlp_file_exporter_factory.h" - #include "opentelemetry/exporters/otlp/otlp_file_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -21,7 +22,16 @@ std::unique_ptr OtlpFileExporterFactory std::unique_ptr OtlpFileExporterFactory::Create( const OtlpFileExporterOptions &options) { - std::unique_ptr exporter(new OtlpFileExporter(options)); + OtlpFileExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr OtlpFileExporterFactory::Create( + const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options) +{ + std::unique_ptr exporter( + new OtlpFileExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_file_log_record_exporter.cc b/exporters/otlp/src/otlp_file_log_record_exporter.cc index 7df1c70b29..10c5ee1599 100644 --- a/exporters/otlp/src/otlp_file_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_file_log_record_exporter.cc @@ -38,7 +38,16 @@ OtlpFileLogRecordExporter::OtlpFileLogRecordExporter() OtlpFileLogRecordExporter::OtlpFileLogRecordExporter( const OtlpFileLogRecordExporterOptions &options) - : options_(options), file_client_(new OtlpFileClient(OtlpFileClientOptions(options))) + : OtlpFileLogRecordExporter(options, OtlpFileLogRecordExporterRuntimeOptions()) +{} + +OtlpFileLogRecordExporter::OtlpFileLogRecordExporter( + const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), + file_client_(new OtlpFileClient(OtlpFileClientOptions(options), + OtlpFileLogRecordExporterRuntimeOptions(runtime_options))) {} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc index 145e52262a..38aeb3b8e2 100644 --- a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc @@ -4,6 +4,8 @@ #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +22,18 @@ OtlpFileLogRecordExporterFactory::Create() std::unique_ptr OtlpFileLogRecordExporterFactory::Create(const OtlpFileLogRecordExporterOptions &options) +{ + OtlpFileLogRecordExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpFileLogRecordExporterFactory::Create( + const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpFileLogRecordExporter(options)); + new OtlpFileLogRecordExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_file_metric_exporter.cc b/exporters/otlp/src/otlp_file_metric_exporter.cc index 78aea66956..fe573e72d7 100644 --- a/exporters/otlp/src/otlp_file_metric_exporter.cc +++ b/exporters/otlp/src/otlp_file_metric_exporter.cc @@ -37,10 +37,18 @@ OtlpFileMetricExporter::OtlpFileMetricExporter() {} OtlpFileMetricExporter::OtlpFileMetricExporter(const OtlpFileMetricExporterOptions &options) + : OtlpFileMetricExporter(options, OtlpFileMetricExporterRuntimeOptions()) +{} + +OtlpFileMetricExporter::OtlpFileMetricExporter( + const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options) : options_(options), + runtime_options_(runtime_options), aggregation_temporality_selector_{ OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, - file_client_(new OtlpFileClient(OtlpFileClientOptions(options))) + file_client_(new OtlpFileClient(OtlpFileClientOptions(options), + OtlpFileClientRuntimeOptions(runtime_options))) {} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_file_metric_exporter_factory.cc b/exporters/otlp/src/otlp_file_metric_exporter_factory.cc index d474bbb914..7b52153e6b 100644 --- a/exporters/otlp/src/otlp_file_metric_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_metric_exporter_factory.cc @@ -4,6 +4,8 @@ #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +22,17 @@ OtlpFileMetricExporterFactory::Create() std::unique_ptr OtlpFileMetricExporterFactory::Create(const OtlpFileMetricExporterOptions &options) +{ + OtlpFileMetricExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpFileMetricExporterFactory::Create(const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpFileMetricExporter(options)); + new OtlpFileMetricExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/test/otlp_file_client_test.cc b/exporters/otlp/test/otlp_file_client_test.cc index cd4ffb5bc9..bb16de579f 100644 --- a/exporters/otlp/test/otlp_file_client_test.cc +++ b/exporters/otlp/test/otlp_file_client_test.cc @@ -159,7 +159,8 @@ TEST(OtlpFileClientTest, Shutdown) opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request; auto client = std::unique_ptr( new opentelemetry::exporter::otlp::OtlpFileClient( - opentelemetry::exporter::otlp::OtlpFileClientOptions())); + opentelemetry::exporter::otlp::OtlpFileClientOptions(), + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions())); ASSERT_FALSE(client->IsShutdown()); ASSERT_TRUE(client->Shutdown()); ASSERT_TRUE(client->IsShutdown()); @@ -182,10 +183,11 @@ TEST(OtlpFileClientTest, ExportToOstreamTest) std::stringstream output_stream; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.backend_options = std::ref(output_stream); auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); client->Export(request, 1); { @@ -281,10 +283,11 @@ TEST(OtlpFileClientTest, ExportToFileSystemRotateIndexTest) backend_opts.rotate_size = 3; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.backend_options = backend_opts; auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); // Write 5 records with rotatation index 1,2,3,1,2 for (int i = 0; i < 4; ++i) @@ -402,10 +405,11 @@ TEST(OtlpFileClientTest, ExportToFileSystemRotateByTimeTest) backend_opts.file_size = 1500; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.backend_options = backend_opts; auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); auto start_time = std::chrono::system_clock::now(); client->Export(request, 1); @@ -509,11 +513,12 @@ TEST(OtlpFileClientTest, ConfigTest) { { opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.console_debug = true; opts.backend_options = std::ref(std::cout); auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); ASSERT_TRUE(client->GetOptions().console_debug); ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( @@ -525,11 +530,12 @@ TEST(OtlpFileClientTest, ConfigTest) backend_opts.file_pattern = "test_file_pattern.jsonl"; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.console_debug = false; opts.backend_options = backend_opts; auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); ASSERT_FALSE(client->GetOptions().console_debug); ASSERT_TRUE(opentelemetry::nostd::holds_alternative< diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 54823a1d1f..527d66af11 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -464,6 +464,11 @@ bool HttpClient::MaybeSpawnBackgroundThread() } else if (still_running || need_wait_more) { + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->BeforeWait(); + } + // curl_multi_poll is added from libcurl 7.66.0, before 7.68.0, we can only wait util // timeout to do the rest jobs #if LIBCURL_VERSION_NUM >= 0x074200 @@ -476,6 +481,11 @@ bool HttpClient::MaybeSpawnBackgroundThread() static_cast(self->scheduled_delay_milliseconds_.count()), nullptr); #endif + + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->AfterWait(); + } } do From 22048a8079ea5f19ea7f6fd6c01c4d56b325eadc Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 19 Dec 2024 17:02:11 +0100 Subject: [PATCH 17/21] fix bazel --- exporters/otlp/BUILD | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 4671a60830..cdaba37e4d 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -181,6 +181,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_environment.h", "include/opentelemetry/exporters/otlp/otlp_file_client.h", "include/opentelemetry/exporters/otlp/otlp_file_client_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -211,6 +212,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_file_exporter.h", "include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_file_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -300,6 +302,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h", "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -357,6 +360,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h", "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], From bf4e9ff8bccd167d2598eb61ba87b0092027c6f9 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 19 Dec 2024 17:46:54 +0100 Subject: [PATCH 18/21] iwyu --- exporters/otlp/src/otlp_file_exporter.cc | 1 + exporters/otlp/src/otlp_file_log_record_exporter.cc | 1 + exporters/otlp/src/otlp_file_metric_exporter.cc | 2 ++ exporters/otlp/src/otlp_http_exporter.cc | 1 + exporters/otlp/src/otlp_http_log_record_exporter.cc | 2 ++ exporters/otlp/src/otlp_http_metric_exporter.cc | 2 ++ exporters/otlp/test/otlp_file_client_test.cc | 1 + sdk/src/logs/batch_log_record_processor_factory.cc | 1 + sdk/src/trace/batch_span_processor.cc | 1 + 9 files changed, 12 insertions(+) diff --git a/exporters/otlp/src/otlp_file_exporter.cc b/exporters/otlp/src/otlp_file_exporter.cc index ccf0906271..158f152fa3 100644 --- a/exporters/otlp/src/otlp_file_exporter.cc +++ b/exporters/otlp/src/otlp_file_exporter.cc @@ -11,6 +11,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" diff --git a/exporters/otlp/src/otlp_file_log_record_exporter.cc b/exporters/otlp/src/otlp_file_log_record_exporter.cc index 10c5ee1599..8767a7fd60 100644 --- a/exporters/otlp/src/otlp_file_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_file_log_record_exporter.cc @@ -11,6 +11,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_log_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" diff --git a/exporters/otlp/src/otlp_file_metric_exporter.cc b/exporters/otlp/src/otlp_file_metric_exporter.cc index fe573e72d7..eafc588d40 100644 --- a/exporters/otlp/src/otlp_file_metric_exporter.cc +++ b/exporters/otlp/src/otlp_file_metric_exporter.cc @@ -10,8 +10,10 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_metric_utils.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index c5afee8f0b..92ada875e9 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -13,6 +13,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index 3423ad8708..b1bd7a75d5 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -13,12 +13,14 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_log_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/logs/recordable.h" #include "opentelemetry/version.h" diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index 18a6908187..4f0c66c70b 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter.cc @@ -14,10 +14,12 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_metric_utils.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/version.h" diff --git a/exporters/otlp/test/otlp_file_client_test.cc b/exporters/otlp/test/otlp_file_client_test.cc index bb16de579f..98db591de3 100644 --- a/exporters/otlp/test/otlp_file_client_test.cc +++ b/exporters/otlp/test/otlp_file_client_test.cc @@ -20,6 +20,7 @@ #include "opentelemetry/common/timestamp.h" #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/shared_ptr.h" diff --git a/sdk/src/logs/batch_log_record_processor_factory.cc b/sdk/src/logs/batch_log_record_processor_factory.cc index df981dd91d..b76f7b20d0 100644 --- a/sdk/src/logs/batch_log_record_processor_factory.cc +++ b/sdk/src/logs/batch_log_record_processor_factory.cc @@ -7,6 +7,7 @@ #include "opentelemetry/sdk/logs/batch_log_record_processor.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_factory.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/version.h" diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index cc9061b2d6..1ab8173944 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -23,6 +23,7 @@ #include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/trace/batch_span_processor.h" #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/recordable.h" From 564fbadc964421681888312189aa0ad85084fe3e Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 19 Dec 2024 18:33:10 +0100 Subject: [PATCH 19/21] Document OTLP FILE in changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7d1c5d99f..ba06fe8e32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,14 @@ New features: * OtlpHttpLogRecordExporterRuntimeOptions * new ThreadInstrumentation parameters, to optionally configure the CURL HttpClient + * new runtime options structures, + to optionally configure the OTLP FILE exporters: + * OtlpFileExporterRuntimeOptions + * OtlpFileMetricExporterRuntimeOptions + * OtlpFileLogRecordExporterRuntimeOptions + * new runtime options structure, + to optionally configure the OTLP FILE client: + * OtlpFileClientRuntimeOptions * Using the optional runtime options structures, an application can subclass the ThreadInstrumentation interface, From 2cd0fb4eae913594a89d261dc8a7b077992f90b3 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 7 Jan 2025 12:17:02 +0100 Subject: [PATCH 20/21] Implement CMake WITH_THREAD_INSTRUMENTATION_PREVIEW flag. --- CHANGELOG.md | 6 ++++++ CMakeLists.txt | 4 ++++ api/CMakeLists.txt | 5 +++++ ci/do_ci.sh | 4 ++++ examples/otlp/http_instrumented_main.cc | 18 +++++++++++++++++- exporters/otlp/src/otlp_file_client.cc | 8 ++++++++ ext/src/http/client/curl/http_client_curl.cc | 8 ++++++++ .../sdk/common/thread_instrumentation.h | 5 +++++ sdk/src/logs/batch_log_record_processor.cc | 12 ++++++++++++ .../export/periodic_exporting_metric_reader.cc | 16 ++++++++++++++++ sdk/src/trace/batch_span_processor.cc | 12 ++++++++++++ 11 files changed, 97 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7e30353a9..089a9541a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,12 @@ New features: shows how to use the feature, and add application logic in the thread execution code path. + * Note that this feature is experimental, + protected by a WITH_THREAD_INSTRUMENTATION_PREVIEW + flag in CMake. Various runtime options structures, + as well as the thread instrumentation interface, + may change without notice before this feature is declared stable. + ## [1.18 2024-11-25] * [EXPORTER] Fix crash in ElasticsearchLogRecordExporter diff --git a/CMakeLists.txt b/CMakeLists.txt index 408ee43046..8c0f4d4a18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,6 +300,10 @@ option(WITH_ASYNC_EXPORT_PREVIEW "Whether to enable async export" OFF) option(WITH_METRICS_EXEMPLAR_PREVIEW "Whether to enable exemplar within metrics" OFF) +# Experimental, so behind feature flag by default +option(WITH_THREAD_INSTRUMENTATION_PREVIEW + "Whether to enable thread instrumentation" OFF) + option(OPENTELEMETRY_SKIP_DYNAMIC_LOADING_TESTS "Whether to build test libraries that are always linked as shared libs" OFF) diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index 78e97ad029..b3c97808f6 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -126,6 +126,11 @@ if(WITH_METRICS_EXEMPLAR_PREVIEW) INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW) endif() +if(WITH_THREAD_INSTRUMENTATION_PREVIEW) + target_compile_definitions(opentelemetry_api + INTERFACE ENABLE_THREAD_INSTRUMENTATION_PREVIEW) +endif() + if(WITH_OTLP_HTTP_COMPRESSION) target_compile_definitions(opentelemetry_api INTERFACE ENABLE_OTLP_COMPRESSION_PREVIEW) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 11dbe4ef80..1e5dd59899 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -131,6 +131,7 @@ elif [[ "$1" == "cmake.maintainer.sync.test" ]]; then -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" @@ -153,6 +154,7 @@ elif [[ "$1" == "cmake.maintainer.async.test" ]]; then -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" @@ -176,6 +178,7 @@ elif [[ "$1" == "cmake.maintainer.cpp11.async.test" ]]; then -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ "${SRC_DIR}" make -k -j $(nproc) make test @@ -199,6 +202,7 @@ elif [[ "$1" == "cmake.maintainer.abiv2.test" ]]; then -DWITH_ABI_VERSION_1=OFF \ -DWITH_ABI_VERSION_2=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" diff --git a/examples/otlp/http_instrumented_main.cc b/examples/otlp/http_instrumented_main.cc index 38f8850360..c156fc9e71 100644 --- a/examples/otlp/http_instrumented_main.cc +++ b/examples/otlp/http_instrumented_main.cc @@ -58,6 +58,8 @@ namespace std::mutex serialize; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + /** The purpose of MyThreadInstrumentation is to demonstrate how notifications are delivered to the application. @@ -143,6 +145,8 @@ class MyThreadInstrumentation : public opentelemetry::sdk::common::ThreadInstrum std::string priority_; }; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + opentelemetry::exporter::otlp::OtlpHttpExporterOptions tracer_opts; opentelemetry::exporter::otlp::OtlpHttpMetricExporterOptions meter_opts; opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterOptions logger_opts; @@ -155,19 +159,23 @@ void InitTracer() { // Create OTLP exporter instance opentelemetry::exporter::otlp::OtlpHttpExporterRuntimeOptions exp_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW auto exp_instr = std::shared_ptr( new MyThreadInstrumentation("OtlpHttpExporter", "trace-net", "high")); exp_rt_opts.thread_instrumentation = exp_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto exporter = opentelemetry::exporter::otlp::OtlpHttpExporterFactory::Create(tracer_opts, exp_rt_opts); // Create Processor instance opentelemetry::sdk::trace::BatchSpanProcessorOptions pro_opts; opentelemetry::sdk::trace::BatchSpanProcessorRuntimeOptions pro_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW auto pro_instr = std::shared_ptr( new MyThreadInstrumentation("BatchSpanProcessor", "", "high")); pro_rt_opts.thread_instrumentation = pro_instr; - auto processor = opentelemetry::sdk::trace::BatchSpanProcessorFactory::Create( +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto processor = opentelemetry::sdk::trace::BatchSpanProcessorFactory::Create( std::move(exporter), pro_opts, pro_rt_opts); // Create Provider instance @@ -196,9 +204,11 @@ void InitMetrics() { // Create OTLP exporter instance opentelemetry::exporter::otlp::OtlpHttpMetricExporterRuntimeOptions exp_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW auto exp_instr = std::shared_ptr( new MyThreadInstrumentation("OtlpHttpMetricExporter", "metric-net", "medium")); exp_rt_opts.thread_instrumentation = exp_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto exporter = opentelemetry::exporter::otlp::OtlpHttpMetricExporterFactory::Create(meter_opts, exp_rt_opts); @@ -211,12 +221,14 @@ void InitMetrics() reader_options.export_timeout_millis = std::chrono::milliseconds(500); opentelemetry::sdk::metrics::PeriodicExportingMetricReaderRuntimeOptions reader_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW auto reader_periodic_instr = std::shared_ptr( new MyThreadInstrumentation("PeriodicExportingMetricReader(periodic)", "", "medium")); auto reader_collect_instr = std::shared_ptr( new MyThreadInstrumentation("PeriodicExportingMetricReader(collect)", "", "medium")); reader_rt_opts.periodic_thread_instrumentation = reader_periodic_instr; reader_rt_opts.collect_thread_instrumentation = reader_collect_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto reader = opentelemetry::sdk::metrics::PeriodicExportingMetricReaderFactory::Create( std::move(exporter), reader_options, reader_rt_opts); @@ -247,18 +259,22 @@ void InitLogger() { // Create OTLP exporter instance opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterRuntimeOptions exp_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW auto exp_instr = std::shared_ptr( new MyThreadInstrumentation("OtlpHttpLogRecordExporter", "log-net", "low")); exp_rt_opts.thread_instrumentation = exp_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto exporter = opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterFactory::Create( logger_opts, exp_rt_opts); // Create Processor instance opentelemetry::sdk::logs::BatchLogRecordProcessorOptions pro_opts; opentelemetry::sdk::logs::BatchLogRecordProcessorRuntimeOptions pro_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW auto pro_instr = std::shared_ptr( new MyThreadInstrumentation("BatchLogRecordProcessor", "", "low")); pro_rt_opts.thread_instrumentation = pro_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto processor = opentelemetry::sdk::logs::BatchLogRecordProcessorFactory::Create( std::move(exporter), pro_opts, pro_rt_opts); diff --git a/exporters/otlp/src/otlp_file_client.cc b/exporters/otlp/src/otlp_file_client.cc index d299ad9af0..e59cbb04be 100644 --- a/exporters/otlp/src/otlp_file_client.cc +++ b/exporters/otlp/src/otlp_file_client.cc @@ -1453,10 +1453,12 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender std::chrono::system_clock::now(); std::size_t last_record_count = 0; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (thread_instrumentation != nullptr) { thread_instrumentation->OnStart(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ while (true) { @@ -1472,20 +1474,24 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender break; } +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (thread_instrumentation != nullptr) { thread_instrumentation->BeforeWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ { std::unique_lock lk(concurrency_file->background_thread_waker_lock); concurrency_file->background_thread_waker_cv.wait_for(lk, flush_interval); } +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (thread_instrumentation != nullptr) { thread_instrumentation->AfterWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ { std::size_t current_record_count = @@ -1516,10 +1522,12 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender background_flush_thread.swap(concurrency_file->background_flush_thread); } +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (thread_instrumentation != nullptr) { thread_instrumentation->OnEnd(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ if (background_flush_thread && background_flush_thread->joinable()) { diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 527d66af11..9b61e44aba 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -441,10 +441,12 @@ bool HttpClient::MaybeSpawnBackgroundThread() background_thread_.reset(new std::thread( [](HttpClient *self) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (self->background_thread_instrumentation_ != nullptr) { self->background_thread_instrumentation_->OnStart(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ int still_running = 1; std::chrono::system_clock::time_point last_free_job_timepoint = @@ -464,10 +466,12 @@ bool HttpClient::MaybeSpawnBackgroundThread() } else if (still_running || need_wait_more) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (self->background_thread_instrumentation_ != nullptr) { self->background_thread_instrumentation_->BeforeWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ // curl_multi_poll is added from libcurl 7.66.0, before 7.68.0, we can only wait util // timeout to do the rest jobs @@ -482,10 +486,12 @@ bool HttpClient::MaybeSpawnBackgroundThread() nullptr); #endif +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (self->background_thread_instrumentation_ != nullptr) { self->background_thread_instrumentation_->AfterWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } do @@ -584,10 +590,12 @@ bool HttpClient::MaybeSpawnBackgroundThread() // If there is no pending jobs, we can stop the background thread. if (still_running == 0) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (self->background_thread_instrumentation_ != nullptr) { self->background_thread_instrumentation_->OnEnd(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ if (self->background_thread_) { diff --git a/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h index 1db45a478f..7886af71b5 100644 --- a/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h +++ b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h @@ -74,12 +74,17 @@ class ThreadInstrumentation ThreadInstrumentation() = default; virtual ~ThreadInstrumentation() = default; +/* + * This feature is experimental, protected by a _PREVIEW flag. + */ +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW virtual void OnStart() {} virtual void OnEnd() {} virtual void BeforeWait() {} virtual void AfterWait() {} virtual void BeforeLoad() {} virtual void AfterLoad() {} +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ }; } // namespace common diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index e5cc700317..ed7f1a86d9 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -178,19 +178,23 @@ bool BatchLogRecordProcessor::ForceFlush(std::chrono::microseconds timeout) noex void BatchLogRecordProcessor::DoBackgroundWork() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->OnStart(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto timeout = scheduled_delay_millis_; while (true) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->BeforeWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ // Wait for `timeout` milliseconds std::unique_lock lk(synchronization_data_->cv_m); @@ -205,10 +209,12 @@ void BatchLogRecordProcessor::DoBackgroundWork() synchronization_data_->is_force_wakeup_background_worker.store(false, std::memory_order_release); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->AfterWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ if (synchronization_data_->is_shutdown.load() == true) { @@ -225,18 +231,22 @@ void BatchLogRecordProcessor::DoBackgroundWork() timeout = scheduled_delay_millis_ - duration; } +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->OnEnd(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchLogRecordProcessor::Export() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->BeforeLoad(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ do { @@ -277,10 +287,12 @@ void BatchLogRecordProcessor::Export() NotifyCompletion(notify_force_flush, exporter_, synchronization_data_); } while (true); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->AfterLoad(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchLogRecordProcessor::NotifyCompletion( diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc index 21ef276c8c..1bb9101618 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -91,26 +91,32 @@ void PeriodicExportingMetricReader::OnInitialized() noexcept void PeriodicExportingMetricReader::DoBackgroundWork() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->OnStart(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ do { auto start = std::chrono::steady_clock::now(); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->BeforeLoad(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto status = CollectAndExportOnce(); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->AfterLoad(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ if (!status) { @@ -121,10 +127,12 @@ void PeriodicExportingMetricReader::DoBackgroundWork() auto export_time_ms = std::chrono::duration_cast(end - start); auto remaining_wait_interval_ms = export_interval_millis_ - export_time_ms; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->BeforeWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ std::unique_lock lk(cv_m_); cv_.wait_for(lk, remaining_wait_interval_ms, [this]() { @@ -136,17 +144,21 @@ void PeriodicExportingMetricReader::DoBackgroundWork() return IsShutdown(); }); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->AfterWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } while (IsShutdown() != true); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->OnEnd(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } bool PeriodicExportingMetricReader::CollectAndExportOnce() @@ -165,11 +177,13 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce() task_thread.reset( new std::thread([this, &cancel_export_for_timeout, sender = std::move(sender)] { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (collect_thread_instrumentation_ != nullptr) { collect_thread_instrumentation_->OnStart(); collect_thread_instrumentation_->BeforeLoad(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ this->Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) { if (cancel_export_for_timeout.load(std::memory_order_acquire)) @@ -185,11 +199,13 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce() const_cast &>(sender).set_value(); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (collect_thread_instrumentation_ != nullptr) { collect_thread_instrumentation_->AfterLoad(); collect_thread_instrumentation_->OnEnd(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ })); std::future_status status; diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index 1ab8173944..614de89818 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -171,19 +171,23 @@ bool BatchSpanProcessor::ForceFlush(std::chrono::microseconds timeout) noexcept void BatchSpanProcessor::DoBackgroundWork() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->OnStart(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ auto timeout = schedule_delay_millis_; while (true) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->BeforeWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ // Wait for `timeout` milliseconds std::unique_lock lk(synchronization_data_->cv_m); @@ -198,10 +202,12 @@ void BatchSpanProcessor::DoBackgroundWork() synchronization_data_->is_force_wakeup_background_worker.store(false, std::memory_order_release); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->AfterWait(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ if (synchronization_data_->is_shutdown.load() == true) { @@ -218,18 +224,22 @@ void BatchSpanProcessor::DoBackgroundWork() timeout = schedule_delay_millis_ - duration; } +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->OnEnd(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchSpanProcessor::Export() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->BeforeLoad(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ do { @@ -270,10 +280,12 @@ void BatchSpanProcessor::Export() NotifyCompletion(notify_force_flush, exporter_, synchronization_data_); } while (true); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW if (worker_thread_instrumentation_ != nullptr) { worker_thread_instrumentation_->AfterLoad(); } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchSpanProcessor::NotifyCompletion( From 960c80f91408c669cdb2d388b5d8ce2b4f91358f Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 8 Jan 2025 14:23:36 +0100 Subject: [PATCH 21/21] Update examples/otlp/http_instrumented_main.cc --- examples/otlp/http_instrumented_main.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/otlp/http_instrumented_main.cc b/examples/otlp/http_instrumented_main.cc index c156fc9e71..39bbea98e7 100644 --- a/examples/otlp/http_instrumented_main.cc +++ b/examples/otlp/http_instrumented_main.cc @@ -354,4 +354,5 @@ int main(int argc, char *argv[]) CleanupTracer(); std::cout << "Done" << std::endl << std::flush; + return 0; }