123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include <cstdio>
- #include <thread>
- #include <chrono>
- #include <jptree.hpp>
- #include "jmetrics.hpp"
- using namespace hpccMetrics;
- void processThread(int, unsigned, bool, const std::string&, unsigned, unsigned);
- std::shared_ptr<CounterMetric> pEventCountMetric;
- std::shared_ptr<GaugeMetric> pQueueSizeMetric;
- MetricsReporter *pReporter;
- const char *globalConfigYml = R"!!(config:
- metrics:
- name: cluster config
- prefix: global_prefix.
- sinks:
- - type: filesink
- name: default
- settings:
- filename: testout.txt
- clear: true
- period: 5
- )!!";
- const char *localConfigYml = R"!!(roxie:
- metrics:
- name: config_name
- prefix: component_prefix.
- sinks:
- - name: default
- metrics:
- - name: requests
- measurement_type: count
- - name: requests
- measurement_type: resetting_count
- - name: requests
- measurement_type: rate
- description: Number of request arriving per second
- - name: queuesize
- - name: requests_dynamic
- measurement_type: count
- )!!";
- const char *testConfigYml = R"!!(component:
- metrics:
- name: config_name
- prefix: component_prefix.
- sinks:
- sink:
- - type: filesink
- name: default
- settings:
- filename: testout.txt
- clear: true
- period: 5
- )!!";
- int main(int argc, char *argv[])
- {
- InitModuleObjects();
- //
- // Simulate retrieving the component and global config
- Owned<IPropertyTree> pSettings = createPTreeFromYAMLString(testConfigYml, ipt_none, ptr_ignoreWhiteSpace, nullptr);
- //
- // Retrieve the global and component metrics config
- Owned<IPropertyTree> pMetricsTree = pSettings->getPropTree("component/metrics");
- //
- // Allow override of output file for the file sink
- if (argc > 1)
- {
- auto pSinkTree = pMetricsTree->getPropTree("component/metrics/sinks[1]/settings");
- pSinkTree->setProp("@filename", argv[1]);
- }
- //
- // Get singleton
- MetricsReporter &myReporter = queryMetricsReporter();
- //
- // Init reporter with config
- myReporter.init(pMetricsTree);
- //
- // Now create the metrics and add them to the reporter
- pEventCountMetric = std::make_shared<CounterMetric>("requests", "The number of requests");
- myReporter.addMetric(pEventCountMetric);
- pQueueSizeMetric = std::make_shared<GaugeMetric>("queuesize", "request queue size");
- myReporter.addMetric(pQueueSizeMetric);
- myReporter.startCollecting();
- //
- // Starts some threads, each updating metrics
- std::thread first (processThread, 20, 2, true, "requests_dynamic", 4, 10);
- std::thread second (processThread, 15, 3, false, "", 0, 0);
- first.join();
- second.join();
- printf("Stopping the collection...");
- myReporter.stopCollecting();
- printf("Stopped. Test complete\n");
- }
- void processThread(int numLoops, unsigned delay, bool addDynamic, const std::string& name, unsigned addAfter, unsigned deleteAfter)
- {
- std::shared_ptr<GaugeMetric> pDynamicMetric;
- for (unsigned i=0; i<numLoops; ++i)
- {
- if (addDynamic && i == addAfter)
- {
- MetricsReporter &myReporter = queryMetricsReporter();
- pDynamicMetric = std::make_shared<GaugeMetric>(name.c_str(), "The dynamic number of requests");
- myReporter.addMetric(pDynamicMetric);
- }
- else if (addDynamic && i == (addAfter + deleteAfter))
- {
- pDynamicMetric.reset();
- }
- if (pDynamicMetric)
- {
- pDynamicMetric->add(1);
- }
- pEventCountMetric->inc(2u);
- pQueueSizeMetric->add(3);
- std::this_thread::sleep_for(std::chrono::seconds(delay));
- pQueueSizeMetric->add(-1);
- }
- }
|