diff --git a/CHANGELOG.md b/CHANGELOG.md index 52f280eb22..e7e30353a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,60 @@ Increment the: * [EXPORTER] Fix scope attributes missing from otlp traces metrics [#3185](https://github.com/open-telemetry/opentelemetry-cpp/pull/3185) +* [SDK] Better control of threads executed by opentelemetry-cpp + [#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3175) + +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 + * 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, + 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 diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index fd4e0dc171..b27cbef32d 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -168,3 +168,25 @@ 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/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..38f8850360 --- /dev/null +++ b/examples/otlp/http_instrumented_main.cc @@ -0,0 +1,341 @@ +// 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/logs_foo_library/foo_library.h" +# include "examples/common/metrics_foo_library/foo_library.h" +#else +# include "logs_foo_library/foo_library.h" +# include "metrics_foo_library/foo_library.h" +#endif + +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: + 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_instrumented_http + - example_otlp_instrumented_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/exporters/otlp/BUILD b/exporters/otlp/BUILD index eb3d23d579..cdaba37e4d 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", ], @@ -180,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", ], @@ -210,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", ], @@ -270,6 +273,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", ], @@ -298,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", ], @@ -326,6 +331,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", ], @@ -354,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", ], 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/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 3753704bb3..1668f0381d 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,33 @@ 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 = + std::shared_ptr(nullptr); + + 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 +133,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.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_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_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_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_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..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" @@ -35,7 +36,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..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" @@ -38,7 +39,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..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" @@ -37,10 +39,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/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index a2620756cc..cedc9a550e 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..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" @@ -39,6 +40,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, @@ -57,7 +92,8 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -69,18 +105,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_.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 bd76bd10bd..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" @@ -42,6 +44,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, @@ -60,7 +97,8 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -72,19 +110,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_.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_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; } diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index 18b4ed0712..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" @@ -41,6 +43,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, @@ -61,7 +100,8 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptio options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -76,18 +116,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_.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/exporters/otlp/test/otlp_file_client_test.cc b/exporters/otlp/test/otlp_file_client_test.cc index cd4ffb5bc9..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" @@ -159,7 +160,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 +184,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 +284,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 +406,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 +514,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 +531,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/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 0b44d98379..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" @@ -76,6 +77,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; @@ -91,7 +93,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 bef15997fc..cd88c9f7c0 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 @@ -302,6 +303,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( @@ -355,6 +357,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_; std::chrono::milliseconds background_thread_wait_for_; 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 a2b73ecb2b..527d66af11 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -24,6 +24,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 @@ -268,6 +269,18 @@ 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)}, + background_thread_wait_for_{std::chrono::minutes{1}}, + curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) +{} + +HttpClient::HttpClient( + const std::shared_ptr &thread_instrumentation) + : 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)}, background_thread_wait_for_{std::chrono::minutes{1}}, curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) @@ -428,6 +441,11 @@ bool 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; std::chrono::system_clock::time_point last_free_job_timepoint = std::chrono::system_clock::now(); @@ -446,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 @@ -458,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 @@ -556,6 +584,11 @@ 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_) { self->background_thread_->detach(); 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..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; @@ -14,6 +15,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..1db45a478f --- /dev/null +++ b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h @@ -0,0 +1,87 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +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() {} + virtual void OnEnd() {} + virtual void BeforeWait() {} + virtual void AfterWait() {} + virtual void BeforeLoad() {} + virtual void AfterLoad() {} +}; + +} // 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..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; @@ -157,6 +170,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor std::shared_ptr synchronization_data_; /* The background worker thread */ + std::shared_ptr worker_thread_instrumentation_; std::thread worker_thread_; }; 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_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 d72ec08839..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 @@ -11,7 +11,9 @@ #include #include +#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" @@ -28,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; @@ -47,15 +53,17 @@ class PeriodicExportingMetricReader : public MetricReader void DoBackgroundWork(); bool CollectAndExportOnce(); - /* The background worker thread */ - std::thread worker_thread_; - /* 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_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_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 e069a461e6..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. * @@ -158,6 +171,7 @@ class BatchSpanProcessor : public SpanProcessor std::shared_ptr synchronization_data_; /* The background worker thread */ + std::shared_ptr worker_thread_instrumentation_; std::thread worker_thread_; }; 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_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 f5c35ad339..e5cc700317 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -19,8 +19,10 @@ #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/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/recordable.h" #include "opentelemetry/version.h" @@ -44,8 +46,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_() +{ + // 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) @@ -55,8 +61,29 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr()), - worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this) -{} + 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. + worker_thread_ = std::thread(&BatchLogRecordProcessor::DoBackgroundWork, this); +} std::unique_ptr BatchLogRecordProcessor::MakeRecordable() noexcept { @@ -151,10 +178,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 +205,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 +224,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 +276,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/logs/batch_log_record_processor_factory.cc b/sdk/src/logs/batch_log_record_processor_factory.cc index a6e29e4967..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" @@ -20,9 +21,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 682badf3a4..21ef276c8c 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -14,9 +14,11 @@ #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" +#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" @@ -40,10 +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} + 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_) { @@ -67,18 +91,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 +135,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 +165,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 +184,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/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 f0f55d20e8..1ab8173944 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -20,8 +20,10 @@ #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/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/recordable.h" @@ -46,8 +48,28 @@ 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_(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. + worker_thread_ = std::thread(&BatchSpanProcessor::DoBackgroundWork, this); +} std::unique_ptr BatchSpanProcessor::MakeRecordable() noexcept { @@ -149,10 +171,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,10 +198,15 @@ 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(); - return; + break; } auto start = std::chrono::steady_clock::now(); @@ -180,10 +217,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 +269,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( diff --git a/sdk/src/trace/batch_span_processor_factory.cc b/sdk/src/trace/batch_span_processor_factory.cc index a21b056cee..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" @@ -16,11 +17,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; }