Code Monkey home page Code Monkey logo

metrics's Introduction

Metrics

Build Status

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:

  • Doctrine DBAL
  • Graphite
  • InfluxDB
  • Telegraf
  • Librato
  • Logger (Psr\Log\LoggerInterface)
  • Null (Dummy that does nothing)
  • Prometheus
  • StatsD
  • Zabbix
  • DogStatsD

Installation

Using Composer:

composer require beberlei/metrics

API

You can instantiate clients:

<?php

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

You can measure stats:

<?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);

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

<?php

$collector->flush();

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

Register 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
            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"

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 convenient functions. Metrics are also added as services:

<?php

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

and the default collector can be fetched:

<?php

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

metrics's People

Contributors

andreybolonin avatar armetiz avatar arnaud-lb avatar baartosz avatar beberlei avatar becoded avatar benjaminpaap avatar christianriesen avatar dsantang avatar jandeschuttere avatar jderusse avatar kimmelserj avatar lyrixx avatar mickaelandrieu avatar nicolas-grekas avatar sasezaki avatar tobion avatar tucksaun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

metrics's Issues

Timing documentation seems inconsistent

It looks like the example documentation on how to use the timing functionality isn't consistent with the expected data type and unit of measure:

README.md:

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

In this example, $diff will be a float representing the time delta value in microseconds, while the interface expects an int representing milliseconds:

Collector.php:

    /**
     * Records a timing.
     *
     * @param string $variable
     * @param int    $time     The duration of the timing in milliseconds
     */
    public function timing($variable, $time);

RFC: implements a graphite collector

If a do a PR, will you accept it ?

I know, it's better to use statsd in production, but I have a use case: I want to push old data and "fake" carbon history. So it's very more simple to use graphite than statsd for this use case

Problem with flush during shutdown in symfony2

Hello,

I just have a problem with the defer sending feature with the librato backend.

Let say I want to count a login event. So I put the $metric->increment('login'); code in the loginSuccess event. The controller then send the redirectResponse. The browser parse it and do the redirect and sadly I see that I am not yet logged.

I have found that the redirect is made before the session has been written (register_shutdown_function) because of the librato post that take too much time. So I wonder what is the best practice in this case. I guess I have to post my metrics before the redirect is sent to the browser.

What is your opinion on that ?

Symfony 4 compatibility

When using this library in Symfony 4, you get the following error

Fatal error: Uncaught Error: Class 'Symfony\Component\DependencyInjection\DefinitionDecorator'
not found in vendor/beberlei/metrics/src/Beberlei/Bundle/MetricsBundle/DependencyInjection/BeberleiMetricsExtension.php
on line 39

In the Symfony 4.0 upgrade guide there is a note:

The DefinitionDecorator class has been removed. Use the ChildDefinition class instead.

Problem: The alternative (ChildDefinition) was introduced in Symfony 3.3, but this package appears to support earlier versions.

So we should either up the minimal requirement to 3.3 (or 4.0?), or do some kind of class_exists hack. Preferences?

Per measurement tags for telegraf and InfluxDB

Hi there,

First of all, thanks for this great lib. I particularly enjoy this no vendor lock-in approach ๐Ÿ‘

Most (if not all) formats that support tags support setting different tags for each measurement.

It would be nice to retain the global (per $collector instance) tags, but also to support additional tags that would be set for only the current measurement, via additional (optional) function parameters.

For example:

$collector = \Beberlei\Metrics\Factory::create('telegraf');
$collector->setTags(['app_name' => 'myApp']);
$collector->increment('foo.bar', ['additional_tag' => 'tag_value']);

I could make a PR if you agree with that change.

EDIT: I didn't see that this just got implemented via the InlineTaggableGaugeableCollector interface. Only DogStatsD seems to implement it but Telegraf and InfluxDB should also support it.

How to use this library?

I have read the docs, code and tests multiple times but i cant really understand it.

In my code i have this

$counter = $registry->getOrRegisterCounter('app_name', 'max', 'helptext');
$counter->incBy(1);

This should add a counter called "max" right?

For simplicity, lets say, that this is my only metric i want to have.

How can i export / display / get all the metrics (in this case only one) to make them public under a certain route like /metric?

Why not flushing on destruct?

I was just looking at the library and it struck me odd why there is no destructor defined that would flush. Is that on purpose and if so whats the rationale behind it? If there is nothing against it I'd gladly add it in.

Add support for tagging metrics?

First, great library, I just thought about implementing something similar to prevent vendor lock-in.
For now we will use datadog for our metric needs.
StatsD (Datadog) supports adding arbitrary tags to metrics. Could that be added on the Collector API level with the default that other Collectors would not support tags? Or perhaps there are even more collectors that also support tags and could be enhanced to support tags.
If we would make the parameter tags optional with an empty default value it would also be backwards compatible for all users of the library. Only the collectors would need to be modified to account for the new parameter.
What do you think?
(I would gladly implement this if there is a chance to include this, otherwise I will probably create my own fork)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.