legacy_protobuf_ut.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "legacy_protobuf.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <library/cpp/monlib/encode/legacy_protobuf/ut/test_cases.pb.h>
  4. #include <library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.pb.h>
  5. #include <library/cpp/monlib/encode/protobuf/protobuf.h>
  6. #include <library/cpp/monlib/encode/text/text.h>
  7. #include <library/cpp/monlib/metrics/labels.h>
  8. #include <util/generic/algorithm.h>
  9. #include <util/generic/hash_set.h>
  10. using namespace NMonitoring;
  11. TSimple MakeSimpleMessage() {
  12. TSimple msg;
  13. msg.SetFoo(1);
  14. msg.SetBar(2.);
  15. msg.SetBaz(42.);
  16. return msg;
  17. }
  18. IMetricEncoderPtr debugPrinter = EncoderText(&Cerr);
  19. namespace NMonitoring {
  20. inline bool operator<(const TLabel& lhs, const TLabel& rhs) {
  21. return lhs.Name() < rhs.Name() ||
  22. (lhs.Name() == rhs.Name() && lhs.Value() < rhs.Value());
  23. }
  24. }
  25. void SetLabelValue(NMonProto::TExtraLabelMetrics::TValue& val, TString s) {
  26. val.SetlabelValue(s);
  27. }
  28. void SetLabelValue(NMonProto::TExtraLabelMetrics::TValue& val, ui64 u) {
  29. val.SetlabelValueUint(u);
  30. }
  31. template <typename T, typename V>
  32. NMonProto::TExtraLabelMetrics MakeExtra(TString labelName, V labelValue, T value, bool isDeriv) {
  33. NMonProto::TExtraLabelMetrics metric;
  34. auto* val = metric.Addvalues();
  35. metric.SetlabelName(labelName);
  36. SetLabelValue(*val, labelValue);
  37. if (isDeriv) {
  38. val->SetlongValue(value);
  39. } else {
  40. val->SetdoubleValue(value);
  41. }
  42. return metric;
  43. }
  44. void AssertLabels(const TLabels& expected, const NProto::TMultiSample& actual) {
  45. UNIT_ASSERT_EQUAL(actual.LabelsSize(), expected.Size());
  46. TSet<TLabel> actualSet;
  47. TSet<TLabel> expectedSet;
  48. Transform(expected.begin(), expected.end(), std::inserter(expectedSet, expectedSet.end()), [] (auto&& l) {
  49. return TLabel{l.Name(), l.Value()};
  50. });
  51. const auto& l = actual.GetLabels();
  52. Transform(std::begin(l), std::end(l), std::inserter(actualSet, std::begin(actualSet)),
  53. [](auto&& elem) -> TLabel {
  54. return {elem.GetName(), elem.GetValue()};
  55. });
  56. TVector<TLabel> diff;
  57. SetSymmetricDifference(std::begin(expectedSet), std::end(expectedSet),
  58. std::begin(actualSet), std::end(actualSet), std::back_inserter(diff));
  59. if (diff.size() > 0) {
  60. for (auto&& l : diff) {
  61. Cerr << l << Endl;
  62. }
  63. UNIT_FAIL("Labels don't match");
  64. }
  65. }
  66. void AssertSimpleMessage(const NProto::TMultiSamplesList& samples, TString pathPrefix = "/") {
  67. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3);
  68. THashSet<TString> expectedValues{pathPrefix + "Foo", pathPrefix + "Bar", pathPrefix + "Baz"};
  69. for (const auto& s : samples.GetSamples()) {
  70. UNIT_ASSERT_EQUAL(s.LabelsSize(), 1);
  71. UNIT_ASSERT_EQUAL(s.PointsSize(), 1);
  72. const auto labelVal = s.GetLabels(0).GetValue();
  73. UNIT_ASSERT(expectedValues.contains(labelVal));
  74. if (labelVal == pathPrefix + "Foo") {
  75. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
  76. UNIT_ASSERT_DOUBLES_EQUAL(s.GetPoints(0).GetFloat64(), 1, 1e-6);
  77. } else if (labelVal == pathPrefix + "Bar") {
  78. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
  79. UNIT_ASSERT_DOUBLES_EQUAL(s.GetPoints(0).GetFloat64(), 2, 1e-6);
  80. } else if (labelVal == pathPrefix + "Baz") {
  81. UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE);
  82. UNIT_ASSERT_EQUAL(s.GetPoints(0).GetUint64(), 42);
  83. }
  84. }
  85. }
  86. Y_UNIT_TEST_SUITE(TLegacyProtoDecoderTest) {
  87. Y_UNIT_TEST(SimpleProto) {
  88. NProto::TMultiSamplesList samples;
  89. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  90. auto msg = MakeSimpleMessage();
  91. DecodeLegacyProto(msg, e.Get());
  92. AssertSimpleMessage(samples);
  93. };
  94. Y_UNIT_TEST(RepeatedProto) {
  95. NProto::TMultiSamplesList samples;
  96. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  97. auto simple = MakeSimpleMessage();
  98. TRepeated msg;
  99. msg.AddMessages()->CopyFrom(simple);
  100. DecodeLegacyProto(msg, e.Get());
  101. AssertSimpleMessage(samples);
  102. }
  103. Y_UNIT_TEST(RepeatedProtoWithPath) {
  104. NProto::TMultiSamplesList samples;
  105. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  106. auto simple = MakeSimpleMessage();
  107. TRepeatedWithPath msg;
  108. msg.AddNamespace()->CopyFrom(simple);
  109. DecodeLegacyProto(msg, e.Get());
  110. AssertSimpleMessage(samples, "/Namespace/");
  111. }
  112. Y_UNIT_TEST(DeepNesting) {
  113. NProto::TMultiSamplesList samples;
  114. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  115. auto simple = MakeSimpleMessage();
  116. TRepeatedWithPath internal;
  117. internal.AddNamespace()->CopyFrom(simple);
  118. TDeepNesting msg;
  119. msg.MutableNested()->CopyFrom(internal);
  120. DecodeLegacyProto(msg, e.Get());
  121. AssertSimpleMessage(samples, "/Namespace/");
  122. }
  123. Y_UNIT_TEST(Keys) {
  124. NProto::TMultiSamplesList samples;
  125. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  126. auto simple = MakeSimpleMessage();
  127. simple.SetLabel("my_label_value");
  128. TNestedWithKeys msg;
  129. msg.AddNamespace()->CopyFrom(simple);
  130. DecodeLegacyProto(msg, e.Get());
  131. auto i = 0;
  132. for (const auto& s : samples.GetSamples()) {
  133. UNIT_ASSERT_EQUAL(s.LabelsSize(), 4);
  134. bool foundLabel = false;
  135. bool foundFixed = false;
  136. bool foundNumbered = false;
  137. for (const auto& label : s.GetLabels()) {
  138. if (label.GetName() == "my_label") {
  139. foundLabel = true;
  140. UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "my_label_value");
  141. } else if (label.GetName() == "fixed_label") {
  142. foundFixed = true;
  143. UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "fixed_value");
  144. } else if (label.GetName() == "numbered") {
  145. foundNumbered = true;
  146. UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), ::ToString(i));
  147. }
  148. }
  149. UNIT_ASSERT(foundLabel);
  150. UNIT_ASSERT(foundFixed);
  151. UNIT_ASSERT(foundNumbered);
  152. }
  153. }
  154. Y_UNIT_TEST(NonStringKeys) {
  155. NProto::TMultiSamplesList samples;
  156. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  157. TNonStringKeys msg;
  158. msg.SetFoo(42);
  159. msg.SetEnum(ENUM);
  160. msg.SetInt(43);
  161. TRepeatedNonStringKeys msgs;
  162. msgs.AddNested()->CopyFrom(msg);
  163. DecodeLegacyProto(msgs, e.Get());
  164. for (const auto& s : samples.GetSamples()) {
  165. bool foundEnum = false;
  166. bool foundInt = false;
  167. for (const auto& label : s.GetLabels()) {
  168. if (label.GetName() == "enum") {
  169. foundEnum = true;
  170. UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "ENUM");
  171. } else if (label.GetName() == "int") {
  172. foundInt = true;
  173. UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "43");
  174. }
  175. }
  176. UNIT_ASSERT(foundEnum);
  177. UNIT_ASSERT(foundInt);
  178. UNIT_ASSERT_DOUBLES_EQUAL(s.GetPoints(0).GetFloat64(), 42, 1e-6);
  179. }
  180. }
  181. Y_UNIT_TEST(KeysFromNonLeafNodes) {
  182. NProto::TMultiSamplesList samples;
  183. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  184. auto simple = MakeSimpleMessage();
  185. simple.SetLabel("label_value");
  186. TRepeatedWithName nested;
  187. nested.SetName("my_name");
  188. nested.AddNested()->CopyFrom(simple);
  189. TKeysFromNonLeaf msg;
  190. msg.AddNested()->CopyFrom(nested);
  191. DecodeLegacyProto(msg, e.Get());
  192. AssertLabels({{"my_label", "label_value"}, {"path", "/Nested/Nested/Foo"}, {"name", "my_name"}}, samples.GetSamples(0));
  193. }
  194. Y_UNIT_TEST(SpacesAreGetReplaced) {
  195. NProto::TMultiSamplesList samples;
  196. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  197. auto simple = MakeSimpleMessage();
  198. simple.SetLabel("my label_value");
  199. TNestedWithKeys msg;
  200. msg.AddNamespace()->CopyFrom(simple);
  201. DecodeLegacyProto(msg, e.Get());
  202. for (const auto& s : samples.GetSamples()) {
  203. UNIT_ASSERT_EQUAL(s.LabelsSize(), 4);
  204. bool foundLabel = false;
  205. for (const auto& label : s.GetLabels()) {
  206. if (label.GetName() == "my_label") {
  207. foundLabel = true;
  208. UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "my_label_value");
  209. }
  210. }
  211. UNIT_ASSERT(foundLabel);
  212. }
  213. }
  214. Y_UNIT_TEST(ExtraLabels) {
  215. NProto::TMultiSamplesList samples;
  216. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  217. TExtraLabels msg;
  218. msg.MutableExtraAsIs()->CopyFrom(MakeExtra("label", "foo", 42, false));
  219. msg.MutableExtraDeriv()->CopyFrom(MakeExtra("deriv_label", "deriv_foo", 43, true));
  220. DecodeLegacyProto(msg, e.Get());
  221. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 2);
  222. {
  223. auto s = samples.GetSamples(0);
  224. AssertLabels({{"label", "foo"}, {"path", "/ExtraAsIs"}}, s);
  225. UNIT_ASSERT_EQUAL(s.PointsSize(), 1);
  226. auto point = s.GetPoints(0);
  227. UNIT_ASSERT_DOUBLES_EQUAL(point.GetFloat64(), 42, 1e-6);
  228. }
  229. {
  230. auto s = samples.GetSamples(1);
  231. AssertLabels({{"deriv_label", "deriv_foo"}, {"path", "/ExtraDeriv"}}, s);
  232. UNIT_ASSERT_EQUAL(s.PointsSize(), 1);
  233. auto point = s.GetPoints(0);
  234. UNIT_ASSERT_EQUAL(point.GetUint64(), 43);
  235. }
  236. }
  237. Y_UNIT_TEST(NestedExtraLabels) {
  238. NProto::TMultiSamplesList samples;
  239. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  240. TExtraLabels msg;
  241. auto extra = MakeExtra("label", "foo", 42, false);
  242. auto* val = extra.Mutablevalues(0);
  243. {
  244. auto child = MakeExtra("child1", "label1", 24, true);
  245. child.Mutablevalues(0)->Settype(NMonProto::EMetricType::RATE);
  246. val->Addchildren()->CopyFrom(child);
  247. }
  248. {
  249. auto child = MakeExtra("child2", 34, 23, false);
  250. val->Addchildren()->CopyFrom(child);
  251. }
  252. msg.MutableExtraAsIs()->CopyFrom(extra);
  253. DecodeLegacyProto(msg, e.Get());
  254. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3);
  255. {
  256. auto s = samples.GetSamples(0);
  257. AssertLabels({{"label", "foo"}, {"path", "/ExtraAsIs"}}, s);
  258. UNIT_ASSERT_EQUAL(s.PointsSize(), 1);
  259. auto point = s.GetPoints(0);
  260. UNIT_ASSERT_DOUBLES_EQUAL(point.GetFloat64(), 42, 1e-6);
  261. }
  262. {
  263. auto s = samples.GetSamples(1);
  264. AssertLabels({{"label", "foo"}, {"child1", "label1"}, {"path", "/ExtraAsIs"}}, s);
  265. UNIT_ASSERT_EQUAL(s.PointsSize(), 1);
  266. auto point = s.GetPoints(0);
  267. UNIT_ASSERT_EQUAL(point.GetUint64(), 24);
  268. }
  269. {
  270. auto s = samples.GetSamples(2);
  271. AssertLabels({{"label", "foo"}, {"child2", "34"}, {"path", "/ExtraAsIs"}}, s);
  272. UNIT_ASSERT_EQUAL(s.PointsSize(), 1);
  273. auto point = s.GetPoints(0);
  274. UNIT_ASSERT_EQUAL(point.GetFloat64(), 23);
  275. }
  276. }
  277. Y_UNIT_TEST(RobotLabels) {
  278. NProto::TMultiSamplesList samples;
  279. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  280. TNamedCounter responses;
  281. responses.SetName("responses");
  282. responses.SetCount(42);
  283. TCrawlerCounters::TStatusCounters statusCounters;
  284. statusCounters.AddZoraResponses()->CopyFrom(responses);
  285. TCrawlerCounters::TPolicyCounters policyCounters;
  286. policyCounters.SetSubComponent("mySubComponent");
  287. policyCounters.SetName("myComponentName");
  288. policyCounters.SetZone("myComponentZone");
  289. policyCounters.MutableStatusCounters()->CopyFrom(statusCounters);
  290. TCrawlerCounters counters;
  291. counters.SetComponent("myComponent");
  292. counters.AddPoliciesCounters()->CopyFrom(policyCounters);
  293. DecodeLegacyProto(counters, e.Get());
  294. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 1);
  295. auto s = samples.GetSamples(0);
  296. AssertLabels({
  297. {"SubComponent", "mySubComponent"}, {"Policy", "myComponentName"}, {"Zone", "myComponentZone"},
  298. {"ZoraResponse", "responses"}, {"path", "/PoliciesCounters/StatusCounters/ZoraResponses/Count"}}, s);
  299. }
  300. Y_UNIT_TEST(ZoraLabels) {
  301. NProto::TMultiSamplesList samples;
  302. IMetricEncoderPtr e = EncoderProtobuf(&samples);
  303. TTimeLogHist hist;
  304. hist.AddBuckets(42);
  305. hist.AddBuckets(0);
  306. TKiwiCounters counters;
  307. counters.MutableTimes()->CopyFrom(hist);
  308. DecodeLegacyProto(counters, e.Get());
  309. UNIT_ASSERT_EQUAL(samples.SamplesSize(), 2);
  310. auto s = samples.GetSamples(0);
  311. AssertLabels({{"slot", "0"}, {"path", "/Times/Buckets"}}, s);
  312. UNIT_ASSERT_EQUAL(s.PointsSize(), 1);
  313. UNIT_ASSERT_EQUAL(s.GetPoints(0).GetUint64(), 42);
  314. s = samples.GetSamples(1);
  315. AssertLabels({{"slot", "1"}, {"path", "/Times/Buckets"}}, s);
  316. UNIT_ASSERT_EQUAL(s.GetPoints(0).GetUint64(), 0);
  317. }
  318. }