Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats: fix listener/filter scope prefixes #1108

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/envoy/server/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Listener {
/**
* @return Stats::Scope& the stats scope to use for all listener specific stats.
*/
virtual Stats::Scope& scope() PURE;
virtual Stats::Scope& listenerScope() PURE;
};

typedef std::unique_ptr<Listener> ListenerPtr;
Expand Down
10 changes: 6 additions & 4 deletions source/server/configuration_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,24 @@ void MainImpl::initializeTracers(const Json::Object& configuration) {

const std::list<ListenerPtr>& MainImpl::listeners() { return listeners_; }

MainImpl::ListenerConfig::ListenerConfig(Instance& server, Json::Object& json) : server_(server) {
address_ = Network::Utility::resolveUrl(json.getString("address"));
MainImpl::ListenerConfig::ListenerConfig(Instance& server, Json::Object& json)
: server_(server), address_(Network::Utility::resolveUrl(json.getString("address"))),
global_scope_(server.stats().createScope("")) {

// ':' is a reserved char in statsd. Do the translation here to avoid costly inline translations
// later.
std::string final_stat_name = fmt::format("listener.{}.", address_->asString());
std::replace(final_stat_name.begin(), final_stat_name.end(), ':', '_');

scope_ = server.stats().createScope(final_stat_name);
listener_scope_ = server.stats().createScope(final_stat_name);
LOG(info, " address={}", address_->asString());

json.validateSchema(Json::Schema::LISTENER_SCHEMA);

if (json.hasObject("ssl_context")) {
Ssl::ContextConfigImpl context_config(*json.getObject("ssl_context"));
ssl_context_ = server.sslContextManager().createSslServerContext(*scope_, context_config);
ssl_context_ =
server.sslContextManager().createSslServerContext(*listener_scope_, context_config);
}

bind_to_port_ = json.getBoolean("bind_to_port", true);
Expand Down
6 changes: 4 additions & 2 deletions source/server/configuration_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class MainImpl : Logger::Loggable<Logger::Id::config>, public Main {
bool useProxyProto() override { return use_proxy_proto_; }
bool useOriginalDst() override { return use_original_dst_; }
uint32_t perConnectionBufferLimitBytes() override { return per_connection_buffer_limit_bytes_; }
Stats::Scope& listenerScope() override { return *listener_scope_; }

// Server::Configuration::FactoryContext
AccessLog::AccessLogManager& accessLogManager() override { return server_.accessLogManager(); }
Expand All @@ -193,7 +194,7 @@ class MainImpl : Logger::Loggable<Logger::Id::config>, public Main {
}
Envoy::Runtime::Loader& runtime() override { return server_.runtime(); }
Instance& server() override { return server_; }
Stats::Scope& scope() override { return *scope_; }
Stats::Scope& scope() override { return *global_scope_; }
ThreadLocal::Instance& threadLocal() override { return server_.threadLocal(); }

// Network::FilterChainFactory
Expand All @@ -203,7 +204,8 @@ class MainImpl : Logger::Loggable<Logger::Id::config>, public Main {
Instance& server_;
Network::Address::InstanceConstSharedPtr address_;
bool bind_to_port_{};
Stats::ScopePtr scope_;
Stats::ScopePtr global_scope_; // Stats with global named scope, but needed for LDS cleanup.
Stats::ScopePtr listener_scope_; // Stats with listener named scope.
Ssl::ServerContextPtr ssl_context_;
bool use_proxy_proto_{};
bool use_original_dst_{};
Expand Down
5 changes: 3 additions & 2 deletions source/server/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ void Worker::initializeConfiguration(Server::Configuration::Main& config,
.per_connection_buffer_limit_bytes_ = listener->perConnectionBufferLimitBytes()};
if (listener->sslContext()) {
handler_->addSslListener(listener->filterChainFactory(), *listener->sslContext(),
*socket_map.at(listener.get()), listener->scope(), listener_options);
*socket_map.at(listener.get()), listener->listenerScope(),
listener_options);
} else {
handler_->addListener(listener->filterChainFactory(), *socket_map.at(listener.get()),
listener->scope(), listener_options);
listener->listenerScope(), listener_options);
}
}

Expand Down
45 changes: 45 additions & 0 deletions test/server/configuration_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,50 @@ TEST_F(ConfigurationImplTest, ConfigurationFailsWhenInvalidTracerSpecified) {
"No HttpTracerFactory found for type: invalid");
}

class TestStatsConfigFactory : public NamedNetworkFilterConfigFactory {
public:
// NetworkFilterConfigFactory
NetworkFilterFactoryCb createFilterFactory(const Json::Object&,
FactoryContext& context) override {
context.scope().counter("bar").inc();
return [](Network::FilterManager&) -> void {};
}
std::string name() override { return "stats_test"; }
NetworkFilterType type() override { return NetworkFilterType::Read; }
};

TEST_F(ConfigurationImplTest, StatsScopeTest) {
RegisterNamedNetworkFilterConfigFactory<TestStatsConfigFactory> registered;

std::string json = R"EOF(
{
"listeners" : [
{
"address": "tcp://127.0.0.1:1234",
"filters": [
{
"type" : "read",
"name" : "stats_test",
"config" : {}
}
]
}
],
"cluster_manager": {
"clusters": []
}
}
)EOF";

Json::ObjectSharedPtr loader = Json::Factory::loadFromString(json);
MainImpl config(server_, cluster_manager_factory_);
config.initialize(*loader);
config.listeners().front()->listenerScope().counter("foo").inc();

EXPECT_EQ(1UL, server_.stats_store_.counter("bar").value());
EXPECT_EQ(1UL, server_.stats_store_.counter("listener.127.0.0.1_1234.foo").value());
}

/**
* Config registration for the echo filter using the deprecated registration class.
*/
Expand Down Expand Up @@ -507,6 +551,7 @@ TEST_F(ConfigurationImplTest, DeprecatedFilterConfigFactoryRegistrationTest) {
MainImpl config(server_, cluster_manager_factory_);
config.initialize(*loader);
}

} // Configuration
} // Server
} // Envoy