1
#include "source/common/stats/isolated_store_impl.h"
2

            
3
#include <algorithm>
4
#include <cstring>
5
#include <string>
6

            
7
#include "source/common/common/utility.h"
8
#include "source/common/stats/histogram_impl.h"
9
#include "source/common/stats/utility.h"
10

            
11
namespace Envoy {
12
namespace Stats {
13

            
14
58296
IsolatedStoreImpl::IsolatedStoreImpl() : IsolatedStoreImpl(std::make_unique<SymbolTableImpl>()) {}
15

            
16
IsolatedStoreImpl::IsolatedStoreImpl(std::unique_ptr<SymbolTable>&& symbol_table)
17
58296
    : IsolatedStoreImpl(*symbol_table) {
18
58296
  symbol_table_storage_ = std::move(symbol_table);
19
58296
}
20

            
21
27237699
static StatNameTagVector tagVectorFromOpt(StatNameTagVectorOptConstRef tags) {
22
27237699
  return tags ? tags->get() : StatNameTagVector{};
23
27237699
}
24

            
25
IsolatedStoreImpl::IsolatedStoreImpl(SymbolTable& symbol_table)
26
1825781
    : alloc_(symbol_table),
27
1825781
      counters_([this](const TagUtility::TagStatNameJoiner& joiner,
28
19210418
                       StatNameTagVectorOptConstRef tags) -> CounterSharedPtr {
29
19208648
        return alloc_.makeCounter(joiner.nameWithTags(), joiner.tagExtractedName(),
30
19208648
                                  tagVectorFromOpt(tags));
31
19208648
      }),
32
1825781
      gauges_([this](const TagUtility::TagStatNameJoiner& joiner, StatNameTagVectorOptConstRef tags,
33
5442415
                     Gauge::ImportMode import_mode) -> GaugeSharedPtr {
34
5439825
        return alloc_.makeGauge(joiner.nameWithTags(), joiner.tagExtractedName(),
35
5439825
                                tagVectorFromOpt(tags), import_mode);
36
5439825
      }),
37
1825781
      histograms_([this](const TagUtility::TagStatNameJoiner& joiner,
38
1825781
                         StatNameTagVectorOptConstRef tags,
39
2595077
                         Histogram::Unit unit) -> HistogramSharedPtr {
40
2588745
        return {new HistogramImpl(joiner.nameWithTags(), unit, *this, joiner.tagExtractedName(),
41
2588745
                                  tagVectorFromOpt(tags))};
42
2588745
      }),
43
1825781
      text_readouts_([this](const TagUtility::TagStatNameJoiner& joiner,
44
1825781
                            StatNameTagVectorOptConstRef tags,
45
1825783
                            TextReadout::Type) -> TextReadoutSharedPtr {
46
481
        return alloc_.makeTextReadout(joiner.nameWithTags(), joiner.tagExtractedName(),
47
481
                                      tagVectorFromOpt(tags));
48
481
      }),
49
1825781
      null_counter_(symbol_table), null_gauge_(symbol_table), null_histogram_(symbol_table),
50
1825781
      null_text_readout_(symbol_table) {}
51

            
52
3403201
ScopeSharedPtr IsolatedStoreImpl::rootScope() {
53
3403201
  if (lazy_default_scope_ == nullptr) {
54
1614999
    StatNameManagedStorage name_storage("", symbolTable());
55
1614999
    lazy_default_scope_ = makeScope(name_storage.statName());
56
1614999
  }
57
3403201
  return lazy_default_scope_;
58
3403201
}
59

            
60
16
ConstScopeSharedPtr IsolatedStoreImpl::constRootScope() const {
61
16
  return const_cast<IsolatedStoreImpl*>(this)->rootScope();
62
16
}
63

            
64
1825719
IsolatedStoreImpl::~IsolatedStoreImpl() = default;
65

            
66
ScopeSharedPtr IsolatedScopeImpl::createScope(const std::string& name, bool,
67
                                              const ScopeStatsLimitSettings& limits,
68
254662
                                              StatsMatcherSharedPtr matcher) {
69
254662
  StatNameManagedStorage stat_name_storage(Utility::sanitizeStatsName(name), symbolTable());
70
254662
  return scopeFromStatName(stat_name_storage.statName(), false, limits, std::move(matcher));
71
254662
}
72

            
73
ScopeSharedPtr IsolatedScopeImpl::scopeFromStatName(StatName name, bool,
74
                                                    const ScopeStatsLimitSettings&,
75
255206
                                                    StatsMatcherSharedPtr matcher) {
76
255206
  SymbolTable::StoragePtr prefix_name_storage = symbolTable().join({prefix(), name});
77
  // Use explicit matcher if provided; otherwise inherit scope_matcher_.
78
255206
  StatsMatcherSharedPtr child_matcher = matcher ? std::move(matcher) : scope_matcher_;
79
255206
  ScopeSharedPtr scope =
80
255206
      store_.makeScope(StatName(prefix_name_storage.get()), std::move(child_matcher));
81
255206
  addScopeToStore(scope);
82
255206
  return scope;
83
255206
}
84

            
85
96103
ScopeSharedPtr IsolatedStoreImpl::makeScope(StatName name, StatsMatcherSharedPtr matcher) {
86
96103
  return std::make_shared<IsolatedScopeImpl>(name, *this, std::move(matcher));
87
96103
}
88

            
89
} // namespace Stats
90
} // namespace Envoy