Skip to content

Commit

Permalink
server: expose flushStats() method (envoyproxy#10097)
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Nino <jnino@lyft.com>
  • Loading branch information
junr03 committed Feb 25, 2020
1 parent a66b51a commit b6d1fec
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
7 changes: 7 additions & 0 deletions include/envoy/server/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ class Instance {
*/
virtual std::chrono::milliseconds statsFlushInterval() const PURE;

/**
* Flush the stats sinks outside of a flushing interval.
* Note: stats flushing may not be synchronous.
* Therefore, this function may return prior to flushing taking place.
*/
virtual void flushStats() PURE;

/**
* @return ProtobufMessage::ValidationContext& validation context for configuration
* messages.
Expand Down
1 change: 1 addition & 0 deletions source/server/config_validation/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class ValidationInstance final : Logger::Loggable<Logger::Id::main>,
std::chrono::milliseconds statsFlushInterval() const override {
return config_.statsFlushInterval();
}
void flushStats() override { NOT_IMPLEMENTED_GCOVR_EXCL_LINE; }
ProtobufMessage::ValidationContext& messageValidationContext() override {
return validation_context_;
}
Expand Down
2 changes: 1 addition & 1 deletion source/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class InstanceImpl final : Logger::Loggable<Logger::Id::main>,
ThreadLocal::Instance& threadLocal() override { return thread_local_; }
const LocalInfo::LocalInfo& localInfo() const override { return *local_info_; }
TimeSource& timeSource() override { return time_source_; }
void flushStats() override;

Configuration::ServerFactoryContext& serverFactoryContext() override { return server_contexts_; }

Expand All @@ -268,7 +269,6 @@ class InstanceImpl final : Logger::Loggable<Logger::Id::main>,

private:
ProtobufTypes::MessagePtr dumpBootstrapConfig();
void flushStats();
void flushStatsInternal();
void updateServerStats();
void initialize(const Options& options, Network::Address::InstanceConstSharedPtr local_address,
Expand Down
1 change: 1 addition & 0 deletions test/mocks/server/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ class MockInstance : public Instance {
MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ());
MOCK_METHOD(const LocalInfo::LocalInfo&, localInfo, (), (const));
MOCK_METHOD(std::chrono::milliseconds, statsFlushInterval, (), (const));
MOCK_METHOD(void, flushStats, ());
MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ());
MOCK_METHOD(Configuration::ServerFactoryContext&, serverFactoryContext, ());
MOCK_METHOD(Configuration::TransportSocketFactoryContext&, transportSocketFactoryContext, ());
Expand Down
38 changes: 31 additions & 7 deletions test/server/server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,43 @@ class TestWithSimTimeAndRealSymbolTables : public Event::TestUsingSimulatedTime
}
};

class ServerStatsTest : public TestWithSimTimeAndRealSymbolTables, public ServerInstanceImplTest {
class ServerStatsTest
: public TestWithSimTimeAndRealSymbolTables,
public ServerInstanceImplTestBase,
public testing::TestWithParam<std::tuple<Network::Address::IpVersion, bool>> {
protected:
ServerStatsTest() {
version_ = std::get<0>(GetParam());
manual_flush_ = std::get<1>(GetParam());
}

void flushStats() {
// Default flush interval is 5 seconds.
simTime().sleep(std::chrono::seconds(6));
if (manual_flush_) {
server_->flushStats();
} else {
// Default flush interval is 5 seconds.
simTime().sleep(std::chrono::seconds(6));
}
server_->dispatcher().run(Event::Dispatcher::RunType::Block);
}

bool manual_flush_;
};

std::string ipFlushingModeTestParamsToString(
const ::testing::TestParamInfo<std::tuple<Network::Address::IpVersion, bool>>& params) {
return fmt::format(
"{}_{}",
TestUtility::ipTestParamsToString(
::testing::TestParamInfo<Network::Address::IpVersion>(std::get<0>(params.param), 0)),
std::get<1>(params.param) ? "with_manual_flush" : "with_time_based_flush");
}

INSTANTIATE_TEST_SUITE_P(
IpVersionsFlushingMode, ServerStatsTest,
testing::Combine(testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), testing::Bool()),
ipFlushingModeTestParamsToString);

TEST_P(ServerStatsTest, FlushStats) {
initialize("test/server/test_data/server/empty_bootstrap.yaml");
Stats::Gauge& recent_lookups =
Expand All @@ -505,10 +533,6 @@ TEST_P(ServerStatsTest, FlushStats) {
EXPECT_EQ(recent_lookups.value(), strobed_recent_lookups);
}

INSTANTIATE_TEST_SUITE_P(IpVersions, ServerStatsTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);

// Default validation mode
TEST_P(ServerInstanceImplTest, ValidationDefault) {
options_.service_cluster_name_ = "some_cluster_name";
Expand Down

0 comments on commit b6d1fec

Please sign in to comment.