From b0a0e2ba5a4374b43f369de5f5ad44a592ea4a0c Mon Sep 17 00:00:00 2001 From: owent Date: Mon, 6 Mar 2023 11:21:08 +0800 Subject: [PATCH] Fix gRPC static build script Signed-off-by: owent --- .github/workflows/ci.yml | 16 ++++ bazel/repository.bzl | 5 +- ci/do_ci.sh | 14 ++++ ci/setup_grpc.sh | 49 +++++++---- exporters/otlp/BUILD | 1 + exporters/otlp/CMakeLists.txt | 11 ++- .../exporters/otlp/otlp_grpc_exporter.h | 1 + .../otlp_grpc_log_record_exporter_test.cc | 84 ++++++++++++++----- 8 files changed, 140 insertions(+), 41 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0c0a060044..67a82f3eb1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -256,6 +256,22 @@ jobs: sudo ./ci/setup_grpc.sh ./ci/do_ci.sh cmake.exporter.otprotocol.test + cmake_otprotocol_shared_libs_with_static_grpc_test: + name: CMake test (build shared libraries with otlp-exporter and static gRPC) + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + with: + submodules: 'recursive' + - name: setup + run: | + sudo ./ci/setup_cmake.sh + sudo ./ci/setup_ci_environment.sh + - name: run otlp exporter tests + run: | + sudo ./ci/setup_grpc.sh -T + ./ci/do_ci.sh cmake.exporter.otprotocol.shared_libs.with_static_grpc.test + plugin_test: name: Plugin -> CMake runs-on: ubuntu-latest diff --git a/bazel/repository.bzl b/bazel/repository.bzl index e03e48ae5a..847b8bb9d1 100644 --- a/bazel/repository.bzl +++ b/bazel/repository.bzl @@ -147,7 +147,10 @@ def opentelemetry_cpp_deps(): build_file = "@io_opentelemetry_cpp//bazel:curl.BUILD", sha256 = "ba98332752257b47b9dea6d8c0ad25ec1745c20424f1dd3ff2c99ab59e97cf91", strip_prefix = "curl-7.73.0", - urls = ["https://curl.haxx.se/download/curl-7.73.0.tar.gz"], + urls = [ + "https://curl.haxx.se/download/curl-7.73.0.tar.gz", + "https://github.com/curl/curl/releases/download/curl-7_73_0/curl-7.73.0.tar.gz", + ], ) # libthrift (optional) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index faa3fdaf88..2fbd8ed9c5 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -217,6 +217,20 @@ elif [[ "$1" == "cmake.exporter.otprotocol.test" ]]; then make -j $(nproc) cd exporters/otlp && make test exit 0 +elif [[ "$1" == "cmake.exporter.otprotocol.shared_libs.with_static_grpc.test" ]]; then + cd "${BUILD_DIR}" + rm -rf * + cmake -DCMAKE_BUILD_TYPE=Debug \ + -DWITH_OTLP=ON \ + -DWITH_OTLP_HTTP=ON \ + -DBUILD_SHARED_LIBS=ON \ + "${SRC_DIR}" + grpc_cpp_plugin=`which grpc_cpp_plugin` + proto_make_file="CMakeFiles/opentelemetry_proto.dir/build.make" + sed -i "s~gRPC_CPP_PLUGIN_EXECUTABLE-NOTFOUND~$grpc_cpp_plugin~" ${proto_make_file} #fixme + make -j $(nproc) + cd exporters/otlp && make test + exit 0 elif [[ "$1" == "cmake.exporter.otprotocol.with_async_export.test" ]]; then cd "${BUILD_DIR}" rm -rf * diff --git a/ci/setup_grpc.sh b/ci/setup_grpc.sh index d6e92d4ea6..e1002b6616 100755 --- a/ci/setup_grpc.sh +++ b/ci/setup_grpc.sh @@ -10,11 +10,11 @@ new_grpc_version='v1.49.2' gcc_version_for_new_grpc='5.1' std_version='14' install_grpc_version=${new_grpc_version} -grpc_version='v1.39.0' install_dir='/usr/local/' +build_shared_libs='' usage() { echo "Usage: $0 [-v ] [-i "] 1>&2; exit 1;} -while getopts ":v:i:r:s:" o; do +while getopts ":v:i:r:s:TH" o; do case "${o}" in v) gcc_version=${OPTARG} @@ -28,6 +28,12 @@ while getopts ":v:i:r:s:" o; do s) std_version=${OPTARG} ;; + T) + build_shared_libs="OFF" + ;; + H) + build_shared_libs="ON" + ;; *) usage ;; @@ -54,19 +60,34 @@ pushd grpc git submodule init git submodule update --depth 1 mkdir -p "third_party/abseil-cpp/build" && pushd "third_party/abseil-cpp/build" -cmake -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE \ - -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR .. -make -j${nproc} install && popd +set -x + +ABSEIL_CPP_BUILD_OPTIONS=( + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE + -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR +) +if [ ! -z "$build_shared_libs" ]; then + ABSEIL_CPP_BUILD_OPTIONS=(${ABSEIL_CPP_BUILD_OPTIONS[@]} "-DBUILD_SHARED_LIBS=$build_shared_libs") +fi +cmake ${ABSEIL_CPP_BUILD_OPTIONS[@]} .. +cmake --build . -j${nproc} --target install && popd mkdir -p build && pushd build -cmake -DgRPC_INSTALL=ON \ - -DCMAKE_CXX_STANDARD=${std_version} \ - -DgRPC_BUILD_TESTS=OFF \ - -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \ - -DCMAKE_PREFIX_PATH=$INSTALL_DIR \ - .. -make -j $(nproc) -make install + +GRPC_BUILD_OPTIONS=( + -DgRPC_INSTALL=ON + -DCMAKE_CXX_STANDARD=${std_version} + -DgRPC_BUILD_TESTS=OFF + -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR + -DCMAKE_PREFIX_PATH=$INSTALL_DIR +) +if [ ! -z "$build_shared_libs" ]; then + GRPC_BUILD_OPTIONS=(${GRPC_BUILD_OPTIONS[@]} "-DBUILD_SHARED_LIBS=$build_shared_libs") +fi + +cmake ${GRPC_BUILD_OPTIONS[@]} .. +cmake --build . -j$(nproc) +cmake --install . popd popd diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 13debc4d2d..01dc83f00b 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -409,6 +409,7 @@ cc_test( "test", ], deps = [ + ":otlp_grpc_exporter", ":otlp_grpc_log_record_exporter", "//api", "//sdk/src/logs", diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 7e654d3a30..fea7d2f9a5 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -247,9 +247,14 @@ if(BUILD_TESTING) add_executable(otlp_grpc_log_record_exporter_test test/otlp_grpc_log_record_exporter_test.cc) target_link_libraries( - otlp_grpc_log_record_exporter_test ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB} - opentelemetry_exporter_otlp_grpc_log opentelemetry_logs) + otlp_grpc_log_record_exporter_test + ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ${GMOCK_LIB} + opentelemetry_exporter_otlp_grpc + opentelemetry_exporter_otlp_grpc_log + opentelemetry_trace + opentelemetry_logs) gtest_add_tests( TARGET otlp_grpc_log_record_exporter_test TEST_PREFIX exporter.otlp. diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h index a28e6fca85..2868bf7211 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h @@ -67,6 +67,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter // For testing friend class OtlpGrpcExporterTestPeer; + friend class OtlpGrpcLogRecordExporterTestPeer; // Store service stub internally. Useful for testing. std::unique_ptr trace_service_stub_; 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 450bebc218..a0455adb70 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc @@ -5,11 +5,13 @@ # include +# include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" # include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h" # include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" # include "opentelemetry/proto/collector/logs/v1/logs_service_mock.grpc.pb.h" +# include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h" # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" @@ -18,6 +20,10 @@ # include "opentelemetry/sdk/logs/exporter.h" # include "opentelemetry/sdk/logs/logger_provider.h" # include "opentelemetry/sdk/resource/resource.h" +# include "opentelemetry/sdk/trace/exporter.h" +# include "opentelemetry/sdk/trace/simple_processor_factory.h" +# include "opentelemetry/sdk/trace/tracer_provider_factory.h" +# include "opentelemetry/trace/provider.h" # include @@ -45,6 +51,13 @@ class OtlpGrpcLogRecordExporterTestPeer : public ::testing::Test new OtlpGrpcLogRecordExporter(std::move(stub_interface))); } + std::unique_ptr GetExporter( + std::unique_ptr &stub_interface) + { + return std::unique_ptr( + new OtlpGrpcExporter(std::move(stub_interface))); + } + // Get the options associated with the given exporter. const OtlpGrpcExporterOptions &GetOptions(std::unique_ptr &exporter) { @@ -135,29 +148,54 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest) '3', '2', '1', '0'}; opentelemetry::trace::SpanId span_id{span_id_bin}; - const std::string schema_url{"https://opentelemetry.io/schemas/1.11.0"}; - auto logger = provider->GetLogger("test", "opentelelemtry_library", "", schema_url, true, - {{"scope_key1", "scope_value"}, {"scope_key2", 2}}); - std::unordered_map attributes; - attributes["service.name"] = "unit_test_service"; - attributes["tenant.id"] = "test_user"; - attributes["bool_value"] = true; - attributes["int32_value"] = static_cast(1); - attributes["uint32_value"] = static_cast(2); - attributes["int64_value"] = static_cast(0x1100000000LL); - attributes["uint64_value"] = static_cast(0x1200000000ULL); - attributes["double_value"] = static_cast(3.1); - attributes["vec_bool_value"] = attribute_storage_bool_value; - attributes["vec_int32_value"] = attribute_storage_int32_value; - attributes["vec_uint32_value"] = attribute_storage_uint32_value; - attributes["vec_int64_value"] = attribute_storage_int64_value; - attributes["vec_uint64_value"] = attribute_storage_uint64_value; - attributes["vec_double_value"] = attribute_storage_double_value; - attributes["vec_string_value"] = attribute_storage_string_value; - logger->EmitLogRecord( - opentelemetry::logs::Severity::kInfo, "Log message", attributes, trace_id, span_id, - opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, - std::chrono::system_clock::now()); + auto trace_mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); + std::unique_ptr trace_stub_interface( + trace_mock_stub); + + auto trace_provider = opentelemetry::nostd::shared_ptr( + opentelemetry::sdk::trace::TracerProviderFactory::Create( + opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create( + GetExporter(trace_stub_interface)))); + + // Trace and Logs should both receive datas when links static gRPC on ELF ABI. + EXPECT_CALL(*trace_mock_stub, Export(_, _, _)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(grpc::Status::OK)); + + { + const std::string schema_url{"https://opentelemetry.io/schemas/1.11.0"}; + + auto tracer = trace_provider->GetTracer("opentelelemtry_library", "", schema_url); + opentelemetry::trace::Provider::SetTracerProvider(std::move(trace_provider)); + auto trace_span = tracer->StartSpan("test_log"); + opentelemetry::trace::Scope trace_scope{trace_span}; + + auto logger = provider->GetLogger("test", "opentelelemtry_library", "", schema_url, true, + {{"scope_key1", "scope_value"}, {"scope_key2", 2}}); + std::unordered_map attributes; + attributes["service.name"] = "unit_test_service"; + attributes["tenant.id"] = "test_user"; + attributes["bool_value"] = true; + attributes["int32_value"] = static_cast(1); + attributes["uint32_value"] = static_cast(2); + attributes["int64_value"] = static_cast(0x1100000000LL); + attributes["uint64_value"] = static_cast(0x1200000000ULL); + attributes["double_value"] = static_cast(3.1); + attributes["vec_bool_value"] = attribute_storage_bool_value; + attributes["vec_int32_value"] = attribute_storage_int32_value; + attributes["vec_uint32_value"] = attribute_storage_uint32_value; + attributes["vec_int64_value"] = attribute_storage_int64_value; + attributes["vec_uint64_value"] = attribute_storage_uint64_value; + attributes["vec_double_value"] = attribute_storage_double_value; + attributes["vec_string_value"] = attribute_storage_string_value; + logger->EmitLogRecord(opentelemetry::logs::Severity::kInfo, "Log message", attributes, + trace_span->GetContext(), std::chrono::system_clock::now()); + } + + opentelemetry::trace::Provider::SetTracerProvider( + opentelemetry::nostd::shared_ptr( + new opentelemetry::trace::NoopTracerProvider())); + trace_provider = opentelemetry::nostd::shared_ptr(); } } // namespace otlp