beberlei/metrics

一个简单的库,用于与指标收集器服务进行通信。

v2.10.0 2024-02-29 16:23 UTC

README

Build Status

一个抽象不同指标收集器的简单库。我发现需要一个一致且简单的指标API,这样不会导致供应商锁定。

它还附带了一个Symfony Bundle。 这并不是一个用于显示指标的库。

当前支持的后端

  • Doctrine DBAL
  • Graphite
  • InfluxDB
  • Telegraf
  • Librato
  • Logger (Psr\Log\LoggerInterface)
  • Null (无操作)
  • Prometheus
  • StatsD
  • Zabbix
  • DogStatsD

安装

使用Composer

composer require beberlei/metrics

API

您可以实例化客户端

<?php

$collector = \Beberlei\Metrics\Factory::create('statsd');

您可以测量统计数据

<?php

$collector->increment('foo.bar');
$collector->decrement('foo.bar');

$start = microtime(true);
$diff  = microtime(true) - $start;
$collector->timing('foo.bar', $diff);

$value = 1234;
$collector->measure('foo.bar', $value);

某些后端延迟发送并汇总所有信息,请确保调用flush

<?php

$collector->flush();

配置

<?php
$statsd = \Beberlei\Metrics\Factory::create('statsd');

$zabbix = \Beberlei\Metrics\Factory::create('zabbix', array(
    'hostname' => 'foo.beberlei.de',
    'server'   => 'localhost',
    'port'     => 10051,
));

$zabbixConfig = \Beberlei\Metrics\Factory::create('zabbix_file', array(
    'hostname' => 'foo.beberlei.de',
    'file'     => '/etc/zabbix/zabbix_agentd.conf'
));

$librato = \Beberlei\Metrics\Factory::create('librato', array(
    'hostname' => 'foo.beberlei.de',
    'username' => 'foo',
    'password' => 'bar',
));

$null = \Beberlei\Metrics\Factory::create('null');

Symfony Bundle集成

将Bundle注册到Kernel中

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        //..
        $bundles[] = new \Beberlei\Bundle\MetricsBundle\BeberleiMetricsBundle();
        //..
    }
}

进行配置

# app/config/config.yml
beberlei_metrics:
    default: foo
    collectors:
        foo:
            type: statsd
        bar:
            type: zabbix
            prefix: foo.beberlei.de
            host: localhost
            port: 10051
        baz:
            type: zabbix_file
            prefix: foo.beberlei.de
            file: /etc/zabbix/zabbix_agentd.conf
        librato:
            type: librato
            username: foo
            password: bar
            source: hermes10
        dbal:
            type: doctrine_dbal
            connection: metrics # using the connection named "metrics"
        monolog:
            type: monolog
        influxdb:
            type: influxdb
            influxdb_client: influxdb_client_service # using the InfluxDB client service named "influxdb_client_service"
            tags:
                dc: "west"
                node_instance: "hermes10"
        prometheus:
            type: prometheus
            prometheus_collector_registry: prometheus_collector_registry_service # using the Prometheus collector registry service named "prometheus_collector_registry_service"
            namespace: app_name # optional
            tags:
                dc: "west"
                node_instance: "hermes10"

这将在Metrics注册表中添加收集器。函数将自动包含在Bundle类中,以便在您的代码中可以开始使用方便的函数。指标也被添加为服务

<?php

$metrics = $container->get('beberlei_metrics.collector.foo');

并且可以获取默认收集器

<?php

$metrics = $container->get('beberlei_metrics.collector');