/proc/self/cwd/source/common/stats/histogram_impl.h
Line | Count | Source (jump to first uncovered line) |
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 | 2.64k | 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 | 75.4k | 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 | double sampleSum() const override { return sample_sum_; } |
64 | | |
65 | | private: |
66 | | ConstSupportedBuckets& supported_buckets_; |
67 | | std::vector<double> computed_quantiles_; |
68 | | std::vector<uint64_t> computed_buckets_; |
69 | | uint64_t sample_count_{0}; |
70 | | double sample_sum_{0}; |
71 | | const Histogram::Unit unit_{Histogram::Unit::Unspecified}; |
72 | | }; |
73 | | |
74 | | class HistogramImplHelper : public MetricImpl<Histogram> { |
75 | | public: |
76 | | HistogramImplHelper(StatName name, StatName tag_extracted_name, |
77 | | const StatNameTagVector& stat_name_tags, SymbolTable& symbol_table) |
78 | 3.28M | : MetricImpl<Histogram>(name, tag_extracted_name, stat_name_tags, symbol_table) {} |
79 | 2.64k | HistogramImplHelper(SymbolTable& symbol_table) : MetricImpl<Histogram>(symbol_table) {} |
80 | | |
81 | | // RefcountInterface |
82 | 6.60M | void incRefCount() override { refcount_helper_.incRefCount(); } |
83 | 6.60M | bool decRefCount() override { return refcount_helper_.decRefCount(); } |
84 | 0 | uint32_t use_count() const override { return refcount_helper_.use_count(); } |
85 | | |
86 | | private: |
87 | | RefcountHelper refcount_helper_; |
88 | | }; |
89 | | |
90 | | /** |
91 | | * Histogram implementation for the heap. |
92 | | */ |
93 | | class HistogramImpl : public HistogramImplHelper { |
94 | | public: |
95 | | HistogramImpl(StatName name, Unit unit, Store& parent, StatName tag_extracted_name, |
96 | | const StatNameTagVector& stat_name_tags) |
97 | | : HistogramImplHelper(name, tag_extracted_name, stat_name_tags, parent.symbolTable()), |
98 | 3.26M | unit_(unit), parent_(parent) {} |
99 | 3.26M | ~HistogramImpl() override { |
100 | | // We must explicitly free the StatName here in order to supply the |
101 | | // SymbolTable reference. An RAII alternative would be to store a |
102 | | // reference to the SymbolTable in MetricImpl, which would cost 8 bytes |
103 | | // per stat. |
104 | 3.26M | MetricImpl::clear(symbolTable()); |
105 | 3.26M | } |
106 | | |
107 | | // Stats::Histogram |
108 | 187k | Unit unit() const override { return unit_; }; |
109 | 11.8k | void recordValue(uint64_t value) override { parent_.deliverHistogramToSinks(*this, value); } |
110 | | |
111 | 0 | bool used() const override { return true; } |
112 | 0 | bool hidden() const override { return false; } |
113 | 3.26M | SymbolTable& symbolTable() final { return parent_.symbolTable(); } |
114 | | |
115 | | private: |
116 | | Unit unit_; |
117 | | |
118 | | // This is used for delivering the histogram data to sinks. |
119 | | Store& parent_; |
120 | | }; |
121 | | |
122 | | /** |
123 | | * Null histogram implementation. |
124 | | * No-ops on all calls and requires no underlying metric or data. |
125 | | */ |
126 | | class NullHistogramImpl : public HistogramImplHelper { |
127 | | public: |
128 | | explicit NullHistogramImpl(SymbolTable& symbol_table) |
129 | 2.64k | : HistogramImplHelper(symbol_table), symbol_table_(symbol_table) {} |
130 | 2.64k | ~NullHistogramImpl() override { MetricImpl::clear(symbol_table_); } |
131 | | |
132 | 0 | bool used() const override { return false; } |
133 | 0 | bool hidden() const override { return false; } |
134 | 0 | SymbolTable& symbolTable() override { return symbol_table_; } |
135 | | |
136 | 0 | Unit unit() const override { return Unit::Null; }; |
137 | 0 | void recordValue(uint64_t) override {} |
138 | | |
139 | | private: |
140 | | SymbolTable& symbol_table_; |
141 | | }; |
142 | | |
143 | | } // namespace Stats |
144 | | } // namespace Envoy |