chaseisabelle / phprom-bundle
Symfony 4 PHProm Bundle
v0.0.6
2020-12-18 11:11 UTC
Requires
- php: ^7.1
- chaseisabelle/phprom-client: >=0.0.5
- symfony/config: ^4.3|^5.0
- symfony/dependency-injection: ^4.3|^5.0
- symfony/http-kernel: ^4.3|^5.0
Requires (Dev)
- phpunit/phpunit: ^7.0
- symfony/browser-kit: ^3.4|^4.0
- symfony/framework-bundle: ^3.4|^4.0
- symfony/yaml: ^3.4|^4.0
This package is auto-updated.
Last update: 2024-09-18 20:44:39 UTC
README
这是一个 phprom 的 symfony 客户端,phprom 是一个用于 php 应用程序的 Prometheus 度量数据存储
示例
查看一个完整功能的示例 这里
先决条件
相关
这实际上是一个用于 phprom 客户端 的 symfony bundle 包装器
安装
使用 symfony flex
composer require chaseisabelle/phprom-bundle
不使用 symfony flex
- 使用 composer 安装
composer require chaseisabelle/phprom-bundle
- 在
app/AppKernel.php
中启用class AppKernel extends Kernel { public function registerBundles() { $bundles = array( ... new ChaseIsabelle\PHPromBundle\ChaseIsabellePHPromBundle(), ); ...
配置
-
创建
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
-
打开
config/routes.yaml
并添加metrics: resource: '@ChaseIsabellePHPromBundle/Resources/config/routes.xml'
或者你可以自定义
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' ]); } }