Commit 3c2d7aa6 by amir

add graphite reporter

parent a0b15826
Pipeline #88 skipped in 0 seconds
...@@ -13,12 +13,12 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) ...@@ -13,12 +13,12 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# linked libs and their locations # linked libs and their locations
set ( PROJECT_LINK_LIBS -ljson -lhiredis -lcpprest -lcppmetrics -lboost_random -lboost_chrono set ( PROJECT_LINK_LIBS -ljson -lhiredis -lcpprest -lcppmetrics -lboost_random -lboost_timer -lboost_chrono
-lboost_system -lboost_thread -lboost_regex -lboost_filesystem -lpthread -lboost_system -lboost_thread -lboost_date_time -lboost_regex -lboost_filesystem -lpthread
-lboost_random -lboost_chrono -lboost_system -lboost_thread -lssl -lboost_random -lboost_chrono -lboost_system -lboost_thread -lssl
-lcrypto -lrabbitmq -llog4cpp -lglog ) -lcrypto -lrabbitmq -llog4cpp -lglog )
link_directories( ../3party/lib ) link_directories( ../3party/lib )
# -lboost_timer -lboost_chrono -lboost_system -lboost_filesystem -lboost_thread -lboost_date_time
# h files locations # h files locations
include_directories(src) include_directories(src)
include_directories(SYSTEM ../3party/rapidjson-0.11/include/rapidjson) include_directories(SYSTEM ../3party/rapidjson-0.11/include/rapidjson)
......
...@@ -244,7 +244,7 @@ cMicroservice_App& cMicroservice_App::build() { ...@@ -244,7 +244,7 @@ cMicroservice_App& cMicroservice_App::build() {
if(enableMetrics) if(enableMetrics)
{ {
metricsFactory_ = new MSIMetricsFactoryDropwisardImpl(); metricsFactory_ = new MSIMetricsFactoryDropwisardImpl(this->mc_AppName);
metricsFactory_->startReporting(); metricsFactory_->startReporting();
} }
mpc_Configuration->Reload(); mpc_Configuration->Reload();
......
...@@ -144,6 +144,7 @@ namespace nsMicroservice_Iface ...@@ -144,6 +144,7 @@ namespace nsMicroservice_Iface
* must be at the end of init , after the netrics are defined * must be at the end of init , after the netrics are defined
*/ */
virtual void startReporting() = 0; virtual void startReporting() = 0;
virtual void stopReporting() = 0;
virtual IMeter* createMeter(std::string& name) = 0; virtual IMeter* createMeter(std::string& name) = 0;
virtual ICounter* createCounter(std::string& name) = 0; virtual ICounter* createCounter(std::string& name) = 0;
virtual ITimer* createTimer(std::string& name) = 0; virtual ITimer* createTimer(std::string& name) = 0;
......
...@@ -14,18 +14,26 @@ ...@@ -14,18 +14,26 @@
#include "MSIMetricsFactoryDropwisardImpl.h" #include "MSIMetricsFactoryDropwisardImpl.h"
#include <boost/chrono.hpp> #include <boost/chrono.hpp>
#include <chrono> #include <chrono>
#include <thread>
#define NANOS_IN_MILLI 1000000 #define NANOS_IN_MILLI 1000000
#define GRAPHITE_DEFAULT_PORT 2003
thread_local boost::chrono::high_resolution_clock::time_point start_time;//cppmetrics::core::TimerContextPtr timer_ctx_; thread_local boost::chrono::high_resolution_clock::time_point start_time;//cppmetrics::core::TimerContextPtr timer_ctx_;
static int s_sig_num = 0;
MSIMetricsFactoryDropwisardImpl::MSIMetricsFactoryDropwisardImpl() : void run_thread(MSIMetricsFactoryDropwisardImpl* p_metrics) {
registry_(cppmetrics::core::MetricRegistry::DEFAULT_REGISTRY()) p_metrics->ReportToGraphite();
{ }
MSIMetricsFactoryDropwisardImpl::MSIMetricsFactoryDropwisardImpl(std::string& app_name) :
registry_(cppmetrics::core::MetricRegistry::DEFAULT_REGISTRY()), app_name_(app_name)//, sstpe_(1)
{
} }
MSIMetricsFactoryDropwisardImpl::MSIMetricsFactoryDropwisardImpl(const MSIMetricsFactoryDropwisardImpl& orig) { MSIMetricsFactoryDropwisardImpl::MSIMetricsFactoryDropwisardImpl(const MSIMetricsFactoryDropwisardImpl& orig) //: sstpe_(1)
{
} }
MSIMetricsFactoryDropwisardImpl::~MSIMetricsFactoryDropwisardImpl() { MSIMetricsFactoryDropwisardImpl::~MSIMetricsFactoryDropwisardImpl() {
...@@ -47,7 +55,36 @@ IMetricsFactory::ITimer* MSIMetricsFactoryDropwisardImpl::createTimer(std::strin ...@@ -47,7 +55,36 @@ IMetricsFactory::ITimer* MSIMetricsFactoryDropwisardImpl::createTimer(std::strin
void MSIMetricsFactoryDropwisardImpl::startReporting() void MSIMetricsFactoryDropwisardImpl::startReporting()
{ {
char* graphite_hostport = getenv("graphite_hostport");
if(graphite_hostport)
{
char* interval_str = getenv("graphite_report_intrerval");
if(interval_str)
graphite_options_.interval_in_secs_ = atoi(interval_str);
else
graphite_options_.interval_in_secs_ = 5;// 5 sec interval
graphite_options_.prefix_ = app_name_;
char* colon = strchr(graphite_hostport,':');
if (colon)
{
graphite_options_.host_ = std::string(graphite_hostport).substr(0, int(colon - graphite_hostport));
graphite_options_.port_ = atoi(colon + 1);
}
else
{
graphite_options_.host_ = nsMicroservice_Constants::LOCALHOST;
graphite_options_.port_ = GRAPHITE_DEFAULT_PORT;
}
ConfigureAndStartGraphiteReporter();
}
}
void MSIMetricsFactoryDropwisardImpl::stopReporting() {
if(graphite_reporter_ && p_GraphiteReportThread_)
{
s_sig_num = 1;
p_GraphiteReportThread_->join();
}
} }
void MSIMetricsFactoryDropwisardImpl::ITimerDropwisardImpl::start() { void MSIMetricsFactoryDropwisardImpl::ITimerDropwisardImpl::start() {
...@@ -84,7 +121,7 @@ void MSIMetricsFactoryDropwisardImpl::GetMetrics(std::map<std::string, long>& me ...@@ -84,7 +121,7 @@ void MSIMetricsFactoryDropwisardImpl::GetMetrics(std::map<std::string, long>& me
for (auto timer : registry_->getTimers()) for (auto timer : registry_->getTimers())
{ {
register auto timer_snapshot_ptr = timer.second->getSnapshot(); register auto timer_snapshot_ptr = timer.second->getSnapshot();
str.assign("timer." + timer.first); str.assign("timer." + timer.first);
metrics_map[str + ".mean_rate(ms)"] = timer_snapshot_ptr->getMean() / NANOS_IN_MILLI; metrics_map[str + ".mean_rate(ms)"] = timer_snapshot_ptr->getMean() / NANOS_IN_MILLI;
metrics_map[str + ".min(ms)"] = timer_snapshot_ptr->getMin() / NANOS_IN_MILLI; metrics_map[str + ".min(ms)"] = timer_snapshot_ptr->getMin() / NANOS_IN_MILLI;
...@@ -93,6 +130,34 @@ void MSIMetricsFactoryDropwisardImpl::GetMetrics(std::map<std::string, long>& me ...@@ -93,6 +130,34 @@ void MSIMetricsFactoryDropwisardImpl::GetMetrics(std::map<std::string, long>& me
} }
}
void MSIMetricsFactoryDropwisardImpl::ConfigureAndStartGraphiteReporter() {
if (!graphite_reporter_)
{
const std::string& graphite_host(graphite_options_.host_);
boost::uint32_t graphite_port(graphite_options_.port_);
cppmetrics::graphite::GraphiteSenderPtr graphite_sender(
new cppmetrics::graphite::GraphiteSenderTCP(graphite_host, graphite_port));
graphite_reporter_.reset(new cppmetrics::graphite::GraphiteReporter(registry_, graphite_sender,
graphite_options_.prefix_));
p_GraphiteReportThread_ = new std::thread(run_thread,this);
}
else
{
printf("Graphite reporter already configured.\n");
}
}
void MSIMetricsFactoryDropwisardImpl::ReportToGraphite() {
if(graphite_reporter_)
{
while (s_sig_num == 0)
{
std::this_thread::sleep_for(std::chrono::seconds(graphite_options_.interval_in_secs_));
((cppmetrics::core::ScheduledReporter*)graphite_reporter_.get())->report();
}
}
} }
...@@ -16,12 +16,25 @@ ...@@ -16,12 +16,25 @@
#include "../Microservice_Iface.h" #include "../Microservice_Iface.h"
#include <cppmetrics/cppmetrics.h> #include <cppmetrics/cppmetrics.h>
#include <cppmetrics/graphite/graphite_reporter.h>
#include <thread>
using namespace nsMicroservice_Iface; using namespace nsMicroservice_Iface;
class MSIMetricsFactoryDropwisardImpl : public IMetricsFactory { class MSIMetricsFactoryDropwisardImpl : public IMetricsFactory {
public: public:
MSIMetricsFactoryDropwisardImpl();
class GraphiteReporterOptions
{
public:
std::string host_; ///< The graphite server.
boost::uint32_t port_; ///< The graphite port.
std::string prefix_; ///< The prefix to the graphite.
boost::uint32_t interval_in_secs_; ///< The reporting period in secs.
};
MSIMetricsFactoryDropwisardImpl(std::string& app_name);
MSIMetricsFactoryDropwisardImpl(const MSIMetricsFactoryDropwisardImpl& orig); MSIMetricsFactoryDropwisardImpl(const MSIMetricsFactoryDropwisardImpl& orig);
virtual ~MSIMetricsFactoryDropwisardImpl(); virtual ~MSIMetricsFactoryDropwisardImpl();
...@@ -29,15 +42,24 @@ public: ...@@ -29,15 +42,24 @@ public:
IMetricsFactory::IMeter* createMeter(std::string& name) override; IMetricsFactory::IMeter* createMeter(std::string& name) override;
IMetricsFactory::ITimer* createTimer(std::string& name) override; IMetricsFactory::ITimer* createTimer(std::string& name) override;
void startReporting() override; void startReporting() override;
void stopReporting() override;
void GetMetrics(std::map<std::string, long>& metrics_map) override; void GetMetrics(std::map<std::string, long>& metrics_map) override;
cppmetrics::core::MetricRegistryPtr GetRegistry() const { cppmetrics::core::MetricRegistryPtr GetRegistry() const {
return registry_; return registry_;
} }
void ConfigureAndStartGraphiteReporter();
void ReportToGraphite();
private: private:
cppmetrics::core::MetricRegistryPtr registry_; cppmetrics::core::MetricRegistryPtr registry_;
boost::scoped_ptr<cppmetrics::graphite::GraphiteReporter> graphite_reporter_;
// cppmetrics::concurrent::SimpleScheduledThreadPoolExecutor sstpe_;
std::thread* p_GraphiteReportThread_;
GraphiteReporterOptions graphite_options_;
std::string app_name_;
public: public:
class IMeterDropwisardImpl : public IMeter class IMeterDropwisardImpl : public IMeter
...@@ -91,6 +113,7 @@ public: ...@@ -91,6 +113,7 @@ public:
}; };
}; };
#endif /* MSIMETRICSFACTORYDROPWISARDIMPL_H */ #endif /* MSIMETRICSFACTORYDROPWISARDIMPL_H */
......
...@@ -156,7 +156,7 @@ int main(int argc, char *argv[]) ...@@ -156,7 +156,7 @@ int main(int argc, char *argv[])
{ {
// testCache(); // testCache();
runNewMS(); runNewMS();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment