Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Simple library that abstracts different metrics collectors. I find this necessary to have a consistent and simple metrics (functional) API that doesn't cause vendor lock-in.

branch: master

minor #22 Typehint the logger (lyrixx)

This PR was merged into the master branch.

Discussion
----------

Typehint the logger

Commits
-------

9267e8e Typehint the logger
latest commit 1765823195
Grégoire Pineau lyrixx authored January 09, 2014
Octocat-spinner-32 example Fix bug in StatsD collector May 23, 2012
Octocat-spinner-32 src Typehint the logger January 09, 2014
Octocat-spinner-32 tests Typehint the logger January 09, 2014
Octocat-spinner-32 .gitignore Added Bundle and tests for configuration May 23, 2012
Octocat-spinner-32 .travis.yml Added php 5.5 in .travis.yml January 08, 2014
Octocat-spinner-32 README.md Wrap doc to 80 columns January 08, 2014
Octocat-spinner-32 composer.json Typehint the logger January 09, 2014
Octocat-spinner-32 phpunit.xml.dist Fixed phpunit config January 09, 2014
README.md

Metrics

Simple library that abstracts different metrics collectors. I find this necessary to have a consistent and simple metrics API that doesn't cause vendor lock-in.

It also ships with a Symfony Bundle. This is not a library for displaying metrics.

Currently supported backends:

  • StatsD
  • Graphite
  • Zabbix
  • Librato
  • Doctrine DBAL
  • Monolog
  • Null (Dummy that does nothing)

Installation

Using Composer:

{
    "require": {
        "beberlei/metrics": "~1.0"
    }
}

API

You can instantiate clients:

<?php
$metrics = \Beberlei\Metrics\Factory::create('statsd');
\Beberlei\Metrics\Registry::set('name', $metrics);
\Beberlei\Metrics\Registry::setDefaultName('name');

$metrics = \Beberlei\Metrics\Registry::get('name');
$metrics = \Beberlei\Metrics\Registry::get(); // Will returns the default metrics

You can measure stats:

<?php

$metrics = \Beberlei\Metrics\Registry::get('name');
$metrics->increment('foo.bar');
$metrics->decrement('foo.bar');

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

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

Some backends defer sending and aggregate all information, make sure to call flush:

<?php

$metrics = \Beberlei\Metrics\Registry::get('name');
$metrics->flush();

There is a convenience functional API. It works with the registry names. If null is provided, the default registry entry is used.

<?php
$registryName = 'name';

bmetrics_increment('foo.bar', $registryName);
bmetrics_decrement('foo.bar', null);
bmetrics_measure('foo.bar', $value, $registryName);
bmetrics_timing('foo.bar', $diff, null);

The functions are automatically available through the Composer autoload files mechanism.

Configuration

<?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 Integration

Activate Bundle into Kernel:

<?php

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

Do Configuration:

# app/config/config.yml
beberlei_metrics:
    default: foo
    collectors:
        foo:
            type: statsd
        bar:
            type: zabbix
            hostname: foo.beberlei.de
            server: localhost
            port: 10051
        baz:
            type: zabbix_file
            hostname: foo.beberlei.de
            file: /etc/zabbix/zabbix_agentd.conf
        librato:
            type: librato
            username: foo
            password: bar
        dbal:
            type: doctrine_dbal
            connection: metrics # using the connection named "metrics"
        monolog:
            type: monolog

This adds collectors to the Metrics registry. The functions are automatically included in the Bundle class so that in your code you can just start using the convenience functions. Metrics are also added as services:

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

Some backends defer sending and aggregate all information, make sure to call flush. But if symfony handle a request, the framework do it for you. But if symfony handle a cli task, or if symfony is running as a deamon, you have to flush by yourself:

<?php
$container->get('beberlei_metrics.flush_service')->onTerminate();
Something went wrong with that request. Please try again.