@@ -39,6 +39,9 @@ Monitoring::Monitoring()
3939 mProcessMonitor = std::make_unique<ProcessMonitor>();
4040 mDerivedHandler = std::make_unique<DerivedMetrics>();
4141 mBuffering = false ;
42+ mProcessMonitoringInterval = 0 ;
43+ mAutoPushInterval = 0 ;
44+ mMonitorRunning = false ;
4245}
4346
4447void Monitoring::enableBuffering (const unsigned int size)
@@ -59,13 +62,16 @@ void Monitoring::flushBuffer() {
5962}
6063
6164void Monitoring::enableProcessMonitoring (const unsigned int interval) {
62- mMonitorRunning = true ;
63- mMonitorThread = std::thread (&Monitoring::processMonitorLoop, this , interval);
64- #ifdef _OS_LINUX
65+ mProcessMonitoringInterval = interval;
66+ if (!mMonitorRunning ) {
67+ mMonitorRunning = true ;
68+ mMonitorThread = std::thread (&Monitoring::pushLoop, this );
69+ }
70+ #ifdef _OS_LINUX
6571 MonLogger::Get () << " Process Monitor : Automatic updates enabled" << MonLogger::End ();
66- #else
72+ #else
6773 MonLogger::Get () << " !! Process Monitor : Limited metrics available" << MonLogger::End ();
68- #endif
74+ #endif
6975}
7076
7177void Monitoring::startTimer (std::string name) {
@@ -115,21 +121,39 @@ Monitoring::~Monitoring()
115121 }
116122}
117123
118- void Monitoring::processMonitorLoop ( int interval )
124+ void Monitoring::pushLoop ( )
119125{
120- // loopCount - no need to wait full sleep time to terminame the thread
121- int loopCount = 0 ;
126+ unsigned int loopCount = 0 ;
127+ std::this_thread::sleep_for ( std::chrono::milliseconds ( 100 )) ;
122128 while (mMonitorRunning ) {
123- std::this_thread::sleep_for (std::chrono::milliseconds (interval*10 ));
124- if ((++loopCount % 100 ) != 0 ) continue ;
125- send (mProcessMonitor ->getCpuAndContexts ());
126- #ifdef _OS_LINUX
127- send (mProcessMonitor ->getMemoryUsage ());
128- #endif
129- loopCount = 0 ;
129+ if (mProcessMonitoringInterval != 0 && (loopCount % (mProcessMonitoringInterval *10 )) == 0 ) {
130+ send (mProcessMonitor ->getCpuAndContexts ());
131+ #ifdef _OS_LINUX
132+ send (mProcessMonitor ->getMemoryUsage ());
133+ #endif
134+ }
135+
136+ if (mAutoPushInterval != 0 && (loopCount % (mAutoPushInterval *10 )) == 0 ) {
137+ std::vector<Metric> metrics;
138+ for (auto & metric : mPushStore ) {
139+ metrics.push_back (metric);
140+ }
141+ send (std::move (metrics));
142+ }
143+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
144+ (loopCount >= 600 ) ? loopCount = 0 : loopCount++;
130145 }
131146}
132147
148+ Metric& Monitoring::getAutoPushMetric (std::string name)
149+ {
150+ if (mAutoPushInterval == 0 ) {
151+ MonLogger::Get () << " [WARN] AutoPush is not enabled" << MonLogger::End ();
152+ }
153+ mPushStore .emplace_back (boost::variant< int , std::string, double , uint64_t > {}, name);
154+ return mPushStore .back ();
155+ }
156+
133157void Monitoring::sendGrouped (std::string measurement, std::vector<Metric>&& metrics)
134158{
135159 for (auto & b: mBackends ) {
@@ -153,6 +177,15 @@ void Monitoring::debug(Metric&& metric)
153177 }
154178}
155179
180+ void Monitoring::enableAutoPush (unsigned int interval)
181+ {
182+ if (!mMonitorRunning ) {
183+ mMonitorRunning = true ;
184+ mMonitorThread = std::thread (&Monitoring::pushLoop, this );
185+ }
186+ mAutoPushInterval = interval;
187+ }
188+
156189void Monitoring::pushToBackends (Metric&& metric)
157190{
158191 if (mBuffering ) {
0 commit comments