Commit a0b15826 by amir

add dropwizard metrics

parent e6902158
Pipeline #83 skipped in 0 seconds
......@@ -13,15 +13,16 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# linked libs and their locations
set ( PROJECT_LINK_LIBS -ljson -lhiredis -lcpprest -lboost_random -lboost_chrono
set ( PROJECT_LINK_LIBS -ljson -lhiredis -lcpprest -lcppmetrics -lboost_random -lboost_chrono
-lboost_system -lboost_thread -lboost_regex -lboost_filesystem -lpthread
-lboost_random -lboost_chrono -lboost_system -lboost_thread -lssl
-lcrypto -lrabbitmq -llog4cpp)
-lcrypto -lrabbitmq -llog4cpp -lglog )
link_directories( ../3party/lib )
# h files locations
include_directories(src)
include_directories(SYSTEM ../3party/rapidjson-0.11/include/rapidjson)
include_directories(SYSTEM ../3party/cppmetrics-0.1.1-Linux/include)
include_directories(SYSTEM ../3party/mongoose)
include_directories(SYSTEM /usr/include/hiredis)
# recursive search files cpp files
......
......@@ -5,4 +5,5 @@
#
# Created on May 8, 2016, 9:59:18 AM
#
sudo apt-get install -y libhiredis0.10 libhiredis-dev libzmq3 libzmq3-dev liblog4cpp5 liblog4cpp5-dev
\ No newline at end of file
sudo apt-get install -y libhiredis0.10 libhiredis-dev libzmq3 libzmq3-dev liblog4cpp5 liblog4cpp5-dev \
libgoogle-glog-dev
\ No newline at end of file
......@@ -19,6 +19,8 @@
#include <condition_variable>
#include <signal.h>
#include "impl/MSIMetricsFactoryDropwisardImpl.h"
static bool exit_app = false;
// sync
......@@ -39,6 +41,7 @@ cMicroservice_App::cMicroservice_App(const char* appName) {
mpc_PubSubClient = nullptr;
mpc_ServiceDiscovery = nullptr;
enableMetrics = false;
metricsFactory_ = nullptr;
mc_AppName.assign(appName);
}
......@@ -239,6 +242,11 @@ cMicroservice_App& cMicroservice_App::build() {
mpc_Configuration->AddConfigurationProvider(mpc_ServiceDiscovery->getConfigurationProvider());
}
if(enableMetrics)
{
metricsFactory_ = new MSIMetricsFactoryDropwisardImpl();
metricsFactory_->startReporting();
}
mpc_Configuration->Reload();
/*
* post setting params for all handlers
......@@ -270,7 +278,7 @@ cMicroservice_App& cMicroservice_App::build() {
*/
for(auto& server : mc_ServerList)
{
server->build(this->mc_AppName,mc_HandlersMap,mpc_Logger,mpc_PubSubClient,enableMetrics);
server->build(this->mc_AppName,mc_HandlersMap,mpc_Logger,mpc_PubSubClient,metricsFactory_);
}
/*
......
......@@ -51,6 +51,7 @@ private:
IPubSub* mpc_PubSubClient;
IConfiguration* mpc_Configuration;
cMicroservice_MonitorHandler* mpc_MonitorHandler;
IMetricsFactory* metricsFactory_;
// servers
// cMicroservice_RestServer* mpc_RestServer;
// cMicroservice_RMQServer* mpc_RMQServer;
......@@ -85,7 +86,7 @@ public:
*/
cMicroservice_App& withMetrics()
{
// enableMetrics = true;
enableMetrics = true;
return *this;
}
/**
......@@ -141,6 +142,7 @@ public:
return mc_ClientMap[ms_name];
}
std::map<std::string, cMicroservice_Client*>& GetClientMap() { return mc_ClientMap; }
IMetricsFactory* GetMetricsFactory() const { return metricsFactory_; }
};
......
......@@ -147,8 +147,7 @@ namespace nsMicroservice_Iface
virtual IMeter* createMeter(std::string& name) = 0;
virtual ICounter* createCounter(std::string& name) = 0;
virtual ITimer* createTimer(std::string& name) = 0;
virtual void GetMetrics(std::map<std::string,long>& metrics_map) = 0;
};
......@@ -184,7 +183,7 @@ namespace nsMicroservice_Iface
std::map<std::string, cMicroservice_BaseHandler*>& msHandlersMap,
ILogger* pc_Logger,
IPubSub* pc_PubSub,
bool withMetrics) = 0;
IMetricsFactory* p_metrics_factory) = 0;
virtual void run() = 0;
virtual void stop() = 0;
virtual void registerService(IServiceDiscovery* pc_ServiceDiscovery, std::string& id) = 0;
......
......@@ -93,6 +93,22 @@ void cMicroservice_MonitorHandler::HandleStatistics(cMicroservice_RequestContext
doc.AddMember(client.first.c_str(),counters,rpj_Alloc);
}
}
/*
* metrics registry
*/
metrics_map.clear();
rapidjson::Value counters(rapidjson::kObjectType);
this->mpc_msApp->GetMetricsFactory()->GetMetrics(metrics_map);
if(!metrics_map.empty())
{
for(auto counter : metrics_map)
{
counters.AddMember(counter.first.c_str(),counter.second,rpj_Alloc);
}
doc.AddMember("handlers",counters,rpj_Alloc);
}
// for (auto p_handler : handlers_)
// {
// doc.AddMember(name,counters,rpj_Alloc);
......
......@@ -18,8 +18,10 @@
#include <sstream>
#include "impl/MSIMetricsFactoryDropwisardImpl.h"
cMicroservice_RestHandler::cMicroservice_RestHandler(std::string apiContextPath,cMicroservice_BaseHandler* pc_Handler):
mpc_Handler(pc_Handler),mpc_Logger(nullptr),mpc_PubSub(nullptr)
mpc_Handler(pc_Handler),mpc_Logger(nullptr),mpc_PubSub(nullptr),p_metrics_factory_(nullptr)
{
mpc_Buffer = new rapidjson::StringBuffer(0,nsMicroservice_Constants::MAX_JSON_BUFFER);
mpc_Writer = new JsonStringWriter(*mpc_Buffer);
......@@ -40,6 +42,7 @@ mpc_Handler(pc_Handler),mpc_Logger(nullptr),mpc_PubSub(nullptr)
*/
void cMicroservice_RestHandler::HandleRequest(mg_connection *conn,http_message *msg)
{
cppmetrics::core::TimerContextPtr timer;
/*
* get request context
*/
......@@ -52,9 +55,13 @@ void cMicroservice_RestHandler::HandleRequest(mg_connection *conn,http_message *
* now check the method
*/
cMicroservice_Enums::eMethod e_Method = GetMethod(msg);
if(p_metrics_factory_)
PreHandleMetrics(e_Method);
switch (e_Method)
{
case cMicroservice_Enums::eGet:
DoGet(mpc_RequestContext);
break;
case cMicroservice_Enums::ePost:
......@@ -71,7 +78,8 @@ void cMicroservice_RestHandler::HandleRequest(mg_connection *conn,http_message *
break;
}
if(p_metrics_factory_)
PostHandleMetrics(e_Method);
}
/**
......@@ -244,3 +252,57 @@ void cMicroservice_RestHandler::LogRequest(http_message* msg) {
str.append(", query string: ").append(msg->query_string.p,msg->query_string.len);
mpc_Logger->debug(str);
}
void cMicroservice_RestHandler::CreateMetrics() {
std::string str;
p_get_meter_ = p_metrics_factory_->createMeter(apiContextPath.substr(1).append(".get.requests.count"));
p_post_meter_ = p_metrics_factory_->createMeter(apiContextPath.substr(1).append(".post.requests.count"));
p_put_meter_ = p_metrics_factory_->createMeter(apiContextPath.substr(1).append(".put.requests.count"));
p_delete_meter_ = p_metrics_factory_->createMeter(apiContextPath.substr(1).append(".delete.requests.count"));
p_get_timer_ = p_metrics_factory_->createTimer(apiContextPath.substr(1).append(".get.requests.timer"));
p_post_timer_ = p_metrics_factory_->createTimer(apiContextPath.substr(1).append(".post.requests.timer"));
}
void cMicroservice_RestHandler::PreHandleMetrics(cMicroservice_Enums::eMethod e_Method) {
switch(e_Method)
{
case cMicroservice_Enums::eGet:
p_get_meter_->mark();
p_get_timer_->start();
break;
case cMicroservice_Enums::ePost:
p_post_meter_->mark();
p_post_timer_->start();
break;
case cMicroservice_Enums::ePut:
p_put_meter_->mark();
break;
case cMicroservice_Enums::eDelete:
p_delete_meter_->mark();
break;
default:
break;
}
}
void cMicroservice_RestHandler::PostHandleMetrics(cMicroservice_Enums::eMethod e_Method)
{
switch(e_Method)
{
case cMicroservice_Enums::eGet:
p_get_timer_->stop();
break;
case cMicroservice_Enums::ePost:
p_post_timer_->stop();
break;
case cMicroservice_Enums::ePut:
break;
case cMicroservice_Enums::eDelete:
break;
default:
break;
}
}
......@@ -32,7 +32,15 @@ private:
cMicroservice_RequestContext* mpc_RequestContext;
char mba_Buff[nsMicroservice_Constants::MAX_URI_LENGTH];
char mba_ErrorBuff[nsMicroservice_Constants::MAX_ERROR_BUFF_URI_LENGTH];
bool mb_EnableMetrics;
nsMicroservice_Iface::IMetricsFactory* p_metrics_factory_;
// metrics
nsMicroservice_Iface::IMetricsFactory::IMeter* p_get_meter_;
nsMicroservice_Iface::IMetricsFactory::IMeter* p_post_meter_;
nsMicroservice_Iface::IMetricsFactory::IMeter* p_put_meter_;
nsMicroservice_Iface::IMetricsFactory::IMeter* p_delete_meter_;
nsMicroservice_Iface::IMetricsFactory::ITimer* p_get_timer_;
nsMicroservice_Iface::IMetricsFactory::ITimer* p_post_timer_;
cMicroservice_Enums::eMethod GetMethod(http_message *msg);
......@@ -49,10 +57,17 @@ private:
void SetRequestContext(mg_connection *conn,http_message *msg);
void GetQueryParams(http_message *msg);
void LogRequest(http_message *msg);
void CreateMetrics();
void PreHandleMetrics(cMicroservice_Enums::eMethod e_Method);
void PostHandleMetrics(cMicroservice_Enums::eMethod e_Method);
public:
cMicroservice_RestHandler(std::string apiContextPath,cMicroservice_BaseHandler* pc_Handler);
void enableMetrics(bool b_EnableMetrics) { this->mb_EnableMetrics = b_EnableMetrics;}
void withMetrics(nsMicroservice_Iface::IMetricsFactory* p_metrics_factory) {
this->p_metrics_factory_ = p_metrics_factory;
CreateMetrics();
}
void withLogger(nsMicroservice_Iface::ILogger* pc_Logger) { this->mpc_Logger = pc_Logger; }
void withPubSub(nsMicroservice_Iface::IPubSub* pc_PubSub) { this->mpc_PubSub = pc_PubSub; }
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: MSIMetricsFactoryDropwisardImpl.cpp
* Author: amir
*
* Created on May 22, 2016, 11:24 AM
*/
#include "MSIMetricsFactoryDropwisardImpl.h"
#include <boost/chrono.hpp>
#include <chrono>
#define NANOS_IN_MILLI 1000000
thread_local boost::chrono::high_resolution_clock::time_point start_time;//cppmetrics::core::TimerContextPtr timer_ctx_;
MSIMetricsFactoryDropwisardImpl::MSIMetricsFactoryDropwisardImpl() :
registry_(cppmetrics::core::MetricRegistry::DEFAULT_REGISTRY())
{
}
MSIMetricsFactoryDropwisardImpl::MSIMetricsFactoryDropwisardImpl(const MSIMetricsFactoryDropwisardImpl& orig) {
}
MSIMetricsFactoryDropwisardImpl::~MSIMetricsFactoryDropwisardImpl() {
}
IMetricsFactory::ICounter* MSIMetricsFactoryDropwisardImpl::createCounter(std::string& name){
return new ICounterDropwisardImpl(registry_->counter(name));
}
IMetricsFactory::IMeter* MSIMetricsFactoryDropwisardImpl::createMeter(std::string& name)
{
return new IMeterDropwisardImpl(registry_->meter(name));
}
IMetricsFactory::ITimer* MSIMetricsFactoryDropwisardImpl::createTimer(std::string& name)
{
return new ITimerDropwisardImpl(registry_->timer(name));
}
void MSIMetricsFactoryDropwisardImpl::startReporting()
{
}
void MSIMetricsFactoryDropwisardImpl::ITimerDropwisardImpl::start() {
//timer_->clear();
start_time = boost::chrono::high_resolution_clock::now();// Clock::now();
}
void MSIMetricsFactoryDropwisardImpl::ITimerDropwisardImpl::stop() {
//auto timer_ctx = timer_->timerContextPtr();
boost::chrono::nanoseconds dur = boost::chrono::high_resolution_clock::now() - start_time;
timer_->update(dur);
}
void MSIMetricsFactoryDropwisardImpl::GetMetrics(std::map<std::string, long>& metrics_map) {
std::string str;
for (auto counter : registry_->getCounters())
{
metrics_map["counter." + counter.first] = counter.second->getCount();
}
for (auto meter : registry_->getMeters())
{
register auto meter_ptr = meter.second;
str.assign("meter." + meter.first);
metrics_map[str] = meter_ptr->getCount();
metrics_map[str + ".mean_rate"] = meter_ptr->getMeanRate();
metrics_map[str + ".1m_rate"] = meter_ptr->getOneMinuteRate();
metrics_map[str + ".5m_rate"] = meter_ptr->getFiveMinuteRate();
metrics_map[str + ".15m_rate"] = meter_ptr->getFifteenMinuteRate();
}
for (auto timer : registry_->getTimers())
{
register auto timer_snapshot_ptr = timer.second->getSnapshot();
str.assign("timer." + timer.first);
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 + ".max(ms)"] = timer_snapshot_ptr->getMax() / NANOS_IN_MILLI;
metrics_map[str + ".median(ms)"] = timer_snapshot_ptr->getMedian() / NANOS_IN_MILLI;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: MSIMetricsFactoryDropwisardImpl.h
* Author: amir
*
* Created on May 22, 2016, 11:24 AM
*/
#ifndef MSIMETRICSFACTORYDROPWISARDIMPL_H
#define MSIMETRICSFACTORYDROPWISARDIMPL_H
#include "../Microservice_Iface.h"
#include <cppmetrics/cppmetrics.h>
using namespace nsMicroservice_Iface;
class MSIMetricsFactoryDropwisardImpl : public IMetricsFactory {
public:
MSIMetricsFactoryDropwisardImpl();
MSIMetricsFactoryDropwisardImpl(const MSIMetricsFactoryDropwisardImpl& orig);
virtual ~MSIMetricsFactoryDropwisardImpl();
IMetricsFactory::ICounter* createCounter(std::string& name) override;
IMetricsFactory::IMeter* createMeter(std::string& name) override;
IMetricsFactory::ITimer* createTimer(std::string& name) override;
void startReporting() override;
void GetMetrics(std::map<std::string, long>& metrics_map) override;
cppmetrics::core::MetricRegistryPtr GetRegistry() const {
return registry_;
}
private:
cppmetrics::core::MetricRegistryPtr registry_;
public:
class IMeterDropwisardImpl : public IMeter
{
public:
IMeterDropwisardImpl(cppmetrics::core::MeterPtr p_meter) : meter_(p_meter){
}
long getCount() { return meter_->getCount(); }
void mark() { meter_->mark(1); }
void mark(long n) { meter_->mark(n);}
private:
cppmetrics::core::MeterPtr meter_;
};
class ICounterDropwisardImpl : public ICounter
{
public:
void dec(long n) { counter_->decrement(n); }
void dec() { counter_->decrement(1);}
long getCount() { return counter_->getCount();}
void inc(long n) { counter_->increment(n); }
void inc() { counter_->increment(1); }
ICounterDropwisardImpl(cppmetrics::core::CounterPtr p_counter) : counter_(p_counter){
counter_->setCount(0);
}
private:
cppmetrics::core::CounterPtr counter_;
};
class ITimerDropwisardImpl : public ITimer
{
public:
ITimerDropwisardImpl(cppmetrics::core::TimerPtr timer_) :
timer_(timer_) {
}
template<class Function>
void measure_func(Function fn){
auto timer_ctx = timer_->timerContextPtr();
fn();
}
virtual void start();
virtual void stop();
private:
cppmetrics::core::TimerPtr timer_;
};
};
#endif /* MSIMETRICSFACTORYDROPWISARDIMPL_H */
......@@ -44,3 +44,7 @@ IMetricsFactory::ITimer* MSIMetricsFactoryStdImpl::createTimer(std::string& name
void MSIMetricsFactoryStdImpl::startReporting() {
}
void MSIMetricsFactoryStdImpl::GetMetrics(std::map<std::string, long>& metrics_map) {
}
......@@ -17,6 +17,7 @@
#include <atomic>
#include <mutex>
using namespace nsMicroservice_Iface;
class MSIMetricsFactoryStdImpl : public IMetricsFactory {
......@@ -58,6 +59,7 @@ public:
IMetricsFactory::IMeter* createMeter(std::string& name) override;
IMetricsFactory::ITimer* createTimer(std::string& name) override;
void startReporting() override;
void GetMetrics(std::map<std::string, long>& metrics_map) override;
......
......@@ -84,6 +84,8 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){
p_cmd_data->p_retstat->SetError(NULL_REST_RESPONSE_OBJECT);
return;
}
try
{
if(BuildUrl(p_cmd_data->p_cmd_params,url))
{
p_cmd_data->p_response->Reset();
......@@ -142,6 +144,13 @@ void MSICommandClientHttpImpl::HandleCommand(HandleCommandData* p_cmd_data){
{
p_cmd_data->p_retstat->SetError(FAILED_BUILD_URL);
}
}
catch (web::http::http_exception exp)
{
p_cmd_data->p_retstat->SetError(exp.what());
LOG_ERROR(exp.what());
p_cmd_data->p_command_counters->failed++;
}
}
MSRetStat MSICommandClientHttpImpl::Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
......
......@@ -63,7 +63,7 @@ cMicroservice_IRestServerMongooseImpl::~cMicroservice_IRestServerMongooseImpl()
bool cMicroservice_IRestServerMongooseImpl::build(std::string& appName, std::map<std::string, cMicroservice_BaseHandler*>& msHandlersMap,
nsMicroservice_Iface::ILogger* pc_Logger,
nsMicroservice_Iface::IPubSub* pc_PubSub,
bool withMetrics) {
nsMicroservice_Iface::IMetricsFactory* p_metrics_factory) {
bool result = false;
if (this->mpc_Param)
......@@ -103,7 +103,7 @@ bool cMicroservice_IRestServerMongooseImpl::build(std::string& appName, std::map
cMicroservice_RestHandler* pc_RestHandler = new cMicroservice_RestHandler(prfxHandler.first,prfxHandler.second);
pc_RestHandler->withLogger(pc_Logger);
pc_RestHandler->withPubSub(pc_PubSub);
pc_RestHandler->enableMetrics(withMetrics);
pc_RestHandler->withMetrics(p_metrics_factory);
this->mc_HandlersMap[prfxHandler.first] = pc_RestHandler;
}
result = true;
......
......@@ -32,7 +32,7 @@ public:
bool build(std::string& appName, std::map<std::string,cMicroservice_BaseHandler*>& msHandlersMap,
nsMicroservice_Iface::ILogger* pc_Logger,
nsMicroservice_Iface::IPubSub* pc_PubSub,
bool withMetrics) override;
nsMicroservice_Iface::IMetricsFactory* p_metrics_factory) override;
void registerService(nsMicroservice_Iface::IServiceDiscovery* pc_ServiceDiscovery, std::string& id) override;
......
......@@ -62,7 +62,10 @@ public:
.WithParamsString("search")
.WithRequestParams("q=base");
MSRetStat retstat = p_client_->Read(&cmd_params,&rest_response);
if(retstat.IsSuccess())
this->WriteObjectToResponse(pc_reqCtx,rest_response);
else
this->SendErrorResp(pc_reqCtx,retstat.GetError());
//this->WriteObjectToResponse(pc_reqCtx,rpj_Doc);
}
......
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