-
Notifications
You must be signed in to change notification settings - Fork 546
Allow user use custom grpc::ChannelArguments #3990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
0d3ab14
cc58164
91086b6
aaa63bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ | |
| namespace grpc | ||
| { | ||
| class ChannelCredentials; | ||
| } | ||
| class ChannelArguments; | ||
| } // namespace grpc | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
|
|
@@ -90,6 +91,13 @@ struct OtlpGrpcClientOptions | |
|
|
||
| /** The backoff will be multiplied by this value after each retry attempt. */ | ||
| float retry_policy_backoff_multiplier{}; | ||
|
|
||
| /** | ||
| * Optional caller-provided gRPC channel arguments. | ||
| * This is a non-owning pointer, and the pointed-to arguments are copied when the channel is | ||
| * created. | ||
| */ | ||
| const grpc::ChannelArguments *channel_arguments{}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit - Question: can we make the lifetime requirement a bit more explicit here? Something like: /*
This is a non-owning pointer; the pointed-to grpc::ChannelArguments must remain valid until client/channel construction copies it.
*/Currently, "Copied when the channel is created" is directionally right, but it still leaves the required lifetime a little ambiguous. |
||
| }; | ||
|
|
||
| } // namespace otlp | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -356,6 +356,54 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO | |
| } | ||
|
|
||
| grpc::ChannelArguments grpc_arguments; | ||
| PopulateChannelArguments(options, grpc_arguments); | ||
|
|
||
| if (options.use_ssl_credentials) | ||
| { | ||
| grpc::SslCredentialsOptions ssl_opts; | ||
| ssl_opts.pem_root_certs = GetFileContentsOrInMemoryContents( | ||
| options.ssl_credentials_cacert_path, options.ssl_credentials_cacert_as_string); | ||
| #ifdef ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW | ||
| ssl_opts.pem_private_key = GetFileContentsOrInMemoryContents(options.ssl_client_key_path, | ||
| options.ssl_client_key_string); | ||
| ssl_opts.pem_cert_chain = GetFileContentsOrInMemoryContents(options.ssl_client_cert_path, | ||
| options.ssl_client_cert_string); | ||
|
|
||
| #endif | ||
| channel = | ||
| grpc::CreateCustomChannel(grpc_target, grpc::SslCredentials(ssl_opts), grpc_arguments); | ||
| } | ||
| else | ||
| { | ||
| channel = | ||
| grpc::CreateCustomChannel(grpc_target, grpc::InsecureChannelCredentials(), grpc_arguments); | ||
| } | ||
|
|
||
| #ifdef ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW | ||
| if (options.credentials) | ||
| { | ||
| if (options.use_ssl_credentials) | ||
| { | ||
| OTEL_INTERNAL_LOG_WARN( | ||
| "[OTLP GRPC Client] Both 'credentials' and 'use_ssl_credentials' options are set. " | ||
| "The former takes priority."); | ||
| } | ||
| channel = grpc::CreateCustomChannel(grpc_target, options.credentials, grpc_arguments); | ||
| } | ||
| #endif // ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW | ||
|
|
||
| return channel; | ||
| } | ||
|
|
||
| void OtlpGrpcClient::PopulateChannelArguments(const OtlpGrpcClientOptions &options, | ||
| grpc::ChannelArguments &grpc_arguments) | ||
| { | ||
| grpc_arguments = grpc::ChannelArguments{}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems redundant because it is already default initialized. |
||
|
|
||
| if (options.channel_arguments != nullptr) | ||
| { | ||
| grpc_arguments = *options.channel_arguments; | ||
| } | ||
| grpc_arguments.SetUserAgentPrefix(options.user_agent); | ||
|
|
||
| if (options.max_threads > 0) | ||
|
|
@@ -399,9 +447,7 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO | |
| ] | ||
| })"}; | ||
|
|
||
| // Allocate string with buffer large enough to hold the formatted json config | ||
| auto service_config = std::string(kServiceConfigJson.size(), '\0'); | ||
| // Prior to C++17, need to explicitly cast away constness from `data()` buffer | ||
| std::snprintf( | ||
| const_cast<decltype(service_config)::value_type *>(service_config.data()), | ||
| service_config.size(), kServiceConfigJson.data(), options.retry_policy_max_attempts, | ||
|
|
@@ -412,42 +458,6 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO | |
| grpc_arguments.SetServiceConfigJSON(service_config); | ||
| } | ||
| #endif // ENABLE_OTLP_RETRY_PREVIEW | ||
|
|
||
| if (options.use_ssl_credentials) | ||
| { | ||
| grpc::SslCredentialsOptions ssl_opts; | ||
| ssl_opts.pem_root_certs = GetFileContentsOrInMemoryContents( | ||
| options.ssl_credentials_cacert_path, options.ssl_credentials_cacert_as_string); | ||
| #ifdef ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW | ||
| ssl_opts.pem_private_key = GetFileContentsOrInMemoryContents(options.ssl_client_key_path, | ||
| options.ssl_client_key_string); | ||
| ssl_opts.pem_cert_chain = GetFileContentsOrInMemoryContents(options.ssl_client_cert_path, | ||
| options.ssl_client_cert_string); | ||
|
|
||
| #endif | ||
| channel = | ||
| grpc::CreateCustomChannel(grpc_target, grpc::SslCredentials(ssl_opts), grpc_arguments); | ||
| } | ||
| else | ||
| { | ||
| channel = | ||
| grpc::CreateCustomChannel(grpc_target, grpc::InsecureChannelCredentials(), grpc_arguments); | ||
| } | ||
|
|
||
| #ifdef ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW | ||
| if (options.credentials) | ||
| { | ||
| if (options.use_ssl_credentials) | ||
| { | ||
| OTEL_INTERNAL_LOG_WARN( | ||
| "[OTLP GRPC Client] Both 'credentials' and 'use_ssl_credentials' options are set. " | ||
| "The former takes priority."); | ||
| } | ||
| channel = grpc::CreateCustomChannel(grpc_target, options.credentials, grpc_arguments); | ||
| } | ||
| #endif // ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW | ||
|
|
||
| return channel; | ||
| } | ||
|
|
||
| std::unique_ptr<grpc::ClientContext> OtlpGrpcClient::MakeClientContext( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.