Line data Source code
1 : #pragma once 2 : 3 : #include <cstdint> 4 : #include <string> 5 : 6 : #include "envoy/config/metrics/v3/stats.pb.h" 7 : #include "envoy/stats/histogram.h" 8 : #include "envoy/stats/stats.h" 9 : #include "envoy/stats/store.h" 10 : 11 : #include "source/common/common/matchers.h" 12 : #include "source/common/common/non_copyable.h" 13 : #include "source/common/stats/metric_impl.h" 14 : 15 : #include "circllhist.h" 16 : 17 : namespace Envoy { 18 : namespace Stats { 19 : 20 : class HistogramSettingsImpl : public HistogramSettings { 21 : public: 22 98 : HistogramSettingsImpl() = default; 23 : HistogramSettingsImpl(const envoy::config::metrics::v3::StatsConfig& config); 24 : 25 : // HistogramSettings 26 : const ConstSupportedBuckets& buckets(absl::string_view stat_name) const override; 27 : 28 : static ConstSupportedBuckets& defaultBuckets(); 29 : 30 : private: 31 : using Config = std::pair<Matchers::StringMatcherImpl<envoy::type::matcher::v3::StringMatcher>, 32 : ConstSupportedBuckets>; 33 : const std::vector<Config> configs_{}; 34 : }; 35 : 36 : /** 37 : * Implementation of HistogramStatistics for circllhist. 38 : */ 39 : class HistogramStatisticsImpl final : public HistogramStatistics, NonCopyable { 40 : public: 41 : HistogramStatisticsImpl(); 42 : 43 : /** 44 : * HistogramStatisticsImpl object is constructed using the passed in histogram. 45 : * @param histogram_ptr pointer to the histogram for which stats will be calculated. This pointer 46 : * will not be retained. 47 : */ 48 : HistogramStatisticsImpl( 49 : const histogram_t* histogram_ptr, Histogram::Unit unit = Histogram::Unit::Unspecified, 50 : ConstSupportedBuckets& supported_buckets = HistogramSettingsImpl::defaultBuckets()); 51 : 52 : void refresh(const histogram_t* new_histogram_ptr); 53 : 54 : // HistogramStatistics 55 : std::string quantileSummary() const override; 56 : std::string bucketSummary() const override; 57 : const std::vector<double>& supportedQuantiles() const final; 58 0 : const std::vector<double>& computedQuantiles() const override { return computed_quantiles_; } 59 3086 : ConstSupportedBuckets& supportedBuckets() const override { return supported_buckets_; } 60 0 : const std::vector<uint64_t>& computedBuckets() const override { return computed_buckets_; } 61 : std::vector<uint64_t> computeDisjointBuckets() const override; 62 0 : uint64_t sampleCount() const override { return sample_count_; } 63 0 : uint64_t outOfBoundCount() const override { return out_of_bound_count_; } 64 0 : double sampleSum() const override { return sample_sum_; } 65 : 66 : private: 67 : ConstSupportedBuckets& supported_buckets_; 68 : std::vector<double> computed_quantiles_; 69 : std::vector<uint64_t> computed_buckets_; 70 : uint64_t sample_count_{0}; 71 : uint64_t out_of_bound_count_{0}; 72 : double sample_sum_{0}; 73 : const Histogram::Unit unit_{Histogram::Unit::Unspecified}; 74 : }; 75 : 76 : class HistogramImplHelper : public MetricImpl<Histogram> { 77 : public: 78 : HistogramImplHelper(StatName name, StatName tag_extracted_name, 79 : const StatNameTagVector& stat_name_tags, SymbolTable& symbol_table) 80 19972 : : MetricImpl<Histogram>(name, tag_extracted_name, stat_name_tags, symbol_table) {} 81 98 : HistogramImplHelper(SymbolTable& symbol_table) : MetricImpl<Histogram>(symbol_table) {} 82 : 83 : // RefcountInterface 84 40994 : void incRefCount() override { refcount_helper_.incRefCount(); } 85 40994 : bool decRefCount() override { return refcount_helper_.decRefCount(); } 86 0 : uint32_t use_count() const override { return refcount_helper_.use_count(); } 87 : 88 : private: 89 : RefcountHelper refcount_helper_; 90 : }; 91 : 92 : /** 93 : * Histogram implementation for the heap. 94 : */ 95 : class HistogramImpl : public HistogramImplHelper { 96 : public: 97 : HistogramImpl(StatName name, Unit unit, Store& parent, StatName tag_extracted_name, 98 : const StatNameTagVector& stat_name_tags) 99 : : HistogramImplHelper(name, tag_extracted_name, stat_name_tags, parent.symbolTable()), 100 18922 : unit_(unit), parent_(parent) {} 101 18922 : ~HistogramImpl() override { 102 0 : // We must explicitly free the StatName here in order to supply the 103 0 : // SymbolTable reference. An RAII alternative would be to store a 104 0 : // reference to the SymbolTable in MetricImpl, which would cost 8 bytes 105 0 : // per stat. 106 18922 : MetricImpl::clear(symbolTable()); 107 18922 : } 108 : 109 : // Stats::Histogram 110 498 : Unit unit() const override { return unit_; }; 111 640 : void recordValue(uint64_t value) override { parent_.deliverHistogramToSinks(*this, value); } 112 : 113 0 : bool used() const override { return true; } 114 0 : bool hidden() const override { return false; } 115 19195 : SymbolTable& symbolTable() final { return parent_.symbolTable(); } 116 : 117 : private: 118 : Unit unit_; 119 : 120 : // This is used for delivering the histogram data to sinks. 121 : Store& parent_; 122 : }; 123 : 124 : /** 125 : * Null histogram implementation. 126 : * No-ops on all calls and requires no underlying metric or data. 127 : */ 128 : class NullHistogramImpl : public HistogramImplHelper { 129 : public: 130 : explicit NullHistogramImpl(SymbolTable& symbol_table) 131 98 : : HistogramImplHelper(symbol_table), symbol_table_(symbol_table) {} 132 98 : ~NullHistogramImpl() override { MetricImpl::clear(symbol_table_); } 133 : 134 0 : bool used() const override { return false; } 135 0 : bool hidden() const override { return false; } 136 0 : SymbolTable& symbolTable() override { return symbol_table_; } 137 : 138 0 : Unit unit() const override { return Unit::Null; }; 139 0 : void recordValue(uint64_t) override {} 140 : 141 : private: 142 : SymbolTable& symbol_table_; 143 : }; 144 : 145 : } // namespace Stats 146 : } // namespace Envoy