histogram_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <algorithm>
  5. #include <cstdint>
  6. #include <initializer_list>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "common.h"
  11. #include "opentelemetry/common/macros.h"
  12. #include "opentelemetry/context/context.h"
  13. #include "opentelemetry/metrics/meter.h"
  14. #include "opentelemetry/metrics/sync_instruments.h"
  15. #include "opentelemetry/nostd/function_ref.h"
  16. #include "opentelemetry/nostd/shared_ptr.h"
  17. #include "opentelemetry/nostd/variant.h"
  18. #include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
  19. #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
  20. #include "opentelemetry/sdk/metrics/data/metric_data.h"
  21. #include "opentelemetry/sdk/metrics/data/point_data.h"
  22. #include "opentelemetry/sdk/metrics/export/metric_producer.h"
  23. #include "opentelemetry/sdk/metrics/instruments.h"
  24. #include "opentelemetry/sdk/metrics/meter_provider.h"
  25. #include "opentelemetry/sdk/metrics/metric_reader.h"
  26. #include "opentelemetry/sdk/metrics/push_metric_exporter.h"
  27. #include "opentelemetry/sdk/metrics/view/instrument_selector.h"
  28. #include "opentelemetry/sdk/metrics/view/meter_selector.h"
  29. #include "opentelemetry/sdk/metrics/view/view.h"
  30. using namespace opentelemetry;
  31. using namespace opentelemetry::sdk::instrumentationscope;
  32. using namespace opentelemetry::sdk::metrics;
  33. TEST(Histogram, Double)
  34. {
  35. MeterProvider mp;
  36. auto m = mp.GetMeter("meter1", "version1", "schema1");
  37. std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
  38. std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
  39. mp.AddMetricReader(reader);
  40. auto h = m->CreateDoubleHistogram("histogram1", "histogram1_description", "histogram1_unit");
  41. h->Record(5, {});
  42. h->Record(10, {});
  43. h->Record(15, {});
  44. h->Record(20, {});
  45. h->Record(25, {});
  46. h->Record(30, {});
  47. h->Record(35, {});
  48. h->Record(40, {});
  49. h->Record(45, {});
  50. h->Record(50, {});
  51. h->Record(1e6, {});
  52. std::vector<HistogramPointData> actuals;
  53. reader->Collect([&](ResourceMetrics &rm) {
  54. for (const ScopeMetrics &smd : rm.scope_metric_data_)
  55. {
  56. for (const MetricData &md : smd.metric_data_)
  57. {
  58. for (const PointDataAttributes &dp : md.point_data_attr_)
  59. {
  60. actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data));
  61. }
  62. }
  63. }
  64. return true;
  65. });
  66. ASSERT_EQ(1, actuals.size());
  67. const auto &actual = actuals.at(0);
  68. ASSERT_EQ(1000275.0, opentelemetry::nostd::get<double>(actual.sum_));
  69. ASSERT_EQ(11, actual.count_);
  70. ASSERT_EQ(5.0, opentelemetry::nostd::get<double>(actual.min_));
  71. ASSERT_EQ(1e6, opentelemetry::nostd::get<double>(actual.max_));
  72. ASSERT_EQ(std::vector<double>(
  73. {0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}),
  74. actual.boundaries_);
  75. ASSERT_EQ(std::vector<uint64_t>({0, 1, 1, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}),
  76. actual.counts_);
  77. }
  78. #if OPENTELEMETRY_HAVE_WORKING_REGEX
  79. // FIXME - View Preficate search is only supported through regex
  80. TEST(Histogram, DoubleCustomBuckets)
  81. {
  82. MeterProvider mp;
  83. auto m = mp.GetMeter("meter1", "version1", "schema1");
  84. std::string instrument_unit = "ms";
  85. std::string instrument_name = "historgram1";
  86. std::string instrument_desc = "histogram metrics";
  87. std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
  88. std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
  89. mp.AddMetricReader(reader);
  90. std::shared_ptr<HistogramAggregationConfig> config(new HistogramAggregationConfig());
  91. config->boundaries_ = {10, 20, 30, 40};
  92. std::unique_ptr<View> view{
  93. new View("view1", "view1_description", instrument_unit, AggregationType::kHistogram, config)};
  94. std::unique_ptr<InstrumentSelector> instrument_selector{
  95. new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)};
  96. std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("meter1", "version1", "schema1")};
  97. mp.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));
  98. auto h = m->CreateDoubleHistogram(instrument_name, instrument_desc, instrument_unit);
  99. h->Record(5, {});
  100. h->Record(10, {});
  101. h->Record(15, {});
  102. h->Record(20, {});
  103. h->Record(25, {});
  104. h->Record(30, {});
  105. h->Record(35, {});
  106. h->Record(40, {});
  107. h->Record(45, {});
  108. h->Record(50, {});
  109. std::vector<HistogramPointData> actuals;
  110. reader->Collect([&](ResourceMetrics &rm) {
  111. for (const ScopeMetrics &smd : rm.scope_metric_data_)
  112. {
  113. for (const MetricData &md : smd.metric_data_)
  114. {
  115. for (const PointDataAttributes &dp : md.point_data_attr_)
  116. {
  117. actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data));
  118. }
  119. }
  120. }
  121. return true;
  122. });
  123. ASSERT_EQ(1, actuals.size());
  124. const auto &actual = actuals.at(0);
  125. ASSERT_EQ(275.0, opentelemetry::nostd::get<double>(actual.sum_));
  126. ASSERT_EQ(10, actual.count_);
  127. ASSERT_EQ(5.0, opentelemetry::nostd::get<double>(actual.min_));
  128. ASSERT_EQ(50.0, opentelemetry::nostd::get<double>(actual.max_));
  129. ASSERT_EQ(std::vector<double>({10, 20, 30, 40}), actual.boundaries_);
  130. ASSERT_EQ(std::vector<uint64_t>({2, 2, 2, 2, 2}), actual.counts_);
  131. }
  132. TEST(Histogram, DoubleEmptyBuckets)
  133. {
  134. MeterProvider mp;
  135. auto m = mp.GetMeter("meter1", "version1", "schema1");
  136. std::string instrument_unit = "ms";
  137. std::string instrument_name = "historgram1";
  138. std::string instrument_desc = "histogram metrics";
  139. std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
  140. std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
  141. mp.AddMetricReader(reader);
  142. std::shared_ptr<HistogramAggregationConfig> config(new HistogramAggregationConfig());
  143. config->boundaries_ = {};
  144. std::unique_ptr<View> view{
  145. new View("view1", "view1_description", instrument_unit, AggregationType::kHistogram, config)};
  146. std::unique_ptr<InstrumentSelector> instrument_selector{
  147. new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)};
  148. std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("meter1", "version1", "schema1")};
  149. mp.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));
  150. auto h = m->CreateDoubleHistogram(instrument_name, instrument_desc, instrument_unit);
  151. h->Record(-5, {});
  152. h->Record(10, {});
  153. h->Record(15, {});
  154. h->Record(20, {});
  155. h->Record(25, {});
  156. h->Record(30, {});
  157. h->Record(35, {});
  158. h->Record(40, {});
  159. h->Record(45, {});
  160. h->Record(50, {});
  161. std::vector<HistogramPointData> actuals;
  162. reader->Collect([&](ResourceMetrics &rm) {
  163. for (const ScopeMetrics &smd : rm.scope_metric_data_)
  164. {
  165. for (const MetricData &md : smd.metric_data_)
  166. {
  167. for (const PointDataAttributes &dp : md.point_data_attr_)
  168. {
  169. actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data));
  170. }
  171. }
  172. }
  173. return true;
  174. });
  175. ASSERT_EQ(1, actuals.size());
  176. const auto &actual = actuals.at(0);
  177. ASSERT_EQ(270.0, opentelemetry::nostd::get<double>(actual.sum_));
  178. ASSERT_EQ(9, actual.count_);
  179. ASSERT_EQ(10.0, opentelemetry::nostd::get<double>(actual.min_));
  180. ASSERT_EQ(50.0, opentelemetry::nostd::get<double>(actual.max_));
  181. ASSERT_EQ(std::vector<double>({}), actual.boundaries_);
  182. ASSERT_EQ(std::vector<uint64_t>({9}), actual.counts_);
  183. }
  184. #endif
  185. TEST(Histogram, UInt64)
  186. {
  187. MeterProvider mp;
  188. auto m = mp.GetMeter("meter1", "version1", "schema1");
  189. std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
  190. std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
  191. mp.AddMetricReader(reader);
  192. auto h = m->CreateUInt64Histogram("histogram1", "histogram1_description", "histogram1_unit");
  193. h->Record(5, {});
  194. h->Record(10, {});
  195. h->Record(15, {});
  196. h->Record(20, {});
  197. h->Record(25, {});
  198. h->Record(30, {});
  199. h->Record(35, {});
  200. h->Record(40, {});
  201. h->Record(45, {});
  202. h->Record(50, {});
  203. h->Record(1000000, {});
  204. std::vector<HistogramPointData> actuals;
  205. reader->Collect([&](ResourceMetrics &rm) {
  206. for (const ScopeMetrics &smd : rm.scope_metric_data_)
  207. {
  208. for (const MetricData &md : smd.metric_data_)
  209. {
  210. for (const PointDataAttributes &dp : md.point_data_attr_)
  211. {
  212. actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data));
  213. }
  214. }
  215. }
  216. return true;
  217. });
  218. ASSERT_EQ(1, actuals.size());
  219. const auto &actual = actuals.at(0);
  220. ASSERT_EQ(1000275, opentelemetry::nostd::get<int64_t>(actual.sum_));
  221. ASSERT_EQ(11, actual.count_);
  222. ASSERT_EQ(5, opentelemetry::nostd::get<int64_t>(actual.min_));
  223. ASSERT_EQ(1000000, opentelemetry::nostd::get<int64_t>(actual.max_));
  224. ASSERT_EQ(std::vector<double>(
  225. {0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}),
  226. actual.boundaries_);
  227. ASSERT_EQ(std::vector<uint64_t>({0, 1, 1, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}),
  228. actual.counts_);
  229. }
  230. #if OPENTELEMETRY_HAVE_WORKING_REGEX
  231. // FIXME - View Preficate search is only supported through regex
  232. TEST(Histogram, UInt64CustomBuckets)
  233. {
  234. MeterProvider mp;
  235. auto m = mp.GetMeter("meter1", "version1", "schema1");
  236. std::string instrument_name = "historgram1";
  237. std::string instrument_desc = "histogram metrics";
  238. std::string instrument_unit = "ms";
  239. std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
  240. std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
  241. mp.AddMetricReader(reader);
  242. std::shared_ptr<HistogramAggregationConfig> config(new HistogramAggregationConfig());
  243. config->boundaries_ = {10, 20, 30, 40};
  244. std::unique_ptr<View> view{
  245. new View("view1", "view1_description", "ms", AggregationType::kHistogram, config)};
  246. std::unique_ptr<InstrumentSelector> instrument_selector{
  247. new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)};
  248. std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("meter1", "version1", "schema1")};
  249. mp.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));
  250. auto h = m->CreateUInt64Histogram(instrument_name, instrument_desc, instrument_unit);
  251. h->Record(5, {});
  252. h->Record(10, {});
  253. h->Record(15, {});
  254. h->Record(20, {});
  255. h->Record(25, {});
  256. h->Record(30, {});
  257. h->Record(35, {});
  258. h->Record(40, {});
  259. h->Record(45, {});
  260. h->Record(50, {});
  261. std::vector<HistogramPointData> actuals;
  262. reader->Collect([&](ResourceMetrics &rm) {
  263. for (const ScopeMetrics &smd : rm.scope_metric_data_)
  264. {
  265. for (const MetricData &md : smd.metric_data_)
  266. {
  267. for (const PointDataAttributes &dp : md.point_data_attr_)
  268. {
  269. actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data));
  270. }
  271. }
  272. }
  273. return true;
  274. });
  275. ASSERT_EQ(1, actuals.size());
  276. const auto &actual = actuals.at(0);
  277. ASSERT_EQ(275, opentelemetry::nostd::get<int64_t>(actual.sum_));
  278. ASSERT_EQ(10, actual.count_);
  279. ASSERT_EQ(5, opentelemetry::nostd::get<int64_t>(actual.min_));
  280. ASSERT_EQ(50, opentelemetry::nostd::get<int64_t>(actual.max_));
  281. ASSERT_EQ(std::vector<double>({10, 20, 30, 40}), actual.boundaries_);
  282. ASSERT_EQ(std::vector<uint64_t>({2, 2, 2, 2, 2}), actual.counts_);
  283. }
  284. TEST(Histogram, UInt64EmptyBuckets)
  285. {
  286. MeterProvider mp;
  287. auto m = mp.GetMeter("meter1", "version1", "schema1");
  288. std::string instrument_name = "historgram1";
  289. std::string instrument_desc = "histogram metrics";
  290. std::string instrument_unit = "ms";
  291. std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
  292. std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
  293. mp.AddMetricReader(reader);
  294. std::shared_ptr<HistogramAggregationConfig> config(new HistogramAggregationConfig());
  295. config->boundaries_ = {};
  296. std::unique_ptr<View> view{
  297. new View("view1", "view1_description", "ms", AggregationType::kHistogram, config)};
  298. std::unique_ptr<InstrumentSelector> instrument_selector{
  299. new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)};
  300. std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("meter1", "version1", "schema1")};
  301. mp.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));
  302. auto h = m->CreateUInt64Histogram(instrument_name, instrument_desc, instrument_unit);
  303. h->Record(5, {});
  304. h->Record(10, {});
  305. h->Record(15, {});
  306. h->Record(20, {});
  307. h->Record(25, {});
  308. h->Record(30, {});
  309. h->Record(35, {});
  310. h->Record(40, {});
  311. h->Record(45, {});
  312. h->Record(50, {});
  313. std::vector<HistogramPointData> actuals;
  314. reader->Collect([&](ResourceMetrics &rm) {
  315. for (const ScopeMetrics &smd : rm.scope_metric_data_)
  316. {
  317. for (const MetricData &md : smd.metric_data_)
  318. {
  319. for (const PointDataAttributes &dp : md.point_data_attr_)
  320. {
  321. actuals.push_back(opentelemetry::nostd::get<HistogramPointData>(dp.point_data));
  322. }
  323. }
  324. }
  325. return true;
  326. });
  327. ASSERT_EQ(1, actuals.size());
  328. const auto &actual = actuals.at(0);
  329. ASSERT_EQ(275, opentelemetry::nostd::get<int64_t>(actual.sum_));
  330. ASSERT_EQ(10, actual.count_);
  331. ASSERT_EQ(5, opentelemetry::nostd::get<int64_t>(actual.min_));
  332. ASSERT_EQ(50, opentelemetry::nostd::get<int64_t>(actual.max_));
  333. ASSERT_EQ(std::vector<double>({}), actual.boundaries_);
  334. ASSERT_EQ(std::vector<uint64_t>({10}), actual.counts_);
  335. }
  336. #endif