Skip to content

Commit

Permalink
Plumb through symbol tables everywhere. Pulled from envoyproxy#6161.
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Marantz <[email protected]>
  • Loading branch information
jmarantz committed Mar 15, 2019
1 parent 2953bab commit ea4d73f
Show file tree
Hide file tree
Showing 61 changed files with 253 additions and 91 deletions.
5 changes: 5 additions & 0 deletions include/envoy/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Api {
* @return a reference to the TimeSource
*/
virtual TimeSource& timeSource() PURE;

/**
* @return a reference to the Stats::Store
*/
virtual Stats::Store& statsStore() PURE;
};

typedef std::unique_ptr<Api> ApiPtr;
Expand Down
5 changes: 5 additions & 0 deletions include/envoy/server/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ class Instance {
*/
virtual TimeSource& timeSource() PURE;

/**
* @return the statistics symbol table.
*/
virtual Stats::SymbolTable& symbolTable() PURE;

/**
* @return the flush interval of stats sinks.
*/
Expand Down
8 changes: 7 additions & 1 deletion include/envoy/stats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion include/envoy/stats/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

#include <cstdint>
#include <memory>
#include <string>

#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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions include/envoy/stats/stat_data_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions source/common/api/api_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
namespace Envoy {
namespace Api {

Impl::Impl(Thread::ThreadFactory& thread_factory, Stats::Store&, Event::TimeSystem& time_system)
: thread_factory_(thread_factory), time_system_(time_system) {}
Impl::Impl(Thread::ThreadFactory& thread_factory, Stats::Store& stats_store,
Event::TimeSystem& time_system)
: thread_factory_(thread_factory), time_system_(time_system), stats_store_(stats_store) {}

Event::DispatcherPtr Impl::allocateDispatcher() {
return std::make_unique<Event::DispatcherImpl>(*this, time_system_);
Expand Down
6 changes: 5 additions & 1 deletion source/common/api/api_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "envoy/api/api.h"
#include "envoy/event/timer.h"
#include "envoy/filesystem/filesystem.h"
#include "envoy/stats/store.h"
#include "envoy/thread/thread.h"

#include "common/filesystem/filesystem_impl.h"
Expand All @@ -18,19 +19,22 @@ namespace Api {
*/
class Impl : public Api {
public:
Impl(Thread::ThreadFactory& thread_factory, Stats::Store&, Event::TimeSystem& time_system);
Impl(Thread::ThreadFactory& thread_factory, Stats::Store& stats_store,
Event::TimeSystem& time_system);

// Api::Api
Event::DispatcherPtr allocateDispatcher() override;
Event::DispatcherPtr allocateDispatcher(Buffer::WatermarkFactoryPtr&& watermark_factory) override;
Thread::ThreadFactory& threadFactory() override { return thread_factory_; }
Filesystem::Instance& fileSystem() override { return file_system_; }
TimeSource& timeSource() override { return time_system_; }
Stats::Store& statsStore() override { return stats_store_; }

private:
Thread::ThreadFactory& thread_factory_;
Filesystem::InstanceImpl file_system_;
Event::TimeSystem& time_system_;
Stats::Store& stats_store_;
};

} // namespace Api
Expand Down
5 changes: 5 additions & 0 deletions source/common/router/router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ FilterUtility::finalTimeout(const RouteEntry& route, Http::HeaderMap& request_he
return timeout;
}

Filter::Filter(FilterConfig& config)
: config_(config), downstream_response_started_(false), downstream_end_stream_(false),
do_shadowing_(false), is_retry_(false),
attempting_internal_redirect_with_complete_stream_(false) {}

Filter::~Filter() {
// Upstream resources should already have been cleaned.
ASSERT(!upstream_request_);
Expand Down
17 changes: 7 additions & 10 deletions source/common/router/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class FilterConfig {
: scope_(scope), local_info_(local_info), cm_(cm), runtime_(runtime),
random_(random), stats_{ALL_ROUTER_STATS(POOL_COUNTER_PREFIX(scope, stat_prefix))},
emit_dynamic_stats_(emit_dynamic_stats), start_child_span_(start_child_span),
suppress_envoy_headers_(suppress_envoy_headers), http_context_(http_context),
shadow_writer_(std::move(shadow_writer)), time_source_(time_source) {}
suppress_envoy_headers_(suppress_envoy_headers), shadow_writer_(std::move(shadow_writer)),
time_source_(time_source), http_context_(http_context) {}

FilterConfig(const std::string& stat_prefix, Server::Configuration::FactoryContext& context,
ShadowWriterPtr&& shadow_writer,
Expand All @@ -121,6 +121,7 @@ class FilterConfig {

ShadowWriter& shadowWriter() { return *shadow_writer_; }
TimeSource& timeSource() { return time_source_; }
Http::Context& httpContext() { return http_context_; }

Stats::Scope& scope_;
const LocalInfo::LocalInfo& local_info_;
Expand All @@ -132,11 +133,11 @@ class FilterConfig {
const bool start_child_span_;
const bool suppress_envoy_headers_;
std::list<AccessLog::InstanceSharedPtr> upstream_logs_;
Http::Context& http_context_;

private:
ShadowWriterPtr shadow_writer_;
TimeSource& time_source_;
Http::Context& http_context_;
};

typedef std::shared_ptr<FilterConfig> FilterConfigSharedPtr;
Expand All @@ -148,12 +149,8 @@ class Filter : Logger::Loggable<Logger::Id::router>,
public Http::StreamDecoderFilter,
public Upstream::LoadBalancerContextBase {
public:
Filter(FilterConfig& config)
: config_(config), downstream_response_started_(false), downstream_end_stream_(false),
do_shadowing_(false), is_retry_(false),
attempting_internal_redirect_with_complete_stream_(false) {}

~Filter();
explicit Filter(FilterConfig& config);
~Filter() override;

// Http::StreamFilterBase
void onDestroy() override;
Expand Down Expand Up @@ -398,7 +395,7 @@ class Filter : Logger::Loggable<Logger::Id::router>,
// and handle difference between gRPC and non-gRPC requests.
void handleNon5xxResponseHeaders(const Http::HeaderMap& headers, bool end_stream);
TimeSource& timeSource() { return config_.timeSource(); }
Http::Context& httpContext() { return config_.http_context_; }
Http::Context& httpContext() { return config_.httpContext(); }

FilterConfig& config_;
Http::StreamDecoderFilterCallbacks* callbacks_{};
Expand Down
5 changes: 5 additions & 0 deletions source/common/stats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
],
Expand Down Expand Up @@ -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",
],
Expand Down Expand Up @@ -202,4 +206,5 @@ envoy_cc_library(
name = "utility_lib",
srcs = ["utility.cc"],
hdrs = ["utility.h"],
deps = [":symbol_table_lib"],
)
2 changes: 0 additions & 2 deletions source/common/stats/heap_stat_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions source/common/stats/heap_stat_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string>
#include <unordered_set>

#include "envoy/stats/symbol_table.h"

#include "common/common/hash.h"
#include "common/common/thread.h"
#include "common/common/thread_annotations.h"
Expand Down Expand Up @@ -53,8 +55,8 @@ struct HeapStatData {
*/
class HeapStatDataAllocator : public StatDataAllocatorImpl<HeapStatData> {
public:
HeapStatDataAllocator();
~HeapStatDataAllocator();
HeapStatDataAllocator(SymbolTable& symbol_table) : StatDataAllocatorImpl(symbol_table) {}
~HeapStatDataAllocator() override;

// StatDataAllocatorImpl
HeapStatData* alloc(absl::string_view name) override;
Expand Down
14 changes: 13 additions & 1 deletion source/common/stats/isolated_store_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
#include <string>

#include "common/common/utility.h"
#include "common/stats/fake_symbol_table_impl.h"
#include "common/stats/histogram_impl.h"
#include "common/stats/utility.h"

namespace Envoy {
namespace Stats {

IsolatedStoreImpl::IsolatedStoreImpl()
: counters_([this](const std::string& name) -> CounterSharedPtr {
: IsolatedStoreImpl(std::make_unique<FakeSymbolTableImpl>()) {}

IsolatedStoreImpl::IsolatedStoreImpl(std::unique_ptr<SymbolTable> 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<Tag> tags;
return alloc_.makeCounter(name, std::move(tag_extracted_name), std::move(tags));
Expand Down Expand Up @@ -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_;
Expand Down
13 changes: 11 additions & 2 deletions source/common/stats/isolated_store_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -22,7 +25,7 @@ namespace Stats {
*/
template <class Base> class IsolatedStatsCache {
public:
typedef std::function<std::shared_ptr<Base>(const std::string& name)> Allocator;
using Allocator = std::function<std::shared_ptr<Base>(const std::string& name)>;

IsolatedStatsCache(Allocator alloc) : alloc_(alloc) {}

Expand All @@ -48,13 +51,15 @@ template <class Base> class IsolatedStatsCache {
}

private:
std::unordered_map<std::string, std::shared_ptr<Base>> stats_;
absl::flat_hash_map<std::string, std::shared_ptr<Base>> stats_;
Allocator alloc_;
};

class IsolatedStoreImpl : public Store {
public:
IsolatedStoreImpl();
explicit IsolatedStoreImpl(std::unique_ptr<SymbolTable> symbol_table);
explicit IsolatedStoreImpl(SymbolTable& symbol_table);

// Stats::Scope
Counter& counter(const std::string& name) override { return counters_.get(name); }
Expand All @@ -66,6 +71,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<CounterSharedPtr> counters() const override { return counters_.toVector(); }
Expand All @@ -75,6 +82,8 @@ class IsolatedStoreImpl : public Store {
}

private:
std::unique_ptr<SymbolTable> symbol_table_storage_;
SymbolTable& symbol_table_;
HeapStatDataAllocator alloc_;
IsolatedStatsCache<Counter> counters_;
IsolatedStatsCache<Gauge> gauges_;
Expand Down
6 changes: 4 additions & 2 deletions source/common/stats/raw_stat_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -97,8 +98,9 @@ using RawStatDataSet = BlockMemoryHashSet<Stats::RawStatData>;
class RawStatDataAllocator : public StatDataAllocatorImpl<RawStatData> {
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; }
Expand Down
Loading

0 comments on commit ea4d73f

Please sign in to comment.