diff --git a/bazel/grpc_deps.bzl b/bazel/grpc_deps.bzl index 72d24c38664..3d5a94b5979 100644 --- a/bazel/grpc_deps.bzl +++ b/bazel/grpc_deps.bzl @@ -170,6 +170,16 @@ def grpc_deps(): actual = "@io_opencensus_cpp//opencensus/tags:context_util", ) + native.bind( + name = "opencensus-trace-stackdriver_exporter", + actual = "@io_opencensus_cpp//opencensus/exporters/trace/stackdriver:stackdriver_exporter", + ) + + native.bind( + name = "opencensus-stats-stackdriver_exporter", + actual = "@io_opencensus_cpp//opencensus/exporters/stats/stackdriver:stackdriver_exporter", + ) + native.bind( name = "libuv", actual = "@com_github_libuv_libuv//:libuv", diff --git a/src/cpp/ext/gcp/BUILD b/src/cpp/ext/gcp/BUILD new file mode 100644 index 00000000000..35a4c173768 --- /dev/null +++ b/src/cpp/ext/gcp/BUILD @@ -0,0 +1,53 @@ +# gRPC Bazel BUILD file. +# +# Copyright 2022 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:private"], + features = [ + "layering_check", + "-parse_headers", + ], +) + +# This is an EXPERIMENTAL target subject to change. +grpc_cc_library( + name = "observability", + srcs = [ + "observability.cc", + ], + hdrs = [ + "observability.h", + ], + external_deps = [ + "opencensus-trace", + "opencensus-trace-stackdriver_exporter", + "opencensus-stats-stackdriver_exporter", + ], + language = "c++", + tags = ["nofixdeps"], + visibility = ["//test:__subpackages__"], + deps = [ + "//:gpr", + "//:grpc_opencensus_plugin", + ], +) diff --git a/src/cpp/ext/gcp/observability.cc b/src/cpp/ext/gcp/observability.cc new file mode 100644 index 00000000000..2ccb8f64e97 --- /dev/null +++ b/src/cpp/ext/gcp/observability.cc @@ -0,0 +1,65 @@ +// +// Copyright 2022 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/gcp/observability.h" + +#include + +#include + +#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" +#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h" +#include "opencensus/trace/sampler.h" +#include "opencensus/trace/trace_config.h" + +#include + +namespace grpc { +namespace experimental { + +namespace { +// TODO(yashykt): These constants are currently derived from the example at +// https://cloud.google.com/traffic-director/docs/observability-proxyless#c++. +// We might want these to be configurable. +constexpr uint32_t kMaxAttributes = 128; +constexpr uint32_t kMaxAnnotations = 128; +constexpr uint32_t kMaxMessageEvents = 128; +constexpr uint32_t kMaxLinks = 128; +} // namespace + +void GcpObservabilityInit() { + // TODO(yashykt): Add code for gRPC config parsing + grpc::RegisterOpenCensusPlugin(); + grpc::RegisterOpenCensusViewsForExport(); + // TODO(yashykt): Setup tracing and stats exporting only if enabled in config. + // TODO(yashykt): Get probability from config + opencensus::trace::TraceConfig::SetCurrentTraceParams( + {kMaxAttributes, kMaxAnnotations, kMaxMessageEvents, kMaxLinks, + opencensus::trace::ProbabilitySampler(1.0)}); + opencensus::exporters::trace::StackdriverOptions trace_opts; + // TODO(yashykt): Set up project ID based on config + opencensus::exporters::trace::StackdriverExporter::Register( + std::move(trace_opts)); + opencensus::exporters::stats::StackdriverOptions stats_opts; + // TODO(yashykt): Set up project ID based on config + opencensus::exporters::stats::StackdriverExporter::Register( + std::move(stats_opts)); +} + +} // namespace experimental +} // namespace grpc diff --git a/src/cpp/ext/gcp/observability.h b/src/cpp/ext/gcp/observability.h new file mode 100644 index 00000000000..f5e401b875a --- /dev/null +++ b/src/cpp/ext/gcp/observability.h @@ -0,0 +1,33 @@ +// +// Copyright 2022 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_INTERNAL_CPP_EXT_GCP_OBSERVABILITY_GCP_OBSERVABILITY_H +#define GRPC_INTERNAL_CPP_EXT_GCP_OBSERVABILITY_GCP_OBSERVABILITY_H + +// TODO(yashykt): This file would have been in the top include/grpcpp directory +// instead of in src/, but I'm not yet sure about the naming, so keeping it here +// till we decide. + +namespace grpc { +namespace experimental { + +// Initialize GCP Observability for gRPC. +void GcpObservabilityInit(); + +} // namespace experimental +} // namespace grpc + +#endif // GRPC_INTERNAL_CPP_EXT_GCP_OBSERVABILITY_GCP_OBSERVABILITY_H diff --git a/test/cpp/ext/gcp/BUILD b/test/cpp/ext/gcp/BUILD new file mode 100644 index 00000000000..cfb3595baca --- /dev/null +++ b/test/cpp/ext/gcp/BUILD @@ -0,0 +1,34 @@ +# Copyright 2022 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/gcp") + +grpc_cc_test( + name = "observability_test", + srcs = [ + "observability_test.cc", + ], + external_deps = [ + "gtest", + ], + language = "C++", + deps = [ + "//src/cpp/ext/gcp:observability", + "//test/cpp/util:test_util", + ], +) diff --git a/test/cpp/ext/gcp/observability_test.cc b/test/cpp/ext/gcp/observability_test.cc new file mode 100644 index 00000000000..5c2cc387de0 --- /dev/null +++ b/test/cpp/ext/gcp/observability_test.cc @@ -0,0 +1,36 @@ +// +// Copyright 2022 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/gcp/observability.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "test/core/util/test_config.h" + +namespace { + +TEST(GcpObservabilityTest, RegistrationTest) { + grpc::experimental::GcpObservabilityInit(); +} + +} // namespace + +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 917517eea1f..9a30155ced3 100755 --- a/tools/buildgen/extract_metadata_from_bazel_xml.py +++ b/tools/buildgen/extract_metadata_from_bazel_xml.py @@ -655,7 +655,8 @@ def _exclude_unwanted_cc_tests(tests: List[str]) -> List[str]: tests = [ test for test in tests if not test.startswith('test/cpp/ext/filters/census:') and - not test.startswith('test/core/xds:xds_channel_stack_modifier_test') + not test.startswith('test/core/xds:xds_channel_stack_modifier_test') and + not test.startswith('test/cpp/ext/gcp:') ] # missing opencensus/stats/stats.h