From 875b7fdcff4c4ec3facff057af3307bca6cb4bb7 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Tue, 27 Jun 2023 00:47:01 -0700 Subject: [PATCH] [otel] Add bazel dependency (#33548) Add bazel dependency on opentelemetry-cpp. --- bazel/grpc_build_system.bzl | 2 + bazel/grpc_deps.bzl | 11 +++++ test/cpp/ext/filters/otel/BUILD | 38 +++++++++++++++ test/cpp/ext/filters/otel/otel_plugin_test.cc | 46 +++++++++++++++++++ .../extract_metadata_from_bazel_xml.py | 7 +++ tools/distrib/fix_build_deps.py | 3 ++ .../run_tests/sanity/check_bazel_workspace.py | 2 + 7 files changed, 109 insertions(+) create mode 100644 test/cpp/ext/filters/otel/BUILD create mode 100644 test/cpp/ext/filters/otel/otel_plugin_test.cc diff --git a/bazel/grpc_build_system.bzl b/bazel/grpc_build_system.bzl index 670bdf87918..1d64ca72f31 100644 --- a/bazel/grpc_build_system.bzl +++ b/bazel/grpc_build_system.bzl @@ -72,6 +72,8 @@ def _get_external_deps(external_deps): ret.append("@com_google_absl//" + dep) elif dep.startswith("google/"): ret.append("@com_google_googleapis//" + dep) + elif dep.startswith("otel/"): + ret.append(dep.replace("otel/", "@io_opentelemetry_cpp//")) else: ret.append("//external:" + dep) return ret diff --git a/bazel/grpc_deps.bzl b/bazel/grpc_deps.bzl index c8f1975bf4f..d6a94a4fbf7 100644 --- a/bazel/grpc_deps.bzl +++ b/bazel/grpc_deps.bzl @@ -510,6 +510,17 @@ def grpc_deps(): ], ) + if "io_opentelemetry_cpp" not in native.existing_rules(): + http_archive( + name = "io_opentelemetry_cpp", + sha256 = "668de24f81c8d36d75092ad9dcb02a97cd41473adbe72485ece05e336db48249", + strip_prefix = "opentelemetry-cpp-1.9.1", + urls = [ + "https://storage.googleapis.com/grpc-bazel-mirror/github.com/open-telemetry/opentelemetry-cpp/archive/refs/tags/v1.9.1.tar.gz", + "https://github.com/open-telemetry/opentelemetry-cpp/archive/refs/tags/v1.9.1.tar.gz", + ], + ) + grpc_python_deps() # TODO: move some dependencies from "grpc_deps" here? diff --git a/test/cpp/ext/filters/otel/BUILD b/test/cpp/ext/filters/otel/BUILD new file mode 100644 index 00000000000..77b0d65ef9c --- /dev/null +++ b/test/cpp/ext/filters/otel/BUILD @@ -0,0 +1,38 @@ +# Copyright 2023 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("//bazel:grpc_build_system.bzl", "grpc_cc_test", "grpc_package") + +licenses(["notice"]) + +grpc_package( + name = "test/cpp/ext/filters/otel", + visibility = "tests", +) + +grpc_cc_test( + name = "otel_plugin_test", + srcs = [ + "otel_plugin_test.cc", + ], + external_deps = [ + "gtest", + "otel/api", + "otel/sdk/src/metrics", + ], + language = "C++", + tags = [ + ], + deps = ["//test/core/util:grpc_test_util"], +) diff --git a/test/cpp/ext/filters/otel/otel_plugin_test.cc b/test/cpp/ext/filters/otel/otel_plugin_test.cc new file mode 100644 index 00000000000..a3fedfaa0b1 --- /dev/null +++ b/test/cpp/ext/filters/otel/otel_plugin_test.cc @@ -0,0 +1,46 @@ +// +// +// Copyright 2023 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// + +#include "api/include/opentelemetry/metrics/provider.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "sdk/include/opentelemetry/sdk/metrics/meter_provider.h" + +#include "test/core/util/test_config.h" + +namespace grpc { +namespace testing { +namespace { + +TEST(OTelPluginTest, ApiDependency) { + opentelemetry::metrics::Provider::GetMeterProvider(); +} + +TEST(OTelPluginTest, SdkDependency) { + opentelemetry::sdk::metrics::MeterProvider(); +} + +} // namespace +} // namespace testing +} // namespace grpc + +int main(int argc, char** argv) { + grpc::testing::TestEnvironment env(&argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tools/buildgen/extract_metadata_from_bazel_xml.py b/tools/buildgen/extract_metadata_from_bazel_xml.py index 044eddf7651..1aabe6c753c 100755 --- a/tools/buildgen/extract_metadata_from_bazel_xml.py +++ b/tools/buildgen/extract_metadata_from_bazel_xml.py @@ -736,6 +736,13 @@ def _exclude_unwanted_cc_tests(tests: List[str]) -> List[str]: and not test.startswith("test/cpp/interop:observability_interop") ] + # we have not added otel dependency outside of bazel + tests = [ + test + for test in tests + if not test.startswith("test/cpp/ext/filters/otel:") + ] + # missing opencensus/stats/stats.h tests = [ test diff --git a/tools/distrib/fix_build_deps.py b/tools/distrib/fix_build_deps.py index 6ad3684fddd..5ec3d460335 100755 --- a/tools/distrib/fix_build_deps.py +++ b/tools/distrib/fix_build_deps.py @@ -93,6 +93,8 @@ EXTERNAL_DEPS = { "absl/types/variant.h": "absl/types:variant", "absl/utility/utility.h": "absl/utility", "address_sorting/address_sorting.h": "address_sorting", + "api/include/opentelemetry/metrics/provider.h": "otel/api", + "sdk/include/opentelemetry/sdk/metrics/meter_provider.h": "otel/sdk/src/metrics", "ares.h": "cares", "fuzztest/fuzztest.h": ["fuzztest", "fuzztest_main"], "google/api/monitored_resource.pb.h": ( @@ -379,6 +381,7 @@ for dirname in [ "test/core/promise", "test/core/resource_quota", "test/core/transport/chaotic_good", + "test/cpp/ext/filters/otel", "fuzztest", "fuzztest/core/channel", ]: diff --git a/tools/run_tests/sanity/check_bazel_workspace.py b/tools/run_tests/sanity/check_bazel_workspace.py index 73b9315aa6f..8d37a4bb92d 100755 --- a/tools/run_tests/sanity/check_bazel_workspace.py +++ b/tools/run_tests/sanity/check_bazel_workspace.py @@ -58,6 +58,7 @@ _GRPC_DEP_NAMES = [ "com_google_absl", "com_google_fuzztest", "io_opencensus_cpp", + "io_opentelemetry_cpp", "envoy_api", _BAZEL_SKYLIB_DEP_NAME, _BAZEL_TOOLCHAINS_DEP_NAME, @@ -86,6 +87,7 @@ _GRPC_BAZEL_ONLY_DEPS = [ "com_google_absl", "com_google_fuzztest", "io_opencensus_cpp", + "io_opentelemetry_cpp", _BAZEL_SKYLIB_DEP_NAME, _BAZEL_TOOLCHAINS_DEP_NAME, _BAZEL_COMPDB_DEP_NAME,