metrictest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <cstdio>
  2. #include <thread>
  3. #include <chrono>
  4. #include <jptree.hpp>
  5. #include "jmetrics.hpp"
  6. using namespace hpccMetrics;
  7. void processThread(int, unsigned, bool, const std::string&, unsigned, unsigned);
  8. std::shared_ptr<CounterMetric> pEventCountMetric;
  9. std::shared_ptr<GaugeMetric> pQueueSizeMetric;
  10. MetricsReporter *pReporter;
  11. const char *globalConfigYml = R"!!(config:
  12. metrics:
  13. name: cluster config
  14. prefix: global_prefix.
  15. sinks:
  16. - type: filesink
  17. name: default
  18. settings:
  19. filename: testout.txt
  20. clear: true
  21. period: 5
  22. )!!";
  23. const char *localConfigYml = R"!!(roxie:
  24. metrics:
  25. name: config_name
  26. prefix: component_prefix.
  27. sinks:
  28. - name: default
  29. metrics:
  30. - name: requests
  31. measurement_type: count
  32. - name: requests
  33. measurement_type: resetting_count
  34. - name: requests
  35. measurement_type: rate
  36. description: Number of request arriving per second
  37. - name: queuesize
  38. - name: requests_dynamic
  39. measurement_type: count
  40. )!!";
  41. const char *testConfigYml = R"!!(component:
  42. metrics:
  43. name: config_name
  44. prefix: component_prefix.
  45. sinks:
  46. sink:
  47. - type: filesink
  48. name: default
  49. settings:
  50. filename: testout.txt
  51. clear: true
  52. period: 5
  53. )!!";
  54. int main(int argc, char *argv[])
  55. {
  56. InitModuleObjects();
  57. //
  58. // Simulate retrieving the component and global config
  59. Owned<IPropertyTree> pSettings = createPTreeFromYAMLString(testConfigYml, ipt_none, ptr_ignoreWhiteSpace, nullptr);
  60. //
  61. // Retrieve the global and component metrics config
  62. Owned<IPropertyTree> pMetricsTree = pSettings->getPropTree("component/metrics");
  63. //
  64. // Allow override of output file for the file sink
  65. if (argc > 1)
  66. {
  67. auto pSinkTree = pMetricsTree->getPropTree("component/metrics/sinks[1]/settings");
  68. pSinkTree->setProp("@filename", argv[1]);
  69. }
  70. //
  71. // Get singleton
  72. MetricsReporter &myReporter = queryMetricsReporter();
  73. //
  74. // Init reporter with config
  75. myReporter.init(pMetricsTree);
  76. //
  77. // Now create the metrics and add them to the reporter
  78. pEventCountMetric = std::make_shared<CounterMetric>("requests", "The number of requests");
  79. myReporter.addMetric(pEventCountMetric);
  80. pQueueSizeMetric = std::make_shared<GaugeMetric>("queuesize", "request queue size");
  81. myReporter.addMetric(pQueueSizeMetric);
  82. myReporter.startCollecting();
  83. //
  84. // Starts some threads, each updating metrics
  85. std::thread first (processThread, 20, 2, true, "requests_dynamic", 4, 10);
  86. std::thread second (processThread, 15, 3, false, "", 0, 0);
  87. first.join();
  88. second.join();
  89. printf("Stopping the collection...");
  90. myReporter.stopCollecting();
  91. printf("Stopped. Test complete\n");
  92. }
  93. void processThread(int numLoops, unsigned delay, bool addDynamic, const std::string& name, unsigned addAfter, unsigned deleteAfter)
  94. {
  95. std::shared_ptr<GaugeMetric> pDynamicMetric;
  96. for (unsigned i=0; i<numLoops; ++i)
  97. {
  98. if (addDynamic && i == addAfter)
  99. {
  100. MetricsReporter &myReporter = queryMetricsReporter();
  101. pDynamicMetric = std::make_shared<GaugeMetric>(name.c_str(), "The dynamic number of requests");
  102. myReporter.addMetric(pDynamicMetric);
  103. }
  104. else if (addDynamic && i == (addAfter + deleteAfter))
  105. {
  106. pDynamicMetric.reset();
  107. }
  108. if (pDynamicMetric)
  109. {
  110. pDynamicMetric->add(1);
  111. }
  112. pEventCountMetric->inc(2u);
  113. pQueueSizeMetric->add(3);
  114. std::this_thread::sleep_for(std::chrono::seconds(delay));
  115. pQueueSizeMetric->add(-1);
  116. }
  117. }