a symfony client for phprom, a prometheus metrics datastore for php apps
see a fully functional example here
- phprom server
- grpc extension
pecl install grpc
- or use the docker image
this is essentially a symfony bundle wrapper for the phprom client
composer require chaseisabelle/phprom-bundle
- install with composer
composer require chaseisabelle/phprom-bundle
- enable in
app/AppKernel.php
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( ... new ChaseIsabelle\PHPromBundle\ChaseIsabellePHPromBundle(), ); ...
-
create
config/packages/phprom.yaml
phprom: address: 127.0.0.1:3333 # optional, defaults to 127.0.0.1:3333 api: grpc # optional, defaults to grpc (use "rest" for rest api) namespace: my_cool_app # required, the prefix for all your metrics routes: # optional, if empty or omitted then all routes will be recorded - my_cool_route # route can be plain string - only routes matching these strings will be recorded - /^.+$/ # route can be a regex - only routes matching this regex will be recorded
-
open
config/routes.yaml
and addmetrics: resource: '@ChaseIsabellePHPromBundle/Resources/config/routes.xml'
or you can customize
metrics: path: /custom/url/path controller: ChaseIsabelle\PHPromBundle\Controller\MetricsController::metrics
src/Controller/DefaultController.php
<?php
namespace App\Controller;
use ChaseIsabelle\PHPromBundle\Service\PHPromService;
use Exception;
use PHProm\Timer;
use Symfony\Component\HttpFoundation\Response;
/**
* @package App\Controller
*/
class DefaultController
{
/**
* @param PHPromService $phpromService
* @return Response
* @throws Exception
*/
public function index(PHPromService $phpromService)
{
$counter = $phpromService->counter()
->setName('custom_counter')
->setDescription('my custom counter')
->setLabels(['foo']); //<< optional
$counter->record(
rand(1, 10),
['foo' => 'bar'] //<< optional
);
$histogram = $phpromService->histogram()
->setName('custom_histogram')
->setDescription('my custom histogram')
->setLabels(['foo']) //<< optional
->setBuckets(range(1, 10)); //<< optional
$histogram->record(
rand(1, 100) / 10,
['foo' => 'bar'] //<< optional
);
$timer = new Timer($histogram);
$timer->start();
// do something
$timer->stop()
->record(['foo' => 'bar'])
->reset();
$summary = $phpromService->summary()
->setName('custom_summary')
->setDescription('my custom summary')
->setLabels(['foo']) //<< optional
->setObjectives(range(1, 5)) //<< optional
->setAgeBuckets(5) //<< optional
->setMaxAge(10) //<< optional
->setBufCap(5); //<< optional
$summary->record(
rand(1, 100) / 10,
['foo' => 'bar'] //<< optional
);
$gauge = $phpromService->gauge()
->setName('custom_gauge')
->setDescription('my custom gauge')
->setLabels(['foo']); //<< optional
$gauge->record(
rand(1, 10),
['foo' => 'bar'] //<< optional
);
return new Response($phpromService->instance()->get(), 200, [
'Content-Type' => 'text/plain'
]);
}
}