Skip to content
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
24 changes: 24 additions & 0 deletions include/envoy/stats/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,27 @@ class Scope {
virtual void deliverHistogramToSinks(const Histogram& histogram, uint64_t value) PURE;

/**
* @param name The name of the stat, obtained from the SymbolTable.
* @return a counter within the scope's namespace.
*/
virtual Counter& counterFromStatName(StatName name) PURE;

/**
* TODO(jmarantz): this variant is deprecated: use counterFromStatName.
* @param name The name, expressed as a string.
* @return a counter within the scope's namespace.
*/
virtual Counter& counter(const std::string& name) PURE;

/**
* @param name The name of the stat, obtained from the SymbolTable.
* @return a gauge within the scope's namespace.
*/
virtual Gauge& gaugeFromStatName(StatName name) PURE;

/**
* TODO(jmarantz): this variant is deprecated: use gaugeFromStatName.
* @param name The name, expressed as a string.
* @return a gauge within the scope's namespace.
*/
virtual Gauge& gauge(const std::string& name) PURE;
Expand All @@ -58,6 +74,14 @@ class Scope {
virtual NullGaugeImpl& nullGauge(const std::string& name) PURE;

/**
* @param name The name of the stat, obtained from the SymbolTable.
* @return a histogram within the scope's namespace with a particular value type.
*/
virtual Histogram& histogramFromStatName(StatName name) PURE;

/**
* TODO(jmarantz): this variant is deprecated: use histogramFromStatName.
* @param name The name, expressed as a string.
* @return a histogram within the scope's namespace with a particular value type.
*/
virtual Histogram& histogram(const std::string& name) PURE;
Expand Down
23 changes: 23 additions & 0 deletions source/common/stats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ envoy_cc_library(
deps = [
":fake_symbol_table_lib",
":histogram_lib",
":scope_prefixer_lib",
":stats_lib",
":stats_options_lib",
":store_impl_lib",
"//include/envoy/stats:stats_macros",
"//source/common/stats:heap_stat_data_lib",
],
Expand All @@ -62,6 +64,15 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "store_impl_lib",
hdrs = ["store_impl.h"],
deps = [
":symbol_table_lib",
"//include/envoy/stats:stats_interface",
],
)

envoy_cc_library(
name = "raw_stat_data_lib",
srcs = ["raw_stat_data.cc"],
Expand All @@ -76,6 +87,17 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "scope_prefixer_lib",
srcs = ["scope_prefixer.cc"],
hdrs = ["scope_prefixer.h"],
deps = [
":symbol_table_lib",
":utility_lib",
"//include/envoy/stats:stats_interface",
],
)

envoy_cc_library(
name = "source_impl_lib",
srcs = ["source_impl.cc"],
Expand Down Expand Up @@ -195,6 +217,7 @@ envoy_cc_library(
hdrs = ["thread_local_store.h"],
deps = [
":heap_stat_data_lib",
":scope_prefixer_lib",
":stats_lib",
":stats_matcher_lib",
":tag_producer_lib",
Expand Down
29 changes: 3 additions & 26 deletions source/common/stats/isolated_store_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/common/utility.h"
#include "common/stats/fake_symbol_table_impl.h"
#include "common/stats/histogram_impl.h"
#include "common/stats/scope_prefixer.h"
#include "common/stats/utility.h"

namespace Envoy {
Expand All @@ -22,7 +23,7 @@ IsolatedStoreImpl::IsolatedStoreImpl(std::unique_ptr<SymbolTable>&& symbol_table
}

IsolatedStoreImpl::IsolatedStoreImpl(SymbolTable& symbol_table)
: symbol_table_(symbol_table), alloc_(symbol_table_),
: StoreImpl(symbol_table), alloc_(symbol_table),
counters_([this](const std::string& name) -> CounterSharedPtr {
std::string tag_extracted_name = name;
std::vector<Tag> tags;
Expand All @@ -37,32 +38,8 @@ IsolatedStoreImpl::IsolatedStoreImpl(SymbolTable& symbol_table)
return std::make_shared<HistogramImpl>(name, *this, std::string(name), std::vector<Tag>());
}) {}

struct IsolatedScopeImpl : public Scope {
IsolatedScopeImpl(IsolatedStoreImpl& parent, const std::string& prefix)
: parent_(parent), prefix_(Utility::sanitizeStatsName(prefix)) {}

// Stats::Scope
ScopePtr createScope(const std::string& name) override {
return ScopePtr{new IsolatedScopeImpl(parent_, prefix_ + name)};
}
void deliverHistogramToSinks(const Histogram&, uint64_t) override {}
Counter& counter(const std::string& name) override { return parent_.counter(prefix_ + name); }
Gauge& gauge(const std::string& name) override { return parent_.gauge(prefix_ + name); }
NullGaugeImpl& nullGauge(const std::string&) override { return null_gauge_; }
Histogram& histogram(const std::string& name) override {
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_;
NullGaugeImpl null_gauge_;
const std::string prefix_;
};

ScopePtr IsolatedStoreImpl::createScope(const std::string& name) {
return ScopePtr{new IsolatedScopeImpl(*this, name)};
return std::make_unique<ScopePrefixer>(name, *this);
}

} // namespace Stats
Expand Down
11 changes: 3 additions & 8 deletions source/common/stats/isolated_store_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "common/common/utility.h"
#include "common/stats/heap_stat_data.h"
#include "common/stats/stats_options_impl.h"
#include "common/stats/store_impl.h"
#include "common/stats/symbol_table_impl.h"
#include "common/stats/utility.h"

Expand Down Expand Up @@ -55,7 +56,7 @@ template <class Base> class IsolatedStatsCache {
Allocator alloc_;
};

class IsolatedStoreImpl : public Store {
class IsolatedStoreImpl : public StoreImpl {
public:
IsolatedStoreImpl();
explicit IsolatedStoreImpl(SymbolTable& symbol_table);
Expand All @@ -66,13 +67,8 @@ class IsolatedStoreImpl : public Store {
void deliverHistogramToSinks(const Histogram&, uint64_t) override {}
Gauge& gauge(const std::string& name) override { return gauges_.get(name); }
NullGaugeImpl& nullGauge(const std::string&) override { return null_gauge_; }
Histogram& histogram(const std::string& name) override {
Histogram& histogram = histograms_.get(name);
return histogram;
}
Histogram& histogram(const std::string& name) override { return histograms_.get(name); }
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 @@ -85,7 +81,6 @@ class IsolatedStoreImpl : public Store {
IsolatedStoreImpl(std::unique_ptr<SymbolTable>&& symbol_table);

std::unique_ptr<SymbolTable> symbol_table_storage_;
SymbolTable& symbol_table_;
HeapStatDataAllocator alloc_;
IsolatedStatsCache<Counter> counters_;
IsolatedStatsCache<Gauge> gauges_;
Expand Down
35 changes: 35 additions & 0 deletions source/common/stats/scope_prefixer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "common/stats/scope_prefixer.h"

#include "envoy/stats/scope.h"

#include "common/stats/symbol_table_impl.h"
#include "common/stats/utility.h"

namespace Envoy {
namespace Stats {

ScopePrefixer::ScopePrefixer(absl::string_view prefix, Scope& scope)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this variant used, is it used in a follow up?

Also, somewhat nitty, but for clarity I would prefer a single constructor that looks like:

ScopePrefixer::ScopePrefixer(absl::string_view prefix, Scope& scope, bool take_ownership)

And then personally I would have the following members:

std::unique_ptr<Scope> owned_scope_;
Scope* scope_to_use_;

And avoid the manual deletion. WDYT?

Copy link
Contributor Author

@jmarantz jmarantz Apr 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I like your pattern better. However I also can't justify the speculative generality here; I needed it in some past iteration and don't see a use-case now. If I have to add it back in the future I'll use your pattern.

update coming.

: prefix_(Utility::sanitizeStatsName(prefix)), scope_(scope) {}

ScopePtr ScopePrefixer::createScope(const std::string& name) {
return std::make_unique<ScopePrefixer>(prefix_ + name, scope_);
}

Counter& ScopePrefixer::counterFromStatName(StatName name) {
return counter(symbolTable().toString(name));
}

Gauge& ScopePrefixer::gaugeFromStatName(StatName name) {
return gauge(symbolTable().toString(name));
}

Histogram& ScopePrefixer::histogramFromStatName(StatName name) {
return histogram(symbolTable().toString(name));
}

void ScopePrefixer::deliverHistogramToSinks(const Histogram& histograms, uint64_t val) {
scope_.deliverHistogramToSinks(histograms, val);
}

} // namespace Stats
} // namespace Envoy
38 changes: 38 additions & 0 deletions source/common/stats/scope_prefixer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "envoy/stats/scope.h"

#include "common/stats/symbol_table_impl.h"

namespace Envoy {
namespace Stats {

// Implements a Scope that delegates to a passed-in scope, prefixing all names
// prior to creation.
class ScopePrefixer : public Scope {
public:
ScopePrefixer(absl::string_view prefix, Scope& scope);

// Scope
ScopePtr createScope(const std::string& name) override;
Counter& counter(const std::string& name) override { return scope_.counter(prefix_ + name); }
Gauge& gauge(const std::string& name) override { return scope_.gauge(prefix_ + name); }
Histogram& histogram(const std::string& name) override {
return scope_.histogram(prefix_ + name);
}
void deliverHistogramToSinks(const Histogram& histograms, uint64_t val) override;
Counter& counterFromStatName(StatName name) override;
Gauge& gaugeFromStatName(StatName name) override;
Histogram& histogramFromStatName(StatName name) override;

const Stats::StatsOptions& statsOptions() const override { return scope_.statsOptions(); }
const SymbolTable& symbolTable() const override { return scope_.symbolTable(); }
virtual SymbolTable& symbolTable() override { return scope_.symbolTable(); }

NullGaugeImpl& nullGauge(const std::string& str) override { return scope_.nullGauge(str); }

private:
std::string prefix_;
Scope& scope_;
};

} // namespace Stats
} // namespace Envoy
36 changes: 36 additions & 0 deletions source/common/stats/store_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "envoy/stats/stats.h"
#include "envoy/stats/store.h"

#include "common/stats/symbol_table_impl.h"

namespace Envoy {
namespace Stats {

/**
* Implements common parts of the Store API needed by multiple derivations of Store.
*/
class StoreImpl : public Store {
public:
explicit StoreImpl(SymbolTable& symbol_table) : symbol_table_(symbol_table) {}

Counter& counterFromStatName(StatName name) override {
return counter(symbol_table_.toString(name));
}

Gauge& gaugeFromStatName(StatName name) override { return gauge(symbol_table_.toString(name)); }

Histogram& histogramFromStatName(StatName name) override {
return histogram(symbol_table_.toString(name));
}

SymbolTable& symbolTable() override { return symbol_table_; }
const SymbolTable& symbolTable() const override { return symbol_table_; }

private:
SymbolTable& symbol_table_;
};

} // namespace Stats
} // namespace Envoy
24 changes: 21 additions & 3 deletions source/common/stats/thread_local_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "common/stats/heap_stat_data.h"
#include "common/stats/histogram_impl.h"
#include "common/stats/source_impl.h"
#include "common/stats/symbol_table_impl.h"
#include "common/stats/utility.h"

#include "absl/container/flat_hash_map.h"
Expand Down Expand Up @@ -139,16 +140,23 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
~ThreadLocalStoreImpl();

// Stats::Scope
Counter& counterFromStatName(StatName name) override {
return default_scope_->counterFromStatName(name);
}
Counter& counter(const std::string& name) override { return default_scope_->counter(name); }
ScopePtr createScope(const std::string& name) override;
void deliverHistogramToSinks(const Histogram& histogram, uint64_t value) override {
return default_scope_->deliverHistogramToSinks(histogram, value);
}
Gauge& gaugeFromStatName(StatName name) override {
return default_scope_->gaugeFromStatName(name);
}
Gauge& gauge(const std::string& name) override { return default_scope_->gauge(name); }
Histogram& histogramFromStatName(StatName name) override {
return default_scope_->histogramFromStatName(name);
}
Histogram& histogram(const std::string& name) override { return default_scope_->histogram(name); }
NullGaugeImpl& nullGauge(const std::string&) override { return null_gauge_; }
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(); }

Expand Down Expand Up @@ -242,6 +250,16 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
StatMap<std::shared_ptr<StatType>>* tls_cache,
SharedStringSet* tls_rejected_stats, StatType& null_stat);

Counter& counterFromStatName(StatName name) override {
return counter(symbolTable().toString(name));
}

Gauge& gaugeFromStatName(StatName name) override { return gauge(symbolTable().toString(name)); }

Histogram& histogramFromStatName(StatName name) override {
return histogram(symbolTable().toString(name));
}

static std::atomic<uint64_t> next_scope_id_;

const uint64_t scope_id_;
Expand Down
4 changes: 2 additions & 2 deletions source/common/stats/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
namespace Envoy {
namespace Stats {

std::string Utility::sanitizeStatsName(const std::string& name) {
std::string stats_name = name;
std::string Utility::sanitizeStatsName(absl::string_view name) {
std::string stats_name = std::string(name);
std::replace(stats_name.begin(), stats_name.end(), ':', '_');
std::replace(stats_name.begin(), stats_name.end(), '\0', '_');
return stats_name;
Expand Down
4 changes: 3 additions & 1 deletion source/common/stats/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <string>

#include "absl/strings/string_view.h"

namespace Envoy {
namespace Stats {

Expand All @@ -12,7 +14,7 @@ class Utility {
public:
// ':' is a reserved char in statsd. Do a character replacement to avoid costly inline
// translations later.
static std::string sanitizeStatsName(const std::string& name);
static std::string sanitizeStatsName(const absl::string_view name);
};

} // namespace Stats
Expand Down
Loading