prometheus-bundle icon indicating copy to clipboard operation
prometheus-bundle copied to clipboard

How to implement collection of metrics

Open estahn opened this issue 6 years ago • 3 comments

I'm not quite sure how to implement the collection of metrics. Do I create a class that extends one of the collectors (e.g. Counter)? Or can I create a plain class injecting the CollectorRegistry and that is somehow getting called when /metrics is getting hit?

estahn avatar Feb 12 '19 00:02 estahn

@rnijveld

estahn avatar Feb 13 '19 06:02 estahn

This is detailed in the README:

  1. You need to specify the collectors in your tweede_golf_prometheus configuration section.
  2. At either an action or an event listener you need to get the collector from the registry and observe or inc.
  3. You can add this bundle to your routing and /metrics will be served by the MetricsController, which will collect all the data from the collectors registered to the registry and present it as Prometheus expects.

Wassasin avatar Feb 15 '19 11:02 Wassasin

@Wassasin I understand the general concept. I just don't see how an action is being executed.

I created a folder src/Metrics which contains a file CoffeeMetrics.php.The file looks like this:

<?php

namespace App\Metrics;

class CoffeeMetrics
{
    public function __construct(CollectorRegistry $collectorRegistry, Connection $connection)
    {
        $coffees = $connection->exec('select count(*) coffees');

        $coffeesTotal = $collectorRegistry->getCounter('coffees_total');
        $coffeesTotal->inc();
    }
}

How do I reference this so its being picked up?

estahn avatar Feb 28 '19 00:02 estahn