10000 add duplicate metrics with server name mecanism · etherscan-io/Ethplorer@c311b4b · GitHub
[go: up one dir, main page]

Skip to content

Commit c311b4b

Browse files
committed
add duplicate metrics with server name mecanism
1 parent d4c1676 commit c311b4b

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

service/lib/metric.php

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Metrics {
66

77
const STATSD_API_METHOD_PREFIX = 'api-method';
88

9+
const STATSD_TOTAL_SERVER_NAME = 'total-servers';
10+
911
static protected $metric = false;
1012

1113
static protected $timings = [];
@@ -95,37 +97,50 @@ static protected function getConnections() {
9597

9698
static public function sendMetrics() {
9799
if (!(empty(self::$metric))) {
98-
$timings = self::$timings;
99-
$statsd = self::getConnections();
100-
if (empty($statsd)) {
101-
return;
100+
self::sendAllTimingWithPrefix();
101+
if (
102+
isset(self::$statsdOptions['duplicate_metrics_with_hostname']) &&
103+
self::$statsdOptions['duplicate_metrics_with_hostname']
104+
) {
105+
$hostname = explode('.', gethostname())[0];
106+
if (!empty($hostname)) {
107+
self::sendAllTimingWithPrefix($hostname);
108+
}
102109
}
103-
$statsd->startBatch();
104-
foreach ($timings as $prefix => $metricValues) {
105-
if (count($metricValues)) {
106-
foreach (self::$meticPrefixWithCountPerRequest as $prefixForCounting) {
107-
if (
108-
strpos($prefix, $prefixForCounting) === 0 &&
109-
strpos($prefix, '.times.') !== -1
110-
) {
111-
$statsd->timing(
112-
str_replace(
113-
'.times.',
114-
'.cnt-per-req.' . self::$apiMethodName . '.',
115-
$prefix
116-
),
117-
count($metricValues)
118-
);
119-
}
110+
self::$timings = [];
111+
}
112+
}
113+
114+
static public function sendAllTimingWithPrefix($serverPrefix = self::STATSD_TOTAL_SERVER_NAME) {
115+
$timings = self::$timings;
116+
$statsd = self::getConnections();
117+
if (empty($statsd)) {
118+
return;
119+
}
120+
$statsd->startBatch();
121+
foreach ($timings as $prefix => $metricValues) {
122+
if (count($metricValues)) {
123+
foreach (self::$meticPrefixWithCountPerRequest as $prefixForCounting) {
124+
if (
125+
strpos($prefix, $prefixForCounting) === 0 &&
126+
strpos($prefix, '.times.') !== -1
127+
) {
128+
$statsd->timing(
129+
str_replace(
130+
'.times.',
131+
'.cnt-per-req.' . self::$apiMethodName . '.',
132+
$serverPrefix . '.' . $prefix
133+
),
134+
count($metricValues)
135+
);
120136
}
121137
}
122-
foreach ($metricValues as $value) {
123-
$statsd->timing($prefix, $value);
124-
}
125138
}
126-
self::$timings = [];
127-
$statsd->endBatch();
139+
foreach ($metricValues as $value) {
140+
$statsd->timing($serverPrefix . '.' . $prefix, $value);
141+
}
128142
}
143+
$statsd->endBatch();
129144
}
130145

131146
static protected function getRedisKeyPrefix($redisKey) {

0 commit comments

Comments
 (0)
0