message_status_counter.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "message_status_counter.h"
  2. #include "key_value_printer.h"
  3. #include "text_utils.h"
  4. #include <library/cpp/messagebus/monitoring/mon_proto.pb.h>
  5. #include <util/stream/str.h>
  6. using namespace NBus;
  7. using namespace NBus::NPrivate;
  8. TMessageStatusCounter::TMessageStatusCounter() {
  9. Zero(Counts);
  10. }
  11. TMessageStatusCounter& TMessageStatusCounter::operator+=(const TMessageStatusCounter& that) {
  12. for (size_t i = 0; i < MESSAGE_STATUS_COUNT; ++i) {
  13. Counts[i] += that.Counts[i];
  14. }
  15. return *this;
  16. }
  17. TString TMessageStatusCounter::PrintToString() const {
  18. TStringStream ss;
  19. TKeyValuePrinter p;
  20. bool hasNonZeros = false;
  21. bool hasZeros = false;
  22. for (size_t i = 0; i < MESSAGE_STATUS_COUNT; ++i) {
  23. if (i == MESSAGE_OK) {
  24. Y_ABORT_UNLESS(Counts[i] == 0);
  25. continue;
  26. }
  27. if (Counts[i] != 0) {
  28. p.AddRow(EMessageStatus(i), Counts[i]);
  29. const char* description = MessageStatusDescription(EMessageStatus(i));
  30. // TODO: add third column
  31. Y_UNUSED(description);
  32. hasNonZeros = true;
  33. } else {
  34. hasZeros = true;
  35. }
  36. }
  37. if (!hasNonZeros) {
  38. ss << "message status counts are zeros\n";
  39. } else {
  40. if (hasZeros) {
  41. ss << "message status counts are zeros, except:\n";
  42. } else {
  43. ss << "message status counts:\n";
  44. }
  45. ss << IndentText(p.PrintToString());
  46. }
  47. return ss.Str();
  48. }
  49. void TMessageStatusCounter::FillErrorsProtobuf(TConnectionStatusMonRecord* status) const {
  50. status->clear_errorcountbystatus();
  51. for (size_t i = 0; i < MESSAGE_STATUS_COUNT; ++i) {
  52. if (i == MESSAGE_OK) {
  53. Y_ABORT_UNLESS(Counts[i] == 0);
  54. continue;
  55. }
  56. if (Counts[i] != 0) {
  57. TMessageStatusRecord* description = status->add_errorcountbystatus();
  58. description->SetStatus(TMessageStatusCounter::MessageStatusToProtobuf((EMessageStatus)i));
  59. description->SetCount(Counts[i]);
  60. }
  61. }
  62. }