grpc_counters.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include <library/cpp/monlib/dynamic_counters/percentile/percentile.h>
  3. #include <library/cpp/monlib/dynamic_counters/counters.h>
  4. #include <util/generic/ptr.h>
  5. namespace NGrpc {
  6. struct ICounterBlock : public TThrRefBase {
  7. virtual void CountNotOkRequest() = 0;
  8. virtual void CountNotOkResponse() = 0;
  9. virtual void CountNotAuthenticated() = 0;
  10. virtual void CountResourceExhausted() = 0;
  11. virtual void CountRequestBytes(ui32 requestSize) = 0;
  12. virtual void CountResponseBytes(ui32 responseSize) = 0;
  13. virtual void StartProcessing(ui32 requestSize) = 0;
  14. virtual void FinishProcessing(ui32 requestSize, ui32 responseSize, bool ok, ui32 status, TDuration requestDuration) = 0;
  15. virtual void CountRequestsWithoutDatabase() {}
  16. virtual void CountRequestsWithoutToken() {}
  17. virtual void CountRequestWithoutTls() {}
  18. virtual TIntrusivePtr<ICounterBlock> Clone() { return this; }
  19. virtual void UseDatabase(const TString& database) { Y_UNUSED(database); }
  20. };
  21. using ICounterBlockPtr = TIntrusivePtr<ICounterBlock>;
  22. class TCounterBlock final : public ICounterBlock {
  23. NMonitoring::TDynamicCounters::TCounterPtr TotalCounter;
  24. NMonitoring::TDynamicCounters::TCounterPtr InflyCounter;
  25. NMonitoring::TDynamicCounters::TCounterPtr NotOkRequestCounter;
  26. NMonitoring::TDynamicCounters::TCounterPtr NotOkResponseCounter;
  27. NMonitoring::TDynamicCounters::TCounterPtr RequestBytes;
  28. NMonitoring::TDynamicCounters::TCounterPtr InflyRequestBytes;
  29. NMonitoring::TDynamicCounters::TCounterPtr ResponseBytes;
  30. NMonitoring::TDynamicCounters::TCounterPtr NotAuthenticated;
  31. NMonitoring::TDynamicCounters::TCounterPtr ResourceExhausted;
  32. bool Percentile = false;
  33. NMonitoring::TPercentileTracker<4, 512, 15> RequestHistMs;
  34. std::array<NMonitoring::TDynamicCounters::TCounterPtr, 2> GRpcStatusCounters;
  35. public:
  36. TCounterBlock(NMonitoring::TDynamicCounters::TCounterPtr totalCounter,
  37. NMonitoring::TDynamicCounters::TCounterPtr inflyCounter,
  38. NMonitoring::TDynamicCounters::TCounterPtr notOkRequestCounter,
  39. NMonitoring::TDynamicCounters::TCounterPtr notOkResponseCounter,
  40. NMonitoring::TDynamicCounters::TCounterPtr requestBytes,
  41. NMonitoring::TDynamicCounters::TCounterPtr inflyRequestBytes,
  42. NMonitoring::TDynamicCounters::TCounterPtr responseBytes,
  43. NMonitoring::TDynamicCounters::TCounterPtr notAuthenticated,
  44. NMonitoring::TDynamicCounters::TCounterPtr resourceExhausted,
  45. TIntrusivePtr<NMonitoring::TDynamicCounters> group)
  46. : TotalCounter(std::move(totalCounter))
  47. , InflyCounter(std::move(inflyCounter))
  48. , NotOkRequestCounter(std::move(notOkRequestCounter))
  49. , NotOkResponseCounter(std::move(notOkResponseCounter))
  50. , RequestBytes(std::move(requestBytes))
  51. , InflyRequestBytes(std::move(inflyRequestBytes))
  52. , ResponseBytes(std::move(responseBytes))
  53. , NotAuthenticated(std::move(notAuthenticated))
  54. , ResourceExhausted(std::move(resourceExhausted))
  55. {
  56. if (group) {
  57. RequestHistMs.Initialize(group, "event", "request", "ms", {0.5f, 0.9f, 0.99f, 0.999f, 1.0f});
  58. Percentile = true;
  59. }
  60. }
  61. void CountNotOkRequest() override {
  62. NotOkRequestCounter->Inc();
  63. }
  64. void CountNotOkResponse() override {
  65. NotOkResponseCounter->Inc();
  66. }
  67. void CountNotAuthenticated() override {
  68. NotAuthenticated->Inc();
  69. }
  70. void CountResourceExhausted() override {
  71. ResourceExhausted->Inc();
  72. }
  73. void CountRequestBytes(ui32 requestSize) override {
  74. *RequestBytes += requestSize;
  75. }
  76. void CountResponseBytes(ui32 responseSize) override {
  77. *ResponseBytes += responseSize;
  78. }
  79. void StartProcessing(ui32 requestSize) override {
  80. TotalCounter->Inc();
  81. InflyCounter->Inc();
  82. *RequestBytes += requestSize;
  83. *InflyRequestBytes += requestSize;
  84. }
  85. void FinishProcessing(ui32 requestSize, ui32 responseSize, bool ok, ui32 status,
  86. TDuration requestDuration) override
  87. {
  88. Y_UNUSED(status);
  89. InflyCounter->Dec();
  90. *InflyRequestBytes -= requestSize;
  91. *ResponseBytes += responseSize;
  92. if (!ok) {
  93. NotOkResponseCounter->Inc();
  94. }
  95. if (Percentile) {
  96. RequestHistMs.Increment(requestDuration.MilliSeconds());
  97. }
  98. }
  99. ICounterBlockPtr Clone() override {
  100. return this;
  101. }
  102. void Update() {
  103. if (Percentile) {
  104. RequestHistMs.Update();
  105. }
  106. }
  107. };
  108. using TCounterBlockPtr = TIntrusivePtr<TCounterBlock>;
  109. /**
  110. * Creates new instance of ICounterBlock implementation which does nothing.
  111. *
  112. * @return new instance
  113. */
  114. ICounterBlockPtr FakeCounterBlock();
  115. } // namespace NGrpc