Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.cpp
/
Microservice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
3c2d7aa6
authored
May 30, 2016
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add graphite reporter
parent
a0b15826
Pipeline
#88
skipped in 0 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
104 additions
and
15 deletions
CMakeLists.txt
src/Microservice_App.cpp
src/Microservice_Iface.h
src/impl/MSIMetricsFactoryDropwisardImpl.cpp
src/impl/MSIMetricsFactoryDropwisardImpl.h
test/Microservice_Test.cpp
CMakeLists.txt
View file @
3c2d7aa6
...
...
@@ -13,12 +13,12 @@ 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 -lcppmetrics -lboost_random -lboost_chrono
-lboost_system -lboost_thread -lboost_regex -lboost_filesystem -lpthread
set
(
PROJECT_LINK_LIBS -ljson -lhiredis -lcpprest -lcppmetrics -lboost_random -lboost_
timer -lboost_
chrono
-lboost_system -lboost_thread -lboost_
date_time -lboost_
regex -lboost_filesystem -lpthread
-lboost_random -lboost_chrono -lboost_system -lboost_thread -lssl
-lcrypto -lrabbitmq -llog4cpp -lglog
)
link_directories
(
../3party/lib
)
# -lboost_timer -lboost_chrono -lboost_system -lboost_filesystem -lboost_thread -lboost_date_time
# h files locations
include_directories
(
src
)
include_directories
(
SYSTEM ../3party/rapidjson-0.11/include/rapidjson
)
...
...
src/Microservice_App.cpp
View file @
3c2d7aa6
...
...
@@ -244,7 +244,7 @@ cMicroservice_App& cMicroservice_App::build() {
if
(
enableMetrics
)
{
metricsFactory_
=
new
MSIMetricsFactoryDropwisardImpl
();
metricsFactory_
=
new
MSIMetricsFactoryDropwisardImpl
(
this
->
mc_AppName
);
metricsFactory_
->
startReporting
();
}
mpc_Configuration
->
Reload
();
...
...
src/Microservice_Iface.h
View file @
3c2d7aa6
...
...
@@ -144,6 +144,7 @@ namespace nsMicroservice_Iface
* must be at the end of init , after the netrics are defined
*/
virtual
void
startReporting
()
=
0
;
virtual
void
stopReporting
()
=
0
;
virtual
IMeter
*
createMeter
(
std
::
string
&
name
)
=
0
;
virtual
ICounter
*
createCounter
(
std
::
string
&
name
)
=
0
;
virtual
ITimer
*
createTimer
(
std
::
string
&
name
)
=
0
;
...
...
src/impl/MSIMetricsFactoryDropwisardImpl.cpp
View file @
3c2d7aa6
...
...
@@ -14,18 +14,26 @@
#include "MSIMetricsFactoryDropwisardImpl.h"
#include <boost/chrono.hpp>
#include <chrono>
#include <thread>
#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_;
static
int
s_sig_num
=
0
;
MSIMetricsFactoryDropwisardImpl
::
MSIMetricsFactoryDropwisardImpl
()
:
registry_
(
cppmetrics
::
core
::
MetricRegistry
::
DEFAULT_REGISTRY
())
{
void
run_thread
(
MSIMetricsFactoryDropwisardImpl
*
p_metrics
)
{
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
()
{
...
...
@@ -47,7 +55,36 @@ IMetricsFactory::ITimer* MSIMetricsFactoryDropwisardImpl::createTimer(std::strin
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
()
{
...
...
@@ -84,7 +121,7 @@ void MSIMetricsFactoryDropwisardImpl::GetMetrics(std::map<std::string, long>& me
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
);
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
;
...
...
@@ -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
();
}
}
}
src/impl/MSIMetricsFactoryDropwisardImpl.h
View file @
3c2d7aa6
...
...
@@ -16,12 +16,25 @@
#include "../Microservice_Iface.h"
#include <cppmetrics/cppmetrics.h>
#include <cppmetrics/graphite/graphite_reporter.h>
#include <thread>
using
namespace
nsMicroservice_Iface
;
class
MSIMetricsFactoryDropwisardImpl
:
public
IMetricsFactory
{
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
);
virtual
~
MSIMetricsFactoryDropwisardImpl
();
...
...
@@ -29,15 +42,24 @@ public:
IMetricsFactory
::
IMeter
*
createMeter
(
std
::
string
&
name
)
override
;
IMetricsFactory
::
ITimer
*
createTimer
(
std
::
string
&
name
)
override
;
void
startReporting
()
override
;
void
stopReporting
()
override
;
void
GetMetrics
(
std
::
map
<
std
::
string
,
long
>&
metrics_map
)
override
;
cppmetrics
::
core
::
MetricRegistryPtr
GetRegistry
()
const
{
return
registry_
;
}
void
ConfigureAndStartGraphiteReporter
();
void
ReportToGraphite
();
private
:
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
:
class
IMeterDropwisardImpl
:
public
IMeter
...
...
@@ -91,6 +113,7 @@ public:
};
};
#endif
/* MSIMETRICSFACTORYDROPWISARDIMPL_H */
...
...
test/Microservice_Test.cpp
View file @
3c2d7aa6
...
...
@@ -156,7 +156,7 @@ int main(int argc, char *argv[])
{
// testCache();
runNewMS
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment