1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <folly/Optional.h>
12 
13 #include <proxygen/lib/http/stats/TLResponseCodeStats.h>
14 #include <proxygen/lib/stats/BaseStats.h>
15 
16 namespace proxygen {
17 
18 /**
19  * Connection stats abstract interface.
20  */
21 class ConnectionStats {
22  public:
23   virtual ~ConnectionStats() = default;
24 
25   virtual void recordConnectionOpen() = 0;
26 
27   virtual void recordConnectionClose() = 0;
28 
29   virtual void recordRequest() = 0;
30 
31   virtual void recordResponse(
32       folly::Optional<uint16_t> responseCode = folly::none) = 0;
33 
34   virtual void recordDuration(size_t duration) = 0;
35 
36   virtual void addEgressBytes(size_t bytes) = 0;
37 
38   virtual void addIngressBytes(size_t bytes) = 0;
39 
40   virtual void addEgressBodyBytes(size_t bytes) = 0;
41 
42   virtual void addIngressBodyBytes(size_t bytes) = 0;
43 };
44 
45 /**
46  * Wraps connection stat counters.  One instance should be created per
47  * uniquely named counter and shared accross threads as necessary.
48  */
49 class TLConnectionStats : public ConnectionStats {
50  public:
51   explicit TLConnectionStats(const std::string& prefix);
52 
53   void recordConnectionOpen() override;
54 
55   void recordConnectionClose() override;
56 
57   void recordRequest() override;
58 
59   void recordResponse(
60       folly::Optional<uint16_t> responseCode = folly::none) override;
61 
62   void recordDuration(size_t duration) override;
63 
64   void addEgressBytes(size_t bytes) override;
65 
66   void addIngressBytes(size_t bytes) override;
67 
68   void addEgressBodyBytes(size_t bytes) override;
69 
70   void addIngressBodyBytes(size_t bytes) override;
71 
72  private:
73   BaseStats::TLTimeseriesMinuteAndAllTime req_;
74   BaseStats::TLTimeseriesMinuteAndAllTime resp_;
75   BaseStats::TLTimeseriesMinuteAndAllTime egressBytes_;
76   BaseStats::TLTimeseriesMinuteAndAllTime ingressBytes_;
77   BaseStats::TLTimeseriesMinuteAndAllTime egressBodyBytes_;
78   BaseStats::TLTimeseriesMinuteAndAllTime ingressBodyBytes_;
79   TLResponseCodeStats responseCodes_;
80   BaseStats::TLHistogram totalDuration_;
81 
82   BaseStats::TLCounter currConns_;
83   BaseStats::TLTimeseries newConns_;
84 };
85 
86 } // namespace proxygen
87