File tree 7 files changed +102
-0
lines changed
7 files changed +102
-0
lines changed Original file line number Diff line number Diff line change @@ -235,7 +235,14 @@ Some Notes:
235
235
- The waterfall display is still very much in alpha.
236
236
- Feedback and pull requests are welcome :)
237
237
238
+ # Monitoring
239
+
240
+ [ Prometheus] ( https://prometheus.io ) metrics suitable for monitoring service
241
+ health are exposed on ` /metrics ` . (This currently only works if using PDO for
242
+ storage.)
243
+
238
244
# Releases / Changelog
245
+ =======
239
246
240
247
See the [ releases] ( https://github.com/perftools/xhgui/releases ) for changelogs,
241
248
and release information.
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Slim \Slim ;
4
+
5
+ class Xhgui_Controller_Metrics extends Xhgui_Controller
6
+ {
7
+ /**
8
+ * @var Xhgui_Searcher_Interface
9
+ */
10
+ protected $ searcher ;
11
+
12
+ public function __construct (Slim $ app , Xhgui_Searcher_Interface $ searcher )
13
+ {
14
+ parent ::__construct ($ app );
15
+ $ this ->searcher = $ searcher ;
16
+ }
17
+
18
+ public function metrics ()
19
+ {
20
+ $ request = $ this ->app ->request ();
21
+ $ response = $ this ->app ->response ();
22
+
23
+ $ stats = $ this ->searcher ->stats ();
24
+
25
+ $ body = "# HELP xhgui_profiles_total Number of profiles collected. \n" ;
26
+ $ body .= "# TYPE xhgui_profiles_total gauge \n" ;
27
+ $ body .= sprintf ("xhgui_profiles_total %0.1F \n\n" , $ stats ['profiles ' ]);
28
+
29
+ $ body .= "# HELP xhgui_profile_bytes_total Size of profiles collected. \n" ;
30
+ $ body .= "# TYPE xhgui_profile_bytes_total gauge \n" ;
31
+ $ body .= sprintf ("xhgui_profile_bytes_total %0.1F \n\n" , $ stats ['bytes ' ]);
32
+
33
+ $ body .= "# HELP xhgui_latest_profile_seconds UNIX timestamp of most recent profile. \n" ;
34
+ $ body .= "# TYPE xhgui_latest_profile_seconds gauge \n" ;
35
+ $ body .= sprintf ("xhgui_latest_profile_seconds %0.1F \n" , $ stats ['latest ' ]);
36
+
37
+ $ response ->body ($ body );
38
+ $ response ['Content-Type ' ] = 'text/plain; version=0.0.4 ' ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change @@ -124,4 +124,12 @@ public function getAllWatches();
124
124
* @return void
125
125
*/
126
126
public function truncateWatches ();
127
+
128
+ /**
129
+ * Return statistics about the size of all profiling data.
130
+ *
131
+ * @return array Array of stats.
132
+ */
133
+ public function stats ();
134
+
127
135
}
Original file line number Diff line number Diff line change @@ -329,4 +329,16 @@ private function _wrap($data)
329
329
}
330
330
return $ results ;
331
331
}
332
+
333
+ /**
334
+ * {@inheritdoc}
335
+ */
336
+ public function stats ()
337
+ {
338
+ return [
339
+ 'profiles ' => 0 ,
340
+ 'latest ' => 0 ,
341
+ 'bytes ' => 0 ,
342
+ ];
343
+ }
332
344
}
Original file line number Diff line number Diff line change @@ -250,4 +250,30 @@ public function getAllWatches()
250
250
public function truncateWatches ()
251
251
{
252
252
}
253
+
254
+ /**
255
+ * {@inheritdoc}
256
+ */
257
+ public function stats ()
258
+ {
259
+ $ stmt = $ this ->pdo ->query ("
260
+ SELECT
261
+ COUNT(*) AS profiles,
262
+ MAX(request_ts) AS latest,
263
+ SUM(LENGTH(profile)) AS bytes
264
+ FROM {$ this ->table }
265
+ " , PDO ::FETCH_ASSOC );
266
+
267
+ $ row = $ stmt ->fetch (PDO ::FETCH_ASSOC );
268
+
269
+ if (false === $ row ) {
270
+ $ row = array (
271
+ 'profiles ' => 0 ,
272
+ 'latest ' => 0 ,
273
+ 'bytes ' => 0 ,
274
+ );
275
+ }
276
+
277
+ return $ row ;
278
+ }
253
279
}
Original file line number Diff line number Diff line change @@ -154,6 +154,10 @@ protected function _controllers()
154
154
$ this ['importController ' ] = function ($ c ) {
155
155
return new Xhgui_Controller_Import ($ c ['app ' ], $ c ['saver ' ], $ c ['config ' ]['upload.token ' ]);
156
156
};
157
+
158
+ $ this ['metricsController ' ] = function ($ c ) {
159
+ return new Xhgui_Controller_Metrics ($ c ['app ' ], $ c ['searcher ' ]);
160
+ };
157
161
}
158
162
159
163
}
Original file line number Diff line number Diff line change 124
124
$ app ->get ('/waterfall/data ' , function () use ($ di ) {
125
125
$ di ['waterfallController ' ]->query ();
126
126
})->name ('waterfall.data ' );
127
+
128
+ // Metrics
129
+ $ app ->get ('/metrics ' , function () use ($ di , $ app ) {
130
+ $ di ['metricsController ' ]->metrics ();
131
+ })->name ('metrics ' );
You can’t perform that action at this time.
0 commit comments