f16eb526ad218c1d3c2d0012a4629c48fdb3ecc8
[platform/upstream/grpc.git] / src / cpp / ext / filters / census / grpc_plugin.cc
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
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   RpcClientRetriesPerCall();
51   RpcClientTransparentRetriesPerCall();
52   RpcClientRetryDelayPerCall();
53
54   RpcServerSentBytesPerRpc();
55   RpcServerReceivedBytesPerRpc();
56   RpcServerServerLatency();
57   RpcServerSentMessagesPerRpc();
58   RpcServerReceivedMessagesPerRpc();
59 }
60
61 ::opencensus::trace::Span GetSpanFromServerContext(
62     grpc::ServerContext* context) {
63   if (context == nullptr) return opencensus::trace::Span::BlankSpan();
64
65   return reinterpret_cast<const grpc::CensusContext*>(context->census_context())
66       ->Span();
67 }
68
69 // These measure definitions should be kept in sync across opencensus
70 // implementations--see
71 // https://github.com/census-instrumentation/opencensus-java/blob/master/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcMeasureConstants.java.
72 ::opencensus::tags::TagKey ClientMethodTagKey() {
73   static const auto method_tag_key =
74       ::opencensus::tags::TagKey::Register("grpc_client_method");
75   return method_tag_key;
76 }
77
78 ::opencensus::tags::TagKey ClientStatusTagKey() {
79   static const auto status_tag_key =
80       ::opencensus::tags::TagKey::Register("grpc_client_status");
81   return status_tag_key;
82 }
83
84 ::opencensus::tags::TagKey ServerMethodTagKey() {
85   static const auto method_tag_key =
86       ::opencensus::tags::TagKey::Register("grpc_server_method");
87   return method_tag_key;
88 }
89
90 ::opencensus::tags::TagKey ServerStatusTagKey() {
91   static const auto status_tag_key =
92       ::opencensus::tags::TagKey::Register("grpc_server_status");
93   return status_tag_key;
94 }
95
96 // Client
97 ABSL_CONST_INIT const absl::string_view
98     kRpcClientSentMessagesPerRpcMeasureName =
99         "grpc.io/client/sent_messages_per_rpc";
100
101 ABSL_CONST_INIT const absl::string_view kRpcClientSentBytesPerRpcMeasureName =
102     "grpc.io/client/sent_bytes_per_rpc";
103
104 ABSL_CONST_INIT const absl::string_view
105     kRpcClientReceivedMessagesPerRpcMeasureName =
106         "grpc.io/client/received_messages_per_rpc";
107
108 ABSL_CONST_INIT const absl::string_view
109     kRpcClientReceivedBytesPerRpcMeasureName =
110         "grpc.io/client/received_bytes_per_rpc";
111
112 ABSL_CONST_INIT const absl::string_view kRpcClientRoundtripLatencyMeasureName =
113     "grpc.io/client/roundtrip_latency";
114
115 ABSL_CONST_INIT const absl::string_view kRpcClientServerLatencyMeasureName =
116     "grpc.io/client/server_latency";
117
118 ABSL_CONST_INIT const absl::string_view kRpcClientRetriesPerCallMeasureName =
119     "grpc.io/client/retries_per_call";
120
121 ABSL_CONST_INIT const absl::string_view
122     kRpcClientTransparentRetriesPerCallMeasureName =
123         "grpc.io/client/transparent_retries_per_call";
124
125 ABSL_CONST_INIT const absl::string_view kRpcClientRetryDelayPerCallMeasureName =
126     "grpc.io/client/retry_delay_per_call";
127
128 // Server
129 ABSL_CONST_INIT const absl::string_view
130     kRpcServerSentMessagesPerRpcMeasureName =
131         "grpc.io/server/sent_messages_per_rpc";
132
133 ABSL_CONST_INIT const absl::string_view kRpcServerSentBytesPerRpcMeasureName =
134     "grpc.io/server/sent_bytes_per_rpc";
135
136 ABSL_CONST_INIT const absl::string_view
137     kRpcServerReceivedMessagesPerRpcMeasureName =
138         "grpc.io/server/received_messages_per_rpc";
139
140 ABSL_CONST_INIT const absl::string_view
141     kRpcServerReceivedBytesPerRpcMeasureName =
142         "grpc.io/server/received_bytes_per_rpc";
143
144 ABSL_CONST_INIT const absl::string_view kRpcServerServerLatencyMeasureName =
145     "grpc.io/server/server_latency";
146 }  // namespace grpc