1
#pragma once
2

            
3
#include "envoy/stats/stats_macros.h"
4

            
5
namespace Envoy {
6
namespace Extensions {
7
namespace HttpFilters {
8
namespace CacheV2 {
9
namespace FileSystemHttpCache {
10

            
11
/**
12
 * All cache stats. @see stats_macros.h
13
 *
14
 * Note that size_bytes and size_count may drift away from true values, due to:
15
 * - Changes to the filesystem may be made outside of the process, which will not be
16
 *   accounted for. (Including, during hot restart, overlapping envoy processes.)
17
 * - Files completed while pre-cache-purge measurement is in progress may not be counted.
18
 * - Changes in file size due to header updates are assumed to be negligible, and are ignored.
19
 *
20
 * Drift will eventually be reconciled at the next pre-cache-purge measurement.
21
 *
22
 * There are also cache_hit_ and cache_miss_, defined separately to accommodate extra tags;
23
 * these two both go into the stat with key `event`, and with tag `event_type=(hit|miss)`
24
 **/
25

            
26
#define ALL_CACHE_STATS(COUNTER, GAUGE, HISTOGRAM, TEXT_READOUT, STATNAME)                         \
27
  COUNTER(eviction_runs)                                                                           \
28
  GAUGE(size_bytes, NeverImport)                                                                   \
29
  GAUGE(size_count, NeverImport)                                                                   \
30
  GAUGE(size_limit_bytes, NeverImport)                                                             \
31
  GAUGE(size_limit_count, NeverImport)                                                             \
32
  STATNAME(cache)                                                                                  \
33
  STATNAME(cache_path)
34
// TODO(ravenblack): Add other stats from DESIGN.md
35

            
36
#define COUNTER_HELPER_(NAME)                                                                      \
37
  , NAME##_(                                                                                       \
38
46
        Envoy::Stats::Utility::counterFromStatNames(scope, {prefix_, stat_names.NAME##_}, tags_))
39
#define GAUGE_HELPER_(NAME, MODE)                                                                  \
40
184
  , NAME##_(Envoy::Stats::Utility::gaugeFromStatNames(                                             \
41
184
        scope, {prefix_, stat_names.NAME##_}, Envoy::Stats::Gauge::ImportMode::MODE, tags_))
42
#define STATNAME_HELPER_(NAME)
43

            
44
MAKE_STAT_NAMES_STRUCT(CacheStatNames, ALL_CACHE_STATS);
45

            
46
struct CacheStats {
47
  CacheStats(const CacheStatNames& stat_names, Envoy::Stats::Scope& scope,
48
             Stats::StatName cache_path)
49
46
      : stat_names_(stat_names), prefix_(stat_names_.cache_), cache_path_(cache_path),
50
46
        tags_({{stat_names_.cache_path_, cache_path_}})
51
            ALL_CACHE_STATS(COUNTER_HELPER_, GAUGE_HELPER_, HISTOGRAM_HELPER_, TEXT_READOUT_HELPER_,
52
46
                            STATNAME_HELPER_) {}
53

            
54
private:
55
  const CacheStatNames& stat_names_;
56
  const Stats::StatName prefix_;
57
  const Stats::StatName cache_path_;
58
  Stats::StatNameTagVector tags_;
59

            
60
public:
61
  ALL_CACHE_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT, GENERATE_HISTOGRAM_STRUCT,
62
                  GENERATE_TEXT_READOUT_STRUCT, GENERATE_STATNAME_STRUCT);
63
};
64

            
65
CacheStats generateStats(CacheStatNames& stat_names, Stats::Scope& scope,
66
                         absl::string_view cache_path);
67

            
68
} // namespace FileSystemHttpCache
69
} // namespace CacheV2
70
} // namespace HttpFilters
71
} // namespace Extensions
72
} // namespace Envoy