lyova / prometheus-bundle
Prometheus 客户端 bundle for Symfony
dev-master
2023-01-18 15:53 UTC
Requires
- php: >=7.1.3
- symfony/config: ~2.7|~3.0|~4.0|~5.0|~6.0
- symfony/dependency-injection: ~2.7|~3.0|~4.0|~5.0|~6.0
- symfony/framework-bundle: ~2.7|~3.0|~4.0|~5.0|~6.0
- symfony/http-kernel: ~2.7|~3.0|~4.0|~5.0|~6.0
- tweedegolf/prometheus-client: ^0.3
This package is auto-updated.
Last update: 2024-09-18 20:04:35 UTC
README
A Symfony bundle for the tweede golf prometheus client. For more information on Prometheus you can check their website.
安装和配置
使用 Composer 通过 require 命令将 bundle 添加到依赖中
composer require tweedegolf/prometheus-bundle
将 bundle 添加到 AppKernel
在 app/AppKernel.php 中添加 bundle
public function registerBundles() { return array( // ... new TweedeGolf\PrometheusBundle\TweedeGolfPrometheusBundle(), // ... ); }
配置存储、收集器和路由
为了允许 Prometheus 从您的应用程序中抓取指标,确保您为 Prometheus 指标控制器提供了一个可用的路由
tweede_golf_prometheus: resource: "@TweedeGolfPrometheusBundle/Resources/config/routing.yml" prefix: /
您还可以实现自己的控制器,查看 TweedeGolf\PrometheusBundle\Controller\MetricsController::metricsAction 的源代码。您可以使用配置来配置 Prometheus 客户端的某些方面,以下显示默认值
tweede_golf_prometheus: storage_adapter_service: TweedeGolf\PrometheusClient\Storage\ApcuAdapter metrics_path: /metrics make_memory_adapter: true register_defaults: true collectors: ~
要调整,请在您的 config.yml 中创建一个 tweede_golf_prometheus 部分。您可以指定任意数量的收集器。以下是一个定义了四个不同收集器的示例
tweede_golf_prometheus: collectors: requests: counter: labels: [url] help: Number of requests throughput: gauge: labels: [url] help: Throughput per url initializer: 10.0 response_timing: histogram: labels: [url] help: Response timings buckets: [0.1, 0.2, 0.3, 0.5, 0.7, 1, 2, 5, 10, 30, 60] shorthand_example: counter: ~
修改(增加/观察/设置)指标
要修改一个指标,通过 CollectorRegistry 服务检索它并调用类型特定的指标修改方法之一。
use TweedeGolf\PrometheusClient\CollectorRegistry; public function exampleAction() { $metric = $this->get(CollectorRegistry::class)->getCounter('requests'); $metric->inc(); }
注册收集器服务
您还可以将服务作为收集器注册到收集器注册表中。为此,请为您的服务添加一个 tweede_golf_prometheus.collector 标签,并确保该服务实现了 CollectorInterface。您还可以使用注册服务工厂方法
services: example.collector.test: class: TweedeGolf\PrometheusClient\Collector\Counter factory: TweedeGolf\PrometheusClient\CollectorRegistry:createCounter arguments: [test] tags: [tweede_golf_prometheus.collector]