1 // 2 // 3 // Copyright 2022 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_CORE_EXT_FILTERS_LOGGING_LOGGING_SINK_H 20 #define GRPC_SRC_CORE_EXT_FILTERS_LOGGING_LOGGING_SINK_H 21 22 #include <grpc/support/port_platform.h> 23 #include <stdint.h> 24 25 #include <map> 26 #include <string> 27 28 #include "absl/numeric/int128.h" 29 #include "absl/strings/str_cat.h" 30 #include "absl/strings/string_view.h" 31 #include "src/core/util/time.h" 32 33 namespace grpc_core { 34 35 // Interface for a logging sink that will be used by the logging filter. 36 class LoggingSink { 37 public: 38 class Config { 39 public: 40 // Constructs a default config which has logging disabled Config()41 Config() {} Config(uint32_t max_metadata_bytes,uint32_t max_message_bytes)42 Config(uint32_t max_metadata_bytes, uint32_t max_message_bytes) 43 : enabled_(true), 44 max_metadata_bytes_(max_metadata_bytes), 45 max_message_bytes_(max_message_bytes) {} ShouldLog()46 bool ShouldLog() { return enabled_; } 47 max_metadata_bytes()48 uint32_t max_metadata_bytes() const { return max_metadata_bytes_; } 49 max_message_bytes()50 uint32_t max_message_bytes() const { return max_message_bytes_; } 51 52 bool operator==(const Config& other) const { 53 return max_metadata_bytes_ == other.max_metadata_bytes_ && 54 max_message_bytes_ == other.max_message_bytes_; 55 } 56 57 private: 58 bool enabled_ = false; 59 uint32_t max_metadata_bytes_ = 0; 60 uint32_t max_message_bytes_ = 0; 61 }; 62 63 struct Entry { 64 enum class EventType { 65 kUnknown = 0, 66 kClientHeader, 67 kServerHeader, 68 kClientMessage, 69 kServerMessage, 70 kClientHalfClose, 71 kServerTrailer, 72 kCancel 73 }; 74 EventTypeStringEntry75 static std::string EventTypeString(EventType type) { 76 switch (type) { 77 case EventType::kUnknown: 78 return "UNKNOWN"; 79 case EventType::kClientHeader: 80 return "CLIENT_HEADER"; 81 case EventType::kServerHeader: 82 return "SERVER_HEADER"; 83 case EventType::kClientMessage: 84 return "CLIENT_MESSAGE"; 85 case EventType::kServerMessage: 86 return "SERVER_MESSAGE"; 87 case EventType::kClientHalfClose: 88 return "CLIENT_HALF_CLOSE"; 89 case EventType::kServerTrailer: 90 return "SERVER_TRAILER"; 91 case EventType::kCancel: 92 return "CANCEL"; 93 } 94 return absl::StrCat("INVALID(", static_cast<int>(type), ")"); 95 } 96 97 enum class Logger { kUnknown = 0, kClient, kServer }; 98 LoggerStringEntry99 static std::string LoggerString(Logger logger) { 100 switch (logger) { 101 case Logger::kUnknown: 102 return "UNKNOWN"; 103 case Logger::kClient: 104 return "CLIENT"; 105 case Logger::kServer: 106 return "SERVER"; 107 } 108 return absl::StrCat("INVALID(", static_cast<int>(logger), ")"); 109 } 110 111 struct Payload { 112 std::map<std::string, std::string> metadata; 113 Duration timeout; 114 uint32_t status_code = 0; 115 std::string status_message; 116 std::string status_details; 117 uint32_t message_length = 0; 118 std::string message; 119 }; 120 121 struct Address { 122 enum class Type { kUnknown = 0, kIpv4, kIpv6, kUnix }; 123 Type type = LoggingSink::Entry::Address::Type::kUnknown; 124 std::string address; 125 uint32_t ip_port = 0; 126 }; 127 128 absl::uint128 call_id = 0; 129 uint64_t sequence_id = 0; 130 EventType type = LoggingSink::Entry::EventType::kUnknown; 131 Logger logger = LoggingSink::Entry::Logger::kUnknown; 132 Payload payload; 133 bool payload_truncated = false; 134 Address peer; 135 std::string authority; 136 std::string service_name; 137 std::string method_name; 138 Timestamp timestamp; 139 // Optional tracing details 140 std::string trace_id; 141 std::string span_id; 142 bool is_sampled = false; 143 bool is_trailer_only = false; 144 }; 145 146 virtual ~LoggingSink() = default; 147 148 virtual Config FindMatch(bool is_client, absl::string_view service, 149 absl::string_view method) = 0; 150 151 virtual void LogEntry(Entry entry) = 0; 152 }; 153 154 inline std::ostream& operator<<(std::ostream& out, 155 const LoggingSink::Entry::EventType& type) { 156 return out << LoggingSink::Entry::EventTypeString(type); 157 } 158 159 inline std::ostream& operator<<(std::ostream& out, 160 const LoggingSink::Entry::Logger& logger) { 161 return out << LoggingSink::Entry::LoggerString(logger); 162 } 163 164 } // namespace grpc_core 165 166 #endif // GRPC_SRC_CORE_EXT_FILTERS_LOGGING_LOGGING_SINK_H 167