text_encoder_ut.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "text.h"
  2. #include <library/cpp/monlib/metrics/histogram_collector.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. using namespace NMonitoring;
  5. Y_UNIT_TEST_SUITE(TTextText) {
  6. template <typename TFunc>
  7. TString EncodeToString(bool humanReadableTs, TFunc fn) {
  8. TStringStream ss;
  9. IMetricEncoderPtr encoder = EncoderText(&ss, humanReadableTs);
  10. fn(encoder.Get());
  11. return ss.Str();
  12. }
  13. Y_UNIT_TEST(Empty) {
  14. auto result = EncodeToString(true, [](IMetricEncoder* e) {
  15. e->OnStreamBegin();
  16. e->OnStreamEnd();
  17. });
  18. UNIT_ASSERT_STRINGS_EQUAL(result, "");
  19. }
  20. Y_UNIT_TEST(CommonPart) {
  21. auto result = EncodeToString(true, [](IMetricEncoder* e) {
  22. e->OnStreamBegin();
  23. e->OnCommonTime(TInstant::ParseIso8601Deprecated("2017-01-02T03:04:05.006Z"));
  24. {
  25. e->OnLabelsBegin();
  26. e->OnLabel("project", "solomon");
  27. e->OnLabel("cluster", "man");
  28. e->OnLabel("service", "stockpile");
  29. e->OnLabelsEnd();
  30. }
  31. e->OnStreamEnd();
  32. });
  33. UNIT_ASSERT_STRINGS_EQUAL(result,
  34. "common time: 2017-01-02T03:04:05Z\n"
  35. "common labels: {project='solomon', cluster='man', service='stockpile'}\n");
  36. }
  37. Y_UNIT_TEST(Gauges) {
  38. auto result = EncodeToString(true, [](IMetricEncoder* e) {
  39. e->OnStreamBegin();
  40. { // no values
  41. e->OnMetricBegin(EMetricType::GAUGE);
  42. {
  43. e->OnLabelsBegin();
  44. e->OnLabel("sensor", "cpuUsage");
  45. e->OnLabelsEnd();
  46. }
  47. e->OnMetricEnd();
  48. }
  49. { // one value no ts
  50. e->OnMetricBegin(EMetricType::GAUGE);
  51. {
  52. e->OnLabelsBegin();
  53. e->OnLabel("sensor", "diskUsage");
  54. e->OnLabel("disk", "sda1");
  55. e->OnLabelsEnd();
  56. }
  57. e->OnDouble(TInstant::Zero(), 1000);
  58. e->OnMetricEnd();
  59. }
  60. { // one value with ts
  61. e->OnMetricBegin(EMetricType::GAUGE);
  62. {
  63. e->OnLabelsBegin();
  64. e->OnLabel("sensor", "memoryUsage");
  65. e->OnLabel("host", "solomon-man-00");
  66. e->OnLabel("dc", "man");
  67. e->OnLabelsEnd();
  68. }
  69. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:00Z"), 1000);
  70. e->OnMetricEnd();
  71. }
  72. { // many values
  73. e->OnMetricBegin(EMetricType::GAUGE);
  74. {
  75. e->OnLabelsBegin();
  76. e->OnLabel("sensor", "bytesRx");
  77. e->OnLabel("host", "solomon-sas-01");
  78. e->OnLabel("dc", "sas");
  79. e->OnLabelsEnd();
  80. }
  81. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:00Z"), 2);
  82. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:05Z"), 4);
  83. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:10Z"), 8);
  84. e->OnMetricEnd();
  85. }
  86. e->OnStreamEnd();
  87. });
  88. UNIT_ASSERT_STRINGS_EQUAL(result,
  89. " GAUGE cpuUsage{}\n"
  90. " GAUGE diskUsage{disk='sda1'} [1000]\n"
  91. " GAUGE memoryUsage{host='solomon-man-00', dc='man'} [(2017-12-02T12:00:00Z, 1000)]\n"
  92. " GAUGE bytesRx{host='solomon-sas-01', dc='sas'} [(2017-12-02T12:00:00Z, 2), (2017-12-02T12:00:05Z, 4), (2017-12-02T12:00:10Z, 8)]\n");
  93. }
  94. Y_UNIT_TEST(IntGauges) {
  95. auto result = EncodeToString(true, [](IMetricEncoder* e) {
  96. e->OnStreamBegin();
  97. { // no values
  98. e->OnMetricBegin(EMetricType::IGAUGE);
  99. {
  100. e->OnLabelsBegin();
  101. e->OnLabel("sensor", "cpuUsage");
  102. e->OnLabelsEnd();
  103. }
  104. e->OnMetricEnd();
  105. }
  106. { // one value no ts
  107. e->OnMetricBegin(EMetricType::IGAUGE);
  108. {
  109. e->OnLabelsBegin();
  110. e->OnLabel("sensor", "diskUsage");
  111. e->OnLabel("disk", "sda1");
  112. e->OnLabelsEnd();
  113. }
  114. e->OnDouble(TInstant::Zero(), 1000);
  115. e->OnMetricEnd();
  116. }
  117. { // one value with ts
  118. e->OnMetricBegin(EMetricType::IGAUGE);
  119. {
  120. e->OnLabelsBegin();
  121. e->OnLabel("sensor", "memoryUsage");
  122. e->OnLabel("host", "solomon-man-00");
  123. e->OnLabel("dc", "man");
  124. e->OnLabelsEnd();
  125. }
  126. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:00Z"), 1000);
  127. e->OnMetricEnd();
  128. }
  129. { // many values
  130. e->OnMetricBegin(EMetricType::IGAUGE);
  131. {
  132. e->OnLabelsBegin();
  133. e->OnLabel("sensor", "bytesRx");
  134. e->OnLabel("host", "solomon-sas-01");
  135. e->OnLabel("dc", "sas");
  136. e->OnLabelsEnd();
  137. }
  138. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:00Z"), 2);
  139. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:05Z"), 4);
  140. e->OnDouble(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:10Z"), 8);
  141. e->OnMetricEnd();
  142. }
  143. e->OnStreamEnd();
  144. });
  145. UNIT_ASSERT_STRINGS_EQUAL(result,
  146. " IGAUGE cpuUsage{}\n"
  147. " IGAUGE diskUsage{disk='sda1'} [1000]\n"
  148. " IGAUGE memoryUsage{host='solomon-man-00', dc='man'} [(2017-12-02T12:00:00Z, 1000)]\n"
  149. " IGAUGE bytesRx{host='solomon-sas-01', dc='sas'} [(2017-12-02T12:00:00Z, 2), (2017-12-02T12:00:05Z, 4), (2017-12-02T12:00:10Z, 8)]\n");
  150. }
  151. Y_UNIT_TEST(Counters) {
  152. auto doEncode = [](IMetricEncoder* e) {
  153. e->OnStreamBegin();
  154. { // no values
  155. e->OnMetricBegin(EMetricType::COUNTER);
  156. {
  157. e->OnLabelsBegin();
  158. e->OnLabel("sensor", "cpuUsage");
  159. e->OnLabelsEnd();
  160. }
  161. e->OnMetricEnd();
  162. }
  163. { // one value no ts
  164. e->OnMetricBegin(EMetricType::COUNTER);
  165. {
  166. e->OnLabelsBegin();
  167. e->OnLabel("sensor", "diskUsage");
  168. e->OnLabel("disk", "sda1");
  169. e->OnLabelsEnd();
  170. }
  171. e->OnUint64(TInstant::Zero(), 1000);
  172. e->OnMetricEnd();
  173. }
  174. { // one value with ts
  175. e->OnMetricBegin(EMetricType::COUNTER);
  176. {
  177. e->OnLabelsBegin();
  178. e->OnLabel("sensor", "memoryUsage");
  179. e->OnLabel("host", "solomon-man-00");
  180. e->OnLabel("dc", "man");
  181. e->OnLabelsEnd();
  182. }
  183. e->OnUint64(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:00Z"), 1000);
  184. e->OnMetricEnd();
  185. }
  186. { // many values
  187. e->OnMetricBegin(EMetricType::COUNTER);
  188. {
  189. e->OnLabelsBegin();
  190. e->OnLabel("sensor", "bytesRx");
  191. e->OnLabel("host", "solomon-sas-01");
  192. e->OnLabel("dc", "sas");
  193. e->OnLabelsEnd();
  194. }
  195. e->OnUint64(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:00Z"), 2);
  196. e->OnUint64(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:05Z"), 4);
  197. e->OnUint64(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:10Z"), 8);
  198. e->OnMetricEnd();
  199. }
  200. e->OnStreamEnd();
  201. };
  202. auto result1 = EncodeToString(false, doEncode);
  203. UNIT_ASSERT_STRINGS_EQUAL(result1,
  204. " COUNTER cpuUsage{}\n"
  205. " COUNTER diskUsage{disk='sda1'} [1000]\n"
  206. " COUNTER memoryUsage{host='solomon-man-00', dc='man'} [(1512216000, 1000)]\n"
  207. " COUNTER bytesRx{host='solomon-sas-01', dc='sas'} [(1512216000, 2), (1512216005, 4), (1512216010, 8)]\n");
  208. auto result2 = EncodeToString(true, doEncode);
  209. UNIT_ASSERT_STRINGS_EQUAL(result2,
  210. " COUNTER cpuUsage{}\n"
  211. " COUNTER diskUsage{disk='sda1'} [1000]\n"
  212. " COUNTER memoryUsage{host='solomon-man-00', dc='man'} [(2017-12-02T12:00:00Z, 1000)]\n"
  213. " COUNTER bytesRx{host='solomon-sas-01', dc='sas'} [(2017-12-02T12:00:00Z, 2), (2017-12-02T12:00:05Z, 4), (2017-12-02T12:00:10Z, 8)]\n");
  214. }
  215. Y_UNIT_TEST(Histograms) {
  216. auto h = ExplicitHistogram({1, 2, 3, 4, 5});
  217. h->Collect(3);
  218. h->Collect(5, 7);
  219. h->Collect(13);
  220. auto s = h->Snapshot();
  221. TString result = EncodeToString(true, [s](IMetricEncoder* e) {
  222. e->OnStreamBegin();
  223. {
  224. e->OnMetricBegin(EMetricType::HIST);
  225. {
  226. e->OnLabelsBegin();
  227. e->OnLabel("sensor", "readTimeMillis");
  228. e->OnLabelsEnd();
  229. }
  230. e->OnHistogram(TInstant::Zero(), s);
  231. e->OnMetricEnd();
  232. }
  233. {
  234. e->OnMetricBegin(EMetricType::HIST_RATE);
  235. {
  236. e->OnLabelsBegin();
  237. e->OnLabel("sensor", "writeTimeMillis");
  238. e->OnLabelsEnd();
  239. }
  240. e->OnHistogram(TInstant::Zero(), s);
  241. e->OnMetricEnd();
  242. }
  243. e->OnStreamEnd();
  244. });
  245. UNIT_ASSERT_STRINGS_EQUAL(result,
  246. " HIST readTimeMillis{} [{1: 0, 2: 0, 3: 1, 4: 0, 5: 7, inf: 1}]\n"
  247. "HIST_RATE writeTimeMillis{} [{1: 0, 2: 0, 3: 1, 4: 0, 5: 7, inf: 1}]\n");
  248. }
  249. Y_UNIT_TEST(Summary) {
  250. auto s = MakeIntrusive<TSummaryDoubleSnapshot>(10.1, -0.45, 0.478, 0.3, 30u);
  251. TString result = EncodeToString(true, [s](IMetricEncoder* e) {
  252. e->OnStreamBegin();
  253. {
  254. e->OnMetricBegin(EMetricType::DSUMMARY);
  255. {
  256. e->OnLabelsBegin();
  257. e->OnLabel("sensor", "temperature");
  258. e->OnLabelsEnd();
  259. }
  260. e->OnSummaryDouble(TInstant::Zero(), s);
  261. e->OnMetricEnd();
  262. }
  263. e->OnStreamEnd();
  264. });
  265. UNIT_ASSERT_STRINGS_EQUAL(result,
  266. " DSUMMARY temperature{} [{sum: 10.1, min: -0.45, max: 0.478, last: 0.3, count: 30}]\n");
  267. }
  268. }