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