diff --git a/include/envoy/stats/BUILD b/include/envoy/stats/BUILD index 9f1e513135b6..9162d1b1ca41 100644 --- a/include/envoy/stats/BUILD +++ b/include/envoy/stats/BUILD @@ -25,12 +25,18 @@ envoy_cc_library( "tag_extractor.h", "tag_producer.h", ], - deps = ["//include/envoy/common:interval_set_interface"], + deps = [ + ":symbol_table_interface", + "//include/envoy/common:interval_set_interface", + ], ) envoy_cc_library( name = "symbol_table_interface", hdrs = ["symbol_table.h"], + deps = [ + "//source/common/common:hash_lib", + ], ) envoy_cc_library( diff --git a/include/envoy/stats/scope.h b/include/envoy/stats/scope.h index ef913edea6e0..7e4a21f7d647 100644 --- a/include/envoy/stats/scope.h +++ b/include/envoy/stats/scope.h @@ -2,11 +2,11 @@ #include #include -#include #include "envoy/common/pure.h" #include "envoy/stats/histogram.h" #include "envoy/stats/stats_options.h" +#include "envoy/stats/symbol_table.h" namespace Envoy { namespace Stats { @@ -61,6 +61,12 @@ class Scope { * maximum allowable object name length and stat suffix length. */ virtual const Stats::StatsOptions& statsOptions() const PURE; + + /** + * @return a reference to the symbol table. + */ + virtual const SymbolTable& symbolTable() const PURE; + virtual SymbolTable& symbolTable() PURE; }; } // namespace Stats diff --git a/include/envoy/stats/stat_data_allocator.h b/include/envoy/stats/stat_data_allocator.h index f0ea93e266d0..b708ffe2fb48 100644 --- a/include/envoy/stats/stat_data_allocator.h +++ b/include/envoy/stats/stat_data_allocator.h @@ -10,6 +10,7 @@ #include "envoy/common/pure.h" #include "envoy/stats/stats.h" +#include "envoy/stats/symbol_table.h" #include "envoy/stats/tag.h" #include "absl/strings/string_view.h" @@ -52,6 +53,9 @@ class StatDataAllocator { */ virtual bool requiresBoundedStatNameSize() const PURE; + virtual const SymbolTable& symbolTable() const PURE; + virtual SymbolTable& symbolTable() PURE; + // TODO(jmarantz): create a parallel mechanism to instantiate histograms. At // the moment, histograms don't fit the same pattern of counters and gauges // as they are not actually created in the context of a stats allocator. diff --git a/source/common/stats/BUILD b/source/common/stats/BUILD index 85052c236194..7efb91a9f944 100644 --- a/source/common/stats/BUILD +++ b/source/common/stats/BUILD @@ -13,6 +13,7 @@ envoy_cc_library( srcs = ["heap_stat_data.cc"], hdrs = ["heap_stat_data.h"], deps = [ + ":metric_impl_lib", ":stat_data_allocator_lib", "//source/common/common:assert_lib", "//source/common/common:hash_lib", @@ -42,6 +43,7 @@ envoy_cc_library( srcs = ["isolated_store_impl.cc"], hdrs = ["isolated_store_impl.h"], deps = [ + ":fake_symbol_table_lib", ":histogram_lib", ":stats_lib", ":stats_options_lib", @@ -54,6 +56,7 @@ envoy_cc_library( name = "metric_impl_lib", hdrs = ["metric_impl.h"], deps = [ + ":symbol_table_lib", "//include/envoy/stats:stats_interface", "//source/common/common:assert_lib", ], @@ -130,6 +133,7 @@ envoy_cc_library( "//include/envoy/stats:symbol_table_interface", "//source/common/common:assert_lib", "//source/common/common:logger_lib", + "//source/common/common:stack_array", "//source/common/common:thread_lib", "//source/common/common:utility_lib", ], @@ -202,4 +206,5 @@ envoy_cc_library( name = "utility_lib", srcs = ["utility.cc"], hdrs = ["utility.h"], + deps = [":symbol_table_lib"], ) diff --git a/source/common/stats/heap_stat_data.cc b/source/common/stats/heap_stat_data.cc index 7177ea19087c..0ac82c344539 100644 --- a/source/common/stats/heap_stat_data.cc +++ b/source/common/stats/heap_stat_data.cc @@ -11,8 +11,6 @@ HeapStatData::HeapStatData(absl::string_view key) { StringUtil::strlcpy(name_, key.data(), key.size() + 1); } -HeapStatDataAllocator::HeapStatDataAllocator() {} - HeapStatDataAllocator::~HeapStatDataAllocator() { ASSERT(stats_.empty()); } HeapStatData* HeapStatDataAllocator::alloc(absl::string_view name) { diff --git a/source/common/stats/heap_stat_data.h b/source/common/stats/heap_stat_data.h index dc14303ec626..799896eb3d4a 100644 --- a/source/common/stats/heap_stat_data.h +++ b/source/common/stats/heap_stat_data.h @@ -4,6 +4,8 @@ #include #include +#include "envoy/stats/symbol_table.h" + #include "common/common/hash.h" #include "common/common/thread.h" #include "common/common/thread_annotations.h" @@ -53,8 +55,8 @@ struct HeapStatData { */ class HeapStatDataAllocator : public StatDataAllocatorImpl { public: - HeapStatDataAllocator(); - ~HeapStatDataAllocator(); + HeapStatDataAllocator(SymbolTable& symbol_table) : StatDataAllocatorImpl(symbol_table) {} + ~HeapStatDataAllocator() override; // StatDataAllocatorImpl HeapStatData* alloc(absl::string_view name) override; diff --git a/source/common/stats/isolated_store_impl.cc b/source/common/stats/isolated_store_impl.cc index d85d38ef0321..072bf4e54b5d 100644 --- a/source/common/stats/isolated_store_impl.cc +++ b/source/common/stats/isolated_store_impl.cc @@ -6,6 +6,7 @@ #include #include "common/common/utility.h" +#include "common/stats/fake_symbol_table_impl.h" #include "common/stats/histogram_impl.h" #include "common/stats/utility.h" @@ -13,7 +14,16 @@ namespace Envoy { namespace Stats { IsolatedStoreImpl::IsolatedStoreImpl() - : counters_([this](const std::string& name) -> CounterSharedPtr { + : IsolatedStoreImpl(std::make_unique()) {} + +IsolatedStoreImpl::IsolatedStoreImpl(std::unique_ptr&& symbol_table) + : IsolatedStoreImpl(*symbol_table) { + symbol_table_storage_ = std::move(symbol_table); +} + +IsolatedStoreImpl::IsolatedStoreImpl(SymbolTable& symbol_table) + : symbol_table_(symbol_table), alloc_(symbol_table_), + counters_([this](const std::string& name) -> CounterSharedPtr { std::string tag_extracted_name = name; std::vector tags; return alloc_.makeCounter(name, std::move(tag_extracted_name), std::move(tags)); @@ -42,6 +52,8 @@ struct IsolatedScopeImpl : public Scope { return parent_.histogram(prefix_ + name); } const Stats::StatsOptions& statsOptions() const override { return parent_.statsOptions(); } + const SymbolTable& symbolTable() const override { return parent_.symbolTable(); } + SymbolTable& symbolTable() override { return parent_.symbolTable(); } IsolatedStoreImpl& parent_; const std::string prefix_; diff --git a/source/common/stats/isolated_store_impl.h b/source/common/stats/isolated_store_impl.h index 7765fd50e3e7..051ffc2e5295 100644 --- a/source/common/stats/isolated_store_impl.h +++ b/source/common/stats/isolated_store_impl.h @@ -12,8 +12,11 @@ #include "common/common/utility.h" #include "common/stats/heap_stat_data.h" #include "common/stats/stats_options_impl.h" +#include "common/stats/symbol_table_impl.h" #include "common/stats/utility.h" +#include "absl/container/flat_hash_map.h" + namespace Envoy { namespace Stats { @@ -22,7 +25,7 @@ namespace Stats { */ template class IsolatedStatsCache { public: - typedef std::function(const std::string& name)> Allocator; + using Allocator = std::function(const std::string& name)>; IsolatedStatsCache(Allocator alloc) : alloc_(alloc) {} @@ -48,13 +51,14 @@ template class IsolatedStatsCache { } private: - std::unordered_map> stats_; + absl::flat_hash_map> stats_; Allocator alloc_; }; class IsolatedStoreImpl : public Store { public: IsolatedStoreImpl(); + explicit IsolatedStoreImpl(SymbolTable& symbol_table); // Stats::Scope Counter& counter(const std::string& name) override { return counters_.get(name); } @@ -66,6 +70,8 @@ class IsolatedStoreImpl : public Store { return histogram; } const Stats::StatsOptions& statsOptions() const override { return stats_options_; } + const SymbolTable& symbolTable() const override { return symbol_table_; } + virtual SymbolTable& symbolTable() override { return symbol_table_; } // Stats::Store std::vector counters() const override { return counters_.toVector(); } @@ -75,6 +81,10 @@ class IsolatedStoreImpl : public Store { } private: + IsolatedStoreImpl(std::unique_ptr&& symbol_table); + + std::unique_ptr symbol_table_storage_; + SymbolTable& symbol_table_; HeapStatDataAllocator alloc_; IsolatedStatsCache counters_; IsolatedStatsCache gauges_; diff --git a/source/common/stats/raw_stat_data.h b/source/common/stats/raw_stat_data.h index 882a35549a97..ccefc082fa4c 100644 --- a/source/common/stats/raw_stat_data.h +++ b/source/common/stats/raw_stat_data.h @@ -13,6 +13,7 @@ #include "envoy/stats/stat_data_allocator.h" #include "envoy/stats/stats_options.h" +#include "envoy/stats/symbol_table.h" #include "common/common/assert.h" #include "common/common/block_memory_hash_set.h" @@ -97,8 +98,9 @@ using RawStatDataSet = BlockMemoryHashSet; class RawStatDataAllocator : public StatDataAllocatorImpl { public: RawStatDataAllocator(Thread::BasicLockable& mutex, RawStatDataSet& stats_set, - const StatsOptions& options) - : mutex_(mutex), stats_set_(stats_set), options_(options) {} + const StatsOptions& options, SymbolTable& symbol_table) + : StatDataAllocatorImpl(symbol_table), mutex_(mutex), stats_set_(stats_set), + options_(options) {} // StatDataAllocator bool requiresBoundedStatNameSize() const override { return true; } diff --git a/source/common/stats/stat_data_allocator_impl.h b/source/common/stats/stat_data_allocator_impl.h index a25b1e450181..5b8bfc994d62 100644 --- a/source/common/stats/stat_data_allocator_impl.h +++ b/source/common/stats/stat_data_allocator_impl.h @@ -5,6 +5,7 @@ #include "envoy/stats/stat_data_allocator.h" #include "envoy/stats/stats.h" +#include "envoy/stats/symbol_table.h" #include "common/common/assert.h" #include "common/stats/metric_impl.h" @@ -29,6 +30,8 @@ namespace Stats { // available. This could be resolved with placed new, or another nesting level. template class StatDataAllocatorImpl : public StatDataAllocator { public: + explicit StatDataAllocatorImpl(SymbolTable& symbol_table) : symbol_table_(symbol_table) {} + // StatDataAllocator CounterSharedPtr makeCounter(absl::string_view name, std::string&& tag_extracted_name, std::vector&& tags) override; @@ -50,6 +53,12 @@ template class StatDataAllocatorImpl : public StatDataAllocator * @param data the data returned by alloc(). */ virtual void free(StatData& data) PURE; + + SymbolTable& symbolTable() override { return symbol_table_; } + const SymbolTable& symbolTable() const override { return symbol_table_; } + +private: + SymbolTable& symbol_table_; }; /** diff --git a/source/common/stats/thread_local_store.cc b/source/common/stats/thread_local_store.cc index 4e58aec9e144..114b7c74d796 100644 --- a/source/common/stats/thread_local_store.cc +++ b/source/common/stats/thread_local_store.cc @@ -26,7 +26,8 @@ ThreadLocalStoreImpl::ThreadLocalStoreImpl(const StatsOptions& stats_options, : stats_options_(stats_options), alloc_(alloc), default_scope_(createScope("")), tag_producer_(std::make_unique()), stats_matcher_(std::make_unique()), - num_last_resort_stats_(default_scope_->counter("stats.overflow")), source_(*this) {} + num_last_resort_stats_(default_scope_->counter("stats.overflow")), + heap_allocator_(alloc.symbolTable()), source_(*this) {} ThreadLocalStoreImpl::~ThreadLocalStoreImpl() { ASSERT(shutting_down_); diff --git a/source/common/stats/thread_local_store.h b/source/common/stats/thread_local_store.h index adbfe7d184a7..666dc4e3d3be 100644 --- a/source/common/stats/thread_local_store.h +++ b/source/common/stats/thread_local_store.h @@ -148,6 +148,8 @@ class ThreadLocalStoreImpl : Logger::Loggable, public StoreRo Histogram& histogram(const std::string& name) override { return default_scope_->histogram(name); }; + const SymbolTable& symbolTable() const override { return alloc_.symbolTable(); } + SymbolTable& symbolTable() override { return alloc_.symbolTable(); } // Stats::Store std::vector counters() const override; @@ -206,6 +208,8 @@ class ThreadLocalStoreImpl : Logger::Loggable, public StoreRo ScopePtr createScope(const std::string& name) override { return parent_.createScope(prefix_ + name); } + const SymbolTable& symbolTable() const override { return parent_.symbolTable(); } + SymbolTable& symbolTable() override { return parent_.symbolTable(); } void deliverHistogramToSinks(const Histogram& histogram, uint64_t value) override; Gauge& gauge(const std::string& name) override; Histogram& histogram(const std::string& name) override; diff --git a/source/exe/BUILD b/source/exe/BUILD index 3ab1d2d4c8fc..f0e4b2b8d3f0 100644 --- a/source/exe/BUILD +++ b/source/exe/BUILD @@ -70,6 +70,7 @@ envoy_cc_library( "//source/common/common:compiler_requirements_lib", "//source/common/http/http2:codec_lib", "//source/common/common:perf_annotation_lib", + "//source/common/stats:fake_symbol_table_lib", "//source/common/thread:thread_factory_singleton_lib", "//source/server:hot_restart_lib", "//source/server:hot_restart_nop_lib", diff --git a/source/exe/main_common.cc b/source/exe/main_common.cc index 5b7f9e14be83..9d123ece2a4c 100644 --- a/source/exe/main_common.cc +++ b/source/exe/main_common.cc @@ -58,11 +58,11 @@ MainCommonBase::MainCommonBase(const OptionsImpl& options, Event::TimeSystem& ti case Server::Mode::Serve: { #ifdef ENVOY_HOT_RESTART if (!options.hotRestartDisabled()) { - restarter_ = std::make_unique(options_); + restarter_ = std::make_unique(options_, symbol_table_); } #endif if (restarter_ == nullptr) { - restarter_ = std::make_unique(); + restarter_ = std::make_unique(symbol_table_); } tls_ = std::make_unique(); @@ -88,7 +88,7 @@ MainCommonBase::MainCommonBase(const OptionsImpl& options, Event::TimeSystem& ti break; } case Server::Mode::Validate: - restarter_ = std::make_unique(); + restarter_ = std::make_unique(symbol_table_); logging_context_ = std::make_unique(options_.logLevel(), options_.logFormat(), restarter_->logLock()); break; diff --git a/source/exe/main_common.h b/source/exe/main_common.h index aa1821a57dc4..06ac9b0a8b3c 100644 --- a/source/exe/main_common.h +++ b/source/exe/main_common.h @@ -5,6 +5,7 @@ #include "common/common/thread.h" #include "common/event/real_time_system.h" +#include "common/stats/fake_symbol_table_impl.h" #include "common/stats/thread_local_store.h" #include "common/thread_local/thread_local_impl.h" @@ -64,7 +65,7 @@ class MainCommonBase { protected: const Envoy::OptionsImpl& options_; - + Stats::FakeSymbolTableImpl symbol_table_; Server::ComponentFactory& component_factory_; Thread::ThreadFactory& thread_factory_; diff --git a/source/server/hot_restart_impl.cc b/source/server/hot_restart_impl.cc index 334ec322ecf1..52f4827dfcb9 100644 --- a/source/server/hot_restart_impl.cc +++ b/source/server/hot_restart_impl.cc @@ -122,7 +122,7 @@ std::string SharedMemory::version(uint64_t max_num_stats, stats_options.maxNameLength()); } -HotRestartImpl::HotRestartImpl(const Options& options) +HotRestartImpl::HotRestartImpl(const Options& options, Stats::SymbolTable& symbol_table) : options_(options), stats_set_options_(blockMemHashOptions(options.maxStats())), shmem_(SharedMemory::initialize( Stats::RawStatDataSet::numBytes(stats_set_options_, options_.statsOptions()), options_)), @@ -136,8 +136,8 @@ HotRestartImpl::HotRestartImpl(const Options& options) std::make_unique(stats_set_options_, options.restartEpoch() == 0, shmem_.stats_set_data_, options_.statsOptions()); } - stats_allocator_ = std::make_unique(stat_lock_, *stats_set_, - options_.statsOptions()); + stats_allocator_ = std::make_unique( + stat_lock_, *stats_set_, options_.statsOptions(), symbol_table); my_domain_socket_ = bindDomainSocket(options.restartEpoch()); child_address_ = createDomainSocketAddress((options.restartEpoch() + 1)); initDomainSocketAddress(&parent_address_); diff --git a/source/server/hot_restart_impl.h b/source/server/hot_restart_impl.h index 5d432c42ad44..8f77c7d61669 100644 --- a/source/server/hot_restart_impl.h +++ b/source/server/hot_restart_impl.h @@ -114,7 +114,7 @@ class ProcessSharedMutex : public Thread::BasicLockable { */ class HotRestartImpl : public HotRestart, Logger::Loggable { public: - HotRestartImpl(const Options& options); + HotRestartImpl(const Options& options, Stats::SymbolTable& symbol_table); // Server::HotRestart void drainParentListeners() override; diff --git a/source/server/hot_restart_nop_impl.h b/source/server/hot_restart_nop_impl.h index bc31f897f29a..756bb8d217fd 100644 --- a/source/server/hot_restart_nop_impl.h +++ b/source/server/hot_restart_nop_impl.h @@ -15,7 +15,7 @@ namespace Server { */ class HotRestartNopImpl : public Server::HotRestart { public: - HotRestartNopImpl() {} + explicit HotRestartNopImpl(Stats::SymbolTable& symbol_table) : stats_allocator_(symbol_table) {} // Server::HotRestart void drainParentListeners() override {} diff --git a/test/common/grpc/BUILD b/test/common/grpc/BUILD index 3d262d90ee43..46657034b178 100644 --- a/test/common/grpc/BUILD +++ b/test/common/grpc/BUILD @@ -108,6 +108,7 @@ envoy_cc_test_library( "//test/mocks/local_info:local_info_mocks", "//test/mocks/server:server_mocks", "//test/proto:helloworld_proto_cc", + "//test/test_common:global_lib", "//test/test_common:test_time_lib", "//test/test_common:utility_lib", ], diff --git a/test/common/grpc/grpc_client_integration_test_harness.h b/test/common/grpc/grpc_client_integration_test_harness.h index d83dbbfb2c02..f241151682fe 100644 --- a/test/common/grpc/grpc_client_integration_test_harness.h +++ b/test/common/grpc/grpc_client_integration_test_harness.h @@ -13,6 +13,7 @@ #include "common/http/async_client_impl.h" #include "common/http/codes.h" #include "common/http/http2/conn_pool.h" +#include "common/stats/fake_symbol_table_impl.h" #include "common/network/connection_impl.h" #include "common/network/raw_buffer_socket.h" @@ -29,6 +30,7 @@ #include "test/mocks/upstream/mocks.h" #include "test/proto/helloworld.pb.h" #include "test/test_common/environment.h" +#include "test/test_common/global.h" #include "test/test_common/test_time.h" #include "test/test_common/utility.h" @@ -413,7 +415,8 @@ class GrpcClientIntegrationTest : public GrpcClientIntegrationParamTest { FakeHttpConnectionPtr fake_connection_; std::vector fake_streams_; const Protobuf::MethodDescriptor* method_descriptor_; - Stats::IsolatedStoreImpl* stats_store_ = new Stats::IsolatedStoreImpl(); + Envoy::Test::Global symbol_table_; + Stats::IsolatedStoreImpl* stats_store_ = new Stats::IsolatedStoreImpl(*symbol_table_); Api::ApiPtr api_; Event::DispatcherPtr dispatcher_; DispatcherHelper dispatcher_helper_{*dispatcher_}; diff --git a/test/common/http/async_client_impl_test.cc b/test/common/http/async_client_impl_test.cc index 06a0f8dcbbbb..f5ae5fd98e07 100644 --- a/test/common/http/async_client_impl_test.cc +++ b/test/common/http/async_client_impl_test.cc @@ -62,6 +62,7 @@ class AsyncClientImplTest : public testing::Test { } MessagePtr message_{new RequestMessageImpl()}; + Stats::MockIsolatedStatsStore stats_store_; MockAsyncClientCallbacks callbacks_; MockAsyncClientStreamCallbacks stream_callbacks_; NiceMock cm_; @@ -71,7 +72,6 @@ class AsyncClientImplTest : public testing::Test { NiceMock dispatcher_; NiceMock runtime_; NiceMock random_; - Stats::IsolatedStoreImpl stats_store_; NiceMock local_info_; Http::ContextImpl http_context_; AsyncClientImpl client_; diff --git a/test/common/http/codes_speed_test.cc b/test/common/http/codes_speed_test.cc index 15634d95fd7d..8ec80973600f 100644 --- a/test/common/http/codes_speed_test.cc +++ b/test/common/http/codes_speed_test.cc @@ -10,6 +10,7 @@ #include "common/common/empty_string.h" #include "common/http/codes.h" +#include "common/stats/fake_symbol_table_impl.h" #include "common/stats/isolated_store_impl.h" #include "benchmark/benchmark.h" @@ -19,6 +20,8 @@ namespace Http { class CodeUtilitySpeedTest { public: + CodeUtilitySpeedTest() : global_store_(symbol_table_), cluster_scope_(symbol_table_) {} + void addResponse(uint64_t code, bool canary, bool internal_request, const std::string& request_vhost_name = EMPTY_STRING, const std::string& request_vcluster_name = EMPTY_STRING, @@ -51,6 +54,7 @@ class CodeUtilitySpeedTest { code_stats_.chargeResponseTiming(info); } + Stats::FakeSymbolTableImpl symbol_table_; Stats::IsolatedStoreImpl global_store_; Stats::IsolatedStoreImpl cluster_scope_; Http::CodeStatsImpl code_stats_; diff --git a/test/common/http/conn_manager_impl_fuzz_test.cc b/test/common/http/conn_manager_impl_fuzz_test.cc index de312082ff4b..334edf234f8f 100644 --- a/test/common/http/conn_manager_impl_fuzz_test.cc +++ b/test/common/http/conn_manager_impl_fuzz_test.cc @@ -384,6 +384,7 @@ DEFINE_PROTO_FUZZER(const test::common::http::ConnManagerImplTestCase& input) { FuzzConfig config; NiceMock drain_close; NiceMock random; + Stats::FakeSymbolTableImpl symbol_table; Http::ContextImpl http_context; NiceMock runtime; NiceMock local_info; diff --git a/test/common/http/conn_manager_impl_test.cc b/test/common/http/conn_manager_impl_test.cc index d6e59199d3af..959bd05eb94e 100644 --- a/test/common/http/conn_manager_impl_test.cc +++ b/test/common/http/conn_manager_impl_test.cc @@ -261,12 +261,12 @@ class HttpConnectionManagerImplTest : public testing::Test, public ConnectionMan DangerousDeprecatedTestTime test_time_; RouteConfigProvider route_config_provider_; NiceMock tracer_; + Stats::IsolatedStoreImpl fake_stats_; Http::ContextImpl http_context_; NiceMock runtime_; NiceMock log_manager_; std::string access_log_path_; std::list access_logs_; - Stats::IsolatedStoreImpl fake_stats_; NiceMock filter_callbacks_; MockServerConnection* codec_; NiceMock filter_factory_; diff --git a/test/common/router/router_test.cc b/test/common/router/router_test.cc index 6946ae2a39b3..0fdbb9ec404a 100644 --- a/test/common/router/router_test.cc +++ b/test/common/router/router_test.cc @@ -212,7 +212,7 @@ class RouterTestBase : public testing::Test { Event::SimulatedTimeSystem test_time_; std::string upstream_zone_{"to_az"}; envoy::api::v2::core::Locality upstream_locality_; - Stats::IsolatedStoreImpl stats_store_; + NiceMock stats_store_; NiceMock cm_; NiceMock runtime_; NiceMock random_; diff --git a/test/common/stats/BUILD b/test/common/stats/BUILD index 4ceea90e1b3c..3e8184772c91 100644 --- a/test/common/stats/BUILD +++ b/test/common/stats/BUILD @@ -14,6 +14,7 @@ envoy_cc_test( name = "heap_stat_data_test", srcs = ["heap_stat_data_test.cc"], deps = [ + "//source/common/stats:fake_symbol_table_lib", "//source/common/stats:heap_stat_data_lib", "//source/common/stats:stats_options_lib", "//test/test_common:logging_lib", diff --git a/test/common/stats/heap_stat_data_test.cc b/test/common/stats/heap_stat_data_test.cc index e9ee25090a98..6f8ee288d1fd 100644 --- a/test/common/stats/heap_stat_data_test.cc +++ b/test/common/stats/heap_stat_data_test.cc @@ -1,5 +1,6 @@ #include +#include "common/stats/fake_symbol_table_impl.h" #include "common/stats/heap_stat_data.h" #include "common/stats/stats_options_impl.h" @@ -11,33 +12,40 @@ namespace Envoy { namespace Stats { namespace { +class HeapStatDataTest : public testing::Test { +protected: + HeapStatDataTest() : alloc_(symbol_table_) {} + ~HeapStatDataTest() {} + + FakeSymbolTableImpl symbol_table_; + HeapStatDataAllocator alloc_; +}; + // No truncation occurs in the implementation of HeapStatData. // Note: a similar test using RawStatData* is in raw_stat_data_test.cc. -TEST(HeapStatDataTest, HeapNoTruncate) { +TEST_F(HeapStatDataTest, HeapNoTruncate) { StatsOptionsImpl stats_options; - HeapStatDataAllocator alloc; const std::string long_string(stats_options.maxNameLength() + 1, 'A'); HeapStatData* stat{}; - EXPECT_NO_LOGS(stat = alloc.alloc(long_string)); + EXPECT_NO_LOGS(stat = alloc_.alloc(long_string)); EXPECT_EQ(stat->key(), long_string); - alloc.free(*stat); + alloc_.free(*stat); } // Note: a similar test using RawStatData* is in raw_stat_data_test.cc. -TEST(HeapStatDataTest, HeapAlloc) { - HeapStatDataAllocator alloc; - HeapStatData* stat_1 = alloc.alloc("ref_name"); +TEST_F(HeapStatDataTest, HeapAlloc) { + HeapStatData* stat_1 = alloc_.alloc("ref_name"); ASSERT_NE(stat_1, nullptr); - HeapStatData* stat_2 = alloc.alloc("ref_name"); + HeapStatData* stat_2 = alloc_.alloc("ref_name"); ASSERT_NE(stat_2, nullptr); - HeapStatData* stat_3 = alloc.alloc("not_ref_name"); + HeapStatData* stat_3 = alloc_.alloc("not_ref_name"); ASSERT_NE(stat_3, nullptr); EXPECT_EQ(stat_1, stat_2); EXPECT_NE(stat_1, stat_3); EXPECT_NE(stat_2, stat_3); - alloc.free(*stat_1); - alloc.free(*stat_2); - alloc.free(*stat_3); + alloc_.free(*stat_1); + alloc_.free(*stat_2); + alloc_.free(*stat_3); } } // namespace diff --git a/test/common/stats/raw_stat_data_test.cc b/test/common/stats/raw_stat_data_test.cc index f7bf288aacda..e1e0f1365b4a 100644 --- a/test/common/stats/raw_stat_data_test.cc +++ b/test/common/stats/raw_stat_data_test.cc @@ -14,9 +14,10 @@ namespace { class RawStatDataTest : public testing::Test { public: - RawStatDataTest() : allocator_(stats_options_) {} + RawStatDataTest() : allocator_(stats_options_, symbol_table_) {} StatsOptionsImpl stats_options_; + FakeSymbolTableImpl symbol_table_; TestAllocator allocator_; // This is RawStatDataAllocator with some size settings. }; diff --git a/test/common/stats/thread_local_store_speed_test.cc b/test/common/stats/thread_local_store_speed_test.cc index 88090b6dc44c..99f6c48ca422 100644 --- a/test/common/stats/thread_local_store_speed_test.cc +++ b/test/common/stats/thread_local_store_speed_test.cc @@ -4,6 +4,7 @@ #include "common/common/logger.h" #include "common/common/thread.h" #include "common/event/dispatcher_impl.h" +#include "common/stats/fake_symbol_table_impl.h" #include "common/stats/heap_stat_data.h" #include "common/stats/stats_options_impl.h" #include "common/stats/tag_producer_impl.h" @@ -22,7 +23,8 @@ namespace Envoy { class ThreadLocalStorePerf { public: ThreadLocalStorePerf() - : store_(options_, heap_alloc_), api_(Api::createApiForTest(store_, time_system_)) { + : heap_alloc_(symbol_table_), store_(options_, heap_alloc_), + api_(Api::createApiForTest(store_, time_system_)) { store_.setTagProducer(std::make_unique(stats_config_)); } @@ -45,6 +47,7 @@ class ThreadLocalStorePerf { } private: + Stats::FakeSymbolTableImpl symbol_table_; Event::SimulatedTimeSystem time_system_; Stats::StatsOptionsImpl options_; Stats::HeapStatDataAllocator heap_alloc_; diff --git a/test/common/stats/thread_local_store_test.cc b/test/common/stats/thread_local_store_test.cc index 5c7c70be1b74..dd6e1d330234 100644 --- a/test/common/stats/thread_local_store_test.cc +++ b/test/common/stats/thread_local_store_test.cc @@ -36,7 +36,7 @@ namespace Stats { class StatsThreadLocalStoreTest : public testing::Test { public: void SetUp() override { - alloc_ = std::make_unique(options_); + alloc_ = std::make_unique(options_, symbol_table_); resetStoreWithAlloc(*alloc_); } @@ -45,6 +45,7 @@ class StatsThreadLocalStoreTest : public testing::Test { store_->addSink(sink_); } + Stats::FakeSymbolTableImpl symbol_table_; NiceMock main_thread_dispatcher_; NiceMock tls_; StatsOptionsImpl options_; @@ -75,7 +76,7 @@ class HistogramTest : public testing::Test { public: using NameHistogramMap = std::map; - HistogramTest() : alloc_(options_) {} + HistogramTest() : alloc_(options_, symbol_table_) {} void SetUp() override { store_ = std::make_unique(options_, alloc_); @@ -168,6 +169,7 @@ class HistogramTest : public testing::Test { MOCK_METHOD1(alloc, RawStatData*(const std::string& name)); MOCK_METHOD1(free, void(RawStatData& data)); + FakeSymbolTableImpl symbol_table_; NiceMock main_thread_dispatcher_; NiceMock tls_; StatsOptionsImpl options_; @@ -767,6 +769,8 @@ TEST_P(RememberStatsMatcherTest, HistogramAcceptsAll) { testAcceptsAll(lookupHis class HeapStatsThreadLocalStoreTest : public StatsThreadLocalStoreTest { public: + HeapStatsThreadLocalStoreTest() : heap_alloc_(symbol_table_) {} + void SetUp() override { resetStoreWithAlloc(heap_alloc_); // Note: we do not call StatsThreadLocalStoreTest::SetUp here as that @@ -1089,13 +1093,15 @@ TEST_F(HistogramTest, BasicHistogramUsed) { class TruncatingAllocTest : public HeapStatsThreadLocalStoreTest { protected: - TruncatingAllocTest() : test_alloc_(options_), long_name_(options_.maxNameLength() + 1, 'A') {} + TruncatingAllocTest() + : test_alloc_(options_, symbol_table_), long_name_(options_.maxNameLength() + 1, 'A') {} void SetUp() override { store_ = std::make_unique(options_, test_alloc_); // Do not call superclass SetUp. } + FakeSymbolTableImpl symbol_table_; TestAllocator test_alloc_; std::string long_name_; }; diff --git a/test/common/tcp_proxy/tcp_proxy_test.cc b/test/common/tcp_proxy/tcp_proxy_test.cc index 1ee04f6b2582..27a056340fe1 100644 --- a/test/common/tcp_proxy/tcp_proxy_test.cc +++ b/test/common/tcp_proxy/tcp_proxy_test.cc @@ -475,9 +475,10 @@ class TcpProxyTest : public testing::Test { Event::TestTimeSystem& timeSystem() { return factory_context_.timeSystem(); } + NiceMock factory_context_; ConfigSharedPtr config_; + std::unique_ptr filter_; NiceMock filter_callbacks_; - NiceMock factory_context_; std::vector>> upstream_hosts_{}; std::vector>> upstream_connections_{}; std::vector>> @@ -486,7 +487,6 @@ class TcpProxyTest : public testing::Test { std::vector>> conn_pool_handles_; NiceMock conn_pool_; Tcp::ConnectionPool::UpstreamCallbacks* upstream_callbacks_; - std::unique_ptr filter_; StringViewSaver access_log_data_; Network::Address::InstanceConstSharedPtr upstream_local_address_; Network::Address::InstanceConstSharedPtr upstream_remote_address_; @@ -1128,10 +1128,10 @@ class TcpProxyRoutingTest : public testing::Test { Event::TestTimeSystem& timeSystem() { return factory_context_.timeSystem(); } + NiceMock factory_context_; ConfigSharedPtr config_; NiceMock connection_; NiceMock filter_callbacks_; - NiceMock factory_context_; std::unique_ptr filter_; }; diff --git a/test/common/upstream/cluster_manager_impl_test.cc b/test/common/upstream/cluster_manager_impl_test.cc index 30c398f85b10..e35a495baed8 100644 --- a/test/common/upstream/cluster_manager_impl_test.cc +++ b/test/common/upstream/cluster_manager_impl_test.cc @@ -157,17 +157,15 @@ class TestClusterManagerImpl : public ClusterManagerImpl { // it with the right values at the right times. class MockedUpdatedClusterManagerImpl : public TestClusterManagerImpl { public: - MockedUpdatedClusterManagerImpl(const envoy::config::bootstrap::v2::Bootstrap& bootstrap, - ClusterManagerFactory& factory, Stats::Store& stats, - ThreadLocal::Instance& tls, Runtime::Loader& runtime, - Runtime::RandomGenerator& random, - const LocalInfo::LocalInfo& local_info, - AccessLog::AccessLogManager& log_manager, - Event::Dispatcher& main_thread_dispatcher, Server::Admin& admin, - Api::Api& api, MockLocalClusterUpdate& local_cluster_update, - MockLocalHostsRemoved& local_hosts_removed) + MockedUpdatedClusterManagerImpl( + const envoy::config::bootstrap::v2::Bootstrap& bootstrap, ClusterManagerFactory& factory, + Stats::Store& stats, ThreadLocal::Instance& tls, Runtime::Loader& runtime, + Runtime::RandomGenerator& random, const LocalInfo::LocalInfo& local_info, + AccessLog::AccessLogManager& log_manager, Event::Dispatcher& main_thread_dispatcher, + Server::Admin& admin, Api::Api& api, MockLocalClusterUpdate& local_cluster_update, + MockLocalHostsRemoved& local_hosts_removed, Http::Context& http_context) : TestClusterManagerImpl(bootstrap, factory, stats, tls, runtime, random, local_info, - log_manager, main_thread_dispatcher, admin, api, http_context_), + log_manager, main_thread_dispatcher, admin, api, http_context), local_cluster_update_(local_cluster_update), local_hosts_removed_(local_hosts_removed) {} protected: @@ -181,7 +179,6 @@ class MockedUpdatedClusterManagerImpl : public TestClusterManagerImpl { local_hosts_removed_.post(hosts_removed); } - Http::ContextImpl http_context_; MockLocalClusterUpdate& local_cluster_update_; MockLocalHostsRemoved& local_hosts_removed_; }; @@ -236,7 +233,7 @@ class ClusterManagerImplTest : public testing::Test { cluster_manager_ = std::make_unique( bootstrap, factory_, factory_.stats_, factory_.tls_, factory_.runtime_, factory_.random_, factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, *api_, - local_cluster_update_, local_hosts_removed_); + local_cluster_update_, local_hosts_removed_, http_context_); } void checkStats(uint64_t added, uint64_t modified, uint64_t removed, uint64_t active, diff --git a/test/extensions/filters/http/ratelimit/ratelimit_test.cc b/test/extensions/filters/http/ratelimit/ratelimit_test.cc index 380d93be9e82..895a79292f78 100644 --- a/test/extensions/filters/http/ratelimit/ratelimit_test.cc +++ b/test/extensions/filters/http/ratelimit/ratelimit_test.cc @@ -86,7 +86,7 @@ class HttpRateLimitFilterTest : public testing::Test { Http::TestHeaderMapImpl response_headers_; Buffer::OwnedImpl data_; Buffer::OwnedImpl response_data_; - Stats::IsolatedStoreImpl stats_store_; + NiceMock stats_store_; NiceMock runtime_; NiceMock route_rate_limit_; NiceMock vh_rate_limit_; diff --git a/test/integration/BUILD b/test/integration/BUILD index db52ef4af1be..ab455adeaa45 100644 --- a/test/integration/BUILD +++ b/test/integration/BUILD @@ -375,6 +375,7 @@ envoy_cc_test_library( "//test/config:utility_lib", "//test/mocks/buffer:buffer_mocks", "//test/mocks/server:server_mocks", + "//test/mocks/stats:stats_mocks", "//test/mocks/upstream:upstream_mocks", "//test/test_common:environment_lib", "//test/test_common:network_utility_lib", diff --git a/test/integration/server.cc b/test/integration/server.cc index 8c1f88bd323b..8d2bd72403bf 100644 --- a/test/integration/server.cc +++ b/test/integration/server.cc @@ -158,9 +158,10 @@ void IntegrationTestServerImpl::createAndRunEnvoyServer( Network::Address::InstanceConstSharedPtr local_address, TestHooks& hooks, Thread::BasicLockable& access_log_lock, Server::ComponentFactory& component_factory, Runtime::RandomGeneratorPtr&& random_generator) { - Server::HotRestartNopImpl restarter; + Stats::FakeSymbolTableImpl symbol_table; + Server::HotRestartNopImpl restarter(symbol_table); ThreadLocal::InstanceImpl tls; - Stats::HeapStatDataAllocator stats_allocator; + Stats::HeapStatDataAllocator stats_allocator(symbol_table); Stats::ThreadLocalStoreImpl stat_store(options.statsOptions(), stats_allocator); Server::InstanceImpl server(options, time_system, local_address, hooks, restarter, stat_store, diff --git a/test/integration/server.h b/test/integration/server.h index 365211908124..381a04355188 100644 --- a/test/integration/server.h +++ b/test/integration/server.h @@ -90,6 +90,8 @@ class TestScopeWrapper : public Scope { return wrapped_scope_->histogram(name); } + const SymbolTable& symbolTable() const override { return wrapped_scope_->symbolTable(); } + SymbolTable& symbolTable() override { return wrapped_scope_->symbolTable(); } const StatsOptions& statsOptions() const override { return stats_options_; } private: @@ -149,6 +151,9 @@ class TestIsolatedStoreImpl : public StoreRoot { void mergeHistograms(PostMergeCb) override {} Source& source() override { return source_; } + const SymbolTable& symbolTable() const override { return store_.symbolTable(); } + SymbolTable& symbolTable() override { return store_.symbolTable(); } + private: mutable Thread::MutexBasicLockable lock_; IsolatedStoreImpl store_; diff --git a/test/integration/utility.h b/test/integration/utility.h index c22c4bc21d8e..e39a295f682f 100644 --- a/test/integration/utility.h +++ b/test/integration/utility.h @@ -100,8 +100,8 @@ class RawConnectionDriver { Network::ConnectionEvent last_connection_event_; }; - Api::ApiPtr api_; Stats::IsolatedStoreImpl stats_store_; + Api::ApiPtr api_; Event::DispatcherPtr dispatcher_; std::unique_ptr callbacks_; Network::ClientConnectionPtr client_; diff --git a/test/mocks/api/BUILD b/test/mocks/api/BUILD index 65ae8f1431d8..7b04009737f6 100644 --- a/test/mocks/api/BUILD +++ b/test/mocks/api/BUILD @@ -18,6 +18,7 @@ envoy_cc_mock( "//source/common/api:os_sys_calls_lib", "//source/common/common:assert_lib", "//test/mocks/filesystem:filesystem_mocks", + "//test/mocks/stats:stats_mocks", "//test/test_common:test_time_lib", ], ) diff --git a/test/mocks/api/mocks.h b/test/mocks/api/mocks.h index 16d40d257406..9230b627aea0 100644 --- a/test/mocks/api/mocks.h +++ b/test/mocks/api/mocks.h @@ -15,6 +15,7 @@ #endif #include "test/mocks/filesystem/mocks.h" +#include "test/mocks/stats/mocks.h" #include "test/test_common/test_time.h" #include "gmock/gmock.h" @@ -41,6 +42,7 @@ class MockApi : public Api { testing::NiceMock file_system_; Event::GlobalTimeSystem time_system_; + testing::NiceMock stats_store_; }; class MockOsSysCalls : public OsSysCallsImpl { diff --git a/test/mocks/server/mocks.cc b/test/mocks/server/mocks.cc index 9428b79a00a4..171338529964 100644 --- a/test/mocks/server/mocks.cc +++ b/test/mocks/server/mocks.cc @@ -72,7 +72,7 @@ MockGuardDog::MockGuardDog() : watch_dog_(new NiceMock()) { } MockGuardDog::~MockGuardDog() = default; -MockHotRestart::MockHotRestart() { +MockHotRestart::MockHotRestart() : stats_allocator_(symbol_table_.get()) { ON_CALL(*this, logLock()).WillByDefault(ReturnRef(log_lock_)); ON_CALL(*this, accessLogLock()).WillByDefault(ReturnRef(access_log_lock_)); ON_CALL(*this, statsAllocator()).WillByDefault(ReturnRef(stats_allocator_)); diff --git a/test/mocks/server/mocks.h b/test/mocks/server/mocks.h index 4dceae9eea83..5ed87edcb06e 100644 --- a/test/mocks/server/mocks.h +++ b/test/mocks/server/mocks.h @@ -23,6 +23,7 @@ #include "common/http/context_impl.h" #include "common/secret/secret_manager_impl.h" +#include "common/stats/fake_symbol_table_impl.h" #include "extensions/transport_sockets/tls/context_manager_impl.h" @@ -209,6 +210,7 @@ class MockHotRestart : public HotRestart { MOCK_METHOD0(statsAllocator, Stats::StatDataAllocator&()); private: + Test::Global symbol_table_; Thread::MutexBasicLockable log_lock_; Thread::MutexBasicLockable access_log_lock_; Stats::HeapStatDataAllocator stats_allocator_; @@ -457,14 +459,13 @@ class MockFactoryContext : public virtual FactoryContext { testing::NiceMock local_info_; testing::NiceMock random_; testing::NiceMock runtime_loader_; - Stats::IsolatedStoreImpl scope_; + testing::NiceMock scope_; testing::NiceMock thread_local_; Singleton::ManagerPtr singleton_manager_; testing::NiceMock admin_; Stats::IsolatedStoreImpl listener_scope_; Event::GlobalTimeSystem time_system_; testing::NiceMock overload_manager_; - Tracing::HttpNullTracer null_tracer_; Http::ContextImpl http_context_; testing::NiceMock api_; }; diff --git a/test/mocks/stats/BUILD b/test/mocks/stats/BUILD index 73cda48a2b41..711c90c01fc2 100644 --- a/test/mocks/stats/BUILD +++ b/test/mocks/stats/BUILD @@ -17,9 +17,11 @@ envoy_cc_mock( "//include/envoy/stats:timespan", "//include/envoy/thread_local:thread_local_interface", "//include/envoy/upstream:cluster_manager_interface", + "//source/common/stats:fake_symbol_table_lib", "//source/common/stats:histogram_lib", "//source/common/stats:isolated_store_lib", "//source/common/stats:stats_lib", "//test/mocks:common_lib", + "//test/test_common:global_lib", ], ) diff --git a/test/mocks/stats/mocks.cc b/test/mocks/stats/mocks.cc index 92dcd6a2ef0e..1b132a3b378c 100644 --- a/test/mocks/stats/mocks.cc +++ b/test/mocks/stats/mocks.cc @@ -1,5 +1,9 @@ #include "mocks.h" +#include + +#include "common/stats/fake_symbol_table_impl.h" + #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -81,7 +85,8 @@ MockStore::MockStore() { } MockStore::~MockStore() {} -MockIsolatedStatsStore::MockIsolatedStatsStore() {} +MockIsolatedStatsStore::MockIsolatedStatsStore() + : IsolatedStoreImpl(Test::Global::get()) {} MockIsolatedStatsStore::~MockIsolatedStatsStore() {} MockStatsMatcher::MockStatsMatcher() {} diff --git a/test/mocks/stats/mocks.h b/test/mocks/stats/mocks.h index 21aade4b5b1d..b2bd3936e976 100644 --- a/test/mocks/stats/mocks.h +++ b/test/mocks/stats/mocks.h @@ -15,9 +15,12 @@ #include "envoy/thread_local/thread_local.h" #include "envoy/upstream/cluster_manager.h" +#include "common/stats/fake_symbol_table_impl.h" #include "common/stats/histogram_impl.h" #include "common/stats/isolated_store_impl.h" +#include "test/test_common/global.h" + #include "gmock/gmock.h" namespace Envoy { @@ -164,6 +167,10 @@ class MockStore : public Store { MOCK_CONST_METHOD0(histograms, std::vector()); MOCK_CONST_METHOD0(statsOptions, const StatsOptions&()); + SymbolTable& symbolTable() override { return symbol_table_.get(); } + const SymbolTable& symbolTable() const override { return symbol_table_.get(); } + + Test::Global symbol_table_; testing::NiceMock counter_; std::vector> histograms_; StatsOptionsImpl stats_options_; @@ -173,7 +180,8 @@ class MockStore : public Store { * With IsolatedStoreImpl it's hard to test timing stats. * MockIsolatedStatsStore mocks only deliverHistogramToSinks for better testing. */ -class MockIsolatedStatsStore : public IsolatedStoreImpl { +class MockIsolatedStatsStore : private Test::Global, + public IsolatedStoreImpl { public: MockIsolatedStatsStore(); ~MockIsolatedStatsStore(); diff --git a/test/mocks/upstream/host.h b/test/mocks/upstream/host.h index e885c9c310b2..d546111bdead 100644 --- a/test/mocks/upstream/host.h +++ b/test/mocks/upstream/host.h @@ -96,7 +96,7 @@ class MockHostDescription : public HostDescription { testing::NiceMock outlier_detector_; testing::NiceMock health_checker_; testing::NiceMock cluster_; - Stats::IsolatedStoreImpl stats_store_; + testing::NiceMock stats_store_; HostStats stats_{ALL_HOST_STATS(POOL_COUNTER(stats_store_), POOL_GAUGE(stats_store_))}; }; @@ -166,7 +166,7 @@ class MockHost : public Host { testing::NiceMock cluster_; testing::NiceMock outlier_detector_; - Stats::IsolatedStoreImpl stats_store_; + NiceMock stats_store_; HostStats stats_{ALL_HOST_STATS(POOL_COUNTER(stats_store_), POOL_GAUGE(stats_store_))}; }; diff --git a/test/server/config_validation/cluster_manager_test.cc b/test/server/config_validation/cluster_manager_test.cc index a9caa4e415a3..b64e43b0b83d 100644 --- a/test/server/config_validation/cluster_manager_test.cc +++ b/test/server/config_validation/cluster_manager_test.cc @@ -48,6 +48,7 @@ TEST(ValidationClusterManagerTest, MockedMethods) { local_info, secret_manager, *api, http_context, log_manager, singleton_manager, time_system); const envoy::config::bootstrap::v2::Bootstrap bootstrap; + Stats::FakeSymbolTableImpl symbol_table; ClusterManagerPtr cluster_manager = factory.clusterManagerFromProto(bootstrap); EXPECT_EQ(nullptr, cluster_manager->httpConnPoolForCluster("cluster", ResourcePriority::Default, Http::Protocol::Http11, nullptr)); diff --git a/test/server/hot_restart_impl_test.cc b/test/server/hot_restart_impl_test.cc index cc77baf4f517..035c44d01301 100644 --- a/test/server/hot_restart_impl_test.cc +++ b/test/server/hot_restart_impl_test.cc @@ -41,10 +41,11 @@ class HotRestartImplTest : public testing::Test { EXPECT_CALL(options_, statsOptions()).WillRepeatedly(ReturnRef(stats_options_)); // Test we match the correct stat with empty-slots before, after, or both. - hot_restart_ = std::make_unique(options_); + hot_restart_ = std::make_unique(options_, symbol_table_); hot_restart_->drainParentListeners(); } + Stats::FakeSymbolTableImpl symbol_table_; Api::MockOsSysCalls os_sys_calls_; TestThreadsafeSingletonInjector os_calls{&os_sys_calls_}; NiceMock options_; @@ -131,7 +132,7 @@ TEST_F(HotRestartImplTest, crossAlloc) { EXPECT_CALL(os_sys_calls_, mmap(_, _, _, _, _, _)) .WillOnce(Return(Api::SysCallPtrResult{buffer_.data(), 0})); EXPECT_CALL(os_sys_calls_, bind(_, _, _)); - HotRestartImpl hot_restart2(options_); + HotRestartImpl hot_restart2(options_, symbol_table_); Stats::RawStatData* stat1_prime = hot_restart2.statsAllocator().alloc("stat1"); Stats::RawStatData* stat3_prime = hot_restart2.statsAllocator().alloc("stat3"); Stats::RawStatData* stat5_prime = hot_restart2.statsAllocator().alloc("stat5"); diff --git a/test/server/http/admin_test.cc b/test/server/http/admin_test.cc index 76723982e456..bae835f4f65a 100644 --- a/test/server/http/admin_test.cc +++ b/test/server/http/admin_test.cc @@ -50,7 +50,7 @@ namespace Server { class AdminStatsTest : public testing::TestWithParam { public: - AdminStatsTest() : alloc_(options_) { + AdminStatsTest() : alloc_(options_, symbol_table_) { store_ = std::make_unique(options_, alloc_); store_->addSink(sink_); } @@ -63,6 +63,7 @@ class AdminStatsTest : public testing::TestWithParam main_thread_dispatcher_; NiceMock tls_; Stats::StatsOptionsImpl options_; @@ -1260,6 +1261,8 @@ class HistogramWrapper { class PrometheusStatsFormatterTest : public testing::Test { protected: + PrometheusStatsFormatterTest() : alloc_(symbol_table_) {} + void addCounter(const std::string& name, std::vector cluster_tags) { std::string tname = std::string(name); counters_.push_back(alloc_.makeCounter(name, std::move(tname), std::move(cluster_tags))); @@ -1274,6 +1277,7 @@ class PrometheusStatsFormatterTest : public testing::Test { histograms_.push_back(histogram); } + Stats::FakeSymbolTableImpl symbol_table_; Stats::StatsOptionsImpl stats_options_; Stats::HeapStatDataAllocator alloc_; std::vector counters_; diff --git a/test/test_common/BUILD b/test/test_common/BUILD index b54f4f282686..50392013f14f 100644 --- a/test/test_common/BUILD +++ b/test/test_common/BUILD @@ -108,6 +108,7 @@ envoy_cc_test_library( "//source/common/network:address_lib", "//source/common/network:utility_lib", "//source/common/protobuf:utility_lib", + "//source/common/stats:fake_symbol_table_lib", "//source/common/stats:stats_lib", "//source/common/stats:stats_options_lib", "//test/mocks/stats:stats_mocks", diff --git a/test/test_common/global.h b/test/test_common/global.h index fc788a0830d8..3628180bd6fc 100644 --- a/test/test_common/global.h +++ b/test/test_common/global.h @@ -117,6 +117,7 @@ template class Global { public: Global() : singleton_(Globals::get()) {} Type& get() { return singleton_->ref(); } + const Type& get() const { return singleton_->ref(); } Type* operator->() { return singleton_->ptr(); } Type& operator*() { return singleton_->ref(); } diff --git a/test/test_common/utility.cc b/test/test_common/utility.cc index 263971a611c8..e9bbcd3e06ae 100644 --- a/test/test_common/utility.cc +++ b/test/test_common/utility.cc @@ -369,8 +369,9 @@ bool TestHeaderMapImpl::has(const LowerCaseString& key) { return get(key) != nul namespace Stats { -MockedTestAllocator::MockedTestAllocator(const StatsOptions& stats_options) - : TestAllocator(stats_options) { +MockedTestAllocator::MockedTestAllocator(const StatsOptions& stats_options, + SymbolTable& symbol_table) + : TestAllocator(stats_options, symbol_table) { ON_CALL(*this, alloc(_)).WillByDefault(Invoke([this](absl::string_view name) -> RawStatData* { return TestAllocator::alloc(name); })); diff --git a/test/test_common/utility.h b/test/test_common/utility.h index a91e68d74176..d30c0f9a7569 100644 --- a/test/test_common/utility.h +++ b/test/test_common/utility.h @@ -21,6 +21,7 @@ #include "common/common/thread.h" #include "common/http/header_map_impl.h" #include "common/protobuf/utility.h" +#include "common/stats/fake_symbol_table_impl.h" #include "common/stats/raw_stat_data.h" #include "test/test_common/printers.h" @@ -475,14 +476,15 @@ class TestAllocator : public RawStatDataAllocator { } }; - explicit TestAllocator(const StatsOptions& stats_options) - : RawStatDataAllocator(mutex_, hash_set_, stats_options), + TestAllocator(const StatsOptions& stats_options, SymbolTable& symbol_table) + : RawStatDataAllocator(mutex_, hash_set_, stats_options, symbol_table), block_memory_(std::make_unique( RawStatDataSet::numBytes(block_hash_options_, stats_options))), hash_set_(block_hash_options_, true /* init */, block_memory_.get(), stats_options) {} ~TestAllocator() { EXPECT_EQ(0, hash_set_.size()); } private: + FakeSymbolTableImpl symbol_table_; Thread::MutexBasicLockable mutex_; TestBlockMemoryHashSetOptions block_hash_options_; std::unique_ptr block_memory_; @@ -491,7 +493,7 @@ class TestAllocator : public RawStatDataAllocator { class MockedTestAllocator : public TestAllocator { public: - MockedTestAllocator(const StatsOptions& stats_options); + MockedTestAllocator(const StatsOptions& stats_options, SymbolTable& symbol_table); virtual ~MockedTestAllocator(); MOCK_METHOD1(alloc, RawStatData*(absl::string_view name));