Skip to content

Commit

Permalink
Add Prometheus exporter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dave Pifke authored and dpifke committed Aug 20, 2020
1 parent f9726c3 commit 9cb8e4d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ Some Notes:
- The waterfall display is still very much in alpha.
- Feedback and pull requests are welcome :)

# Monitoring

[Prometheus](https://prometheus.io) metrics suitable for monitoring service
health are exposed on `/metrics`. (This currently only works if using PDO for
storage.)

# Releases / Changelog
=======

See the [releases](https://github.com/perftools/xhgui/releases) for changelogs,
and release information.
Expand Down
40 changes: 40 additions & 0 deletions src/Xhgui/Controller/Metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Slim\Slim;

class Xhgui_Controller_Metrics extends Xhgui_Controller
{
/**
* @var Xhgui_Searcher_Interface
*/
protected $searcher;

public function __construct(Slim $app, Xhgui_Searcher_Interface $searcher)
{
parent::__construct($app);
$this->searcher = $searcher;
}

public function metrics()
{
$request = $this->app->request();
$response = $this->app->response();

$stats = $this->searcher->stats();

$body = "# HELP xhgui_profiles_total Number of profiles collected.\n";
$body .= "# TYPE xhgui_profiles_total gauge\n";
$body .= sprintf("xhgui_profiles_total %0.1F\n\n", $stats['profiles']);

$body .= "# HELP xhgui_profile_bytes_total Size of profiles collected.\n";
$body .= "# TYPE xhgui_profile_bytes_total gauge\n";
$body .= sprintf("xhgui_profile_bytes_total %0.1F\n\n", $stats['bytes']);

$body .= "# HELP xhgui_latest_profile_seconds UNIX timestamp of most recent profile.\n";
$body .= "# TYPE xhgui_latest_profile_seconds gauge\n";
$body .= sprintf("xhgui_latest_profile_seconds %0.1F\n", $stats['latest']);

$response->body($body);
$response['Content-Type'] = 'text/plain; version=0.0.4';
}
}
8 changes: 8 additions & 0 deletions src/Xhgui/Searcher/Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,12 @@ public function getAllWatches();
* @return void
*/
public function truncateWatches();

/**
* Return statistics about the size of all profiling data.
*
* @return array Array of stats.
*/
public function stats();

}
12 changes: 12 additions & 0 deletions src/Xhgui/Searcher/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,16 @@ private function _wrap($data)
}
return $results;
}

/**
* {@inheritdoc}
*/
public function stats()
{
return [
'profiles' => 0,
'latest' => 0,
'bytes' => 0,
];
}
}
26 changes: 26 additions & 0 deletions src/Xhgui/Searcher/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,30 @@ public function getAllWatches()
public function truncateWatches()
{
}

/**
* {@inheritdoc}
*/
public function stats()
{
$stmt = $this->pdo->query("
SELECT
COUNT(*) AS profiles,
MAX(request_ts) AS latest,
SUM(LENGTH(profile)) AS bytes
FROM {$this->table}
", PDO::FETCH_ASSOC);

$row = $stmt->fetch(PDO::FETCH_ASSOC);

if (false === $row) {
$row = array(
'profiles' => 0,
'latest' => 0,
'bytes' => 0,
);
}

return $row;
}
}
4 changes: 4 additions & 0 deletions src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ protected function _controllers()
$this['importController'] = function ($c) {
return new Xhgui_Controller_Import($c['app'], $c['saver'], $c['config']['upload.token']);
};

$this['metricsController'] = function ($c) {
return new Xhgui_Controller_Metrics($c['app'], $c['searcher']);
};
}

}
5 changes: 5 additions & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@
$app->get('/waterfall/data', function () use ($di) {
$di['waterfallController']->query();
})->name('waterfall.data');

// Metrics
$app->get('/metrics', function () use ($di, $app) {
$di['metricsController']->metrics();
})->name('metrics');

0 comments on commit 9cb8e4d

Please sign in to comment.