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 #ifndef GRPC_SRC_CPP_EXT_FILTERS_CENSUS_GRPC_PLUGIN_H 20 #define GRPC_SRC_CPP_EXT_FILTERS_CENSUS_GRPC_PLUGIN_H 21 22 #include <grpc/support/port_platform.h> 23 #include <grpcpp/opencensus.h> 24 25 #include <algorithm> 26 #include <map> 27 #include <string> 28 #include <utility> 29 #include <vector> 30 31 #include "absl/strings/string_view.h" 32 #include "opencensus/stats/stats.h" 33 #include "opencensus/tags/tag_key.h" 34 #include "opencensus/tags/tag_map.h" 35 36 namespace grpc { 37 38 // The following using declarations have been added to prevent breaking users 39 // that were directly using this header file. 40 using experimental::ClientMethodTagKey; // NOLINT 41 using experimental::ClientStatusTagKey; // NOLINT 42 using experimental::ServerMethodTagKey; // NOLINT 43 using experimental::ServerStatusTagKey; // NOLINT 44 45 using experimental::kRpcClientReceivedBytesPerRpcMeasureName; // NOLINT 46 using experimental::kRpcClientReceivedMessagesPerRpcMeasureName; // NOLINT 47 using experimental::kRpcClientRetriesPerCallMeasureName; // NOLINT 48 using experimental::kRpcClientRetryDelayPerCallMeasureName; // NOLINT 49 using experimental::kRpcClientRoundtripLatencyMeasureName; // NOLINT 50 using experimental::kRpcClientSentBytesPerRpcMeasureName; // NOLINT 51 using experimental::kRpcClientSentMessagesPerRpcMeasureName; // NOLINT 52 using experimental::kRpcClientServerLatencyMeasureName; // NOLINT 53 using experimental::kRpcClientStartedRpcsMeasureName; // NOLINT 54 using experimental::kRpcClientTransparentRetriesPerCallMeasureName; // NOLINT 55 56 using experimental::kRpcServerReceivedBytesPerRpcMeasureName; // NOLINT 57 using experimental::kRpcServerReceivedMessagesPerRpcMeasureName; // NOLINT 58 using experimental::kRpcServerSentBytesPerRpcMeasureName; // NOLINT 59 using experimental::kRpcServerSentMessagesPerRpcMeasureName; // NOLINT 60 using experimental::kRpcServerServerLatencyMeasureName; // NOLINT 61 using experimental::kRpcServerStartedRpcsMeasureName; // NOLINT 62 63 using experimental::ClientCompletedRpcsCumulative; // NOLINT 64 using experimental::ClientReceivedBytesPerRpcCumulative; // NOLINT 65 using experimental::ClientReceivedMessagesPerRpcCumulative; // NOLINT 66 using experimental::ClientRetriesCumulative; // NOLINT 67 using experimental::ClientRetriesPerCallCumulative; // NOLINT 68 using experimental::ClientRetryDelayPerCallCumulative; // NOLINT 69 using experimental::ClientRoundtripLatencyCumulative; // NOLINT 70 using experimental::ClientSentBytesPerRpcCumulative; // NOLINT 71 using experimental::ClientSentMessagesPerRpcCumulative; // NOLINT 72 using experimental::ClientServerLatencyCumulative; // NOLINT 73 using experimental::ClientStartedRpcsCumulative; // NOLINT 74 using experimental::ClientTransparentRetriesCumulative; // NOLINT 75 using experimental::ClientTransparentRetriesPerCallCumulative; // NOLINT 76 77 using experimental::ServerCompletedRpcsCumulative; // NOLINT 78 using experimental::ServerReceivedBytesPerRpcCumulative; // NOLINT 79 using experimental::ServerReceivedMessagesPerRpcCumulative; // NOLINT 80 using experimental::ServerSentBytesPerRpcCumulative; // NOLINT 81 using experimental::ServerSentMessagesPerRpcCumulative; // NOLINT 82 using experimental::ServerServerLatencyCumulative; // NOLINT 83 using experimental::ServerStartedRpcsCumulative; // NOLINT 84 85 using experimental::ClientCompletedRpcsMinute; // NOLINT 86 using experimental::ClientReceivedBytesPerRpcMinute; // NOLINT 87 using experimental::ClientReceivedMessagesPerRpcMinute; // NOLINT 88 using experimental::ClientRetriesMinute; // NOLINT 89 using experimental::ClientRetriesPerCallMinute; // NOLINT 90 using experimental::ClientRetryDelayPerCallMinute; // NOLINT 91 using experimental::ClientRoundtripLatencyMinute; // NOLINT 92 using experimental::ClientSentBytesPerRpcMinute; // NOLINT 93 using experimental::ClientSentMessagesPerRpcMinute; // NOLINT 94 using experimental::ClientServerLatencyMinute; // NOLINT 95 using experimental::ClientStartedRpcsMinute; // NOLINT 96 using experimental::ClientTransparentRetriesMinute; // NOLINT 97 using experimental::ClientTransparentRetriesPerCallMinute; // NOLINT 98 99 using experimental::ServerCompletedRpcsMinute; // NOLINT 100 using experimental::ServerReceivedBytesPerRpcMinute; // NOLINT 101 using experimental::ServerReceivedMessagesPerRpcMinute; // NOLINT 102 using experimental::ServerSentBytesPerRpcMinute; // NOLINT 103 using experimental::ServerSentMessagesPerRpcMinute; // NOLINT 104 using experimental::ServerServerLatencyMinute; // NOLINT 105 using experimental::ServerStartedRpcsMinute; // NOLINT 106 107 using experimental::ClientCompletedRpcsHour; // NOLINT 108 using experimental::ClientReceivedBytesPerRpcHour; // NOLINT 109 using experimental::ClientReceivedMessagesPerRpcHour; // NOLINT 110 using experimental::ClientRetriesHour; // NOLINT 111 using experimental::ClientRetriesPerCallHour; // NOLINT 112 using experimental::ClientRetryDelayPerCallHour; // NOLINT 113 using experimental::ClientRoundtripLatencyHour; // NOLINT 114 using experimental::ClientSentBytesPerRpcHour; // NOLINT 115 using experimental::ClientSentMessagesPerRpcHour; // NOLINT 116 using experimental::ClientServerLatencyHour; // NOLINT 117 using experimental::ClientStartedRpcsHour; // NOLINT 118 using experimental::ClientTransparentRetriesHour; // NOLINT 119 using experimental::ClientTransparentRetriesPerCallHour; // NOLINT 120 121 using experimental::ServerCompletedRpcsHour; // NOLINT 122 using experimental::ServerReceivedBytesPerRpcHour; // NOLINT 123 using experimental::ServerReceivedMessagesPerRpcHour; // NOLINT 124 using experimental::ServerSentBytesPerRpcHour; // NOLINT 125 using experimental::ServerSentMessagesPerRpcHour; // NOLINT 126 using experimental::ServerServerLatencyHour; // NOLINT 127 using experimental::ServerStartedRpcsHour; // NOLINT 128 129 namespace internal { 130 131 extern const absl::string_view kRpcClientApiLatencyMeasureName; 132 133 // This view is in an internal namespace since this is meant just for GCP 134 // Observability purposes. 135 const ::opencensus::stats::ViewDescriptor& ClientApiLatency(); 136 137 // Enables/Disables OpenCensus stats/tracing. It's only safe to do at the 138 // start of a program, before any channels/servers are built. 139 void EnableOpenCensusStats(bool enable); 140 void EnableOpenCensusTracing(bool enable); 141 // Gets the current status of OpenCensus stats/tracing 142 bool OpenCensusStatsEnabled(); 143 bool OpenCensusTracingEnabled(); 144 145 // Registers various things for the OpenCensus plugin. 146 class OpenCensusRegistry { 147 public: 148 struct Label { 149 std::string key; 150 opencensus::tags::TagKey tag_key; 151 std::string value; 152 }; 153 154 struct Attribute { 155 std::string key; 156 std::string value; 157 }; 158 159 static OpenCensusRegistry& Get(); 160 RegisterConstantLabels(const std::map<std::string,std::string> & labels)161 void RegisterConstantLabels( 162 const std::map<std::string, std::string>& labels) { 163 constant_labels_.reserve(labels.size()); 164 for (const auto& label : labels) { 165 auto tag_key = opencensus::tags::TagKey::Register(label.first); 166 constant_labels_.emplace_back(Label{label.first, tag_key, label.second}); 167 } 168 } 169 RegisterConstantAttributes(std::vector<Attribute> attributes)170 void RegisterConstantAttributes(std::vector<Attribute> attributes) { 171 constant_attributes_ = std::move(attributes); 172 } 173 174 ::opencensus::tags::TagMap PopulateTagMapWithConstantLabels( 175 const ::opencensus::tags::TagMap& tag_map); 176 177 void PopulateCensusContextWithConstantAttributes( 178 grpc::experimental::CensusContext* context); 179 ConstantLabels()180 const std::vector<Label>& ConstantLabels() { return constant_labels_; } 181 ConstantAttributes()182 const std::vector<Attribute>& ConstantAttributes() { 183 return constant_attributes_; 184 } 185 186 private: 187 OpenCensusRegistry() = default; 188 189 std::vector<Label> constant_labels_; 190 std::vector<Attribute> constant_attributes_; 191 }; 192 193 } // namespace internal 194 195 } // namespace grpc 196 197 #endif // GRPC_SRC_CPP_EXT_FILTERS_CENSUS_GRPC_PLUGIN_H 198