Skip to content

Commit 3975855

Browse files
committed
[POC] Better control of threads executed by opentelemetry-cpp
Fixes open-telemetry#3174
1 parent fe68d51 commit 3975855

25 files changed

+254
-37
lines changed

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h

+29-23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "opentelemetry/nostd/string_view.h"
2121
#include "opentelemetry/nostd/variant.h"
2222
#include "opentelemetry/sdk/common/exporter_utils.h"
23+
#include "opentelemetry/sdk/common/thread_instrumentation.h"
2324
#include "opentelemetry/version.h"
2425

2526
// forward declare google::protobuf::Message
@@ -83,28 +84,32 @@ struct OtlpHttpClientOptions
8384
// User agent
8485
std::string user_agent;
8586

86-
inline OtlpHttpClientOptions(nostd::string_view input_url,
87-
bool input_ssl_insecure_skip_verify,
88-
nostd::string_view input_ssl_ca_cert_path,
89-
nostd::string_view input_ssl_ca_cert_string,
90-
nostd::string_view input_ssl_client_key_path,
91-
nostd::string_view input_ssl_client_key_string,
92-
nostd::string_view input_ssl_client_cert_path,
93-
nostd::string_view input_ssl_client_cert_string,
94-
nostd::string_view input_ssl_min_tls,
95-
nostd::string_view input_ssl_max_tls,
96-
nostd::string_view input_ssl_cipher,
97-
nostd::string_view input_ssl_cipher_suite,
98-
HttpRequestContentType input_content_type,
99-
JsonBytesMappingKind input_json_bytes_mapping,
100-
nostd::string_view input_compression,
101-
bool input_use_json_name,
102-
bool input_console_debug,
103-
std::chrono::system_clock::duration input_timeout,
104-
const OtlpHeaders &input_http_headers,
105-
std::size_t input_concurrent_sessions = 64,
106-
std::size_t input_max_requests_per_connection = 8,
107-
nostd::string_view input_user_agent = GetOtlpDefaultUserAgent())
87+
std::shared_ptr<sdk::common::ThreadInstrumentation> thread_instrumentation;
88+
89+
inline OtlpHttpClientOptions(
90+
nostd::string_view input_url,
91+
bool input_ssl_insecure_skip_verify,
92+
nostd::string_view input_ssl_ca_cert_path,
93+
nostd::string_view input_ssl_ca_cert_string,
94+
nostd::string_view input_ssl_client_key_path,
95+
nostd::string_view input_ssl_client_key_string,
96+
nostd::string_view input_ssl_client_cert_path,
97+
nostd::string_view input_ssl_client_cert_string,
98+
nostd::string_view input_ssl_min_tls,
99+
nostd::string_view input_ssl_max_tls,
100+
nostd::string_view input_ssl_cipher,
101+
nostd::string_view input_ssl_cipher_suite,
102+
HttpRequestContentType input_content_type,
103+
JsonBytesMappingKind input_json_bytes_mapping,
104+
nostd::string_view input_compression,
105+
bool input_use_json_name,
106+
bool input_console_debug,
107+
std::chrono::system_clock::duration input_timeout,
108+
const OtlpHeaders &input_http_headers,
109+
const std::shared_ptr<sdk::common::ThreadInstrumentation> &input_thread_instrumentation,
110+
std::size_t input_concurrent_sessions = 64,
111+
std::size_t input_max_requests_per_connection = 8,
112+
nostd::string_view input_user_agent = GetOtlpDefaultUserAgent())
108113
: url(input_url),
109114
ssl_options(input_url,
110115
input_ssl_insecure_skip_verify,
@@ -127,7 +132,8 @@ struct OtlpHttpClientOptions
127132
http_headers(input_http_headers),
128133
max_concurrent_requests(input_concurrent_sessions),
129134
max_requests_per_connection(input_max_requests_per_connection),
130-
user_agent(input_user_agent)
135+
user_agent(input_user_agent),
136+
thread_instrumentation(input_thread_instrumentation)
131137
{}
132138
};
133139

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_options.h

+4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#pragma once
55

66
#include <chrono>
7+
#include <memory>
78
#include <string>
89

910
#include "opentelemetry/exporters/otlp/otlp_environment.h"
1011
#include "opentelemetry/exporters/otlp/otlp_http.h"
12+
#include "opentelemetry/sdk/common/thread_instrumentation.h"
1113
#include "opentelemetry/version.h"
1214

1315
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -101,6 +103,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpExporterOptions
101103

102104
/** Compression type. */
103105
std::string compression;
106+
107+
std::shared_ptr<sdk::common::ThreadInstrumentation> thread_instrumentation;
104108
};
105109

106110
} // namespace otlp

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h

+4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#pragma once
55

66
#include <chrono>
7+
#include <memory>
78
#include <string>
89

910
#include "opentelemetry/exporters/otlp/otlp_environment.h"
1011
#include "opentelemetry/exporters/otlp/otlp_http.h"
12+
#include "opentelemetry/sdk/common/thread_instrumentation.h"
1113
#include "opentelemetry/version.h"
1214

1315
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -101,6 +103,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterOptions
101103

102104
/** Compression type. */
103105
std::string compression;
106+
107+
std::shared_ptr<sdk::common::ThreadInstrumentation> thread_instrumentation;
104108
};
105109

106110
} // namespace otlp

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h

+4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
#pragma once
55

66
#include <chrono>
7+
#include <memory>
78
#include <string>
89

910
#include "opentelemetry/exporters/otlp/otlp_environment.h"
1011
#include "opentelemetry/exporters/otlp/otlp_http.h"
1112
#include "opentelemetry/exporters/otlp/otlp_preferred_temporality.h"
13+
#include "opentelemetry/sdk/common/thread_instrumentation.h"
1214
#include "opentelemetry/version.h"
1315

1416
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -104,6 +106,8 @@ struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterOptions
104106

105107
/** Compression type. */
106108
std::string compression;
109+
110+
std::shared_ptr<sdk::common::ThreadInstrumentation> thread_instrumentation;
107111
};
108112

109113
} // namespace otlp

exporters/otlp/src/otlp_http_client.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ void ConvertListFieldToJson(nlohmann::json &value,
665665
OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options)
666666
: is_shutdown_(false),
667667
options_(options),
668-
http_client_(http_client::HttpClientFactory::Create()),
668+
http_client_(http_client::HttpClientFactory::Create(options.thread_instrumentation)),
669669
start_session_counter_(0),
670670
finished_session_counter_(0)
671671
{

exporters/otlp/src/otlp_http_exporter.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options)
5757
options.use_json_name,
5858
options.console_debug,
5959
options.timeout,
60-
options.http_headers
60+
options.http_headers,
61+
options.thread_instrumentation
6162
#ifdef ENABLE_ASYNC_EXPORT
6263
,
6364
options.max_concurrent_requests,

exporters/otlp/src/otlp_http_log_record_exporter.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(
6060
options.use_json_name,
6161
options.console_debug,
6262
options.timeout,
63-
options.http_headers
63+
options.http_headers,
64+
options.thread_instrumentation
6465
#ifdef ENABLE_ASYNC_EXPORT
6566
,
6667
options.max_concurrent_requests,

exporters/otlp/src/otlp_http_metric_exporter.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptio
6161
options.use_json_name,
6262
options.console_debug,
6363
options.timeout,
64-
options.http_headers
64+
options.http_headers,
65+
options.thread_instrumentation
6566
#ifdef ENABLE_ASYNC_EXPORT
6667
,
6768
options.max_concurrent_requests,

exporters/otlp/test/otlp_http_exporter_test.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static nostd::span<T, N> MakeSpan(T (&array)[N])
5454
OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type,
5555
bool async_mode)
5656
{
57+
std::shared_ptr<opentelemetry::sdk::common::ThreadInstrumentation> not_instrumented;
5758
OtlpHttpExporterOptions options;
5859
options.content_type = content_type;
5960
options.console_debug = true;
@@ -70,7 +71,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t
7071
"", /* ssl_cipher */
7172
"", /* ssl_cipher_suite */
7273
options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name,
73-
options.console_debug, options.timeout, options.http_headers);
74+
options.console_debug, options.timeout, options.http_headers, not_instrumented);
7475
if (!async_mode)
7576
{
7677
otlp_http_client_options.max_concurrent_requests = 0;

exporters/otlp/test/otlp_http_log_record_exporter_test.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static nostd::span<T, N> MakeSpan(T (&array)[N])
5454
OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type,
5555
bool async_mode)
5656
{
57+
std::shared_ptr<opentelemetry::sdk::common::ThreadInstrumentation> not_instrumented;
5758
OtlpHttpLogRecordExporterOptions options;
5859
options.content_type = content_type;
5960
options.console_debug = true;
@@ -69,7 +70,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t
6970
"", /* ssl_cipher */
7071
"", /* ssl_cipher_suite */
7172
options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name,
72-
options.console_debug, options.timeout, options.http_headers);
73+
options.console_debug, options.timeout, options.http_headers, not_instrumented);
7374
if (!async_mode)
7475
{
7576
otlp_http_client_options.max_concurrent_requests = 0;

exporters/otlp/test/otlp_http_metric_exporter_test.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static IntegerType JsonToInteger(nlohmann::json value)
6161
OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type,
6262
bool async_mode)
6363
{
64+
std::shared_ptr<opentelemetry::sdk::common::ThreadInstrumentation> not_instrumented;
6465
OtlpHttpMetricExporterOptions options;
6566
options.content_type = content_type;
6667
options.console_debug = true;
@@ -76,7 +77,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t
7677
"", /* ssl_cipher */
7778
"", /* ssl_cipher_suite */
7879
options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name,
79-
options.console_debug, options.timeout, options.http_headers);
80+
options.console_debug, options.timeout, options.http_headers, not_instrumented);
8081
if (!async_mode)
8182
{
8283
otlp_http_client_options.max_concurrent_requests = 0;

ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "opentelemetry/nostd/function_ref.h"
2424
#include "opentelemetry/nostd/shared_ptr.h"
2525
#include "opentelemetry/nostd/string_view.h"
26+
#include "opentelemetry/sdk/common/thread_instrumentation.h"
2627
#include "opentelemetry/version.h"
2728

2829
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -304,6 +305,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient
304305
public:
305306
// The call (curl_global_init) is not thread safe. Ensure this is called only once.
306307
HttpClient();
308+
HttpClient(const std::shared_ptr<sdk::common::ThreadInstrumentation> &thread_instrumentation);
307309
~HttpClient() override;
308310

309311
std::shared_ptr<opentelemetry::ext::http::client::Session> CreateSession(
@@ -366,6 +368,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient
366368

367369
std::mutex background_thread_m_;
368370
std::unique_ptr<std::thread> background_thread_;
371+
std::shared_ptr<sdk::common::ThreadInstrumentation> background_thread_instrumentation_;
369372
std::chrono::milliseconds scheduled_delay_milliseconds_;
370373

371374
nostd::shared_ptr<HttpCurlGlobalInitializer> curl_global_initializer_;

ext/include/opentelemetry/ext/http/client/http_client_factory.h

+5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#pragma once
5+
56
#include "opentelemetry/ext/http/client/http_client.h"
7+
#include "opentelemetry/sdk/common/thread_instrumentation.h"
8+
#include "opentelemetry/version.h"
69

710
OPENTELEMETRY_BEGIN_NAMESPACE
811
namespace ext
@@ -17,6 +20,8 @@ class HttpClientFactory
1720
static std::shared_ptr<HttpClientSync> CreateSync();
1821

1922
static std::shared_ptr<HttpClient> Create();
23+
static std::shared_ptr<HttpClient> Create(
24+
const std::shared_ptr<sdk::common::ThreadInstrumentation> &thread_instrumentation);
2025
};
2126
} // namespace client
2227
} // namespace http

ext/src/http/client/curl/http_client_curl.cc

+22-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,18 @@ HttpClient::HttpClient()
189189
next_session_id_{0},
190190
max_sessions_per_connection_{8},
191191
scheduled_delay_milliseconds_{std::chrono::milliseconds(256)},
192-
curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance())
192+
curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()),
193+
background_thread_instrumentation_(nullptr)
194+
{}
195+
196+
HttpClient::HttpClient(
197+
const std::shared_ptr<sdk::common::ThreadInstrumentation> &thread_instrumentation)
198+
: multi_handle_(curl_multi_init()),
199+
next_session_id_{0},
200+
max_sessions_per_connection_{8},
201+
scheduled_delay_milliseconds_{std::chrono::milliseconds(256)},
202+
curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()),
203+
background_thread_instrumentation_(thread_instrumentation)
193204
{}
194205

195206
HttpClient::~HttpClient()
@@ -345,6 +356,11 @@ void HttpClient::MaybeSpawnBackgroundThread()
345356

346357
background_thread_.reset(new std::thread(
347358
[](HttpClient *self) {
359+
if (self->background_thread_instrumentation_ != nullptr)
360+
{
361+
self->background_thread_instrumentation_->OnStart();
362+
}
363+
348364
int still_running = 1;
349365
while (true)
350366
{
@@ -452,6 +468,11 @@ void HttpClient::MaybeSpawnBackgroundThread()
452468
}
453469
}
454470
}
471+
472+
if (self->background_thread_instrumentation_ != nullptr)
473+
{
474+
self->background_thread_instrumentation_->OnEnd();
475+
}
455476
},
456477
this));
457478
}

ext/src/http/client/curl/http_client_factory_curl.cc

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ std::shared_ptr<http_client::HttpClient> http_client::HttpClientFactory::Create(
1414
return std::make_shared<http_client::curl::HttpClient>();
1515
}
1616

17+
std::shared_ptr<http_client::HttpClient> http_client::HttpClientFactory::Create(
18+
const std::shared_ptr<sdk::common::ThreadInstrumentation> &thread_instrumentation)
19+
{
20+
return std::make_shared<http_client::curl::HttpClient>(thread_instrumentation);
21+
}
22+
1723
std::shared_ptr<http_client::HttpClientSync> http_client::HttpClientFactory::CreateSync()
1824
{
1925
return std::make_shared<http_client::curl::HttpClientSync>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include "opentelemetry/version.h"
7+
8+
OPENTELEMETRY_BEGIN_NAMESPACE
9+
namespace sdk
10+
{
11+
namespace common
12+
{
13+
14+
class ThreadInstrumentation
15+
{
16+
public:
17+
ThreadInstrumentation() = default;
18+
virtual ~ThreadInstrumentation() = default;
19+
20+
virtual void OnStart() = 0;
21+
virtual void OnEnd() = 0;
22+
virtual void BeforeWait() = 0;
23+
virtual void AfterWait() = 0;
24+
virtual void BeforeLoad() = 0;
25+
virtual void AfterLoad() = 0;
26+
};
27+
28+
} // namespace common
29+
} // namespace sdk
30+
OPENTELEMETRY_END_NAMESPACE

sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor
158158

159159
/* The background worker thread */
160160
std::thread worker_thread_;
161+
std::shared_ptr<sdk::common::ThreadInstrumentation> worker_thread_instrumentation_;
161162
};
162163

163164
} // namespace logs

sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <chrono>
77
#include <cstddef>
88

9+
#include "opentelemetry/sdk/common/thread_instrumentation.h"
910
#include "opentelemetry/version.h"
1011

1112
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -34,6 +35,8 @@ struct BatchLogRecordProcessorOptions
3435
* equal to max_queue_size.
3536
*/
3637
size_t max_export_batch_size = 512;
38+
39+
std::shared_ptr<sdk::common::ThreadInstrumentation> thread_instrumentation;
3740
};
3841

3942
} // namespace logs

0 commit comments

Comments
 (0)