3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/cpp/ext/filters/census/grpc_plugin.h"
23 #include <grpcpp/server_context.h>
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"
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 */);
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();
54 RpcServerSentBytesPerRpc();
55 RpcServerReceivedBytesPerRpc();
56 RpcServerServerLatency();
57 RpcServerSentMessagesPerRpc();
58 RpcServerReceivedMessagesPerRpc();
61 ::opencensus::trace::Span GetSpanFromServerContext(
62 grpc::ServerContext* context) {
63 if (context == nullptr) return opencensus::trace::Span::BlankSpan();
65 return reinterpret_cast<const grpc::CensusContext*>(context->census_context())
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;
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;
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;
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;
97 ABSL_CONST_INIT const absl::string_view
98 kRpcClientSentMessagesPerRpcMeasureName =
99 "grpc.io/client/sent_messages_per_rpc";
101 ABSL_CONST_INIT const absl::string_view kRpcClientSentBytesPerRpcMeasureName =
102 "grpc.io/client/sent_bytes_per_rpc";
104 ABSL_CONST_INIT const absl::string_view
105 kRpcClientReceivedMessagesPerRpcMeasureName =
106 "grpc.io/client/received_messages_per_rpc";
108 ABSL_CONST_INIT const absl::string_view
109 kRpcClientReceivedBytesPerRpcMeasureName =
110 "grpc.io/client/received_bytes_per_rpc";
112 ABSL_CONST_INIT const absl::string_view kRpcClientRoundtripLatencyMeasureName =
113 "grpc.io/client/roundtrip_latency";
115 ABSL_CONST_INIT const absl::string_view kRpcClientServerLatencyMeasureName =
116 "grpc.io/client/server_latency";
118 ABSL_CONST_INIT const absl::string_view kRpcClientRetriesPerCallMeasureName =
119 "grpc.io/client/retries_per_call";
121 ABSL_CONST_INIT const absl::string_view
122 kRpcClientTransparentRetriesPerCallMeasureName =
123 "grpc.io/client/transparent_retries_per_call";
125 ABSL_CONST_INIT const absl::string_view kRpcClientRetryDelayPerCallMeasureName =
126 "grpc.io/client/retry_delay_per_call";
129 ABSL_CONST_INIT const absl::string_view
130 kRpcServerSentMessagesPerRpcMeasureName =
131 "grpc.io/server/sent_messages_per_rpc";
133 ABSL_CONST_INIT const absl::string_view kRpcServerSentBytesPerRpcMeasureName =
134 "grpc.io/server/sent_bytes_per_rpc";
136 ABSL_CONST_INIT const absl::string_view
137 kRpcServerReceivedMessagesPerRpcMeasureName =
138 "grpc.io/server/received_messages_per_rpc";
140 ABSL_CONST_INIT const absl::string_view
141 kRpcServerReceivedBytesPerRpcMeasureName =
142 "grpc.io/server/received_bytes_per_rpc";
144 ABSL_CONST_INIT const absl::string_view kRpcServerServerLatencyMeasureName =
145 "grpc.io/server/server_latency";