cors/prometheus

CORS - Prometheus

1.1.0 2024-02-21 15:50 UTC

This package is auto-updated.

Last update: 2024-04-21 16:26:55 UTC


README

CORS.gmbh

CORS Pimcore Prometheus

Expose Pimcore Metrics to Prometheus. This includes the following metrics:

  • Installed Bundles and Versions
  • Pimcore Sites
  • Tables and row Counts
  • Symfony Messenger Processed Messages Count

Installation

  1. Install and enable the bundle
composer require cors/prometheus
bin/console pimcore:bundle:enable CORSPrometheusBundle
  1. Configure the routes
# app/routes.yaml

_cors_prometheus:
  resource: "@CORSPrometheusBundle/Resources/config/routing.yaml"

Make sure to not expose the /metrics route to public access! We do that with nginx configs:

location ~* /metrics {
  deny all;
  return 403;
}

You can also add a event listener that uses a Key to protect the route:

<?php

declare(strict_types=1);

namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

final class MetricsListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => 'request',
        ];
    }

    public function request(KernelEvent $event): void
    {
        if ('cors_prometheus' === $event->getRequest()->attributes->get('_route')) {
            if ($event->getRequest()->query->get('apiKey') !== 'your-super-secret-key') {
                throw new NotFoundHttpException('Access denied');
            }
        }
    }
}

Storage

Also make sure you have some kind of storage to temporarily store the metrics. We use a Redis instance for that.

Prometheus\Storage\Adapter:
    factory: [ 'CORS\Bundle\PrometheusBundle\StorageFactory', 'create' ]
    arguments:
        $dsn: 'redis://redis:6379'