Line data Source code
1 : #pragma once 2 : 3 : #include <map> 4 : #include <memory> 5 : #include <string> 6 : #include <vector> 7 : 8 : #include "envoy/common/time.h" 9 : #include "envoy/config/core/v3/base.pb.h" 10 : #include "envoy/network/address.h" 11 : #include "envoy/network/transport_socket.h" 12 : #include "envoy/stats/primitive_stats_macros.h" 13 : #include "envoy/stats/stats_macros.h" 14 : #include "envoy/upstream/health_check_host_monitor.h" 15 : #include "envoy/upstream/outlier_detection.h" 16 : #include "envoy/upstream/resource_manager.h" 17 : 18 : #include "absl/strings/string_view.h" 19 : 20 : namespace Envoy { 21 : namespace Upstream { 22 : 23 : using MetadataConstSharedPtr = std::shared_ptr<const envoy::config::core::v3::Metadata>; 24 : 25 : /** 26 : * All per host stats. @see stats_macros.h 27 : * 28 : * {rq_success, rq_error} have specific semantics driven by the needs of EDS load reporting. See 29 : * envoy.api.v2.endpoint.UpstreamLocalityStats for the definitions of success/error. These are 30 : * latched by LoadStatsReporter, independent of the normal stats sink flushing. 31 : */ 32 : #define ALL_HOST_STATS(COUNTER, GAUGE) \ 33 0 : COUNTER(cx_connect_fail) \ 34 0 : COUNTER(cx_total) \ 35 0 : COUNTER(rq_error) \ 36 0 : COUNTER(rq_success) \ 37 0 : COUNTER(rq_timeout) \ 38 0 : COUNTER(rq_total) \ 39 0 : GAUGE(cx_active) \ 40 0 : GAUGE(rq_active) 41 : 42 : /** 43 : * All per host stats defined. @see stats_macros.h 44 : */ 45 : struct HostStats { 46 : ALL_HOST_STATS(GENERATE_PRIMITIVE_COUNTER_STRUCT, GENERATE_PRIMITIVE_GAUGE_STRUCT); 47 : 48 : // Provide access to name,counter pairs. 49 0 : std::vector<std::pair<absl::string_view, Stats::PrimitiveCounterReference>> counters() { 50 0 : return {ALL_HOST_STATS(PRIMITIVE_COUNTER_NAME_AND_REFERENCE, IGNORE_PRIMITIVE_GAUGE)}; 51 0 : } 52 : 53 : // Provide access to name,gauge pairs. 54 0 : std::vector<std::pair<absl::string_view, Stats::PrimitiveGaugeReference>> gauges() { 55 0 : return {ALL_HOST_STATS(IGNORE_PRIMITIVE_COUNTER, PRIMITIVE_GAUGE_NAME_AND_REFERENCE)}; 56 0 : } 57 : }; 58 : 59 : /** 60 : * Weakly-named load metrics to be reported as EndpointLoadMetricStats. Individual stats are 61 : * accumulated by calling add(), which combines stats with the same name. The aggregated stats are 62 : * retrieved by calling latch(), which also clears the current load metrics. 63 : */ 64 : class LoadMetricStats { 65 : public: 66 182133 : virtual ~LoadMetricStats() = default; 67 : 68 : struct Stat { 69 : uint64_t num_requests_with_metric = 0; 70 : double total_metric_value = 0.0; 71 : }; 72 : 73 : using StatMap = absl::flat_hash_map<std::string, Stat>; 74 : using StatMapPtr = std::unique_ptr<StatMap>; 75 : 76 : // Adds the given stat to the map. If the stat already exists in the map, then the stat is 77 : // combined with the existing map entry by incrementing num_requests_with_metric and summing the 78 : // total_metric_value fields. Otherwise, the stat is added with the provided value to the map, 79 : // which retains all entries until the next call to latch(). This allows metrics to be added 80 : // whose keys may not necessarily be known at startup time. 81 : virtual void add(const absl::string_view key, double value) PURE; 82 : 83 : // Returns an owning pointer to the current load metrics and clears the map. 84 : virtual StatMapPtr latch() PURE; 85 : }; 86 : 87 : class ClusterInfo; 88 : 89 : /** 90 : * A description of an upstream host. 91 : */ 92 : class HostDescription { 93 : public: 94 182133 : virtual ~HostDescription() = default; 95 : 96 : /** 97 : * @return whether the host is a canary. 98 : */ 99 : virtual bool canary() const PURE; 100 : 101 : /** 102 : * Update the canary status of the host. 103 : */ 104 : virtual void canary(bool is_canary) PURE; 105 : 106 : /** 107 : * @return the metadata associated with this host 108 : */ 109 : virtual MetadataConstSharedPtr metadata() const PURE; 110 : 111 : /** 112 : * Set the current metadata. 113 : */ 114 : virtual void metadata(MetadataConstSharedPtr new_metadata) PURE; 115 : 116 : /** 117 : * @return the cluster the host is a member of. 118 : */ 119 : virtual const ClusterInfo& cluster() const PURE; 120 : 121 : /** 122 : * @return true if the cluster can create a connection for this priority, false otherwise. 123 : * @param priority the priority the connection would have. 124 : */ 125 : virtual bool canCreateConnection(Upstream::ResourcePriority priority) const PURE; 126 : 127 : /** 128 : * @return the host's outlier detection monitor. 129 : */ 130 : virtual Outlier::DetectorHostMonitor& outlierDetector() const PURE; 131 : 132 : /** 133 : * @return the host's health checker monitor. 134 : */ 135 : virtual HealthCheckHostMonitor& healthChecker() const PURE; 136 : 137 : /** 138 : * @return The hostname used as the host header for health checking. 139 : */ 140 : virtual const std::string& hostnameForHealthChecks() const PURE; 141 : 142 : /** 143 : * @return the hostname associated with the host if any. 144 : * Empty string "" indicates that hostname is not a DNS name. 145 : */ 146 : virtual const std::string& hostname() const PURE; 147 : 148 : /** 149 : * @return the transport socket factory responsible for this host. 150 : */ 151 : virtual Network::UpstreamTransportSocketFactory& transportSocketFactory() const PURE; 152 : 153 : /** 154 : * @return the address used to connect to the host. 155 : */ 156 : virtual Network::Address::InstanceConstSharedPtr address() const PURE; 157 : 158 : /** 159 : * @return a optional list of additional addresses which the host resolved to. These addresses 160 : * may be used to create upstream connections if the primary address is unreachable. 161 : */ 162 : virtual const std::vector<Network::Address::InstanceConstSharedPtr>& addressList() const PURE; 163 : 164 : /** 165 : * @return host specific stats. 166 : */ 167 : virtual HostStats& stats() const PURE; 168 : 169 : /** 170 : * @return custom stats for multi-dimensional load balancing. 171 : */ 172 : virtual LoadMetricStats& loadMetricStats() const PURE; 173 : 174 : /** 175 : * @return the locality of the host (deployment specific). This will be the default instance if 176 : * unknown. 177 : */ 178 : virtual const envoy::config::core::v3::Locality& locality() const PURE; 179 : 180 : /** 181 : * @return the human readable name of the host's locality zone as a StatName. 182 : */ 183 : virtual Stats::StatName localityZoneStatName() const PURE; 184 : 185 : /** 186 : * @return the address used to health check the host. 187 : */ 188 : virtual Network::Address::InstanceConstSharedPtr healthCheckAddress() const PURE; 189 : 190 : /** 191 : * @return the priority of the host. 192 : */ 193 : virtual uint32_t priority() const PURE; 194 : 195 : /** 196 : * Set the current priority. 197 : */ 198 : virtual void priority(uint32_t) PURE; 199 : 200 : /** 201 : * @return timestamp of when host has transitioned from unhealthy to 202 : * healthy state via an active healthchecking. 203 : */ 204 : virtual absl::optional<MonotonicTime> lastHcPassTime() const PURE; 205 : }; 206 : 207 : using HostDescriptionConstSharedPtr = std::shared_ptr<const HostDescription>; 208 : 209 159 : #define ALL_TRANSPORT_SOCKET_MATCH_STATS(COUNTER) COUNTER(total_match_count) 210 : 211 : /** 212 : * The stats for transport socket match. 213 : */ 214 : struct TransportSocketMatchStats { 215 : ALL_TRANSPORT_SOCKET_MATCH_STATS(GENERATE_COUNTER_STRUCT) 216 : }; 217 : 218 : /** 219 : * Library to determine what transport socket configuration to use for a given host. 220 : */ 221 : class TransportSocketMatcher { 222 : public: 223 : struct MatchData { 224 : MatchData(Network::UpstreamTransportSocketFactory& factory, TransportSocketMatchStats& stats, 225 : std::string name) 226 2376 : : factory_(factory), stats_(stats), name_(std::move(name)) {} 227 : Network::UpstreamTransportSocketFactory& factory_; 228 : TransportSocketMatchStats& stats_; 229 : std::string name_; 230 : }; 231 2376 : virtual ~TransportSocketMatcher() = default; 232 : 233 : /** 234 : * Resolve the transport socket configuration for a particular host. 235 : * @param metadata the metadata of the given host. 236 : * @return the match information of the transport socket selected. 237 : */ 238 : virtual MatchData resolve(const envoy::config::core::v3::Metadata* metadata) const PURE; 239 : 240 : /* 241 : * return true if all matches support ALPN, false otherwise. 242 : */ 243 : virtual bool allMatchesSupportAlpn() const PURE; 244 : }; 245 : 246 : using TransportSocketMatcherPtr = std::unique_ptr<TransportSocketMatcher>; 247 : 248 : } // namespace Upstream 249 : } // namespace Envoy