Coverage Report

Created: 2024-09-19 09:45

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