• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include "src/cpp/ext/filters/census/grpc_plugin.h"
20 
21 #include <grpc/support/port_platform.h>
22 #include <grpcpp/opencensus.h>
23 #include <grpcpp/server_context.h>
24 
25 #include <atomic>
26 
27 #include "absl/base/attributes.h"
28 #include "absl/strings/string_view.h"
29 #include "opencensus/tags/tag_key.h"
30 #include "opencensus/trace/span.h"
31 #include "src/core/config/core_configuration.h"
32 #include "src/core/ext/filters/logging/logging_filter.h"
33 #include "src/core/lib/surface/channel_stack_type.h"
34 #include "src/core/telemetry/call_tracer.h"
35 #include "src/cpp/ext/filters/census/client_filter.h"
36 #include "src/cpp/ext/filters/census/measures.h"
37 #include "src/cpp/ext/filters/census/server_call_tracer.h"
38 
39 namespace grpc {
40 
RegisterOpenCensusPlugin()41 void RegisterOpenCensusPlugin() {
42   grpc_core::ServerCallTracerFactory::RegisterGlobal(
43       new grpc::internal::OpenCensusServerCallTracerFactory);
44   grpc_core::CoreConfiguration::RegisterBuilder(
45       [](grpc_core::CoreConfiguration::Builder* builder) {
46         builder->channel_init()
47             ->RegisterFilter(GRPC_CLIENT_CHANNEL,
48                              &grpc::internal::OpenCensusClientFilter::kFilter)
49             .Before<grpc_core::ClientLoggingFilter>();
50       });
51 
52   // Access measures to ensure they are initialized. Otherwise, creating a view
53   // before the first RPC would cause an error.
54   RpcClientSentBytesPerRpc();
55   RpcClientReceivedBytesPerRpc();
56   RpcClientRoundtripLatency();
57   RpcClientServerLatency();
58   RpcClientStartedRpcs();
59   RpcClientSentMessagesPerRpc();
60   RpcClientReceivedMessagesPerRpc();
61   RpcClientRetriesPerCall();
62   RpcClientTransparentRetriesPerCall();
63   RpcClientRetryDelayPerCall();
64   RpcClientTransportLatency();
65   internal::RpcClientApiLatency();
66 
67   RpcServerSentBytesPerRpc();
68   RpcServerReceivedBytesPerRpc();
69   RpcServerServerLatency();
70   RpcServerStartedRpcs();
71   RpcServerSentMessagesPerRpc();
72   RpcServerReceivedMessagesPerRpc();
73 }
74 
GetSpanFromServerContext(grpc::ServerContext * context)75 ::opencensus::trace::Span GetSpanFromServerContext(
76     grpc::ServerContext* context) {
77   if (context == nullptr) return opencensus::trace::Span::BlankSpan();
78 
79   return reinterpret_cast<const grpc::experimental::CensusContext*>(
80              context->census_context())
81       ->Span();
82 }
83 
84 namespace experimental {
85 
86 // These measure definitions should be kept in sync across opencensus
87 // implementations--see
88 // https://github.com/census-instrumentation/opencensus-java/blob/master/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcMeasureConstants.java.
ClientMethodTagKey()89 ::opencensus::tags::TagKey ClientMethodTagKey() {
90   static const auto method_tag_key =
91       ::opencensus::tags::TagKey::Register("grpc_client_method");
92   return method_tag_key;
93 }
94 
ClientStatusTagKey()95 ::opencensus::tags::TagKey ClientStatusTagKey() {
96   static const auto status_tag_key =
97       ::opencensus::tags::TagKey::Register("grpc_client_status");
98   return status_tag_key;
99 }
100 
ServerMethodTagKey()101 ::opencensus::tags::TagKey ServerMethodTagKey() {
102   static const auto method_tag_key =
103       ::opencensus::tags::TagKey::Register("grpc_server_method");
104   return method_tag_key;
105 }
106 
ServerStatusTagKey()107 ::opencensus::tags::TagKey ServerStatusTagKey() {
108   static const auto status_tag_key =
109       ::opencensus::tags::TagKey::Register("grpc_server_status");
110   return status_tag_key;
111 }
112 
113 // Client
114 ABSL_CONST_INIT const absl::string_view
115     kRpcClientSentMessagesPerRpcMeasureName =
116         "grpc.io/client/sent_messages_per_rpc";
117 
118 ABSL_CONST_INIT const absl::string_view kRpcClientSentBytesPerRpcMeasureName =
119     "grpc.io/client/sent_bytes_per_rpc";
120 
121 ABSL_CONST_INIT const absl::string_view
122     kRpcClientReceivedMessagesPerRpcMeasureName =
123         "grpc.io/client/received_messages_per_rpc";
124 
125 ABSL_CONST_INIT const absl::string_view
126     kRpcClientReceivedBytesPerRpcMeasureName =
127         "grpc.io/client/received_bytes_per_rpc";
128 
129 ABSL_CONST_INIT const absl::string_view kRpcClientRoundtripLatencyMeasureName =
130     "grpc.io/client/roundtrip_latency";
131 
132 ABSL_CONST_INIT const absl::string_view kRpcClientServerLatencyMeasureName =
133     "grpc.io/client/server_latency";
134 
135 ABSL_CONST_INIT const absl::string_view kRpcClientStartedRpcsMeasureName =
136     "grpc.io/client/started_rpcs";
137 
138 ABSL_CONST_INIT const absl::string_view kRpcClientRetriesPerCallMeasureName =
139     "grpc.io/client/retries_per_call";
140 
141 ABSL_CONST_INIT const absl::string_view
142     kRpcClientTransparentRetriesPerCallMeasureName =
143         "grpc.io/client/transparent_retries_per_call";
144 
145 ABSL_CONST_INIT const absl::string_view kRpcClientRetryDelayPerCallMeasureName =
146     "grpc.io/client/retry_delay_per_call";
147 
148 ABSL_CONST_INIT const absl::string_view kRpcClientTransportLatencyMeasureName =
149     "grpc.io/client/transport_latency";
150 
151 // Server
152 ABSL_CONST_INIT const absl::string_view
153     kRpcServerSentMessagesPerRpcMeasureName =
154         "grpc.io/server/sent_messages_per_rpc";
155 
156 ABSL_CONST_INIT const absl::string_view kRpcServerSentBytesPerRpcMeasureName =
157     "grpc.io/server/sent_bytes_per_rpc";
158 
159 ABSL_CONST_INIT const absl::string_view
160     kRpcServerReceivedMessagesPerRpcMeasureName =
161         "grpc.io/server/received_messages_per_rpc";
162 
163 ABSL_CONST_INIT const absl::string_view
164     kRpcServerReceivedBytesPerRpcMeasureName =
165         "grpc.io/server/received_bytes_per_rpc";
166 
167 ABSL_CONST_INIT const absl::string_view kRpcServerServerLatencyMeasureName =
168     "grpc.io/server/server_latency";
169 
170 ABSL_CONST_INIT const absl::string_view kRpcServerStartedRpcsMeasureName =
171     "grpc.io/server/started_rpcs";
172 
173 }  // namespace experimental
174 
175 namespace internal {
176 
177 ABSL_CONST_INIT const absl::string_view kRpcClientApiLatencyMeasureName =
178     "grpc.io/client/api_latency";
179 namespace {
180 std::atomic<bool> g_open_census_stats_enabled(true);
181 std::atomic<bool> g_open_census_tracing_enabled(true);
182 }  // namespace
183 
184 //
185 // OpenCensusRegistry
186 //
187 
Get()188 OpenCensusRegistry& OpenCensusRegistry::Get() {
189   static OpenCensusRegistry* registry = new OpenCensusRegistry;
190   return *registry;
191 }
192 
PopulateTagMapWithConstantLabels(const::opencensus::tags::TagMap & tag_map)193 ::opencensus::tags::TagMap OpenCensusRegistry::PopulateTagMapWithConstantLabels(
194     const ::opencensus::tags::TagMap& tag_map) {
195   std::vector<std::pair<::opencensus::tags::TagKey, std::string>> tags =
196       tag_map.tags();
197   for (const auto& label : ConstantLabels()) {
198     tags.emplace_back(label.tag_key, label.value);
199   }
200   return ::opencensus::tags::TagMap(std::move(tags));
201 }
202 
PopulateCensusContextWithConstantAttributes(grpc::experimental::CensusContext * context)203 void OpenCensusRegistry::PopulateCensusContextWithConstantAttributes(
204     grpc::experimental::CensusContext* context) {
205   for (const auto& attribute : ConstantAttributes()) {
206     context->AddSpanAttribute(attribute.key, attribute.value);
207   }
208 }
209 
EnableOpenCensusStats(bool enable)210 void EnableOpenCensusStats(bool enable) {
211   g_open_census_stats_enabled = enable;
212 }
213 
EnableOpenCensusTracing(bool enable)214 void EnableOpenCensusTracing(bool enable) {
215   g_open_census_tracing_enabled = enable;
216 }
217 
OpenCensusStatsEnabled()218 bool OpenCensusStatsEnabled() {
219   return g_open_census_stats_enabled.load(std::memory_order_relaxed);
220 }
221 
OpenCensusTracingEnabled()222 bool OpenCensusTracingEnabled() {
223   return g_open_census_tracing_enabled.load(std::memory_order_relaxed);
224 }
225 
226 }  // namespace internal
227 
228 }  // namespace grpc
229