Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/stats/primitive_stats.h" 4 : 5 : #include "absl/strings/string_view.h" 6 : 7 : namespace Envoy { 8 : /** 9 : * These are helper macros for allocating "fixed" stats throughout the code base in a way that 10 : * is also easy to mock and test. The general flow looks like this: 11 : * 12 : * Define a block of stats like this: 13 : * #define MY_COOL_STATS(COUNTER, GAUGE) \ 14 : * COUNTER(counter1) \ 15 : * GAUGE(gauge1) \ 16 : * ... 17 : * 18 : * By convention, starting with #7083, we sort the lines of this macro block, so 19 : * all the counters are grouped together, then all the gauges, etc. We do not 20 : * use clang-format-on/off etc. "bazel run //tools/code_format:check_format -- fix" will take 21 : * care of lining up the backslashes. 22 : * 23 : * Now actually put these stats somewhere, usually as a member of a struct: 24 : * struct MyCoolStats { 25 : * MY_COOL_STATS(GENERATE_PRIMITIVE_COUNTER_STRUCT, GENERATE_PRIMITIVE_GAUGE_STRUCT); 26 : * 27 : * // Optional: Provide access to counters as a map. 28 : * std::vector<std::pair<absl::string_view, PrimitiveCounterReference>> counters() const { 29 : * return {MY_COOL_STATS(PRIMITIVE_COUNTER_NAME_AND_REFERENCE, IGNORE_PRIMITIVE_GAUGE)}; 30 : * } 31 : * 32 : * // Optional: Provide access to gauges as a map. 33 : * std::vector<std::pair<absl::string_view, PrimitiveGaugeReference>> gauges() const { 34 : * return {MY_COOL_STATS(IGNORE_PRIMITIVE_COUNTER, PRIMITIVE_GAUGE_NAME_AND_REFERENCE)}; 35 : * } 36 : * }; 37 : * 38 : * Finally, when you want to actually instantiate the above struct you do: 39 : * MyCoolStats stats; 40 : */ 41 : 42 : // Fully-qualified for use in external callsites. 43 : #define GENERATE_PRIMITIVE_COUNTER_STRUCT(NAME) Envoy::Stats::PrimitiveCounter NAME##_; 44 : #define GENERATE_PRIMITIVE_GAUGE_STRUCT(NAME) Envoy::Stats::PrimitiveGauge NAME##_; 45 : 46 : // Name and counter/gauge reference pair used to construct map of counters/gauges. 47 0 : #define PRIMITIVE_COUNTER_NAME_AND_REFERENCE(X) {absl::string_view(#X), std::ref(X##_)}, 48 0 : #define PRIMITIVE_GAUGE_NAME_AND_REFERENCE(X) {absl::string_view(#X), std::ref(X##_)}, 49 : 50 : // Ignore a counter or gauge. 51 : #define IGNORE_PRIMITIVE_COUNTER(X) 52 : #define IGNORE_PRIMITIVE_GAUGE(X) 53 : 54 : } // namespace Envoy