Line data Source code
1 : #include "source/common/stats/utility.h"
2 :
3 : #include <algorithm>
4 : #include <string>
5 :
6 : #include "absl/strings/match.h"
7 : #include "absl/strings/str_replace.h"
8 : #include "absl/types/optional.h"
9 :
10 : namespace Envoy {
11 : namespace Stats {
12 :
13 6072 : std::string Utility::sanitizeStatsName(absl::string_view name) {
14 6072 : if (absl::EndsWith(name, ".")) {
15 875 : name.remove_suffix(1);
16 875 : }
17 6072 : if (absl::StartsWith(name, ".")) {
18 23 : name.remove_prefix(1);
19 23 : }
20 :
21 6072 : return absl::StrReplaceAll(name, {
22 6072 : {"://", "_"},
23 6072 : {":/", "_"},
24 6072 : {":", "_"},
25 6072 : {absl::string_view("\0", 1), "_"},
26 6072 : });
27 6072 : }
28 :
29 0 : absl::optional<StatName> Utility::findTag(const Metric& metric, StatName find_tag_name) {
30 0 : absl::optional<StatName> value;
31 0 : metric.iterateTagStatNames(
32 0 : [&value, &find_tag_name](Stats::StatName tag_name, Stats::StatName tag_value) -> bool {
33 0 : if (tag_name == find_tag_name) {
34 0 : value = tag_value;
35 0 : return false;
36 0 : }
37 0 : return true;
38 0 : });
39 0 : return value;
40 0 : }
41 :
42 : namespace {
43 :
44 : // Helper class for the three Utility::*FromElements implementations to build up
45 : // a joined StatName from a mix of StatName and string_view.
46 : struct ElementVisitor {
47 : ElementVisitor(SymbolTable& symbol_table, const ElementVec& elements)
48 32224 : : symbol_table_(symbol_table), pool_(symbol_table) {
49 32224 : stat_names_.resize(elements.size());
50 113295 : for (const Element& element : elements) {
51 113295 : absl::visit(*this, element);
52 113295 : }
53 32224 : joined_ = symbol_table_.join(stat_names_);
54 32224 : }
55 :
56 : // Overloads provides for absl::visit to call.
57 71320 : void operator()(StatName stat_name) { stat_names_.push_back(stat_name); }
58 41975 : void operator()(absl::string_view name) { stat_names_.push_back(pool_.add(name)); }
59 :
60 : /**
61 : * @return the StatName constructed by joining the elements.
62 : */
63 32224 : StatName statName() { return StatName(joined_.get()); }
64 :
65 : SymbolTable& symbol_table_;
66 : StatNameVec stat_names_;
67 : StatNameDynamicPool pool_;
68 : SymbolTable::StoragePtr joined_;
69 : };
70 :
71 : } // namespace
72 :
73 : namespace Utility {
74 :
75 5 : ScopeSharedPtr scopeFromStatNames(Scope& scope, const StatNameVec& elements) {
76 5 : SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
77 5 : return scope.scopeFromStatName(StatName(joined.get()));
78 5 : }
79 :
80 : Counter& counterFromElements(Scope& scope, const ElementVec& elements,
81 8464 : StatNameTagVectorOptConstRef tags) {
82 8464 : ElementVisitor visitor(scope.symbolTable(), elements);
83 8464 : return scope.counterFromStatNameWithTags(visitor.statName(), tags);
84 8464 : }
85 :
86 : Counter& counterFromStatNames(Scope& scope, const StatNameVec& elements,
87 205416 : StatNameTagVectorOptConstRef tags) {
88 205416 : SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
89 205416 : return scope.counterFromStatNameWithTags(StatName(joined.get()), tags);
90 205416 : }
91 :
92 : Gauge& gaugeFromElements(Scope& scope, const ElementVec& elements, Gauge::ImportMode import_mode,
93 23760 : StatNameTagVectorOptConstRef tags) {
94 23760 : ElementVisitor visitor(scope.symbolTable(), elements);
95 23760 : return scope.gaugeFromStatNameWithTags(visitor.statName(), tags, import_mode);
96 23760 : }
97 :
98 : Gauge& gaugeFromStatNames(Scope& scope, const StatNameVec& elements, Gauge::ImportMode import_mode,
99 31229 : StatNameTagVectorOptConstRef tags) {
100 31229 : SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
101 31229 : return scope.gaugeFromStatNameWithTags(StatName(joined.get()), tags, import_mode);
102 31229 : }
103 :
104 : Histogram& histogramFromElements(Scope& scope, const ElementVec& elements, Histogram::Unit unit,
105 0 : StatNameTagVectorOptConstRef tags) {
106 0 : ElementVisitor visitor(scope.symbolTable(), elements);
107 0 : return scope.histogramFromStatNameWithTags(visitor.statName(), tags, unit);
108 0 : }
109 :
110 : Histogram& histogramFromStatNames(Scope& scope, const StatNameVec& elements, Histogram::Unit unit,
111 18054 : StatNameTagVectorOptConstRef tags) {
112 18054 : SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
113 18054 : return scope.histogramFromStatNameWithTags(StatName(joined.get()), tags, unit);
114 18054 : }
115 :
116 : TextReadout& textReadoutFromElements(Scope& scope, const ElementVec& elements,
117 0 : StatNameTagVectorOptConstRef tags) {
118 0 : ElementVisitor visitor(scope.symbolTable(), elements);
119 0 : return scope.textReadoutFromStatNameWithTags(visitor.statName(), tags);
120 0 : }
121 :
122 : TextReadout& textReadoutFromStatNames(Scope& scope, const StatNameVec& elements,
123 0 : StatNameTagVectorOptConstRef tags) {
124 0 : SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
125 0 : return scope.textReadoutFromStatNameWithTags(StatName(joined.get()), tags);
126 0 : }
127 :
128 : } // namespace Utility
129 : } // namespace Stats
130 : } // namespace Envoy
|