Symfony 5/6/7/8 Prometheus Metrics Bundle
Symfony 5/6/7/8 Prometheus Metrics Bundle
| Master |
|---|
A Symfony bundle for the promphp/prometheus_client_php.
The bundle is currently maintained against these versions:
Compatibility policy:
Open a command console, enter your project directory and execute:
composer require artprima/prometheus-metrics-bundleOpen a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require artprima/prometheus-metrics-bundleThis command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:
/metrics/prometheus.
Custom Metrics Collector
========================
If you want to collect your own metrics, you should create a class that will implement one or several interfaces that are
the children of the `Artprima\PrometheusMetricsBundle\Metrics\MetricsCollectorInterface`.
```php
namespace = $namespace;
$this->collectionRegistry = $collectionRegistry;
}
private function incRequestsTotal(?string $method = null, ?string $route = null): void
{
$counter = $this->collectionRegistry->getOrRegisterCounter(
$this->namespace,
'http_requests_total',
'total request count',
['action']
);
$counter->inc(['all']);
if (null !== $method && null !== $route) {
$counter->inc([sprintf('%s-%s', $method, $route)]);
}
}
private function incResponsesTotal(?string $method = null, ?string $route = null): void
{
$counter = $this->collectionRegistry->getOrRegisterCounter(
$this->namespace,
'http_responses_total',
'total response count',
['action']
);
$counter->inc(['all']);
if (null !== $method && null !== $route) {
$counter->inc([sprintf('%s-%s', $method, $route)]);
}
}
// called on the `kernel.request` event
public function collectRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$requestMethod = $request->getMethod();
$requestRoute = $request->attributes->get('_route');
// do not track "OPTIONS" requests
if ('OPTIONS' === $requestMethod) {
return;
}
$this->incRequestsTotal($requestMethod, $requestRoute);
}
// called on the `kernel.response` event
public function collectResponse(ResponseEvent $event): void
{
$response = $event->getResponse();
$request = $event->getRequest();
$requestMethod = $request->getMethod();
$requestRoute = $request->attributes->get('_route');
$this->incResponsesTotal($requestMethod, $requestRoute);
}
}
…
yaml
App\Metrics\MyMetricsCollector:
tags:
- { name: prometheus_metrics_bundle.metrics_collector }A storage adapter is an instance of Prometheus\Storage\Adapter.
To create your own storage adapter you should create a custom factory implementing Artprima\PrometheusMetricsBundle\StorageFactory\StorageFactoryInterface.
getRequest();
$request->attributes->set('_color', 'red');
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}Requests will be tracked with the color label:
http_2xx_responses_total{action="GET-app_dummy_homepage",color="red"}
http_2xx_responses_total{action="GET-all",color=""}These are default metrics exported by the application:
# TYPE php_info gauge
php_info{version="7.3.25-1+ubuntu18.04.1+deb.sury.org+1"} 1
# HELP symfony_http_2xx_responses_total total 2xx response count
# TYPE symfony_http_2xx_responses_total counter
symfony_http_2xx_responses_total{action="GET-app_dummy_homepage"} 1
symfony_http_2xx_responses_total{action="all"} 1
# HELP symfony_http_requests_total total request count
# TYPE symfony_http_requests_total counter
symfony_http_requests_total{action="GET-app_dummy_homepage"} 1
symfony_http_requests_total{action="all"} 1
# HELP symfony_instance_name app instance name
# TYPE symfony_instance_name gauge
symfony_instance_name{instance="dev"} 1
…
yaml
App\Metrics\MyMetricInfoResolver:
tags:
- { name: prometheus_metrics_bundle.metric_info_resolver }The bundle provides a console command to clear metrics from the storage. Simply run:
./bin/console artprima:prometheus:metrics:clearExample Grafana dashboards are provided in the grafana/ directory to help you visualize the metrics exposed by this bundle.
symfony-app-overview.json - Comprehensive application overview dashboard featuring:
symfony-app-monitoring.json - Focused monitoring dashboard with:
See how the dashboards look with real data:
For more details about the screenshots and dashboard features, see the screenshots documentation.
/metrics/prometheus endpointnamespace template variable to match your bundle configuration (default: symfony)job to filter metrics by your application instancesThese dashboards are designed to work with the default metrics provided by the bundle and can be customized further based on your specific monitoring needs.
The dashboards have been validated and tested with a working demo environment. See the demo/ directory for:
Run php demo/validate-dashboards.php to validate the dashboard configurations.
This repository includes a GitHub Actions workflow that automatically:
The integration workflow runs automatically on changes to the dashboard files and can also be triggered manually. You can download the latest dashboard screenshots from the GitHub Actions artifacts.
| denisvmedia | Johnmeurt | rmbl | InboxViktorV | jb-reynaud | luca-nardelli |
| alshenetsky | edditor | marein | ctrl-f5 | ns3777k | scrutinizer-auto-fixer |
| Yozhef | karolmalinowski |
You are free to use the code in this repository under the terms of the MIT license. LICENSE contains a copy of this license.
No open issues yet, or sync has not completed.