unistat_ut.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "unistat.h"
  2. #include <library/cpp/monlib/encode/protobuf/protobuf.h>
  3. #include <library/cpp/monlib/metrics/labels.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. using namespace NMonitoring;
  6. Y_UNIT_TEST_SUITE(TUnistatDecoderTest) {
  7. Y_UNIT_TEST(MetricNameLabel) {
  8. constexpr auto input = TStringBuf(R"([["something_axxx", 42]])");
  9. NProto::TMultiSamplesList samples;
  10. auto encoder = EncoderProtobuf(&samples);
  11. DecodeUnistat(input, encoder.Get(), "metric_name_label");
  12. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1);
  13. auto sample = samples.GetSamples(0);
  14. auto label = sample.GetLabels(0);
  15. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "metric_name_label");
  16. }
  17. Y_UNIT_TEST(MetricNamePrefix) {
  18. constexpr auto input = TStringBuf(R"([["something_axxx", 42]])");
  19. NProto::TMultiSamplesList samples;
  20. auto encoder = EncoderProtobuf(&samples);
  21. DecodeUnistat(input, encoder.Get(), "metric_name_label", "prefix.");
  22. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1);
  23. auto sample = samples.GetSamples(0);
  24. auto label = sample.GetLabels(0);
  25. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "metric_name_label");
  26. UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "prefix.something_axxx");
  27. }
  28. Y_UNIT_TEST(ScalarMetric) {
  29. constexpr auto input = TStringBuf(R"([["something_axxx", 42]])");
  30. NProto::TMultiSamplesList samples;
  31. auto encoder = EncoderProtobuf(&samples);
  32. DecodeUnistat(input, encoder.Get());
  33. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1);
  34. auto sample = samples.GetSamples(0);
  35. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::GAUGE);
  36. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  37. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  38. auto label = sample.GetLabels(0);
  39. auto point = sample.GetPoints(0);
  40. UNIT_ASSERT_VALUES_EQUAL(point.GetFloat64(), 42.);
  41. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor");
  42. UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_axxx");
  43. }
  44. Y_UNIT_TEST(OverriddenTags) {
  45. constexpr auto input = TStringBuf(R"([["ctype=foo;prj=bar;custom_tag=qwe;something_axxx", 42]])");
  46. NProto::TMultiSamplesList samples;
  47. auto encoder = EncoderProtobuf(&samples);
  48. DecodeUnistat(input, encoder.Get());
  49. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1);
  50. auto sample = samples.GetSamples(0);
  51. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  52. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 4);
  53. const auto& labels = sample.GetLabels();
  54. TLabels actual;
  55. for (auto&& l : labels) {
  56. actual.Add(l.GetName(), l.GetValue());
  57. }
  58. TLabels expected{{"ctype", "foo"}, {"prj", "bar"}, {"custom_tag", "qwe"}, {"sensor", "something_axxx"}};
  59. UNIT_ASSERT_VALUES_EQUAL(actual.size(), expected.size());
  60. for (auto&& l : actual) {
  61. UNIT_ASSERT(expected.Extract(l.Name())->Value() == l.Value());
  62. }
  63. }
  64. Y_UNIT_TEST(ThrowsOnTopLevelObject) {
  65. constexpr auto input = TStringBuf(R"({["something_axxx", 42]})");
  66. NProto::TMultiSamplesList samples;
  67. auto encoder = EncoderProtobuf(&samples);
  68. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  69. }
  70. Y_UNIT_TEST(ThrowsOnUnwrappedMetric) {
  71. constexpr auto input = TStringBuf(R"(["something_axxx", 42])");
  72. NProto::TMultiSamplesList samples;
  73. auto encoder = EncoderProtobuf(&samples);
  74. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  75. }
  76. Y_UNIT_TEST(HistogramMetric) {
  77. constexpr auto input = TStringBuf(R"([["something_hgram", [[0, 1], [200, 2], [500, 3]] ]])");
  78. NProto::TMultiSamplesList samples;
  79. auto encoder = EncoderProtobuf(&samples);
  80. DecodeUnistat(input, encoder.Get());
  81. auto sample = samples.GetSamples(0);
  82. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::HIST_RATE);
  83. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  84. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  85. auto label = sample.GetLabels(0);
  86. const auto point = sample.GetPoints(0);
  87. const auto histogram = point.GetHistogram();
  88. const auto size = histogram.BoundsSize();
  89. UNIT_ASSERT_VALUES_EQUAL(size, 4);
  90. const TVector<double> expectedBounds {0, 200, 500, std::numeric_limits<double>::max()};
  91. const TVector<ui64> expectedValues {0, 1, 2, 3};
  92. for (auto i = 0; i < 4; ++i) {
  93. UNIT_ASSERT_VALUES_EQUAL(histogram.GetBounds(i), expectedBounds[i]);
  94. UNIT_ASSERT_VALUES_EQUAL(histogram.GetValues(i), expectedValues[i]);
  95. }
  96. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor");
  97. UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_hgram");
  98. }
  99. Y_UNIT_TEST(AbsoluteHistogram) {
  100. constexpr auto input = TStringBuf(R"([["something_ahhh", [[0, 1], [200, 2], [500, 3]] ]])");
  101. NProto::TMultiSamplesList samples;
  102. auto encoder = EncoderProtobuf(&samples);
  103. DecodeUnistat(input, encoder.Get());
  104. auto sample = samples.GetSamples(0);
  105. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::HISTOGRAM);
  106. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  107. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  108. }
  109. Y_UNIT_TEST(LogHistogram) {
  110. constexpr auto input = TStringBuf(R"([["something_ahhh", [1, 2, 3] ]])");
  111. NProto::TMultiSamplesList samples;
  112. auto encoder = EncoderProtobuf(&samples);
  113. DecodeUnistat(input, encoder.Get());
  114. auto sample = samples.GetSamples(0);
  115. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::HISTOGRAM);
  116. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  117. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  118. auto histogram = sample.GetPoints(0).GetHistogram();
  119. UNIT_ASSERT_VALUES_EQUAL(histogram.BoundsSize(), 4);
  120. TVector<double> expectedBounds = {1, 2, 4, std::numeric_limits<double>::max()};
  121. TVector<ui64> expectedValues = {1, 1, 1, 0};
  122. for (auto i = 0; i < 4; ++i) {
  123. UNIT_ASSERT_VALUES_EQUAL(histogram.GetBounds(i), expectedBounds[i]);
  124. UNIT_ASSERT_VALUES_EQUAL(histogram.GetValues(i), expectedValues[i]);
  125. }
  126. }
  127. Y_UNIT_TEST(LogHistogramOverflow) {
  128. constexpr auto input = TStringBuf(R"([["something_ahhh", [1125899906842624, 2251799813685248] ]])");
  129. NProto::TMultiSamplesList samples;
  130. auto encoder = EncoderProtobuf(&samples);
  131. DecodeUnistat(input, encoder.Get());
  132. auto sample = samples.GetSamples(0);
  133. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::HISTOGRAM);
  134. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  135. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  136. auto histogram = sample.GetPoints(0).GetHistogram();
  137. UNIT_ASSERT_VALUES_EQUAL(histogram.BoundsSize(), HISTOGRAM_MAX_BUCKETS_COUNT);
  138. TVector<double> expectedBounds;
  139. for (ui64 i = 0; i < 50; ++i) {
  140. expectedBounds.push_back(std::pow(2, i));
  141. }
  142. expectedBounds.push_back(std::numeric_limits<double>::max());
  143. TVector<ui64> expectedValues;
  144. for (ui64 i = 0; i < 50; ++i) {
  145. expectedValues.push_back(0);
  146. }
  147. expectedValues.push_back(2);
  148. for (auto i = 0; i < 4; ++i) {
  149. UNIT_ASSERT_VALUES_EQUAL(histogram.GetBounds(i), expectedBounds[i]);
  150. UNIT_ASSERT_VALUES_EQUAL(histogram.GetValues(i), expectedValues[i]);
  151. }
  152. }
  153. Y_UNIT_TEST(LogHistogramSingle) {
  154. constexpr auto input = TStringBuf(R"([["something_ahhh", 4 ]])");
  155. NProto::TMultiSamplesList samples;
  156. auto encoder = EncoderProtobuf(&samples);
  157. DecodeUnistat(input, encoder.Get());
  158. auto sample = samples.GetSamples(0);
  159. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::HISTOGRAM);
  160. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  161. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  162. auto histogram = sample.GetPoints(0).GetHistogram();
  163. UNIT_ASSERT_VALUES_EQUAL(histogram.BoundsSize(), 4);
  164. TVector<double> expectedBounds = {1, 2, 4, std::numeric_limits<double>::max()};
  165. TVector<ui64> expectedValues = {0, 0, 1, 0};
  166. for (auto i = 0; i < 4; ++i) {
  167. UNIT_ASSERT_VALUES_EQUAL(histogram.GetBounds(i), expectedBounds[i]);
  168. UNIT_ASSERT_VALUES_EQUAL(histogram.GetValues(i), expectedValues[i]);
  169. }
  170. }
  171. Y_UNIT_TEST(LogHistogramInvalid) {
  172. NProto::TMultiSamplesList samples;
  173. auto encoder = EncoderProtobuf(&samples);
  174. {
  175. constexpr auto input = TStringBuf(R"([["something_ahhh", [1, 2, [3, 4]] ]])");
  176. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  177. }
  178. {
  179. constexpr auto input = TStringBuf(R"([["something_ahhh", [[3, 4], 1, 2] ]])");
  180. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  181. }
  182. {
  183. constexpr auto input = TStringBuf(R"([["something_hgram", 1, 2, 3, 4 ]])");
  184. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  185. }
  186. }
  187. Y_UNIT_TEST(AllowedMetricNames) {
  188. NProto::TMultiSamplesList samples;
  189. auto encoder = EncoderProtobuf(&samples);
  190. {
  191. constexpr auto input = TStringBuf(R"([["a/A-b/c_D/__G_dmmm", [[0, 1], [200, 2], [500, 3]] ]])");
  192. UNIT_ASSERT_NO_EXCEPTION(DecodeUnistat(input, encoder.Get()));
  193. }
  194. }
  195. Y_UNIT_TEST(DisallowedMetricNames) {
  196. NProto::TMultiSamplesList samples;
  197. auto encoder = EncoderProtobuf(&samples);
  198. {
  199. constexpr auto input = TStringBuf(R"([["someth!ng_ahhh", [[0, 1], [200, 2], [500, 3]] ]])");
  200. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  201. }
  202. {
  203. constexpr auto input = TStringBuf(R"([["foo_a", [[0, 1], [200, 2], [500, 3]] ]])");
  204. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  205. }
  206. {
  207. constexpr auto input = TStringBuf(R"([["foo_ahhh;tag=value", [[0, 1], [200, 2], [500, 3]] ]])");
  208. UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception);
  209. }
  210. }
  211. Y_UNIT_TEST(MultipleMetrics) {
  212. constexpr auto input = TStringBuf(R"([["something_axxx", 42], ["some-other_dhhh", 53]])");
  213. NProto::TMultiSamplesList samples;
  214. auto encoder = EncoderProtobuf(&samples);
  215. DecodeUnistat(input, encoder.Get());
  216. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 2);
  217. auto sample = samples.GetSamples(0);
  218. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::GAUGE);
  219. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  220. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  221. auto label = sample.GetLabels(0);
  222. auto point = sample.GetPoints(0);
  223. UNIT_ASSERT_VALUES_EQUAL(point.GetFloat64(), 42.);
  224. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor");
  225. UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_axxx");
  226. sample = samples.GetSamples(1);
  227. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::RATE);
  228. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  229. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  230. label = sample.GetLabels(0);
  231. point = sample.GetPoints(0);
  232. UNIT_ASSERT_VALUES_EQUAL(point.GetUint64(), 53);
  233. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor");
  234. UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "some-other_dhhh");
  235. }
  236. Y_UNIT_TEST(UnderscoreName) {
  237. constexpr auto input = TStringBuf(R"([["something_anything_dmmm", 42]])");
  238. NProto::TMultiSamplesList samples;
  239. auto encoder = EncoderProtobuf(&samples);
  240. DecodeUnistat(input, encoder.Get());
  241. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1);
  242. auto sample = samples.GetSamples(0);
  243. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::RATE);
  244. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  245. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  246. auto label = sample.GetLabels(0);
  247. auto point = sample.GetPoints(0);
  248. UNIT_ASSERT_VALUES_EQUAL(point.GetUint64(), 42);
  249. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor");
  250. UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_anything_dmmm");
  251. }
  252. Y_UNIT_TEST(MaxAggr) {
  253. constexpr auto input = TStringBuf(R"([["something_anything_max", 42]])");
  254. NProto::TMultiSamplesList samples;
  255. auto encoder = EncoderProtobuf(&samples);
  256. DecodeUnistat(input, encoder.Get());
  257. UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1);
  258. auto sample = samples.GetSamples(0);
  259. UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::GAUGE);
  260. UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1);
  261. UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1);
  262. auto label = sample.GetLabels(0);
  263. auto point = sample.GetPoints(0);
  264. UNIT_ASSERT_VALUES_EQUAL(point.GetFloat64(), 42.);
  265. UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor");
  266. UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_anything_max");
  267. }
  268. Y_UNIT_TEST(AllowClientAggregations) {
  269. constexpr auto input = TStringBuf(R"(
  270. [
  271. ["signal_dmmm", 1],
  272. ["signal_ammm", 1],
  273. ["signal_xmmm", 1],
  274. ["signal_emmm", 1],
  275. ["signal_tmmm", 1]
  276. ])");
  277. NProto::TMultiSamplesList samples;
  278. auto encoder = EncoderProtobuf(&samples);
  279. DecodeUnistat(input, encoder.Get());
  280. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 5);
  281. UNIT_ASSERT_EQUAL(samples.GetSamples(0).GetMetricType(), NProto::RATE);
  282. for (size_t i = 1; i < samples.SamplesSize(); ++i) {
  283. UNIT_ASSERT_EQUAL(samples.GetSamples(i).GetMetricType(), NProto::GAUGE);
  284. }
  285. }
  286. Y_UNIT_TEST(AllowClientHistAggregation) {
  287. constexpr auto input = TStringBuf(R"([["something_hhhh", 1]])");
  288. NProto::TMultiSamplesList samples;
  289. auto encoder = EncoderProtobuf(&samples);
  290. DecodeUnistat(input, encoder.Get());
  291. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 1);
  292. UNIT_ASSERT_EQUAL(samples.GetSamples(0).GetMetricType(), NProto::HISTOGRAM);
  293. UNIT_ASSERT_EQUAL(samples.GetSamples(0).PointsSize(), 1);
  294. }
  295. }