golovan_page.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "golovan_page.h"
  2. #include <library/cpp/monlib/service/pages/templates.h>
  3. #include <util/string/split.h>
  4. #include <util/system/tls.h>
  5. using namespace NMonitoring;
  6. class TGolovanCountableConsumer: public ICountableConsumer {
  7. public:
  8. using TOutputCallback = std::function<void()>;
  9. TGolovanCountableConsumer(IOutputStream& out, TOutputCallback& OutputCallback)
  10. : out(out)
  11. {
  12. if (OutputCallback) {
  13. OutputCallback();
  14. }
  15. out << HTTPOKJSON << "[";
  16. FirstCounter = true;
  17. }
  18. void OnCounter(const TString&, const TString& value, const TCounterForPtr* counter) override {
  19. if (FirstCounter) {
  20. FirstCounter = false;
  21. } else {
  22. out << ",";
  23. }
  24. out << "[\"" << prefix + value;
  25. if (counter->ForDerivative()) {
  26. out << "_dmmm";
  27. } else {
  28. out << "_ahhh";
  29. }
  30. out << "\"," << counter->Val() << "]";
  31. }
  32. void OnHistogram(const TString&, const TString&, IHistogramSnapshotPtr, bool) override {
  33. }
  34. void OnGroupBegin(const TString&, const TString& value, const TDynamicCounters*) override {
  35. prefix += value;
  36. if (!value.empty()) {
  37. prefix += "_";
  38. }
  39. }
  40. void OnGroupEnd(const TString&, const TString&, const TDynamicCounters*) override {
  41. prefix = "";
  42. }
  43. void Flush() {
  44. out << "]";
  45. out.Flush();
  46. }
  47. private:
  48. IOutputStream& out;
  49. bool FirstCounter;
  50. TString prefix;
  51. };
  52. TGolovanCountersPage::TGolovanCountersPage(const TString& path, TIntrusivePtr<NMonitoring::TDynamicCounters> counters,
  53. TOutputCallback outputCallback)
  54. : IMonPage(path)
  55. , Counters(counters)
  56. , OutputCallback(outputCallback)
  57. {
  58. }
  59. void TGolovanCountersPage::Output(IMonHttpRequest& request) {
  60. TGolovanCountableConsumer consumer(request.Output(), OutputCallback);
  61. Counters->Accept("", "", consumer);
  62. consumer.Flush();
  63. }