1
#pragma once
2

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

            
5
namespace Envoy {
6
namespace Extensions {
7
namespace HttpFilters {
8
namespace Cache {
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
  STATNAME(event)                                                                                  \
35
  STATNAME(event_type)                                                                             \
36
  STATNAME(hit)                                                                                    \
37
  STATNAME(miss)
38
// TODO(ravenblack): Add other stats from DESIGN.md
39

            
40
#define COUNTER_HELPER_(NAME)                                                                      \
41
  , NAME##_(                                                                                       \
42
63
        Envoy::Stats::Utility::counterFromStatNames(scope, {prefix_, stat_names.NAME##_}, tags_))
43
#define GAUGE_HELPER_(NAME, MODE)                                                                  \
44
252
  , NAME##_(Envoy::Stats::Utility::gaugeFromStatNames(                                             \
45
252
        scope, {prefix_, stat_names.NAME##_}, Envoy::Stats::Gauge::ImportMode::MODE, tags_))
46
#define STATNAME_HELPER_(NAME)
47

            
48
MAKE_STAT_NAMES_STRUCT(CacheStatNames, ALL_CACHE_STATS);
49

            
50
struct CacheStats {
51
  CacheStats(const CacheStatNames& stat_names, Envoy::Stats::Scope& scope,
52
             Stats::StatName cache_path)
53
63
      : stat_names_(stat_names), prefix_(stat_names_.cache_), cache_path_(cache_path),
54
63
        tags_({{stat_names_.cache_path_, cache_path_}}),
55
63
        tags_hit_(
56
63
            {{stat_names_.cache_path_, cache_path_}, {stat_names_.event_type_, stat_names_.hit_}}),
57
63
        tags_miss_(
58
63
            {{stat_names_.cache_path_, cache_path_}, {stat_names_.event_type_, stat_names_.miss_}})
59
            ALL_CACHE_STATS(COUNTER_HELPER_, GAUGE_HELPER_, HISTOGRAM_HELPER_, TEXT_READOUT_HELPER_,
60
                            STATNAME_HELPER_),
61
63
        cache_hit_(Envoy::Stats::Utility::counterFromStatNames(scope, {prefix_, stat_names.event_},
62
63
                                                               tags_hit_)),
63
63
        cache_miss_(Envoy::Stats::Utility::counterFromStatNames(scope, {prefix_, stat_names.event_},
64
63
                                                                tags_miss_)) {}
65

            
66
private:
67
  const CacheStatNames& stat_names_;
68
  const Stats::StatName prefix_;
69
  const Stats::StatName cache_path_;
70
  Stats::StatNameTagVector tags_;
71
  Stats::StatNameTagVector tags_hit_;
72
  Stats::StatNameTagVector tags_miss_;
73

            
74
public:
75
  ALL_CACHE_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT, GENERATE_HISTOGRAM_STRUCT,
76
                  GENERATE_TEXT_READOUT_STRUCT, GENERATE_STATNAME_STRUCT);
77
  Stats::Counter& cache_hit_;
78
  Stats::Counter& cache_miss_;
79
};
80

            
81
CacheStats generateStats(CacheStatNames& stat_names, Stats::Scope& scope,
82
                         absl::string_view cache_path);
83

            
84
} // namespace FileSystemHttpCache
85
} // namespace Cache
86
} // namespace HttpFilters
87
} // namespace Extensions
88
} // namespace Envoy