prometheus_decoder_ut.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #include "prometheus.h"
  2. #include <library/cpp/monlib/encode/protobuf/protobuf.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. using namespace NMonitoring;
  5. #define ASSERT_LABEL_EQUAL(label, name, value) do { \
  6. UNIT_ASSERT_STRINGS_EQUAL((label).GetName(), name); \
  7. UNIT_ASSERT_STRINGS_EQUAL((label).GetValue(), value); \
  8. } while (false)
  9. #define ASSERT_DOUBLE_POINT(s, time, value) do { \
  10. UNIT_ASSERT_VALUES_EQUAL((s).GetTime(), (time).MilliSeconds()); \
  11. UNIT_ASSERT_EQUAL((s).GetValueCase(), NProto::TSingleSample::kFloat64); \
  12. UNIT_ASSERT_DOUBLES_EQUAL((s).GetFloat64(), value, std::numeric_limits<double>::epsilon()); \
  13. } while (false)
  14. #define ASSERT_UINT_POINT(s, time, value) do { \
  15. UNIT_ASSERT_VALUES_EQUAL((s).GetTime(), (time).MilliSeconds()); \
  16. UNIT_ASSERT_EQUAL((s).GetValueCase(), NProto::TSingleSample::kUint64); \
  17. UNIT_ASSERT_VALUES_EQUAL((s).GetUint64(), value); \
  18. } while (false)
  19. #define ASSERT_HIST_POINT(s, time, expected) do { \
  20. UNIT_ASSERT_VALUES_EQUAL((s).GetTime(), time.MilliSeconds()); \
  21. UNIT_ASSERT_EQUAL((s).GetValueCase(), NProto::TSingleSample::kHistogram);\
  22. UNIT_ASSERT_VALUES_EQUAL((s).GetHistogram().BoundsSize(), (expected).Count()); \
  23. UNIT_ASSERT_VALUES_EQUAL((s).GetHistogram().ValuesSize(), (expected).Count()); \
  24. for (size_t i = 0; i < (s).GetHistogram().BoundsSize(); i++) { \
  25. UNIT_ASSERT_DOUBLES_EQUAL((s).GetHistogram().GetBounds(i), (expected).UpperBound(i), Min<double>()); \
  26. UNIT_ASSERT_VALUES_EQUAL((s).GetHistogram().GetValues(i), (expected).Value(i)); \
  27. } \
  28. } while (false)
  29. Y_UNIT_TEST_SUITE(TPrometheusDecoderTest) {
  30. NProto::TSingleSamplesList Decode(TStringBuf data, const TPrometheusDecodeSettings& settings = TPrometheusDecodeSettings{}) {
  31. NProto::TSingleSamplesList samples;
  32. ;
  33. {
  34. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  35. DecodePrometheus(data, e.Get(), "sensor", settings);
  36. }
  37. return samples;
  38. }
  39. Y_UNIT_TEST(Empty) {
  40. {
  41. auto samples = Decode("");
  42. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 0);
  43. }
  44. {
  45. auto samples = Decode("\n");
  46. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 0);
  47. }
  48. {
  49. auto samples = Decode("\n \n \n");
  50. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 0);
  51. }
  52. {
  53. auto samples = Decode("\t\n\t\n");
  54. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 0);
  55. }
  56. }
  57. Y_UNIT_TEST(Minimal) {
  58. auto samples = Decode(
  59. "minimal_metric 1.234\n"
  60. "another_metric -3e3 103948\n"
  61. "# Even that:\n"
  62. "no_labels{} 3\n"
  63. "# HELP line for non-existing metric will be ignored.\n");
  64. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3);
  65. {
  66. auto& s = samples.GetSamples(0);
  67. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  68. UNIT_ASSERT_EQUAL(1, s.LabelsSize());
  69. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "minimal_metric");
  70. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 1.234);
  71. }
  72. {
  73. auto& s = samples.GetSamples(1);
  74. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  75. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  76. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "another_metric");
  77. ASSERT_DOUBLE_POINT(s, TInstant::MilliSeconds(103948), -3000.0);
  78. }
  79. {
  80. auto& s = samples.GetSamples(2);
  81. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  82. UNIT_ASSERT_EQUAL(1, s.LabelsSize());
  83. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "no_labels");
  84. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 3.0);
  85. }
  86. }
  87. Y_UNIT_TEST(Counter) {
  88. constexpr auto inputMetrics =
  89. "# A normal comment.\n"
  90. "#\n"
  91. "# TYPE name counter\n"
  92. "name{labelname=\"val1\",basename=\"basevalue\"} NaN\n"
  93. "name {labelname=\"val2\",basename=\"basevalue\"} 2.3 1234567890\n"
  94. "# HELP name two-line\\n doc str\\\\ing\n";
  95. {
  96. auto samples = Decode(inputMetrics);
  97. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 2);
  98. {
  99. auto& s = samples.GetSamples(0);
  100. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  101. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  102. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "name");
  103. ASSERT_LABEL_EQUAL(s.GetLabels(1), "basename", "basevalue");
  104. ASSERT_LABEL_EQUAL(s.GetLabels(2), "labelname", "val1");
  105. ASSERT_UINT_POINT(s, TInstant::Zero(), ui64(0));
  106. }
  107. {
  108. auto& s = samples.GetSamples(1);
  109. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  110. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  111. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "name");
  112. ASSERT_LABEL_EQUAL(s.GetLabels(1), "basename", "basevalue");
  113. ASSERT_LABEL_EQUAL(s.GetLabels(2), "labelname", "val2");
  114. ASSERT_UINT_POINT(s, TInstant::MilliSeconds(1234567890), i64(2));
  115. }
  116. }
  117. {
  118. TPrometheusDecodeSettings settings;
  119. settings.Mode = EPrometheusDecodeMode::RAW;
  120. auto samples = Decode(inputMetrics, settings);
  121. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 2);
  122. {
  123. auto& s = samples.GetSamples(0);
  124. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  125. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  126. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "name");
  127. ASSERT_LABEL_EQUAL(s.GetLabels(1), "basename", "basevalue");
  128. ASSERT_LABEL_EQUAL(s.GetLabels(2), "labelname", "val1");
  129. ASSERT_DOUBLE_POINT(s, TInstant::MilliSeconds(0), NAN);
  130. }
  131. {
  132. auto& s = samples.GetSamples(1);
  133. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  134. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  135. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "name");
  136. ASSERT_LABEL_EQUAL(s.GetLabels(1), "basename", "basevalue");
  137. ASSERT_LABEL_EQUAL(s.GetLabels(2), "labelname", "val2");
  138. ASSERT_DOUBLE_POINT(s, TInstant::MilliSeconds(1234567890), 2.3);
  139. }
  140. }
  141. }
  142. Y_UNIT_TEST(Gauge) {
  143. auto samples = Decode(
  144. "# A normal comment.\n"
  145. "#\n"
  146. " # HELP name2 \tdoc str\"ing 2\n"
  147. " # TYPE name2 gauge\n"
  148. "name2{labelname=\"val2\"\t,basename = \"basevalue2\"\t\t} +Inf 54321\n"
  149. "name2{ labelname = \"val1\" , }-Inf\n");
  150. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 2);
  151. {
  152. auto& s = samples.GetSamples(0);
  153. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  154. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  155. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "name2");
  156. ASSERT_LABEL_EQUAL(s.GetLabels(1), "basename", "basevalue2");
  157. ASSERT_LABEL_EQUAL(s.GetLabels(2), "labelname", "val2");
  158. ASSERT_DOUBLE_POINT(s, TInstant::MilliSeconds(54321), INFINITY);
  159. }
  160. {
  161. auto& s = samples.GetSamples(1);
  162. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  163. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  164. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "name2");
  165. ASSERT_LABEL_EQUAL(s.GetLabels(1), "labelname", "val1");
  166. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), -INFINITY);
  167. }
  168. }
  169. Y_UNIT_TEST(Summary) {
  170. auto samples = Decode(
  171. "# HELP \n"
  172. "# TYPE my_summary summary\n"
  173. "my_summary{n1=\"val1\",quantile=\"0.5\"} 110\n"
  174. "my_summary{n1=\"val1\",quantile=\"0.9\"} 140 1\n"
  175. "my_summary_count{n1=\"val1\"} 42\n"
  176. "my_summary_sum{n1=\"val1\"} 08 15\n"
  177. "# some\n"
  178. "# funny comments\n"
  179. "# HELP\n"
  180. "# HELP my_summary\n"
  181. "# HELP my_summary \n");
  182. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 4);
  183. {
  184. auto& s = samples.GetSamples(0);
  185. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  186. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  187. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "my_summary");
  188. ASSERT_LABEL_EQUAL(s.GetLabels(1), "quantile", "0.5");
  189. ASSERT_LABEL_EQUAL(s.GetLabels(2), "n1", "val1");
  190. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 110.0);
  191. }
  192. {
  193. auto& s = samples.GetSamples(1);
  194. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  195. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  196. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "my_summary");
  197. ASSERT_LABEL_EQUAL(s.GetLabels(1), "quantile", "0.9");
  198. ASSERT_LABEL_EQUAL(s.GetLabels(2), "n1", "val1");
  199. ASSERT_DOUBLE_POINT(s, TInstant::MilliSeconds(1), 140.0);
  200. }
  201. {
  202. auto& s = samples.GetSamples(2);
  203. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  204. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  205. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "my_summary_count");
  206. ASSERT_LABEL_EQUAL(s.GetLabels(1), "n1", "val1");
  207. ASSERT_UINT_POINT(s, TInstant::Zero(), 42);
  208. }
  209. {
  210. auto& s = samples.GetSamples(3);
  211. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  212. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  213. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "my_summary_sum");
  214. ASSERT_LABEL_EQUAL(s.GetLabels(1), "n1", "val1");
  215. ASSERT_DOUBLE_POINT(s, TInstant::MilliSeconds(15), 8.0);
  216. }
  217. }
  218. Y_UNIT_TEST(Histogram) {
  219. constexpr auto inputMetrics =
  220. "# HELP request_duration_microseconds The response latency.\n"
  221. "# TYPE request_duration_microseconds histogram\n"
  222. "request_duration_microseconds_bucket{le=\"0\"} 0\n"
  223. "request_duration_microseconds_bucket{le=\"100\"} 123\n"
  224. "request_duration_microseconds_bucket{le=\"120\"} 412\n"
  225. "request_duration_microseconds_bucket{le=\"144\"} 592\n"
  226. "request_duration_microseconds_bucket{le=\"172.8\"} 1524\n"
  227. "request_duration_microseconds_bucket{le=\"+Inf\"} 2693\n"
  228. "request_duration_microseconds_sum 1.7560473e+06\n"
  229. "request_duration_microseconds_count 2693\n";
  230. {
  231. auto samples = Decode(inputMetrics);
  232. {
  233. auto& s = samples.GetSamples(0);
  234. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  235. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  236. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "request_duration_microseconds_sum");
  237. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 1756047.3);
  238. }
  239. {
  240. auto& s = samples.GetSamples(1);
  241. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  242. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  243. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "request_duration_microseconds_count");
  244. ASSERT_UINT_POINT(s, TInstant::Zero(), 2693);
  245. }
  246. {
  247. auto& s = samples.GetSamples(2);
  248. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::HIST_RATE);
  249. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  250. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "request_duration_microseconds");
  251. auto hist = ExplicitHistogramSnapshot(
  252. {0, 100, 120, 144, 172.8, HISTOGRAM_INF_BOUND},
  253. {0, 123, 289, 180, 932, 1169});
  254. ASSERT_HIST_POINT(s, TInstant::Zero(), *hist);
  255. }
  256. }
  257. {
  258. TPrometheusDecodeSettings settings;
  259. settings.Mode = EPrometheusDecodeMode::RAW;
  260. auto samples = Decode(inputMetrics, settings);
  261. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 8);
  262. {
  263. auto& s = samples.GetSamples(0);
  264. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  265. UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2);
  266. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "request_duration_microseconds_bucket");
  267. ASSERT_LABEL_EQUAL(s.GetLabels(1), "le", "0");
  268. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 0);
  269. }
  270. }
  271. }
  272. Y_UNIT_TEST(HistogramWithLabels) {
  273. auto samples = Decode(
  274. "# A histogram, which has a pretty complex representation in the text format:\n"
  275. "# HELP http_request_duration_seconds A histogram of the request duration.\n"
  276. "# TYPE http_request_duration_seconds histogram\n"
  277. "http_request_duration_seconds_bucket{le=\"0.05\", method=\"POST\"} 24054\n"
  278. "http_request_duration_seconds_bucket{method=\"POST\", le=\"0.1\"} 33444\n"
  279. "http_request_duration_seconds_bucket{le=\"0.2\", method=\"POST\", } 100392\n"
  280. "http_request_duration_seconds_bucket{le=\"0.5\",method=\"POST\",} 129389\n"
  281. "http_request_duration_seconds_bucket{ method=\"POST\", le=\"1\", } 133988\n"
  282. "http_request_duration_seconds_bucket{ le=\"+Inf\", method=\"POST\", } 144320\n"
  283. "http_request_duration_seconds_sum{method=\"POST\"} 53423\n"
  284. "http_request_duration_seconds_count{ method=\"POST\", } 144320\n");
  285. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3);
  286. {
  287. auto& s = samples.GetSamples(0);
  288. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  289. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  290. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "http_request_duration_seconds_sum");
  291. ASSERT_LABEL_EQUAL(s.GetLabels(1), "method", "POST");
  292. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 53423.0);
  293. }
  294. {
  295. auto& s = samples.GetSamples(1);
  296. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  297. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  298. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "http_request_duration_seconds_count");
  299. ASSERT_LABEL_EQUAL(s.GetLabels(1), "method", "POST");
  300. ASSERT_UINT_POINT(s, TInstant::Zero(), 144320);
  301. }
  302. {
  303. auto& s = samples.GetSamples(2);
  304. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::HIST_RATE);
  305. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  306. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "http_request_duration_seconds");
  307. ASSERT_LABEL_EQUAL(s.GetLabels(1), "method", "POST");
  308. auto hist = ExplicitHistogramSnapshot(
  309. { 0.05, 0.1, 0.2, 0.5, 1, HISTOGRAM_INF_BOUND },
  310. { 24054, 9390, 66948, 28997, 4599, 10332 });
  311. ASSERT_HIST_POINT(s, TInstant::Zero(), *hist);
  312. }
  313. }
  314. Y_UNIT_TEST(MultipleHistograms) {
  315. auto samples = Decode(
  316. "# TYPE inboundBytesPerSec histogram\n"
  317. "inboundBytesPerSec_bucket{client=\"mbus\", le=\"10.0\"} 1.0\n"
  318. "inboundBytesPerSec_bucket{client=\"mbus\", le=\"20.0\"} 5.0\n"
  319. "inboundBytesPerSec_bucket{client=\"mbus\", le=\"+Inf\"} 5.0\n"
  320. "inboundBytesPerSec_count{client=\"mbus\"} 5.0\n"
  321. "inboundBytesPerSec_bucket{client=\"grpc\", le=\"10.0\"} 1.0\n"
  322. "inboundBytesPerSec_bucket{client=\"grpc\", le=\"20.0\"} 5.0\n"
  323. "inboundBytesPerSec_bucket{client=\"grpc\", le=\"30.0\"} 5.0\n"
  324. "inboundBytesPerSec_count{client=\"grpc\"} 5.0\n"
  325. "# TYPE outboundBytesPerSec histogram\n"
  326. "outboundBytesPerSec_bucket{client=\"grpc\", le=\"100.0\"} 1.0 1512216000000\n"
  327. "outboundBytesPerSec_bucket{client=\"grpc\", le=\"200.0\"} 1.0 1512216000000\n"
  328. "outboundBytesPerSec_bucket{client=\"grpc\", le=\"+Inf\"} 1.0 1512216000000\n"
  329. "outboundBytesPerSec_count{client=\"grpc\"} 1.0 1512216000000\n");
  330. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 6);
  331. {
  332. auto& s = samples.GetSamples(0);
  333. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  334. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  335. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "inboundBytesPerSec_count");
  336. ASSERT_LABEL_EQUAL(s.GetLabels(1), "client", "mbus");
  337. ASSERT_UINT_POINT(s, TInstant::Zero(), 5);
  338. }
  339. {
  340. auto& s = samples.GetSamples(1);
  341. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::HIST_RATE);
  342. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  343. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "inboundBytesPerSec");
  344. ASSERT_LABEL_EQUAL(s.GetLabels(1), "client", "mbus");
  345. auto hist = ExplicitHistogramSnapshot(
  346. { 10, 20, HISTOGRAM_INF_BOUND },
  347. { 1, 4, 0 });
  348. ASSERT_HIST_POINT(s, TInstant::Zero(), *hist);
  349. }
  350. {
  351. auto& s = samples.GetSamples(2);
  352. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  353. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  354. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "inboundBytesPerSec_count");
  355. ASSERT_LABEL_EQUAL(s.GetLabels(1), "client", "grpc");
  356. ASSERT_UINT_POINT(s, TInstant::Zero(), 5);
  357. }
  358. {
  359. auto& s = samples.GetSamples(3);
  360. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::HIST_RATE);
  361. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  362. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "inboundBytesPerSec");
  363. ASSERT_LABEL_EQUAL(s.GetLabels(1), "client", "grpc");
  364. auto hist = ExplicitHistogramSnapshot(
  365. { 10, 20, 30 },
  366. { 1, 4, 0 });
  367. ASSERT_HIST_POINT(s, TInstant::Zero(), *hist);
  368. }
  369. {
  370. auto& s = samples.GetSamples(4);
  371. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  372. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  373. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "outboundBytesPerSec_count");
  374. ASSERT_LABEL_EQUAL(s.GetLabels(1), "client", "grpc");
  375. ASSERT_UINT_POINT(s, TInstant::Seconds(1512216000), 1) ;
  376. }
  377. {
  378. auto& s = samples.GetSamples(5);
  379. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::HIST_RATE);
  380. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  381. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "outboundBytesPerSec");
  382. ASSERT_LABEL_EQUAL(s.GetLabels(1), "client", "grpc");
  383. auto hist = ExplicitHistogramSnapshot(
  384. { 100, 200, HISTOGRAM_INF_BOUND },
  385. { 1, 0, 0 });
  386. ASSERT_HIST_POINT(s, TInstant::Seconds(1512216000), *hist);
  387. }
  388. }
  389. Y_UNIT_TEST(MixedTypes) {
  390. auto samples = Decode(
  391. "# HELP http_requests_total The total number of HTTP requests.\n"
  392. "# TYPE http_requests_total counter\n"
  393. "http_requests_total { } 1027 1395066363000\n"
  394. "http_requests_total{method=\"post\",code=\"200\"} 1027 1395066363000\n"
  395. "http_requests_total{method=\"post\",code=\"400\"} 3 1395066363000\n"
  396. "\n"
  397. "# Minimalistic line:\n"
  398. "metric_without_timestamp_and_labels 12.47\n"
  399. "\n"
  400. "# HELP rpc_duration_seconds A summary of the RPC duration in seconds.\n"
  401. "# TYPE rpc_duration_seconds summary\n"
  402. "rpc_duration_seconds{quantile=\"0.01\"} 3102\n"
  403. "rpc_duration_seconds{quantile=\"0.5\"} 4773\n"
  404. "rpc_duration_seconds{quantile=\"0.9\"} 9001\n"
  405. "rpc_duration_seconds_sum 1.7560473e+07\n"
  406. "rpc_duration_seconds_count 2693\n"
  407. "\n"
  408. "# Another mMinimalistic line:\n"
  409. "metric_with_timestamp 12.47 1234567890\n");
  410. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 10);
  411. {
  412. auto& s = samples.GetSamples(0);
  413. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  414. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  415. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "http_requests_total");
  416. ASSERT_UINT_POINT(s, TInstant::Seconds(1395066363), 1027);
  417. }
  418. {
  419. auto& s = samples.GetSamples(1);
  420. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  421. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  422. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "http_requests_total");
  423. ASSERT_LABEL_EQUAL(s.GetLabels(1), "method", "post");
  424. ASSERT_LABEL_EQUAL(s.GetLabels(2), "code", "200");
  425. ASSERT_UINT_POINT(s, TInstant::Seconds(1395066363), 1027);
  426. }
  427. {
  428. auto& s = samples.GetSamples(2);
  429. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  430. UNIT_ASSERT_EQUAL(s.LabelsSize(), 3);
  431. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "http_requests_total");
  432. ASSERT_LABEL_EQUAL(s.GetLabels(1), "method", "post");
  433. ASSERT_LABEL_EQUAL(s.GetLabels(2), "code", "400");
  434. ASSERT_UINT_POINT(s, TInstant::Seconds(1395066363), 3);
  435. }
  436. {
  437. auto& s = samples.GetSamples(3);
  438. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  439. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  440. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "metric_without_timestamp_and_labels");
  441. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 12.47);
  442. }
  443. {
  444. auto& s = samples.GetSamples(4);
  445. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  446. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  447. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "rpc_duration_seconds");
  448. ASSERT_LABEL_EQUAL(s.GetLabels(1), "quantile", "0.01");
  449. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 3102);
  450. }
  451. {
  452. auto& s = samples.GetSamples(5);
  453. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  454. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  455. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "rpc_duration_seconds");
  456. ASSERT_LABEL_EQUAL(s.GetLabels(1), "quantile", "0.5");
  457. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 4773);
  458. }
  459. {
  460. auto& s = samples.GetSamples(6);
  461. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  462. UNIT_ASSERT_EQUAL(s.LabelsSize(), 2);
  463. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "rpc_duration_seconds");
  464. ASSERT_LABEL_EQUAL(s.GetLabels(1), "quantile", "0.9");
  465. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 9001);
  466. }
  467. {
  468. auto& s = samples.GetSamples(7);
  469. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  470. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  471. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "rpc_duration_seconds_sum");
  472. ASSERT_DOUBLE_POINT(s, TInstant::Zero(), 17560473);
  473. }
  474. {
  475. auto& s = samples.GetSamples(8);
  476. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::RATE);
  477. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  478. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "rpc_duration_seconds_count");
  479. ASSERT_UINT_POINT(s, TInstant::Zero(), 2693);
  480. }
  481. {
  482. auto& s = samples.GetSamples(9);
  483. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::EMetricType::GAUGE);
  484. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  485. ASSERT_LABEL_EQUAL(s.GetLabels(0), "sensor", "metric_with_timestamp");
  486. ASSERT_DOUBLE_POINT(s, TInstant::MilliSeconds(1234567890), 12.47);
  487. }
  488. }
  489. }