1
#include "source/common/quic/quic_stat_names.h"
2

            
3
namespace Envoy {
4
namespace Quic {
5

            
6
#ifdef ENVOY_ENABLE_QUIC
7

            
8
// TODO(renjietang): Currently these stats are only available in downstream. Wire it up to upstream
9
// QUIC also.
10
QuicStatNames::QuicStatNames(Stats::SymbolTable& symbol_table)
11
43783
    : stat_name_pool_(symbol_table), symbol_table_(symbol_table),
12
43783
      http3_prefix_(stat_name_pool_.add("http3")), downstream_(stat_name_pool_.add("downstream")),
13
43783
      upstream_(stat_name_pool_.add("upstream")), from_self_(stat_name_pool_.add("tx")),
14
43783
      from_peer_(stat_name_pool_.add("rx")) {
15
  // Preallocate most used counters
16
  // Most popular in client initiated connection close.
17
43783
  connectionCloseStatName(quic::QUIC_NETWORK_IDLE_TIMEOUT);
18
  // Most popular in server initiated connection close.
19
43783
  connectionCloseStatName(quic::QUIC_SILENT_IDLE_TIMEOUT);
20
  // Most popular in client initiated stream reset.
21
43783
  resetStreamErrorStatName(quic::QUIC_STREAM_CANCELLED);
22
  // Most popular in server initiated stream reset.
23
43783
  resetStreamErrorStatName(quic::QUIC_STREAM_STREAM_CREATION_ERROR);
24
43783
}
25

            
26
7676
void QuicStatNames::incCounter(Stats::Scope& scope, const Stats::StatNameVec& names) {
27
7676
  Stats::SymbolTable::StoragePtr stat_name_storage = symbol_table_.join(names);
28
7676
  scope.counterFromStatName(Stats::StatName(stat_name_storage.get())).inc();
29
7676
}
30

            
31
void QuicStatNames::chargeQuicConnectionCloseStats(Stats::Scope& scope,
32
                                                   quic::QuicErrorCode error_code,
33
                                                   quic::ConnectionCloseSource source,
34
6071
                                                   bool is_upstream) {
35
6071
  ASSERT(&symbol_table_ == &scope.symbolTable());
36

            
37
6071
  if (error_code > quic::QUIC_LAST_ERROR) {
38
1
    error_code = quic::QUIC_LAST_ERROR;
39
1
  }
40

            
41
6071
  const Stats::StatName connection_close = connectionCloseStatName(error_code);
42
6071
  incCounter(scope, {http3_prefix_, (is_upstream ? upstream_ : downstream_),
43
6071
                     (source == quic::ConnectionCloseSource::FROM_SELF ? from_self_ : from_peer_),
44
6071
                     connection_close});
45
6071
}
46

            
47
void QuicStatNames::chargeQuicResetStreamErrorStats(Stats::Scope& scope,
48
                                                    quic::QuicResetStreamError error_code,
49
1605
                                                    bool from_self, bool is_upstream) {
50
1605
  ASSERT(&symbol_table_ == &scope.symbolTable());
51

            
52
1605
  auto internal_code = error_code.internal_code();
53
1605
  if (internal_code > quic::QUIC_STREAM_LAST_ERROR) {
54
1
    internal_code = quic::QUIC_STREAM_LAST_ERROR;
55
1
  }
56

            
57
1605
  const Stats::StatName stream_error = resetStreamErrorStatName(internal_code);
58
1605
  incCounter(scope, {http3_prefix_, (is_upstream ? upstream_ : downstream_),
59
1605
                     (from_self ? from_self_ : from_peer_), stream_error});
60
1605
}
61

            
62
93637
Stats::StatName QuicStatNames::connectionCloseStatName(quic::QuicErrorCode error_code) {
63
93637
  ASSERT(error_code <= quic::QUIC_LAST_ERROR);
64

            
65
93637
  return Stats::StatName(
66
93637
      connection_error_stat_names_.get(error_code, [this, error_code]() -> const uint8_t* {
67
91150
        return stat_name_pool_.addReturningStorage(
68
91150
            absl::StrCat("quic_connection_close_error_code_", QuicErrorCodeToString(error_code)));
69
91150
      }));
70
93637
}
71

            
72
89171
Stats::StatName QuicStatNames::resetStreamErrorStatName(quic::QuicRstStreamErrorCode error_code) {
73
89171
  ASSERT(error_code <= quic::QUIC_STREAM_LAST_ERROR);
74

            
75
89171
  return Stats::StatName(
76
89171
      reset_stream_error_stat_names_.get(error_code, [this, error_code]() -> const uint8_t* {
77
88361
        return stat_name_pool_.addReturningStorage(absl::StrCat(
78
88361
            "quic_reset_stream_error_code_", QuicRstStreamErrorCodeToString(error_code)));
79
88361
      }));
80
89171
}
81

            
82
#else
83
QuicStatNames::QuicStatNames(Stats::SymbolTable& /*symbol_table*/) {}
84

            
85
#endif
86

            
87
} // namespace Quic
88
} // namespace Envoy