• 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   return reinterpret_cast<const grpc::CensusContext*>(context->census_context())
61       ->Span();
62 }
63 
64 // These measure definitions should be kept in sync across opencensus
65 // implementations--see
66 // https://github.com/census-instrumentation/opencensus-java/blob/master/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcMeasureConstants.java.
ClientMethodTagKey()67 ::opencensus::tags::TagKey ClientMethodTagKey() {
68   static const auto method_tag_key =
69       ::opencensus::tags::TagKey::Register("grpc_client_method");
70   return method_tag_key;
71 }
72 
ClientStatusTagKey()73 ::opencensus::tags::TagKey ClientStatusTagKey() {
74   static const auto status_tag_key =
75       ::opencensus::tags::TagKey::Register("grpc_client_status");
76   return status_tag_key;
77 }
78 
ServerMethodTagKey()79 ::opencensus::tags::TagKey ServerMethodTagKey() {
80   static const auto method_tag_key =
81       ::opencensus::tags::TagKey::Register("grpc_server_method");
82   return method_tag_key;
83 }
84 
ServerStatusTagKey()85 ::opencensus::tags::TagKey ServerStatusTagKey() {
86   static const auto status_tag_key =
87       ::opencensus::tags::TagKey::Register("grpc_server_status");
88   return status_tag_key;
89 }
90 
91 // Client
92 ABSL_CONST_INIT const absl::string_view
93     kRpcClientSentMessagesPerRpcMeasureName =
94         "grpc.io/client/sent_messages_per_rpc";
95 
96 ABSL_CONST_INIT const absl::string_view kRpcClientSentBytesPerRpcMeasureName =
97     "grpc.io/client/sent_bytes_per_rpc";
98 
99 ABSL_CONST_INIT const absl::string_view
100     kRpcClientReceivedMessagesPerRpcMeasureName =
101         "grpc.io/client/received_messages_per_rpc";
102 
103 ABSL_CONST_INIT const absl::string_view
104     kRpcClientReceivedBytesPerRpcMeasureName =
105         "grpc.io/client/received_bytes_per_rpc";
106 
107 ABSL_CONST_INIT const absl::string_view kRpcClientRoundtripLatencyMeasureName =
108     "grpc.io/client/roundtrip_latency";
109 
110 ABSL_CONST_INIT const absl::string_view kRpcClientServerLatencyMeasureName =
111     "grpc.io/client/server_latency";
112 
113 // Server
114 ABSL_CONST_INIT const absl::string_view
115     kRpcServerSentMessagesPerRpcMeasureName =
116         "grpc.io/server/sent_messages_per_rpc";
117 
118 ABSL_CONST_INIT const absl::string_view kRpcServerSentBytesPerRpcMeasureName =
119     "grpc.io/server/sent_bytes_per_rpc";
120 
121 ABSL_CONST_INIT const absl::string_view
122     kRpcServerReceivedMessagesPerRpcMeasureName =
123         "grpc.io/server/received_messages_per_rpc";
124 
125 ABSL_CONST_INIT const absl::string_view
126     kRpcServerReceivedBytesPerRpcMeasureName =
127         "grpc.io/server/received_bytes_per_rpc";
128 
129 ABSL_CONST_INIT const absl::string_view kRpcServerServerLatencyMeasureName =
130     "grpc.io/server/server_latency";
131 }  // namespace grpc
132