diff --git a/BUILD b/BUILD index 0b6fff354f4..e158059b30f 100644 --- a/BUILD +++ b/BUILD @@ -2279,7 +2279,6 @@ grpc_cc_library( ], hdrs = [ "include/grpcpp/opencensus.h", - "include/grpcpp/opencensus_impl.h", "src/cpp/ext/filters/census/channel_filter.h", "src/cpp/ext/filters/census/client_filter.h", "src/cpp/ext/filters/census/context.h", diff --git a/include/grpcpp/opencensus.h b/include/grpcpp/opencensus.h index 3b170336834..29b221f7674 100644 --- a/include/grpcpp/opencensus.h +++ b/include/grpcpp/opencensus.h @@ -19,6 +19,30 @@ #ifndef GRPCPP_OPENCENSUS_H #define GRPCPP_OPENCENSUS_H -#include "grpcpp/opencensus_impl.h" +#include "opencensus/trace/span.h" + +namespace grpc { +// These symbols in this file will not be included in the binary unless +// grpc_opencensus_plugin build target was added as a dependency. At the moment +// it is only setup to be built with Bazel. + +// Registers the OpenCensus plugin with gRPC, so that it will be used for future +// RPCs. This must be called before any views are created. +void RegisterOpenCensusPlugin(); + +// RPC stats definitions, defined by +// https://github.com/census-instrumentation/opencensus-specs/blob/master/stats/gRPC.md + +// Registers the cumulative gRPC views so that they will be exported by any +// registered stats exporter. For on-task stats, construct a View using the +// ViewDescriptors below. +void RegisterOpenCensusViewsForExport(); + +class ServerContext; + +// Returns the tracing Span for the current RPC. +::opencensus::trace::Span GetSpanFromServerContext(ServerContext* context); + +} // namespace grpc #endif // GRPCPP_OPENCENSUS_H diff --git a/include/grpcpp/opencensus_impl.h b/include/grpcpp/opencensus_impl.h deleted file mode 100644 index 631d2b861fd..00000000000 --- a/include/grpcpp/opencensus_impl.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * - * Copyright 2019 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 GRPCPP_OPENCENSUS_IMPL_H -#define GRPCPP_OPENCENSUS_IMPL_H - -#include "opencensus/trace/span.h" - -namespace grpc { - -class ServerContext; -} -namespace grpc_impl { -// These symbols in this file will not be included in the binary unless -// grpc_opencensus_plugin build target was added as a dependency. At the moment -// it is only setup to be built with Bazel. - -// Registers the OpenCensus plugin with gRPC, so that it will be used for future -// RPCs. This must be called before any views are created. -void RegisterOpenCensusPlugin(); - -// RPC stats definitions, defined by -// https://github.com/census-instrumentation/opencensus-specs/blob/master/stats/gRPC.md - -// Registers the cumulative gRPC views so that they will be exported by any -// registered stats exporter. For on-task stats, construct a View using the -// ViewDescriptors below. -void RegisterOpenCensusViewsForExport(); - -// Returns the tracing Span for the current RPC. -::opencensus::trace::Span GetSpanFromServerContext( - grpc::ServerContext* context); - -} // namespace grpc_impl - -#endif // GRPCPP_OPENCENSUS_IMPL_H diff --git a/src/cpp/ext/filters/census/grpc_plugin.cc b/src/cpp/ext/filters/census/grpc_plugin.cc index c5018f0673a..f978ed3bf51 100644 --- a/src/cpp/ext/filters/census/grpc_plugin.cc +++ b/src/cpp/ext/filters/census/grpc_plugin.cc @@ -30,6 +30,35 @@ namespace grpc { +void RegisterOpenCensusPlugin() { + RegisterChannelFilter( + "opencensus_client", GRPC_CLIENT_CHANNEL, INT_MAX /* priority */, + nullptr /* condition function */); + RegisterChannelFilter( + "opencensus_server", GRPC_SERVER_CHANNEL, INT_MAX /* priority */, + nullptr /* condition function */); + + // Access measures to ensure they are initialized. Otherwise, creating a view + // before the first RPC would cause an error. + RpcClientSentBytesPerRpc(); + RpcClientReceivedBytesPerRpc(); + RpcClientRoundtripLatency(); + RpcClientServerLatency(); + RpcClientSentMessagesPerRpc(); + RpcClientReceivedMessagesPerRpc(); + + RpcServerSentBytesPerRpc(); + RpcServerReceivedBytesPerRpc(); + RpcServerServerLatency(); + RpcServerSentMessagesPerRpc(); + RpcServerReceivedMessagesPerRpc(); +} + +::opencensus::trace::Span GetSpanFromServerContext(ServerContext* context) { + return reinterpret_cast(context->census_context()) + ->Span(); +} + // These measure definitions should be kept in sync across opencensus // implementations--see // https://github.com/census-instrumentation/opencensus-java/blob/master/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcMeasureConstants.java. @@ -97,39 +126,5 @@ ABSL_CONST_INIT const absl::string_view ABSL_CONST_INIT const absl::string_view kRpcServerServerLatencyMeasureName = "grpc.io/server/server_latency"; -} // namespace grpc -namespace grpc_impl { -void RegisterOpenCensusPlugin() { - grpc::RegisterChannelFilter( - "opencensus_client", GRPC_CLIENT_CHANNEL, INT_MAX /* priority */, - nullptr /* condition function */); - grpc::RegisterChannelFilter( - "opencensus_server", GRPC_SERVER_CHANNEL, INT_MAX /* priority */, - nullptr /* condition function */); - - // Access measures to ensure they are initialized. Otherwise, creating a view - // before the first RPC would cause an error. - grpc::RpcClientSentBytesPerRpc(); - grpc::RpcClientReceivedBytesPerRpc(); - grpc::RpcClientRoundtripLatency(); - grpc::RpcClientServerLatency(); - grpc::RpcClientSentMessagesPerRpc(); - grpc::RpcClientReceivedMessagesPerRpc(); - - grpc::RpcServerSentBytesPerRpc(); - grpc::RpcServerReceivedBytesPerRpc(); - grpc::RpcServerServerLatency(); - grpc::RpcServerSentMessagesPerRpc(); - grpc::RpcServerReceivedMessagesPerRpc(); -} - -::opencensus::trace::Span GetSpanFromServerContext( - grpc::ServerContext* context) { - return reinterpret_cast(context->census_context()) - ->Span(); -} - -} // namespace grpc_impl +} // namespace grpc diff --git a/src/cpp/ext/filters/census/grpc_plugin.h b/src/cpp/ext/filters/census/grpc_plugin.h index 209fad139ce..9e319cb994e 100644 --- a/src/cpp/ext/filters/census/grpc_plugin.h +++ b/src/cpp/ext/filters/census/grpc_plugin.h @@ -22,14 +22,12 @@ #include #include "absl/strings/string_view.h" -#include "include/grpcpp/opencensus_impl.h" +#include "include/grpcpp/opencensus.h" #include "opencensus/stats/stats.h" -namespace grpc_impl { +namespace grpc { class ServerContext; -} -namespace grpc { // The tag keys set when recording RPC stats. ::opencensus::stats::TagKey ClientMethodTagKey(); diff --git a/src/cpp/ext/filters/census/views.cc b/src/cpp/ext/filters/census/views.cc index d7e3c81a955..2c0c5f72950 100644 --- a/src/cpp/ext/filters/census/views.cc +++ b/src/cpp/ext/filters/census/views.cc @@ -25,23 +25,6 @@ #include "opencensus/stats/internal/set_aggregation_window.h" #include "opencensus/stats/stats.h" -namespace grpc_impl { - -void RegisterOpenCensusViewsForExport() { - grpc::ClientSentMessagesPerRpcCumulative().RegisterForExport(); - grpc::ClientSentBytesPerRpcCumulative().RegisterForExport(); - grpc::ClientReceivedMessagesPerRpcCumulative().RegisterForExport(); - grpc::ClientReceivedBytesPerRpcCumulative().RegisterForExport(); - grpc::ClientRoundtripLatencyCumulative().RegisterForExport(); - grpc::ClientServerLatencyCumulative().RegisterForExport(); - - grpc::ServerSentMessagesPerRpcCumulative().RegisterForExport(); - grpc::ServerSentBytesPerRpcCumulative().RegisterForExport(); - grpc::ServerReceivedMessagesPerRpcCumulative().RegisterForExport(); - grpc::ServerReceivedBytesPerRpcCumulative().RegisterForExport(); - grpc::ServerServerLatencyCumulative().RegisterForExport(); -} -} // namespace grpc_impl namespace grpc { using ::opencensus::stats::Aggregation; @@ -88,6 +71,21 @@ ViewDescriptor HourDescriptor() { } // namespace +void RegisterOpenCensusViewsForExport() { + ClientSentMessagesPerRpcCumulative().RegisterForExport(); + ClientSentBytesPerRpcCumulative().RegisterForExport(); + ClientReceivedMessagesPerRpcCumulative().RegisterForExport(); + ClientReceivedBytesPerRpcCumulative().RegisterForExport(); + ClientRoundtripLatencyCumulative().RegisterForExport(); + ClientServerLatencyCumulative().RegisterForExport(); + + ServerSentMessagesPerRpcCumulative().RegisterForExport(); + ServerSentBytesPerRpcCumulative().RegisterForExport(); + ServerReceivedMessagesPerRpcCumulative().RegisterForExport(); + ServerReceivedBytesPerRpcCumulative().RegisterForExport(); + ServerServerLatencyCumulative().RegisterForExport(); +} + // client cumulative const ViewDescriptor& ClientSentBytesPerRpcCumulative() { const static ViewDescriptor descriptor = diff --git a/test/cpp/ext/filters/census/stats_plugin_end2end_test.cc b/test/cpp/ext/filters/census/stats_plugin_end2end_test.cc index ad788a2dd68..73394028309 100644 --- a/test/cpp/ext/filters/census/stats_plugin_end2end_test.cc +++ b/test/cpp/ext/filters/census/stats_plugin_end2end_test.cc @@ -58,7 +58,7 @@ class EchoServer final : public EchoTestService::Service { class StatsPluginEnd2EndTest : public ::testing::Test { protected: - static void SetUpTestCase() { grpc_impl::RegisterOpenCensusPlugin(); } + static void SetUpTestCase() { RegisterOpenCensusPlugin(); } void SetUp() { // Set up a synchronous server on a different thread to avoid the asynch diff --git a/test/cpp/microbenchmarks/bm_opencensus_plugin.cc b/test/cpp/microbenchmarks/bm_opencensus_plugin.cc index d23c4f0573f..9d42eb891df 100644 --- a/test/cpp/microbenchmarks/bm_opencensus_plugin.cc +++ b/test/cpp/microbenchmarks/bm_opencensus_plugin.cc @@ -29,9 +29,7 @@ #include "test/cpp/microbenchmarks/helpers.h" absl::once_flag once; -void RegisterOnce() { - absl::call_once(once, grpc_impl::RegisterOpenCensusPlugin); -} +void RegisterOnce() { absl::call_once(once, grpc::RegisterOpenCensusPlugin); } class EchoServer final : public grpc::testing::EchoTestService::Service { grpc::Status Echo(grpc::ServerContext* context, @@ -101,7 +99,7 @@ static void BM_E2eLatencyCensusEnabled(benchmark::State& state) { RegisterOnce(); // This we can safely repeat, and doing so clears accumulated data to avoid // initialization costs varying between runs. - grpc_impl::RegisterOpenCensusViewsForExport(); + grpc::RegisterOpenCensusViewsForExport(); EchoServerThread server; std::unique_ptr stub =