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