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