Line data Source code
1 : #pragma once 2 : 3 : #include <cstdint> 4 : #include <memory> 5 : #include <string> 6 : 7 : #include "envoy/http/header_map.h" 8 : #include "envoy/network/connection.h" 9 : #include "envoy/stats/scope.h" 10 : #include "envoy/stats/stats_macros.h" 11 : #include "envoy/stats/timespan.h" 12 : 13 : #include "source/common/stats/symbol_table.h" 14 : 15 : namespace Envoy { 16 : namespace Http { 17 : 18 : /** 19 : * Captures the stat tokens used for recording user-agent stats. These are 20 : * independent of scope. 21 : */ 22 : struct UserAgentContext { 23 : UserAgentContext(Stats::SymbolTable& symbol_table); 24 : 25 : Stats::SymbolTable& symbol_table_; 26 : Stats::StatNamePool pool_; 27 : Stats::StatName downstream_cx_length_ms_; 28 : Stats::StatName ios_; 29 : Stats::StatName android_; 30 : Stats::StatName downstream_cx_total_; 31 : Stats::StatName downstream_cx_destroy_remote_active_rq_; 32 : Stats::StatName downstream_rq_total_; 33 : }; 34 : 35 : /** 36 : * Captures the stats (counters and histograms) for user-agents. These are 37 : * established within a stats scope. You must supply a UserAgentContext so that 38 : * none of the symbols have to be looked up in the symbol-table in the 39 : * request-path. 40 : */ 41 : struct UserAgentStats { 42 : UserAgentStats(Stats::StatName prefix, Stats::StatName device, Stats::Scope& scope, 43 : const UserAgentContext& context); 44 : 45 : Stats::Counter& downstream_cx_total_; 46 : Stats::Counter& downstream_cx_destroy_remote_active_rq_; 47 : Stats::Counter& downstream_rq_total_; 48 : Stats::Histogram& downstream_cx_length_ms_; 49 : }; 50 : 51 : /** 52 : * Stats support for specific user agents. 53 : */ 54 : class UserAgent { 55 : public: 56 922 : UserAgent(const UserAgentContext& context) : context_(context) {} 57 : 58 : /** 59 : * Complete a connection length timespan for the target user agent. 60 : * @param span supplies the timespan to complete. 61 : */ 62 : void completeConnectionLength(Stats::Timespan& span); 63 : 64 : /** 65 : * Initialize the user agent from request headers. This is only done once and the user-agent 66 : * is assumed to be the same for further requests. 67 : * @param headers supplies the request headers. 68 : * @param prefix supplies the stat prefix for the UA stats. 69 : * @param scope supplies the backing stat scope. 70 : */ 71 : void initializeFromHeaders(const RequestHeaderMap& headers, Stats::StatName prefix, 72 : Stats::Scope& scope); 73 : 74 : /** 75 : * Called when a connection is being destroyed. 76 : * @param event supplies the network event that caused destruction. 77 : * @param active_streams supplies whether there are still active streams at the time of closing. 78 : */ 79 : void onConnectionDestroy(Network::ConnectionEvent event, bool active_streams); 80 : 81 : void initStats(Stats::StatName prefix, Stats::StatName device, Stats::Scope& scope); 82 : 83 : private: 84 : const UserAgentContext& context_; 85 : bool initialized_{false}; 86 : std::unique_ptr<UserAgentStats> stats_; 87 : }; 88 : 89 : } // namespace Http 90 : } // namespace Envoy