1
#pragma once
2

            
3
#include <memory>
4
#include <string>
5

            
6
#include "envoy/buffer/buffer.h"
7
#include "envoy/http/codes.h"
8

            
9
#include "source/server/admin/utils.h"
10

            
11
#include "re2/re2.h"
12

            
13
namespace Envoy {
14
namespace Server {
15

            
16
namespace StatLabels {
17
constexpr absl::string_view All = "All";
18
constexpr absl::string_view Counters = "Counters";
19
constexpr absl::string_view Gauges = "Gauges";
20
constexpr absl::string_view Histograms = "Histograms";
21
constexpr absl::string_view TextReadouts = "TextReadouts";
22
} // namespace StatLabels
23

            
24
enum class StatsFormat {
25
#ifdef ENVOY_ADMIN_HTML
26
  Html,
27
  ActiveHtml,
28
#endif
29
  Json,
30
  Prometheus,
31
  Text,
32
};
33

            
34
// The order is used to linearize the ordering of stats of all types.
35
enum class StatsType {
36
  TextReadouts,
37
  Counters,
38
  Gauges,
39
  Histograms,
40
  All,
41
};
42

            
43
enum class HiddenFlag {
44
  Include,  // Will include hidden stats along side non-hidden stats
45
  ShowOnly, // Will only show hidden stats and exclude hidden stats
46
  Exclude,  // Default behavior. Will exclude all hidden stats
47
};
48

            
49
struct StatsParams {
50
  /**
51
   * Parses the URL's query parameter, populating this.
52
   *
53
   * @param url the URL from which to parse the query params.
54
   * @param response used to write error messages, if necessary.
55
   */
56
  Http::Code parse(absl::string_view url, Buffer::Instance& response);
57

            
58
  /**
59
   * @return a string representation for a type.
60
   */
61
  static absl::string_view typeToString(StatsType type);
62

            
63
  StatsType type_{StatsType::All};
64
  bool used_only_{false};
65
  bool prometheus_text_readouts_{false};
66
  bool pretty_{false};
67
  StatsFormat format_{StatsFormat::Text};
68
  HiddenFlag hidden_{HiddenFlag::Exclude};
69
  std::string filter_string_;
70
  std::shared_ptr<re2::RE2> re2_filter_;
71
  Utility::HistogramBucketsMode histogram_buckets_mode_{Utility::HistogramBucketsMode::Unset};
72
  // If set, emit native histograms with at most this many buckets per histogram.
73
  absl::optional<uint32_t> native_histogram_max_buckets_;
74
  Http::Utility::QueryParamsMulti query_;
75

            
76
  /**
77
   * Determines whether a metric should be shown based on the specified query-parameters. This
78
   * covers:
79
   * ``usedonly``, hidden, and filter.
80
   *
81
   * @param metric the metric to test
82
   * @param name_out if non-null, and the return value is true,
83
   *   will contain the metric name. This improves performance because computing the name is
84
   *   somewhat expensive, and in some cases it isn't needed.
85
   */
86
3753
  template <class StatType> bool shouldShowMetric(const StatType& metric) const {
87
3753
    if (!shouldShowMetricWithoutFilter(metric)) {
88
7
      return false;
89
7
    }
90

            
91
3746
    if (re2_filter_ != nullptr && !re2::RE2::PartialMatch(metric.name(), *re2_filter_)) {
92
27
      return false;
93
27
    }
94

            
95
3719
    return true;
96
3746
  }
97

            
98
211719
  template <class StatType> bool shouldShowMetricWithoutFilter(const StatType& metric) const {
99
211719
    if (used_only_ && !metric.used()) {
100
2942
      return false;
101
2942
    }
102
208777
    if (hidden_ == HiddenFlag::ShowOnly && !metric.hidden()) {
103
2
      return false;
104
2
    }
105
208775
    if (hidden_ == HiddenFlag::Exclude && metric.hidden()) {
106
3
      return false;
107
3
    }
108

            
109
208772
    return true;
110
208775
  }
111
};
112

            
113
} // namespace Server
114
} // namespace Envoy