diff --git a/BUILD b/BUILD index 8fe2d6d2e15..a62ca7e8ec2 100644 --- a/BUILD +++ b/BUILD @@ -2322,7 +2322,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 39bac6bc28c..c99e203e2e6 100644 --- a/include/grpcpp/opencensus.h +++ b/include/grpcpp/opencensus.h @@ -19,7 +19,31 @@ #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 namespace grpc { diff --git a/include/grpcpp/opencensus_impl.h b/include/grpcpp/opencensus_impl.h deleted file mode 100644 index dbcb7c9caeb..00000000000 --- a/include/grpcpp/opencensus_impl.h +++ /dev/null @@ -1,47 +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_impl { -class ServerContext; -// 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(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 63d6f1bde48..51b7264e10a 100644 --- a/src/cpp/ext/filters/census/grpc_plugin.cc +++ b/src/cpp/ext/filters/census/grpc_plugin.cc @@ -31,6 +31,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. @@ -98,39 +127,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/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 =