From 5fef32a8c627d06b3fd9c4ce6e1f4eaf7aba659b Mon Sep 17 00:00:00 2001 From: owent Date: Thu, 16 Nov 2023 21:35:11 +0800 Subject: [PATCH] Fix style and benchmark --- api/include/opentelemetry/common/macros.h | 4 +- exporters/otlp/src/otlp_grpc_client.cc | 4 +- .../otlp/test/otlp_grpc_exporter_benchmark.cc | 74 +++++++++++++++++-- .../otlp/test/otlp_grpc_exporter_test.cc | 6 +- .../otlp_grpc_log_record_exporter_test.cc | 12 ++- 5 files changed, 84 insertions(+), 16 deletions(-) diff --git a/api/include/opentelemetry/common/macros.h b/api/include/opentelemetry/common/macros.h index c3b9fb82c0..88ec2f0182 100644 --- a/api/include/opentelemetry/common/macros.h +++ b/api/include/opentelemetry/common/macros.h @@ -7,7 +7,9 @@ // GCC 9 has likely attribute but do not support declare it at the beginning of statement # if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9) # if __has_cpp_attribute(likely) -# define OPENTELEMETRY_LIKELY_IF(...) if (__VA_ARGS__) [[likely]] +# define OPENTELEMETRY_LIKELY_IF(...) \ + if (__VA_ARGS__) \ + [[likely]] # endif # endif diff --git a/exporters/otlp/src/otlp_grpc_client.cc b/exporters/otlp/src/otlp_grpc_client.cc index 27fb170a0d..1c589b963c 100644 --- a/exporters/otlp/src/otlp_grpc_client.cc +++ b/exporters/otlp/src/otlp_grpc_client.cc @@ -191,7 +191,8 @@ static sdk::common::ExportResult InternalDelegateAsyncExport( ++async_data->start_request_counter; ++async_data->running_requests; -# if (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 +# if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 stub->async() # else stub->experimental_async() @@ -289,6 +290,7 @@ std::shared_ptr OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO 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); diff --git a/exporters/otlp/test/otlp_grpc_exporter_benchmark.cc b/exporters/otlp/test/otlp_grpc_exporter_benchmark.cc index b4a23c04a5..e791dd4459 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_benchmark.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_benchmark.cc @@ -49,6 +49,59 @@ const trace_api::SpanContext kSpanContext{ // Create a fake service stub to avoid dependency on gmock class FakeServiceStub : public proto::collector::trace::v1::TraceService::StubInterface { +public: +#if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 + using async_interface_base = + proto::collector::trace::v1::TraceService::StubInterface::async_interface; +#else + using async_interface_base = + proto::collector::trace::v1::TraceService::StubInterface::experimental_async_interface; +#endif + + FakeServiceStub() : async_interface_(this) {} + + class async_interface : public async_interface_base + { + public: + async_interface(FakeServiceStub *owner) : stub_(owner) {} + + virtual ~async_interface() {} + + void Export( + ::grpc::ClientContext *context, + const ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest *request, + ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse *response, + std::function callback) override + { + callback(stub_->Export(context, *request, response)); + } + +#if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 || \ + defined(GRPC_CALLBACK_API_NONEXPERIMENTAL) + void Export( + ::grpc::ClientContext * /*context*/, + const ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest * /*request*/, + ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse * /*response*/, + ::grpc::ClientUnaryReactor * /*reactor*/) override + {} +#else + void Export( + ::grpc::ClientContext * /*context*/, + const ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest * /*request*/, + ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse * /*response*/, + ::grpc::experimental::ClientUnaryReactor * /*reactor*/) + {} +#endif + + private: + FakeServiceStub *stub_; + }; + + async_interface_base *async() { return &async_interface_; } + async_interface_base *experimental_async() { return &async_interface_; } + grpc::Status Export(grpc::ClientContext *, const proto::collector::trace::v1::ExportTraceServiceRequest &, proto::collector::trace::v1::ExportTraceServiceResponse *) override @@ -56,21 +109,26 @@ class FakeServiceStub : public proto::collector::trace::v1::TraceService::StubIn return grpc::Status::OK; } - grpc::ClientAsyncResponseReaderInterface - *AsyncExportRaw(grpc::ClientContext *, - const proto::collector::trace::v1::ExportTraceServiceRequest &, - grpc::CompletionQueue *) override + grpc::ClientAsyncResponseReaderInterface< + proto::collector::trace::v1::ExportTraceServiceResponse> * + AsyncExportRaw(grpc::ClientContext *, + const proto::collector::trace::v1::ExportTraceServiceRequest &, + grpc::CompletionQueue *) override { return nullptr; } - grpc::ClientAsyncResponseReaderInterface - *PrepareAsyncExportRaw(grpc::ClientContext *, - const proto::collector::trace::v1::ExportTraceServiceRequest &, - grpc::CompletionQueue *) override + grpc::ClientAsyncResponseReaderInterface< + proto::collector::trace::v1::ExportTraceServiceResponse> * + PrepareAsyncExportRaw(grpc::ClientContext *, + const proto::collector::trace::v1::ExportTraceServiceRequest &, + grpc::CompletionQueue *) override { return nullptr; } + +private: + async_interface async_interface_; }; // OtlpGrpcExporterTestPeer is a friend class of OtlpGrpcExporter diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index bced58b833..233de4eef7 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -51,7 +51,8 @@ namespace class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceServiceStub { public: -# if (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 +# if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 using async_interface_base = proto::collector::trace::v1::TraceService::StubInterface::async_interface; # else @@ -78,7 +79,8 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe callback(stub_->last_async_status_); } -# if (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 || \ +# if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 || \ defined(GRPC_CALLBACK_API_NONEXPERIMENTAL) void Export( ::grpc::ClientContext * /*context*/, diff --git a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc index c47afb646e..c0c04c577a 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc @@ -47,7 +47,8 @@ namespace class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceServiceStub { public: -#if (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 +#if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 using async_interface_base = proto::collector::trace::v1::TraceService::StubInterface::async_interface; #else @@ -74,7 +75,8 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe callback(stub_->last_async_status_); } -#if (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 || \ +#if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 || \ defined(GRPC_CALLBACK_API_NONEXPERIMENTAL) void Export( ::grpc::ClientContext * /*context*/, @@ -108,7 +110,8 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe class OtlpMockLogsServiceStub : public proto::collector::logs::v1::MockLogsServiceStub { public: -#if (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 +#if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 using async_interface_base = proto::collector::logs::v1::LogsService::StubInterface::async_interface; #else @@ -135,7 +138,8 @@ class OtlpMockLogsServiceStub : public proto::collector::logs::v1::MockLogsServi callback(stub_->last_async_status_); } -#if (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 || \ +#if defined(GRPC_CPP_VERSION_MAJOR) && \ + (GRPC_CPP_VERSION_MAJOR * 1000 + GRPC_CPP_VERSION_MINOR) >= 1039 || \ defined(GRPC_CALLBACK_API_NONEXPERIMENTAL) void Export( ::grpc::ClientContext * /*context*/,