• 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 <grpc/support/port_platform.h>
20 
21 #include "src/cpp/ext/filters/census/grpc_plugin.h"
22 
23 #include <grpcpp/server_context.h>
24 
25 #include "opencensus/tags/tag_key.h"
26 #include "opencensus/trace/span.h"
27 #include "src/cpp/ext/filters/census/channel_filter.h"
28 #include "src/cpp/ext/filters/census/client_filter.h"
29 #include "src/cpp/ext/filters/census/measures.h"
30 #include "src/cpp/ext/filters/census/server_filter.h"
31 
32 namespace grpc {
33 
RegisterOpenCensusPlugin()34 void RegisterOpenCensusPlugin() {
35   RegisterChannelFilter<CensusChannelData, CensusClientCallData>(
36       "opencensus_client", GRPC_CLIENT_CHANNEL, INT_MAX /* priority */,
37       nullptr /* condition function */);
38   RegisterChannelFilter<CensusChannelData, CensusServerCallData>(
39       "opencensus_server", GRPC_SERVER_CHANNEL, INT_MAX /* priority */,
40       nullptr /* condition function */);
41 
42   // Access measures to ensure they are initialized. Otherwise, creating a view
43   // before the first RPC would cause an error.
44   RpcClientSentBytesPerRpc();
45   RpcClientReceivedBytesPerRpc();
46   RpcClientRoundtripLatency();
47   RpcClientServerLatency();
48   RpcClientSentMessagesPerRpc();
49   RpcClientReceivedMessagesPerRpc();
50 
51   RpcServerSentBytesPerRpc();
52   RpcServerReceivedBytesPerRpc();
53   RpcServerServerLatency();
54   RpcServerSentMessagesPerRpc();
55   RpcServerReceivedMessagesPerRpc();
56 }
57 
GetSpanFromServerContext(grpc::ServerContext * context)58 ::opencensus::trace::Span GetSpanFromServerContext(
59     grpc::ServerContext* context) {
60   if (context == nullptr) return opencensus::trace::Span::BlankSpan();
61 
62   return reinterpret_cast<const grpc::CensusContext*>(context->census_context())
63       ->Span();
64 }
65 
66 // These measure definitions should be kept in sync across opencensus
67 // implementations--see
68 // https://github.com/census-instrumentation/opencensus-java/blob/master/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcMeasureConstants.java.
ClientMethodTagKey()69 ::opencensus::tags::TagKey ClientMethodTagKey() {
70   static const auto method_tag_key =
71       ::opencensus::tags::TagKey::Register("grpc_client_method");
72   return method_tag_key;
73 }
74 
ClientStatusTagKey()75 ::opencensus::tags::TagKey ClientStatusTagKey() {
76   static const auto status_tag_key =
77       ::opencensus::tags::TagKey::Register("grpc_client_status");
78   return status_tag_key;
79 }
80 
ServerMethodTagKey()81 ::opencensus::tags::TagKey ServerMethodTagKey() {
82   static const auto method_tag_key =
83       ::opencensus::tags::TagKey::Register("grpc_server_method");
84   return method_tag_key;
85 }
86 
ServerStatusTagKey()87 ::opencensus::tags::TagKey ServerStatusTagKey() {
88   static const auto status_tag_key =
89       ::opencensus::tags::TagKey::Register("grpc_server_status");
90   return status_tag_key;
91 }
92 
93 // Client
94 ABSL_CONST_INIT const absl::string_view
95     kRpcClientSentMessagesPerRpcMeasureName =
96         "grpc.io/client/sent_messages_per_rpc";
97 
98 ABSL_CONST_INIT const absl::string_view kRpcClientSentBytesPerRpcMeasureName =
99     "grpc.io/client/sent_bytes_per_rpc";
100 
101 ABSL_CONST_INIT const absl::string_view
102     kRpcClientReceivedMessagesPerRpcMeasureName =
103         "grpc.io/client/received_messages_per_rpc";
104 
105 ABSL_CONST_INIT const absl::string_view
106     kRpcClientReceivedBytesPerRpcMeasureName =
107         "grpc.io/client/received_bytes_per_rpc";
108 
109 ABSL_CONST_INIT const absl::string_view kRpcClientRoundtripLatencyMeasureName =
110     "grpc.io/client/roundtrip_latency";
111 
112 ABSL_CONST_INIT const absl::string_view kRpcClientServerLatencyMeasureName =
113     "grpc.io/client/server_latency";
114 
115 // Server
116 ABSL_CONST_INIT const absl::string_view
117     kRpcServerSentMessagesPerRpcMeasureName =
118         "grpc.io/server/sent_messages_per_rpc";
119 
120 ABSL_CONST_INIT const absl::string_view kRpcServerSentBytesPerRpcMeasureName =
121     "grpc.io/server/sent_bytes_per_rpc";
122 
123 ABSL_CONST_INIT const absl::string_view
124     kRpcServerReceivedMessagesPerRpcMeasureName =
125         "grpc.io/server/received_messages_per_rpc";
126 
127 ABSL_CONST_INIT const absl::string_view
128     kRpcServerReceivedBytesPerRpcMeasureName =
129         "grpc.io/server/received_bytes_per_rpc";
130 
131 ABSL_CONST_INIT const absl::string_view kRpcServerServerLatencyMeasureName =
132     "grpc.io/server/server_latency";
133 }  // namespace grpc
134