@@ -309,6 +309,32 @@ OtlpGrpcClient::~OtlpGrpcClient()
309309#endif
310310}
311311
312+ std::string OtlpGrpcClient::GetGrpcTarget (const std::string &endpoint)
313+ {
314+ //
315+ // Scheme is allowed in OTLP endpoint definition, but is not allowed for creating gRPC
316+ // channel. Passing URI with scheme to grpc::CreateChannel could resolve the endpoint to some
317+ // unexpected address.
318+ //
319+ ext::http::common::UrlParser url (endpoint);
320+ if (!url.success_ )
321+ {
322+ OTEL_INTERNAL_LOG_ERROR (" [OTLP GRPC Client] invalid endpoint: " << endpoint);
323+ return " " ;
324+ }
325+
326+ std::string grpc_target;
327+ if (url.scheme_ == " unix" )
328+ {
329+ grpc_target = " unix:" + url.path_ ;
330+ }
331+ else
332+ {
333+ grpc_target = url.host_ + " :" + std::to_string (static_cast <int >(url.port_ ));
334+ }
335+ return grpc_target;
336+ }
337+
312338std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel (const OtlpGrpcClientOptions &options)
313339{
314340
@@ -318,22 +344,16 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO
318344
319345 return nullptr ;
320346 }
321- //
322- // Scheme is allowed in OTLP endpoint definition, but is not allowed for creating gRPC
323- // channel. Passing URI with scheme to grpc::CreateChannel could resolve the endpoint to some
324- // unexpected address.
325- //
326347
327- ext::http::common::UrlParser url (options.endpoint );
328- if (!url.success_ )
348+ std::shared_ptr<grpc::Channel> channel;
349+ std::string grpc_target = GetGrpcTarget (options.endpoint );
350+
351+ if (grpc_target.empty ())
329352 {
330353 OTEL_INTERNAL_LOG_ERROR (" [OTLP GRPC Client] invalid endpoint: " << options.endpoint );
331-
332354 return nullptr ;
333355 }
334356
335- std::shared_ptr<grpc::Channel> channel;
336- std::string grpc_target = url.host_ + " :" + std::to_string (static_cast <int >(url.port_ ));
337357 grpc::ChannelArguments grpc_arguments;
338358 grpc_arguments.SetUserAgentPrefix (options.user_agent );
339359
0 commit comments