@@ -6,6 +6,8 @@ class Metrics {
6
6
7
7
const STATSD_API_METHOD_PREFIX = 'api-method ' ;
8
8
9
+ const STATSD_TOTAL_SERVER_NAME = 'total-servers ' ;
10
+
9
11
static protected $ metric = false ;
10
12
11
13
static protected $ timings = [];
@@ -95,37 +97,50 @@ static protected function getConnections() {
95
97
96
98
static public function sendMetrics () {
97
99
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
+ }
102
109
}
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
+ );
120
136
}
121
137
}
122
- foreach ($ metricValues as $ value ) {
123
- $ statsd ->timing ($ prefix , $ value );
124
- }
125
138
}
126
- self ::$ timings = [];
127
- $ statsd ->endBatch ();
139
+ foreach ($ metricValues as $ value ) {
140
+ $ statsd ->timing ($ serverPrefix . '. ' . $ prefix , $ value );
141
+ }
128
142
}
143
+ $ statsd ->endBatch ();
129
144
}
130
145
131
146
static protected function getRedisKeyPrefix ($ redisKey ) {
0 commit comments