chaseisabelle/phprom-client

v0.0.5 2020-11-21 14:47 UTC

This package is auto-updated.

Last update: 2024-09-17 08:31:52 UTC


README

一个用于 phprom 的客户端库,phprom 是一个为 PHP 应用程序提供的 Prometheus 度量数据存储

示例

查看一个完全功能的示例 这里

先决条件

安装并运行 服务器

安装

  • 安装 客户端
    • composer require chaseisabelle/phprom-client
    • composer require chaseisabelle/phprom-bundle

安装 gRPC 需求

  • 注意:仅当您使用 gRPC API 时才需要此操作
  • 安装 gRPC
    • composer require 'grpc/grpc:1.30.0' 'google/protobuf:3.13.*'
  • 安装 gRPC 扩展

用法

实例化客户端

gRPC
// connect to the server using grpc
$phprom = new PHProm('127.0.0.1:3333');
// or
$phprom = new PHProm('127.0.0.1:3333', PHProm::GRPC_API);
REST/HTTP
// or connect to the server using rest/http
$phprom = new PHProm('127.0.0.1:3333', PHProm::REST_API);

注意! 请参阅 composer.json 中的建议包列表

  "suggest": {
    "ext-grpc": "* required for grpc api",
    "ext-curl": "* required for rest/http api"
  }
gRPC 与 REST/HTTP 的比较

当在本地网络上运行时,gRPC 和 REST/HTTP API 的基准测试大约相同

  • 最小值:2ms
  • 最大值:12ms
  • 平均值:约 6ms

获取 Prometheus 拉取器的度量指标

print($phprom->get());
// or...
echo $phprom->get();

自动注册和记录指标

计数器
$counter = (new Counter($phprom))
    ->setNamespace('namespace')
    ->setName('name')
    ->setDescription('description')
    ->setLabels(['label1', 'label2']); //<< optional

$counter->record(
    1.2345, 
    ['label1' => 'foo', 'label2' => 'bar']
);
直方图
$histogram = (new Histogram($phprom))
    ->setNamespace('namespace')
    ->setName('name')
    ->setDescription('description')
    ->setLabels(['label1', 'label2']) //<< optional
    ->setBuckets([0.1, 0.5, 1, 2, 5]); //<< optional

$histogram->record(
    1.2345, 
    ['label1' => 'foo', 'label2' => 'bar']
);
摘要
$summary = (new Summary($phprom))
    ->setNamespace('namespace')
    ->setName('name')
    ->setDescription('description')
    ->setLabels(['label1', 'label2']) //<< optional
    ->setObjectives([0.1, 0.5, 1, 2, 5]) //<< optional
    ->setMaxAge(0) //<< optional
    ->setAgeBuckets(0) //<< optional
    ->setBufCap(0); //<< optional

$summary->record(
    1.2345, 
    ['label1' => 'foo', 'label2' => 'bar']
);
仪表
$gauge = (new Gauge($phprom))
    ->setNamespace('namespace')
    ->setName('name')
    ->setDescription('description')
    ->setLabels(['label1', 'label2']); //<< optional

$gauge->record(
    1.2345, 
    ['label1' => 'foo', 'label2' => 'bar']
);

创建计时器以记录延迟

$histogram = (new Histogram($phprom))
    ->setNamespace('namespace')
    ->setName('name')
    ->setDescription('description')
    ->setLabels(['label1', 'label2'])
    ->setBuckets(range(1, 10));

$timer = new Timer($histogram);

$timer->start();

sleep(rand(1, 10));

$timer->stop()
    ->record(['label1' => 'foo', 'label2' => 'bar'])
    ->reset();

手动注册和记录指标

计数器
$phprom->registerCounter(
    'namespace',
    'name',
    'description',
    ['label1', 'label2'] //<< optional
);

$phprom->recordCounter(
    'namespace',
    'name',
    1.2345,
    ['label1' => 'foo', 'label2' => 'bar']
);
直方图
$phprom->registerHistogram(
    'namespace',
    'name',
    'description',
    ['label1', 'label2'], //<< optional
    [0.1, 0.5, 1, 2, 5] //<< custom buckets, optional
);

$phprom->recordHistogram(
    'namespace',
    'name',
    1.2345,
    ['label1' => 'foo', 'label2' => 'bar']
);
摘要
$phprom->registerSummary(
    'namespace',
    'name',
    'description',
    ['label1', 'label2'], //<< optional
    [0.1, 0.5, 1, 2, 5], //<< objectives, optional
    0, //<< max age, optional
    0, //<< age buckets, optional
    0 //<< buf cap, optional
);

$phprom->recordSummary(
    'namespace',
    'name',
    1.2345,
    ['label1' => 'foo', 'label2' => 'bar']
);
仪表
$phprom->registerGauge(
    'namespace',
    'name',
    'description',
    ['label1', 'label2'] //<< optional
);

$phprom->recordGauge(
    'namespace',
    'name',
    1.2345,
    ['label1' => 'foo', 'label2' => 'bar']
);