l3tum / prometheus-metrics-bundle
Symfony 4/5 Prometheus Metrics Bundle
Requires
- php: ^7.2|^8.0
- ext-json: *
- promphp/prometheus_client_php: ^2.2
- symfony/config: ^4.4|^5.1.5
- symfony/dependency-injection: ^4.4|^5.1.5
- symfony/http-kernel: ^4.4|^5.1.5
Requires (Dev)
- escapestudios/symfony2-coding-standard: ^3.11
- friendsofphp/php-cs-fixer: ^2.17
- phpunit/phpunit: ^8.0|^9.0
- squizlabs/php_codesniffer: ^3.5
- symfony/browser-kit: ^4.4|^5.0
- symfony/framework-bundle: ^4.4|^5.0
- symfony/stopwatch: ^4.4|^5.0
- symfony/yaml: ^4.4|^5.0
Suggests
- ext-apcu: Required if using APCu as prometheus metrics backend
- ext-redis: Required if using Redis as prometheus metrics backend
- symfony/stopwatch: Required if you want to measure request duration
This package is auto-updated.
Last update: 2024-09-18 23:16:33 UTC
README
Symfony 5 Prometheus Metrics Bundle
安装
使用 Symfony Flex 的应用程序
打开命令行控制台,进入您的项目目录并执行
$ composer require l3tum/prometheus-metrics-bundle
不使用 Symfony Flex 的应用程序
步骤 1: 下载 Bundle
打开命令行控制台,进入您的项目目录并执行以下命令以下载此 Bundle 的最新稳定版本
$ composer require l3tum/prometheus-metrics-bundle
此命令需要您已全局安装 Composer,请参阅 Composer 文档中的 安装章节。
步骤 2: 启用 Bundle
然后,通过将其添加到项目中 app/AppKernel.php
文件中注册的 Bundle 列表中来启用此 Bundle
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new L3tum\PrometheusMetricsBundle\L3tumPrometheusMetricsBundle(), ); // ... } // ... }
配置
config.yaml
l3tum_prometheus_metrics: # namespace is used to prefix the prometheus metrics namespace: myapp # metrics backend type type: in_memory # possible values: in_memory, apcu, redis # ignoring some routes in metrics ignored_routes: [some_route_name, another_route_name] # used in case of type = "redis" redis: host: 127.0.0.1 port: 6379 timeout: 0.1 read_timeout: 10 persistent_connections: false password: ~
routes.yaml
# expose /metrics/prometheus in your application app_metrics: resource: '@L3tumPrometheusMetricsBundle/Resources/config/routing.xml'
您也可以定义自己的路径和规则
app_metrics: path: /mypath/mymetrics controller: L3tum\PrometheusMetricsBundle\Controller\MetricsController::prometheus
现在,您的指标可以通过 http://<yourapp_url>/metrics/prometheus 使用 Prometheus 查询。
自定义指标收集器
如果您想收集自己的指标,应创建一个类来实现 L3tum\PrometheusMetricsBundle\Metrics\MetricsCollectorInterface
接口。例如:
<?php declare(strict_types=1); namespace App\Metrics; use L3tum\PrometheusMetricsBundle\Metrics\MetricsCollectorInterface; use Prometheus\CollectorRegistry; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\TerminateEvent; /** * Class MyMetricsCollector. */ class MyMetricsCollector implements MetricsCollectorInterface { /** * @var string */ private $namespace; /** * @var CollectorRegistry */ private $collectionRegistry; public function init(string $namespace, CollectorRegistry $collectionRegistry): void { $this->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); } }
当使用 autoconfigure = true 时,通过实现 L3tum\PrometheusMetricsBundle\Metrics\MetricsCollectorInterface
接口,Symfony 将自动配置您的指标收集器,以便由收集器注册表使用。
如果您不使用 autoconfigure = true,则必须将其添加到您的 services.yaml
文件中
App\Metrics\MyMetricsCollector: tags: - { name: prometheus_metrics_bundle.metrics_generator }
默认指标
以下是应用程序导出的默认指标
# 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
请注意,php_info 来自底层库 promphp/prometheus_client_php
。其他指标由内置类 L3tum\PrometheusMetricsBundle\Metrics
收集。在此示例中,我们有一个前缀 symfony
,指标显示对根的单一请求,命名为 app_dummy_homepage
。此处 Symfony 实例命名为 dev
。实例名称来自服务器变量 HOSTNAME
($request->server->get('HOSTNAME')
),默认为 dev
。
代码许可
您可以在 MIT 许可证的条款下免费使用此存储库中的代码。LICENSE 包含此许可证的副本。