mon.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #pragma once
  2. #include "events.h"
  3. #include "event_local.h"
  4. #include <library/cpp/monlib/service/monservice.h>
  5. #include <library/cpp/monlib/service/pages/mon_page.h>
  6. namespace NActors {
  7. namespace NMon {
  8. enum {
  9. HttpInfo = EventSpaceBegin(NActors::TEvents::ES_MON),
  10. HttpInfoRes,
  11. RemoteHttpInfo,
  12. RemoteHttpInfoRes,
  13. RemoteJsonInfoRes,
  14. RemoteBinaryInfoRes,
  15. End
  16. };
  17. static_assert(End < EventSpaceEnd(NActors::TEvents::ES_MON), "expect End < EventSpaceEnd(NActors::TEvents::ES_MON)");
  18. // request info from an actor in HTML format
  19. struct TEvHttpInfo: public NActors::TEventLocal<TEvHttpInfo, HttpInfo> {
  20. TEvHttpInfo(const NMonitoring::IMonHttpRequest& request, int subReqId = 0)
  21. : Request(request)
  22. , SubRequestId(subReqId)
  23. {
  24. }
  25. TEvHttpInfo(const NMonitoring::IMonHttpRequest& request, const TString& userToken)
  26. : Request(request)
  27. , UserToken(userToken)
  28. , SubRequestId(0)
  29. {
  30. }
  31. const NMonitoring::IMonHttpRequest& Request;
  32. TString UserToken; // built and serialized
  33. // SubRequestId != 0 means that we assemble reply from multiple parts and SubRequestId contains this part id
  34. int SubRequestId;
  35. };
  36. // base class for HTTP info response
  37. struct IEvHttpInfoRes: public NActors::TEventLocal<IEvHttpInfoRes, HttpInfoRes> {
  38. enum EContentType {
  39. Html,
  40. Custom,
  41. };
  42. IEvHttpInfoRes() {
  43. }
  44. virtual ~IEvHttpInfoRes() {
  45. }
  46. virtual void Output(IOutputStream& out) const = 0;
  47. virtual EContentType GetContentType() const = 0;
  48. };
  49. // Ready to output HTML in TString
  50. struct TEvHttpInfoRes: public IEvHttpInfoRes {
  51. TEvHttpInfoRes(const TString& answer, int subReqId = 0, EContentType contentType = Html)
  52. : Answer(answer)
  53. , SubRequestId(subReqId)
  54. , ContentType(contentType)
  55. {
  56. }
  57. void Output(IOutputStream& out) const override {
  58. out << Answer;
  59. }
  60. EContentType GetContentType() const override {
  61. return ContentType;
  62. }
  63. const TString Answer;
  64. const int SubRequestId;
  65. const EContentType ContentType;
  66. };
  67. struct TEvRemoteHttpInfo: public NActors::TEventBase<TEvRemoteHttpInfo, RemoteHttpInfo> {
  68. TEvRemoteHttpInfo() = default;
  69. TEvRemoteHttpInfo(const TString& query, HTTP_METHOD method = HTTP_METHOD_UNDEFINED)
  70. : Query(query)
  71. , Method(method)
  72. {
  73. }
  74. TEvRemoteHttpInfo(NActorsProto::TRemoteHttpInfo info)
  75. : Query(MakeSerializedQuery(info))
  76. , ExtendedQuery(std::move(info))
  77. {}
  78. static TString MakeSerializedQuery(const NActorsProto::TRemoteHttpInfo& info) {
  79. TString s(1, '\0');
  80. const bool success = info.AppendToString(&s);
  81. Y_ABORT_UNLESS(success);
  82. return s;
  83. }
  84. TString Query;
  85. HTTP_METHOD Method = HTTP_METHOD_UNDEFINED;
  86. std::optional<NActorsProto::TRemoteHttpInfo> ExtendedQuery;
  87. TString PathInfo() const {
  88. if (ExtendedQuery) {
  89. return ExtendedQuery->GetPath();
  90. } else {
  91. const size_t pos = Query.find('?');
  92. return (pos == TString::npos) ? TString() : Query.substr(0, pos);
  93. }
  94. }
  95. TCgiParameters Cgi() const {
  96. if (ExtendedQuery) {
  97. TCgiParameters params;
  98. for (const auto& kv : ExtendedQuery->GetQueryParams()) {
  99. params.emplace(kv.GetKey(), kv.GetValue());
  100. }
  101. return params;
  102. } else {
  103. const size_t pos = Query.find('?');
  104. return TCgiParameters((pos == TString::npos) ? TString() : Query.substr(pos + 1));
  105. }
  106. }
  107. HTTP_METHOD GetMethod() const {
  108. return ExtendedQuery ? static_cast<HTTP_METHOD>(ExtendedQuery->GetMethod()) : Method;
  109. }
  110. TString ToStringHeader() const override {
  111. return "TEvRemoteHttpInfo";
  112. }
  113. bool SerializeToArcadiaStream(TChunkSerializer *serializer) const override {
  114. return serializer->WriteString(&Query);
  115. }
  116. ui32 CalculateSerializedSize() const override {
  117. return Query.size();
  118. }
  119. bool IsSerializable() const override {
  120. return true;
  121. }
  122. static IEventBase* Load(TEventSerializedData* bufs) {
  123. TString s = bufs->GetString();
  124. if (s.size() && s[0] == '\0') {
  125. TRope::TConstIterator iter = bufs->GetBeginIter();
  126. ui64 size = bufs->GetSize();
  127. iter += 1, --size; // skip '\0'
  128. TRopeStream stream(iter, size);
  129. auto res = std::make_unique<TEvRemoteHttpInfo>();
  130. res->Query = s;
  131. res->ExtendedQuery.emplace();
  132. const bool success = res->ExtendedQuery->ParseFromZeroCopyStream(&stream);
  133. Y_ABORT_UNLESS(success);
  134. return res.release();
  135. } else {
  136. return new TEvRemoteHttpInfo(s);
  137. }
  138. }
  139. };
  140. struct TEvRemoteHttpInfoRes: public NActors::TEventBase<TEvRemoteHttpInfoRes, RemoteHttpInfoRes> {
  141. TEvRemoteHttpInfoRes() {
  142. }
  143. TEvRemoteHttpInfoRes(const TString& html)
  144. : Html(html)
  145. {
  146. }
  147. TString Html;
  148. TString ToStringHeader() const override {
  149. return "TEvRemoteHttpInfoRes";
  150. }
  151. bool SerializeToArcadiaStream(TChunkSerializer *serializer) const override {
  152. return serializer->WriteString(&Html);
  153. }
  154. ui32 CalculateSerializedSize() const override {
  155. return Html.size();
  156. }
  157. bool IsSerializable() const override {
  158. return true;
  159. }
  160. static IEventBase* Load(TEventSerializedData* bufs) {
  161. return new TEvRemoteHttpInfoRes(bufs->GetString());
  162. }
  163. };
  164. struct TEvRemoteJsonInfoRes: public NActors::TEventBase<TEvRemoteJsonInfoRes, RemoteJsonInfoRes> {
  165. TEvRemoteJsonInfoRes() {
  166. }
  167. TEvRemoteJsonInfoRes(const TString& json)
  168. : Json(json)
  169. {
  170. }
  171. TString Json;
  172. TString ToStringHeader() const override {
  173. return "TEvRemoteJsonInfoRes";
  174. }
  175. bool SerializeToArcadiaStream(TChunkSerializer *serializer) const override {
  176. return serializer->WriteString(&Json);
  177. }
  178. ui32 CalculateSerializedSize() const override {
  179. return Json.size();
  180. }
  181. bool IsSerializable() const override {
  182. return true;
  183. }
  184. static IEventBase* Load(TEventSerializedData* bufs) {
  185. return new TEvRemoteJsonInfoRes(bufs->GetString());
  186. }
  187. };
  188. struct TEvRemoteBinaryInfoRes: public NActors::TEventBase<TEvRemoteBinaryInfoRes, RemoteBinaryInfoRes> {
  189. TEvRemoteBinaryInfoRes() {
  190. }
  191. TEvRemoteBinaryInfoRes(const TString& blob)
  192. : Blob(blob)
  193. {
  194. }
  195. TString Blob;
  196. TString ToStringHeader() const override {
  197. return "TEvRemoteBinaryInfoRes";
  198. }
  199. bool SerializeToArcadiaStream(TChunkSerializer *serializer) const override {
  200. return serializer->WriteString(&Blob);
  201. }
  202. ui32 CalculateSerializedSize() const override {
  203. return Blob.size();
  204. }
  205. bool IsSerializable() const override {
  206. return true;
  207. }
  208. static IEventBase* Load(TEventSerializedData* bufs) {
  209. return new TEvRemoteBinaryInfoRes(bufs->GetString());
  210. }
  211. };
  212. }
  213. }