Skip to content

Commit 9cb8e4d

Browse files
Dave Pifkedpifke
Dave Pifke
authored andcommitted
Add Prometheus exporter
This adds a /metrics endpoint, to export stats about the size of the database table. (This is currently PDO-only.) Bug: T256039 Change-Id: I5f83c6fe648db6065a46ef51a110fe4278bfaeaf
1 parent f9726c3 commit 9cb8e4d

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,14 @@ Some Notes:
235235
- The waterfall display is still very much in alpha.
236236
- Feedback and pull requests are welcome :)
237237

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+
238244
# Releases / Changelog
245+
=======
239246

240247
See the [releases](https://github.com/perftools/xhgui/releases) for changelogs,
241248
and release information.

src/Xhgui/Controller/Metrics.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

src/Xhgui/Searcher/Interface.php

+8
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,12 @@ public function getAllWatches();
124124
* @return void
125125
*/
126126
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+
127135
}

src/Xhgui/Searcher/Mongo.php

+12
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,16 @@ private function _wrap($data)
329329
}
330330
return $results;
331331
}
332+
333+
/**
334+
* {@inheritdoc}
335+
*/
336+
public function stats()
337+
{
338+
return [
339+
'profiles' => 0,
340+
'latest' => 0,
341+
'bytes' => 0,
342+
];
343+
}
332344
}

src/Xhgui/Searcher/Pdo.php

+26
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,30 @@ public function getAllWatches()
250250
public function truncateWatches()
251251
{
252252
}
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+
}
253279
}

src/Xhgui/ServiceContainer.php

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ protected function _controllers()
154154
$this['importController'] = function ($c) {
155155
return new Xhgui_Controller_Import($c['app'], $c['saver'], $c['config']['upload.token']);
156156
};
157+
158+
$this['metricsController'] = function ($c) {
159+
return new Xhgui_Controller_Metrics($c['app'], $c['searcher']);
160+
};
157161
}
158162

159163
}

src/routes.php

+5
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@
124124
$app->get('/waterfall/data', function () use ($di) {
125125
$di['waterfallController']->query();
126126
})->name('waterfall.data');
127+
128+
// Metrics
129+
$app->get('/metrics', function () use ($di, $app) {
130+
$di['metricsController']->metrics();
131+
})->name('metrics');

0 commit comments

Comments
 (0)