|
16 | 16 | #include "envoy/config/core/v3/health_check.pb.h" |
17 | 17 | #include "envoy/config/core/v3/protocol.pb.h" |
18 | 18 | #include "envoy/config/endpoint/v3/endpoint_components.pb.h" |
| 19 | +#include "envoy/config/metrics/v3/stats.pb.h" |
| 20 | +#include "envoy/config/metrics/v3/stats.pb.validate.h" |
19 | 21 | #include "envoy/config/upstream/local_address_selector/v3/default_local_address_selector.pb.h" |
20 | 22 | #include "envoy/event/dispatcher.h" |
21 | 23 | #include "envoy/event/timer.h" |
|
55 | 57 | #include "source/common/runtime/runtime_features.h" |
56 | 58 | #include "source/common/runtime/runtime_impl.h" |
57 | 59 | #include "source/common/stats/deferred_creation.h" |
| 60 | +#include "source/common/stats/stats_matcher_impl.h" |
58 | 61 | #include "source/common/upstream/cluster_factory_impl.h" |
59 | 62 | #include "source/common/upstream/health_checker_impl.h" |
60 | 63 | #include "source/common/upstream/locality_pool.h" |
@@ -174,12 +177,6 @@ HostVector filterHosts(const absl::node_hash_set<HostSharedPtr>& hosts, |
174 | 177 | return net_hosts; |
175 | 178 | } |
176 | 179 |
|
177 | | -Stats::ScopeSharedPtr generateStatsScope(const envoy::config::cluster::v3::Cluster& config, |
178 | | - Stats::Store& stats) { |
179 | | - return stats.createScope(fmt::format( |
180 | | - "cluster.{}.", config.alt_stat_name().empty() ? config.name() : config.alt_stat_name())); |
181 | | -} |
182 | | - |
183 | 180 | Network::ConnectionSocket::OptionsSharedPtr |
184 | 181 | buildBaseSocketOptions(const envoy::config::cluster::v3::Cluster& cluster_config, |
185 | 182 | const envoy::config::core::v3::BindConfig& bootstrap_bind_config) { |
@@ -419,6 +416,39 @@ const absl::string_view ClusterImplBase::DoNotValidateAlpnRuntimeKey = |
419 | 416 | const absl::string_view ClusterImplBase::DropOverloadRuntimeKey = |
420 | 417 | "load_balancing_policy.drop_overload_limit"; |
421 | 418 |
|
| 419 | +constexpr absl::string_view StatsMatcherMetadataKey = "envoy.stats_matcher"; |
| 420 | + |
| 421 | +Stats::ScopeSharedPtr |
| 422 | +generateStatsScope(const envoy::config::cluster::v3::Cluster& config, |
| 423 | + Server::Configuration::ServerFactoryContext& server_context, |
| 424 | + bool use_alt_stat_name) { |
| 425 | + auto& stats = server_context.serverScope().store(); |
| 426 | + Stats::StatsMatcherSharedPtr scope_matcher; |
| 427 | + |
| 428 | + // Check for a per-cluster stats matcher in typed_filter_metadata under the specific key. If |
| 429 | + // present, unpack it as StatsMatcher and use it to restrict which stats are created for this |
| 430 | + // cluster's scope. |
| 431 | + const auto& typed_meta = config.metadata().typed_filter_metadata(); |
| 432 | + if (auto it = typed_meta.find(StatsMatcherMetadataKey); it != typed_meta.end()) { |
| 433 | + envoy::config::metrics::v3::StatsMatcher stats_matcher_proto; |
| 434 | + if (auto status = MessageUtil::unpackTo(it->second, stats_matcher_proto); status.ok()) { |
| 435 | + MessageUtil::validate(stats_matcher_proto, server_context.messageValidationVisitor()); |
| 436 | + scope_matcher = std::make_shared<Stats::StatsMatcherImpl>( |
| 437 | + stats_matcher_proto, stats.symbolTable(), server_context); |
| 438 | + } else { |
| 439 | + ENVOY_LOG_TO_LOGGER(Envoy::Logger::Registry::getLog(Envoy::Logger::Id::upstream), warn, |
| 440 | + "Failed to unpack stats matcher for cluster {}: {}", config.name(), |
| 441 | + status.message()); |
| 442 | + } |
| 443 | + } |
| 444 | + |
| 445 | + return stats.createScope( |
| 446 | + fmt::format("cluster.{}.", (!config.alt_stat_name().empty() && use_alt_stat_name) |
| 447 | + ? config.alt_stat_name() |
| 448 | + : config.name()), |
| 449 | + false, {}, std::move(scope_matcher)); |
| 450 | +} |
| 451 | + |
422 | 452 | // TODO(pianiststickman): this implementation takes a lock on the hot path and puts a copy of the |
423 | 453 | // stat name into every host that receives a copy of that metric. This can be improved by putting |
424 | 454 | // a single copy of the stat name into a thread-local key->index map so that the lock can be avoided |
@@ -1554,7 +1584,7 @@ ClusterImplBase::ClusterImplBase(const envoy::config::cluster::v3::Cluster& clus |
1554 | 1584 | cluster_context.serverFactoryContext().mainThreadDispatcher())) { |
1555 | 1585 | auto& server_context = cluster_context.serverFactoryContext(); |
1556 | 1586 |
|
1557 | | - auto stats_scope = generateStatsScope(cluster, server_context.serverScope().store()); |
| 1587 | + auto stats_scope = generateStatsScope(cluster, server_context); |
1558 | 1588 | transport_factory_context_ = |
1559 | 1589 | std::make_unique<Server::Configuration::TransportSocketFactoryContextImpl>( |
1560 | 1590 | server_context, *stats_scope, cluster_context.messageValidationVisitor()); |
|
0 commit comments