Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/artprima/prometheus-metrics-bundle
Symfony 5/6 Prometheus Metrics Bundle
https://github.com/artprima/prometheus-metrics-bundle
metrics prometheus symfony symfony-bundle
Last synced: 4 days ago
JSON representation
Symfony 5/6 Prometheus Metrics Bundle
- Host: GitHub
- URL: https://github.com/artprima/prometheus-metrics-bundle
- Owner: artprima
- License: mit
- Created: 2018-10-19T14:25:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-18T11:28:39.000Z (24 days ago)
- Last Synced: 2025-01-01T04:05:08.028Z (11 days ago)
- Topics: metrics, prometheus, symfony, symfony-bundle
- Language: PHP
- Homepage:
- Size: 166 KB
- Stars: 136
- Watchers: 9
- Forks: 30
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
| [Master][Master Link] |
|:------------------------------------------------------------:|
| [![Build Status][Master image]][Master] |
| [![Coverage Status][Master coverage image]][Master coverage] |Symfony 5/6/7 Prometheus Metrics Bundle
=========================================A Symfony bundle for the `promphp/prometheus_client_php`.
Installation
============Applications that use Symfony Flex
----------------------------------Open a command console, enter your project directory and execute:
```console
composer require artprima/prometheus-metrics-bundle
```Applications that don't use Symfony Flex
----------------------------------------### Step 1: Download the Bundle
Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:```console
composer require artprima/prometheus-metrics-bundle
```This command requires you to have Composer installed globally, as explained
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.### Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the `app/AppKernel.php` file of your project:```php
/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.terminate` event
public function collectResponse(TerminateEvent $event): void
{
$response = $event->getResponse();
$request = $event->getRequest();$requestMethod = $request->getMethod();
$requestRoute = $request->attributes->get('_route');$this->incResponsesTotal($requestMethod, $requestRoute);
}
}
```When using autoconfigure = true, by implementing `Artprima\PrometheusMetricsBundle\Metrics\MetricsCollectorInterface`
Symfony will automatically configure your metrics collector to be used by the collector registry.By implementing one of the following interfaces you can collect the metrics on one of the listed Symfony kernel events:
- `Artprima\PrometheusMetricsBundle\Metrics\PreRequestMetricsCollectorInterface`
- collect metrics on "kernel.request" event with a priority of 1024.
- `Artprima\PrometheusMetricsBundle\Metrics\RequestMetricsCollectorInterface`
- collect metrics on "kernel.request" event (default priority).
- `Artprima\PrometheusMetricsBundle\Metrics\PreExceptionMetricsCollectorInterface`
- collect metrics on "kernel.exception" event with a priority of 1024.
- `Artprima\PrometheusMetricsBundle\Metrics\ExceptionMetricsCollectorInterface`
- collect metrics on "kernel.exception" event with (default priority).
- `Artprima\PrometheusMetricsBundle\Metrics\TerminateMetricsCollectorInterface`
- collect metrics on "kernel.terminate" event.
The following collectors will only work if you define `enable_console_metrics: true` in the bundle configuration:- `Artprima\PrometheusMetricsBundle\Metrics\ConsoleCommandMetricsCollectorInterface`
- collect metrics on "console.command" event.
- `Artprima\PrometheusMetricsBundle\Metrics\ConsoleTerminateMetricsCollectorInterface`
- collect metrics on "console.terminate" event.
- `Artprima\PrometheusMetricsBundle\Metrics\ConsoleErrorMetricsCollectorInterface`
- collect metrics on "console.error" event.For advanced usage you can implement `Artprima\PrometheusMetricsBundle\Metrics\MetricsCollectorInterface` directly.
There is also `Artprima\PrometheusMetricsBundle\Metrics\MetricsCollectorInitTrait` will add the `init` method to your
collector.If you don't use autoconfigure = true, then you will have to add this to your `services.yaml`:
```yaml
App\Metrics\MyMetricsCollector:
tags:
- { name: prometheus_metrics_bundle.metrics_collector }
```Custom Storage Adapter Factory
========================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`.```php
server->get('HOSTNAME')`) and defaults to `dev`.Clear Metrics
=============The bundle provides a console command to clear metrics from the storage. Simply run:
```bash
./bin/console artprima:prometheus:metrics:clear
```Contributors
============[](https://github.com/denisvmedia) |[](https://github.com/Johnmeurt) |[](https://github.com/rmbl) |[](https://github.com/InboxViktorV) |[](https://github.com/jb-reynaud) |[](https://github.com/luca-nardelli) |
:---: |:---: |:---: |:---: |:---: |:---: |
[denisvmedia](https://github.com/denisvmedia) |[Johnmeurt](https://github.com/Johnmeurt) |[rmbl](https://github.com/rmbl) |[InboxViktorV](https://github.com/InboxViktorV) |[jb-reynaud](https://github.com/jb-reynaud) |[luca-nardelli](https://github.com/luca-nardelli) |[](https://github.com/alshenetsky) |[](https://github.com/edditor) |[](https://github.com/marein) |[](https://github.com/ctrl-f5) |[](https://github.com/ns3777k) |[](https://github.com/scrutinizer-auto-fixer) |
:---: |:---: |:---: |:---: |:---: |:---: |
[alshenetsky](https://github.com/alshenetsky) |[edditor](https://github.com/edditor) |[marein](https://github.com/marein) |[ctrl-f5](https://github.com/ctrl-f5) |[ns3777k](https://github.com/ns3777k) |[scrutinizer-auto-fixer](https://github.com/scrutinizer-auto-fixer) |[](https://github.com/Yozhef) |[](https://github.com/karolmalinowski) |
:---: |:---: |
[Yozhef](https://github.com/Yozhef) |[karolmalinowski](https://github.com/karolmalinowski) |Code license
============You are free to use the code in this repository under the terms of the MIT license. LICENSE contains a copy of this license.
[Master Link]: https://github.com/artprima/prometheus-metrics-bundle/tree/master
[Master image]: https://github.com/artprima/prometheus-metrics-bundle/workflows/PHP/badge.svg?branch=master
[Master]: https://github.com/artprima/prometheus-metrics-bundle/actions?query=workflow%3APHP+branch%3Amaster
[Master coverage image]: https://img.shields.io/codecov/c/github/artprima/prometheus-metrics-bundle/master.svg
[Master coverage]: https://codecov.io/gh/artprima/prometheus-metrics-bundle