helpers.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "helpers.h"
  2. #include "context.h"
  3. #include "requests.h"
  4. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  5. #include <library/cpp/yson/node/node_io.h>
  6. namespace NYT {
  7. ///////////////////////////////////////////////////////////////////////////////
  8. TString CreateHostNameWithPort(const TString& hostName, const TClientContext& context)
  9. {
  10. static constexpr int HttpProxyPort = 80;
  11. static constexpr int HttpsProxyPort = 443;
  12. static constexpr int TvmOnlyHttpProxyPort = 9026;
  13. static constexpr int TvmOnlyHttpsProxyPort = 9443;
  14. if (hostName.find(':') == TString::npos) {
  15. int port;
  16. if (context.TvmOnly) {
  17. port = context.UseTLS
  18. ? TvmOnlyHttpsProxyPort
  19. : TvmOnlyHttpProxyPort;
  20. } else {
  21. port = context.UseTLS
  22. ? HttpsProxyPort
  23. : HttpProxyPort;
  24. }
  25. return Format("%v:%v", hostName, port);
  26. }
  27. return hostName;
  28. }
  29. TString GetFullUrl(const TString& hostName, const TClientContext& context, THttpHeader& header)
  30. {
  31. Y_UNUSED(context);
  32. return Format("http://%v%v", hostName, header.GetUrl());
  33. }
  34. static TString GetParametersDebugString(const THttpHeader& header)
  35. {
  36. const auto& parameters = header.GetParameters();
  37. if (parameters.Empty()) {
  38. return "<empty>";
  39. } else {
  40. return NodeToYsonString(parameters);
  41. }
  42. }
  43. TString TruncateForLogs(const TString& text, size_t maxSize)
  44. {
  45. Y_VERIFY(maxSize > 10);
  46. if (text.empty()) {
  47. static TString empty = "empty";
  48. return empty;
  49. } else if (text.size() > maxSize) {
  50. TStringStream out;
  51. out << text.substr(0, maxSize) + "... (" << text.size() << " bytes total)";
  52. return out.Str();
  53. } else {
  54. return text;
  55. }
  56. }
  57. TString GetLoggedAttributes(const THttpHeader& header, const TString& url, bool includeParameters, size_t sizeLimit)
  58. {
  59. const auto parametersDebugString = GetParametersDebugString(header);
  60. TStringStream out;
  61. out << "Method: " << url << "; "
  62. << "X-YT-Parameters (sent in " << (includeParameters ? "header" : "body") << "): " << TruncateForLogs(parametersDebugString, sizeLimit);
  63. return out.Str();
  64. }
  65. void LogRequest(const THttpHeader& header, const TString& url, bool includeParameters, const TString& requestId, const TString& hostName)
  66. {
  67. YT_LOG_DEBUG("REQ %v - sending request (HostName: %v; %v)",
  68. requestId,
  69. hostName,
  70. GetLoggedAttributes(header, url, includeParameters, Max<size_t>()));
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. } // namespace NYT