Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/common/stats/utility.cc
Line
Count
Source (jump to first uncovered line)
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
474k
std::string Utility::sanitizeStatsName(absl::string_view name) {
14
474k
  if (absl::EndsWith(name, ".")) {
15
23.6k
    name.remove_suffix(1);
16
23.6k
  }
17
474k
  if (absl::StartsWith(name, ".")) {
18
0
    name.remove_prefix(1);
19
0
  }
20
21
474k
  return absl::StrReplaceAll(name, {
22
474k
                                       {"://", "_"},
23
474k
                                       {":/", "_"},
24
474k
                                       {":", "_"},
25
474k
                                       {absl::string_view("\0", 1), "_"},
26
474k
                                   });
27
474k
}
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
4.05M
      : symbol_table_(symbol_table), pool_(symbol_table) {
49
4.05M
    stat_names_.resize(elements.size());
50
12.2M
    for (const Element& element : elements) {
51
12.2M
      absl::visit(*this, element);
52
12.2M
    }
53
4.05M
    joined_ = symbol_table_.join(stat_names_);
54
4.05M
  }
55
56
  // Overloads provides for absl::visit to call.
57
12.1M
  void operator()(StatName stat_name) { stat_names_.push_back(stat_name); }
58
16.0k
  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
4.05M
  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
33.7k
ScopeSharedPtr scopeFromStatNames(Scope& scope, const StatNameVec& elements) {
76
33.7k
  SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
77
33.7k
  return scope.scopeFromStatName(StatName(joined.get()));
78
33.7k
}
79
80
Counter& counterFromElements(Scope& scope, const ElementVec& elements,
81
16.6k
                             StatNameTagVectorOptConstRef tags) {
82
16.6k
  ElementVisitor visitor(scope.symbolTable(), elements);
83
16.6k
  return scope.counterFromStatNameWithTags(visitor.statName(), tags);
84
16.6k
}
85
86
Counter& counterFromStatNames(Scope& scope, const StatNameVec& elements,
87
32.3M
                              StatNameTagVectorOptConstRef tags) {
88
32.3M
  SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
89
32.3M
  return scope.counterFromStatNameWithTags(StatName(joined.get()), tags);
90
32.3M
}
91
92
Gauge& gaugeFromElements(Scope& scope, const ElementVec& elements, Gauge::ImportMode import_mode,
93
4.03M
                         StatNameTagVectorOptConstRef tags) {
94
4.03M
  ElementVisitor visitor(scope.symbolTable(), elements);
95
4.03M
  return scope.gaugeFromStatNameWithTags(visitor.statName(), tags, import_mode);
96
4.03M
}
97
98
Gauge& gaugeFromStatNames(Scope& scope, const StatNameVec& elements, Gauge::ImportMode import_mode,
99
5.25M
                          StatNameTagVectorOptConstRef tags) {
100
5.25M
  SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
101
5.25M
  return scope.gaugeFromStatNameWithTags(StatName(joined.get()), tags, import_mode);
102
5.25M
}
103
104
Histogram& histogramFromElements(Scope& scope, const ElementVec& elements, Histogram::Unit unit,
105
989
                                 StatNameTagVectorOptConstRef tags) {
106
989
  ElementVisitor visitor(scope.symbolTable(), elements);
107
989
  return scope.histogramFromStatNameWithTags(visitor.statName(), tags, unit);
108
989
}
109
110
Histogram& histogramFromStatNames(Scope& scope, const StatNameVec& elements, Histogram::Unit unit,
111
3.21M
                                  StatNameTagVectorOptConstRef tags) {
112
3.21M
  SymbolTable::StoragePtr joined = scope.symbolTable().join(elements);
113
3.21M
  return scope.histogramFromStatNameWithTags(StatName(joined.get()), tags, unit);
114
3.21M
}
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