Git Product home page Git Product logo

influxdb-preset-bundle's Introduction

InfluxDbPresetBundle

CI (master) PHP Version

InfluxDbPresetBundle: send metrics to InfluxDB server based on Events

Since it relies on the great official influxdb-php library client (via the Symfony bundle) you can configure the latter to benefit from:

  • Send multiple metrics at once (batch sending)
  • Udp Events (sends the metrics using UDP)
  • Http Events (sends the metrics to the InfluxDB API over HTTP)

Both methods (Udp/Http) can also be deferred, meaning you can send the metrics only when kernel.terminate event is fired in order not to slow your application. You can read more on the documentation of influxdb-bundle

profiler_influx

This bundle is inspired from StatsdBundle

Installation

Require yproximite/influxdb-preset-bundle to your composer.json file:

$ composer require yproximite/influxdb-preset-bundle

Register the bundle in app/AppKernel.php:

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Yproximite\Bundle\InfluxDbPresetBundle\YproximiteInfluxDbPresetBundle(),
        new Yproximite\InfluxDbBundle\InfluxDbBundle(),
    );
}

Also, be sure that you followed the configuration procedure for influxdb-bundle since it uses it to take the pre-configured service to communicate with the InfluxDB server.

Configuration

Here is the configuration reference:

# app/config/config.yml
yproximite_influx_db_preset:
    default_profile_name: default # by default it's "default"
    profiles:
        default:
            connections:
                default:
                    protocol: udp
                    deferred: true
            presets:
                app.user.created:
                    measurement: users
                    tags: { type: member, action: created, free: yes, foo: bar }
                    fields: { extra_value: true }
                api.company.created:
                    measurement: api
                    tags: { action: created, object: company }
                api.company.deleted:
                    measurement: api
                    tags: { action: deleted, object: company }
                app.memory_usage:
                    measurement: app_memory_usage
                    tags: { metric_type: memory }
                app.exception:
                    measurement: app
                    tags: { metric_type: exception, code: "<value>" }
                app.page_views:
                    measurement: app
                    tags: { metric_type: page_views }
        other:
            connections:
                default:
                    protocol: http
            presets:
                app.response_time:
                    measurement: app
                    tags: { metric_type: response_time }
                app.order.requested:
                    measurement: orders
                    tags: { action: requested, delivery: false }
                    fields: { extra_value: true }
    extensions:
        memory:
            enabled: true
            preset_name: app.memory_usage
        response_time:
            enabled: true
            preset_name: app.response_time
            profile_name: other # by default it's "default"
        exception:
            enabled: true
            preset_name: app.exception
        request_count:
            enabled: true
            preset_name: app.page_views

# influx_db:
#     default_connection:   ~
#     connections:
#         default:
#             host:                 influxdb.example.com
#             database:             my_db
#             udp:                  true
#             udp_port:             4444
#             http_port:            8086
#         other:
#             host:                 important.example.com
#             database:             my_db
#             udp:                  false
#             udp_port:             4444
#             http_port:            8086

Usage

Sending

through events:

use Yproximite\Bundle\InfluxDbPresetBundle\Event\InfluxDbEvent;

// Symfony\Component\EventDispatcher\EventDispatcherInterface
$eventDispatcher = $this->get('event_dispatcher');

// Preset from default profile
$eventDispatcher->dispatch('app.user.created', new InfluxDbEvent(1));

// Advanced event parameters
$event = new InfluxDbEvent(
    $value = 1, // will be converted to float
    string $profileName = 'other',
    ?\DateTimeInterface $dateTime = new \DateTime()
);

$eventDispatcher->dispatch('app.order.requested', $event);

using the client:

// Yproximite\Bundle\InfluxDbPresetBundle\Client\ClientInterface
$client = $this->get('yproximite.influx_db_preset.client.client');

$client->sendPoint(
    string $profileName = 'other',
    string $presetName = 'app.user.created',
    float $value = 0.5,
    ?\DateTimeInterface $dateTime = new \DateTime()
);

Listening for events

you can listen each client request through ClientRequestEvent:

use Yproximite\Bundle\InfluxDbPresetBundle\Event\ClientRequestEvent;

final class MyAppListener
{
    public function onClientRequest(ClientRequestEvent $event)
    {
        dump(
            $event->getProfileName(),
            $event->getPresetName(),
            $event->getValue(),
            $event->getDateTime()
        );
    }
}

do not forget to add the following code into services.yml:

services:
    app.event_listener.my_app_listener:
        class: AppBundle\EventListener\MyAppListener
        tags:
            - { name: kernel.event_listener, event: yproximite.bundle.influx_db_preset.client_request, method: onClientRequest }

You can enable extensions that will automatically (see configuration example) send the metrics for the memory usage, how much time the Request/Response cycle last, the status code of errors and the quantity of page views.

influxdb-preset-bundle's People

Contributors

kocal avatar n-sviridenko avatar romulused69 avatar tristanbes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

influxdb-preset-bundle's Issues

Compatibility with Symfony 5.1

Seems like there's some issues:

php.CRITICAL: Uncaught Error: Argument 1 passed to Yproximite\Bundle\InfluxDbPresetBundle\EventListener\Extension\ExceptionListener::onKernelException() 
must be an instance of Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent, 

instance of Symfony\Component\HttpKernel\Event\ExceptionEvent given, called in /srv/app/vendor/symfony/event-dispatcher/Debug/WrappedListener.php on line 117 {"exception":"[object] (TypeError(code: 0): 
Argument 1 passed to Yproximite\\Bundle\\InfluxDbPresetBundle\\EventListener\\Extension\\ExceptionListener::onKernelException() must be an instance of Symfony\\Component\\HttpKernel\\Event\\GetResponseForExceptionEvent, 
instance of Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent given, called in /srv/app/vendor/symfony/event-dispatcher/Debug/WrappedListener.php on 
line 117 at /srv/app/vendor/yproximite/influxdb-preset-bundle/src/EventListener/Extension/ExceptionListener.php:15)"} []
 php.CRITICAL: Uncaught Error: Argument 1 passed to Yproximite\Bundle\InfluxDbPresetBundle\EventListener\Extension\MemoryListener::onKernelTerminate() 
must be an instance of Symfony\Component\HttpKernel\Event\PostResponseEvent,
 instance of Symfony\Component\HttpKernel\Event\TerminateEvent given, called in /srv/app/vendor/symfony/event-dispatcher/Debug/WrappedListener.php on line 117 {"exception":"[object] (TypeError(code: 0): Argument 1 passed to Yproximite\\Bundle\\InfluxDbPresetBundle\\EventListener\\Extension\\MemoryListener::onKernelTerminate() must be an instance of Symfony\\Component\\HttpKernel\\Event\\PostResponseEvent, instance of Symfony\\Component\\HttpKernel\\Event\\TerminateEvent given, called in /srv/app/vendor/symfony/event-dispatcher/Debug/WrappedListener.php on line 117 at /srv/app/vendor/yproximite/influxdb-preset-bundle/src/EventListener/Extension/MemoryListener.php:14)"} []

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.