From 7e63a2f3820e8857572aed246f398ce2d8193049 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Wed, 2 Aug 2023 16:23:01 -0700 Subject: [PATCH] [GSM] Some initial structure (#33952) --- src/cpp/ext/gsm/BUILD | 52 +++++++++++++++ src/cpp/ext/gsm/gsm_observability.cc | 59 +++++++++++++++++ src/cpp/ext/gsm/gsm_observability.h | 64 +++++++++++++++++++ test/cpp/ext/gsm/BUILD | 41 ++++++++++++ test/cpp/ext/gsm/gsm_observability_test.cc | 43 +++++++++++++ .../extract_metadata_from_bazel_xml.py | 5 +- tools/distrib/fix_build_deps.py | 3 +- tools/distrib/iwyu.sh | 1 + 8 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 src/cpp/ext/gsm/BUILD create mode 100644 src/cpp/ext/gsm/gsm_observability.cc create mode 100644 src/cpp/ext/gsm/gsm_observability.h create mode 100644 test/cpp/ext/gsm/BUILD create mode 100644 test/cpp/ext/gsm/gsm_observability_test.cc diff --git a/src/cpp/ext/gsm/BUILD b/src/cpp/ext/gsm/BUILD new file mode 100644 index 0000000000000..658c7cebc88e4 --- /dev/null +++ b/src/cpp/ext/gsm/BUILD @@ -0,0 +1,52 @@ +# gRPC Bazel BUILD file. +# +# 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_library", +) + +licenses(["reciprocal"]) + +package( + default_visibility = ["//visibility:public"], + features = [ + "layering_check", + ], +) + +grpc_cc_library( + name = "gsm_observability", + srcs = [ + "gsm_observability.cc", + ], + hdrs = [ + "gsm_observability.h", + ], + external_deps = [ + "absl/container:flat_hash_set", + "absl/status", + "absl/status:statusor", + "absl/strings", + "otel/sdk/src/metrics", + ], + language = "c++", + visibility = ["//:__subpackages__"], + deps = [ + "//:gpr_platform", + "//src/cpp/ext/otel:otel_plugin", + ], +) diff --git a/src/cpp/ext/gsm/gsm_observability.cc b/src/cpp/ext/gsm/gsm_observability.cc new file mode 100644 index 0000000000000..370c2b446d6b2 --- /dev/null +++ b/src/cpp/ext/gsm/gsm_observability.cc @@ -0,0 +1,59 @@ +// +// +// 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 + +#include "src/cpp/ext/gsm/gsm_observability.h" + +#include "absl/status/status.h" + +#include "src/cpp/ext/otel/otel_plugin.h" + +namespace grpc { +namespace internal { + +// +// GsmCustomObservabilityBuilder +// + +GsmCustomObservabilityBuilder& GsmCustomObservabilityBuilder::SetMeterProvider( + std::shared_ptr + meter_provider) { + builder_.SetMeterProvider(meter_provider); + return *this; +} + +GsmCustomObservabilityBuilder& GsmCustomObservabilityBuilder::EnableMetrics( + const absl::flat_hash_set& metric_names) { + builder_.EnableMetrics(metric_names); + return *this; +} + +GsmCustomObservabilityBuilder& GsmCustomObservabilityBuilder::DisableMetrics( + const absl::flat_hash_set& metric_names) { + builder_.DisableMetrics(metric_names); + return *this; +} + +absl::StatusOr +GsmCustomObservabilityBuilder::BuildAndRegister() { + return absl::UnimplementedError("Not Implemented"); +} + +} // namespace internal +} // namespace grpc diff --git a/src/cpp/ext/gsm/gsm_observability.h b/src/cpp/ext/gsm/gsm_observability.h new file mode 100644 index 0000000000000..812ed2452b8e1 --- /dev/null +++ b/src/cpp/ext/gsm/gsm_observability.h @@ -0,0 +1,64 @@ +// +// +// 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. +// +// + +#ifndef GRPC_SRC_CPP_EXT_GSM_GSM_OBSERVABILITY_H +#define GRPC_SRC_CPP_EXT_GSM_GSM_OBSERVABILITY_H + +#include + +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "opentelemetry/sdk/metrics/meter_provider.h" + +#include "src/cpp/ext/otel/otel_plugin.h" + +namespace grpc { +namespace internal { + +class GsmObservability {}; + +class GsmCustomObservabilityBuilder { + public: + // TODO(yashykt): Should this take the SDK or the API MeterProvider? Benefit + // of SDK MeterProvider - Can explicitly set histogram bucket boundaries, but + // in the next iteration of the API, we would have it there as well. + GsmCustomObservabilityBuilder& SetMeterProvider( + std::shared_ptr + meter_provider); + // Enable metrics in \a metric_names + GsmCustomObservabilityBuilder& EnableMetrics( + const absl::flat_hash_set& metric_names); + // Disable metrics in \a metric_names + GsmCustomObservabilityBuilder& DisableMetrics( + const absl::flat_hash_set& metric_names); + // Builds the GsmObservability plugin. The return status shows whether + // GsmObservability was successfully enabled or not. TODO(): Is the + // GsmObservability object useful? + absl::StatusOr BuildAndRegister(); + + private: + OpenTelemetryPluginBuilder builder_; +}; + +} // namespace internal +} // namespace grpc + +#endif // GRPC_SRC_CPP_EXT_GSM_GSM_OBSERVABILITY_H diff --git a/test/cpp/ext/gsm/BUILD b/test/cpp/ext/gsm/BUILD new file mode 100644 index 0000000000000..fc96886fc5881 --- /dev/null +++ b/test/cpp/ext/gsm/BUILD @@ -0,0 +1,41 @@ +# 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/gsm", + visibility = "tests", +) + +grpc_cc_test( + name = "gsm_observability_test", + srcs = [ + "gsm_observability_test.cc", + ], + external_deps = [ + "gtest", + "otel/sdk/src/metrics", + ], + language = "C++", + tags = [ + ], + deps = [ + "//:grpc++", + "//src/cpp/ext/gsm:gsm_observability", + "//test/core/util:grpc_test_util", + ], +) diff --git a/test/cpp/ext/gsm/gsm_observability_test.cc b/test/cpp/ext/gsm/gsm_observability_test.cc new file mode 100644 index 0000000000000..2530149d0ded9 --- /dev/null +++ b/test/cpp/ext/gsm/gsm_observability_test.cc @@ -0,0 +1,43 @@ +// +// +// 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 "src/cpp/ext/gsm/gsm_observability.h" + +#include "gtest/gtest.h" + +#include "test/core/util/test_config.h" + +namespace grpc { +namespace testing { +namespace { + +TEST(GsmCustomObservabilityBuilderTest, Basic) { + EXPECT_EQ( + internal::GsmCustomObservabilityBuilder().BuildAndRegister().status(), + absl::UnimplementedError("Not Implemented")); +} + +} // 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 55b1dd467db50..348a81c2bbaaa 100755 --- a/tools/buildgen/extract_metadata_from_bazel_xml.py +++ b/tools/buildgen/extract_metadata_from_bazel_xml.py @@ -738,7 +738,10 @@ def _exclude_unwanted_cc_tests(tests: List[str]) -> List[str]: # we have not added otel dependency outside of bazel tests = [ - test for test in tests if not test.startswith("test/cpp/ext/otel:") + test + for test in tests + if not test.startswith("test/cpp/ext/otel:") + and not test.startswith("test/cpp/ext/gsm:") ] # missing opencensus/stats/stats.h diff --git a/tools/distrib/fix_build_deps.py b/tools/distrib/fix_build_deps.py index 5350fade9fb90..7511ca232fd4c 100755 --- a/tools/distrib/fix_build_deps.py +++ b/tools/distrib/fix_build_deps.py @@ -100,7 +100,7 @@ "opentelemetry/metrics/sync_instruments.h": "otel/api", "opentelemetry/nostd/shared_ptr.h": "otel/api", "opentelemetry/nostd/unique_ptr.h": "otel/api", - "sdk/include/opentelemetry/sdk/metrics/meter_provider.h": "otel/sdk/src/metrics", + "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 +379,7 @@ def score_best(proposed, existing): "", "src/core", "src/cpp/ext/gcp", + "src/cpp/ext/gsm", "src/cpp/ext/otel", "test/core/backoff", "test/core/experiments", diff --git a/tools/distrib/iwyu.sh b/tools/distrib/iwyu.sh index 11ade96b7520e..bf7e65cfb6c1e 100755 --- a/tools/distrib/iwyu.sh +++ b/tools/distrib/iwyu.sh @@ -32,6 +32,7 @@ tools/distrib/gen_compilation_database.py \ --dedup_targets \ "//:*" \ "//src/core/..." \ + "//src/cpp/ext/gsm/..." \ "//src/cpp/ext/otel/..." \ "//src/compiler/..." \ "//test/core/..." \