registry_mon_page.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "registry_mon_page.h"
  2. #include <library/cpp/monlib/encode/text/text.h>
  3. #include <library/cpp/monlib/encode/json/json.h>
  4. #include <library/cpp/monlib/encode/prometheus/prometheus.h>
  5. #include <library/cpp/monlib/encode/spack/spack_v1.h>
  6. #include <library/cpp/monlib/service/format.h>
  7. namespace NMonitoring {
  8. void TMetricRegistryPage::Output(NMonitoring::IMonHttpRequest& request) {
  9. const auto formatStr = TStringBuf{request.GetPathInfo()}.RNextTok('/');
  10. auto& out = request.Output();
  11. if (!formatStr.empty()) {
  12. IMetricEncoderPtr encoder;
  13. TString resp;
  14. if (formatStr == TStringBuf("json")) {
  15. resp = HTTPOKJSON;
  16. encoder = NMonitoring::EncoderJson(&out);
  17. } else if (formatStr == TStringBuf("spack")) {
  18. resp = HTTPOKSPACK;
  19. const auto compression = ParseCompression(request);
  20. encoder = NMonitoring::EncoderSpackV1(&out, ETimePrecision::SECONDS, compression);
  21. } else if (formatStr == TStringBuf("prometheus")) {
  22. resp = HTTPOKPROMETHEUS;
  23. encoder = NMonitoring::EncoderPrometheus(&out);
  24. } else {
  25. ythrow yexception() << "unsupported metric encoding format: " << formatStr;
  26. }
  27. out.Write(resp);
  28. RegistryRawPtr_->Accept(TInstant::Zero(), encoder.Get());
  29. encoder->Close();
  30. } else {
  31. THtmlMonPage::Output(request);
  32. }
  33. }
  34. void TMetricRegistryPage::OutputText(IOutputStream& out, NMonitoring::IMonHttpRequest&) {
  35. IMetricEncoderPtr encoder = NMonitoring::EncoderText(&out);
  36. RegistryRawPtr_->Accept(TInstant::Zero(), encoder.Get());
  37. }
  38. }