log_metrics.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #include <library/cpp/monlib/dynamic_counters/counters.h>
  3. #include <library/cpp/monlib/metrics/metric_registry.h>
  4. #include <library/cpp/monlib/service/pages/templates.h>
  5. namespace NActors {
  6. class ILoggerMetrics {
  7. public:
  8. virtual ~ILoggerMetrics() = default;
  9. virtual void IncActorMsgs() = 0;
  10. virtual void IncDirectMsgs() = 0;
  11. virtual void IncLevelRequests() = 0;
  12. virtual void IncIgnoredMsgs() = 0;
  13. virtual void IncAlertMsgs() = 0;
  14. virtual void IncEmergMsgs() = 0;
  15. virtual void IncDroppedMsgs() = 0;
  16. virtual void GetOutputHtml(IOutputStream&) = 0;
  17. };
  18. class TLoggerCounters : public ILoggerMetrics {
  19. public:
  20. TLoggerCounters(TIntrusivePtr<NMonitoring::TDynamicCounters> counters)
  21. : DynamicCounters(counters)
  22. {
  23. ActorMsgs_ = DynamicCounters->GetCounter("ActorMsgs", true);
  24. DirectMsgs_ = DynamicCounters->GetCounter("DirectMsgs", true);
  25. LevelRequests_ = DynamicCounters->GetCounter("LevelRequests", true);
  26. IgnoredMsgs_ = DynamicCounters->GetCounter("IgnoredMsgs", true);
  27. DroppedMsgs_ = DynamicCounters->GetCounter("DroppedMsgs", true);
  28. AlertMsgs_ = DynamicCounters->GetCounter("AlertMsgs", true);
  29. EmergMsgs_ = DynamicCounters->GetCounter("EmergMsgs", true);
  30. }
  31. ~TLoggerCounters() = default;
  32. void IncActorMsgs() override {
  33. ++*ActorMsgs_;
  34. }
  35. void IncDirectMsgs() override {
  36. ++*DirectMsgs_;
  37. }
  38. void IncLevelRequests() override {
  39. ++*LevelRequests_;
  40. }
  41. void IncIgnoredMsgs() override {
  42. ++*IgnoredMsgs_;
  43. }
  44. void IncAlertMsgs() override {
  45. ++*AlertMsgs_;
  46. }
  47. void IncEmergMsgs() override {
  48. ++*EmergMsgs_;
  49. }
  50. void IncDroppedMsgs() override {
  51. DroppedMsgs_->Inc();
  52. }
  53. void GetOutputHtml(IOutputStream& str) override {
  54. HTML(str) {
  55. DIV_CLASS("row") {
  56. DIV_CLASS("col-md-12") {
  57. TAG(TH4) {
  58. str << "Counters" << Endl;
  59. }
  60. DynamicCounters->OutputHtml(str);
  61. }
  62. }
  63. }
  64. }
  65. private:
  66. NMonitoring::TDynamicCounters::TCounterPtr ActorMsgs_;
  67. NMonitoring::TDynamicCounters::TCounterPtr DirectMsgs_;
  68. NMonitoring::TDynamicCounters::TCounterPtr LevelRequests_;
  69. NMonitoring::TDynamicCounters::TCounterPtr IgnoredMsgs_;
  70. NMonitoring::TDynamicCounters::TCounterPtr AlertMsgs_;
  71. NMonitoring::TDynamicCounters::TCounterPtr EmergMsgs_;
  72. // Dropped while the logger backend was unavailable
  73. NMonitoring::TDynamicCounters::TCounterPtr DroppedMsgs_;
  74. TIntrusivePtr<NMonitoring::TDynamicCounters> DynamicCounters;
  75. };
  76. class TLoggerMetrics : public ILoggerMetrics {
  77. public:
  78. TLoggerMetrics(std::shared_ptr<NMonitoring::TMetricRegistry> metrics)
  79. : Metrics(metrics)
  80. {
  81. ActorMsgs_ = Metrics->Rate(NMonitoring::TLabels{{"sensor", "logger.actor_msgs"}});
  82. DirectMsgs_ = Metrics->Rate(NMonitoring::TLabels{{"sensor", "logger.direct_msgs"}});
  83. LevelRequests_ = Metrics->Rate(NMonitoring::TLabels{{"sensor", "logger.level_requests"}});
  84. IgnoredMsgs_ = Metrics->Rate(NMonitoring::TLabels{{"sensor", "logger.ignored_msgs"}});
  85. DroppedMsgs_ = Metrics->Rate(NMonitoring::TLabels{{"sensor", "logger.dropped_msgs"}});
  86. AlertMsgs_ = Metrics->Rate(NMonitoring::TLabels{{"sensor", "logger.alert_msgs"}});
  87. EmergMsgs_ = Metrics->Rate(NMonitoring::TLabels{{"sensor", "logger.emerg_msgs"}});
  88. }
  89. ~TLoggerMetrics() = default;
  90. void IncActorMsgs() override {
  91. ActorMsgs_->Inc();
  92. }
  93. void IncDirectMsgs() override {
  94. DirectMsgs_->Inc();
  95. }
  96. void IncLevelRequests() override {
  97. LevelRequests_->Inc();
  98. }
  99. void IncIgnoredMsgs() override {
  100. IgnoredMsgs_->Inc();
  101. }
  102. void IncAlertMsgs() override {
  103. AlertMsgs_->Inc();
  104. }
  105. void IncEmergMsgs() override {
  106. EmergMsgs_->Inc();
  107. }
  108. void IncDroppedMsgs() override {
  109. DroppedMsgs_->Inc();
  110. }
  111. void GetOutputHtml(IOutputStream& str) override {
  112. HTML(str) {
  113. DIV_CLASS("row") {
  114. DIV_CLASS("col-md-12") {
  115. TAG(TH4) {
  116. str << "Metrics" << Endl;
  117. }
  118. // TODO: Now, TMetricRegistry does not have the GetOutputHtml function
  119. }
  120. }
  121. }
  122. }
  123. private:
  124. NMonitoring::TRate* ActorMsgs_;
  125. NMonitoring::TRate* DirectMsgs_;
  126. NMonitoring::TRate* LevelRequests_;
  127. NMonitoring::TRate* IgnoredMsgs_;
  128. NMonitoring::TRate* AlertMsgs_;
  129. NMonitoring::TRate* EmergMsgs_;
  130. // Dropped while the logger backend was unavailable
  131. NMonitoring::TRate* DroppedMsgs_;
  132. std::shared_ptr<NMonitoring::TMetricRegistry> Metrics;
  133. };
  134. }